-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stage 2 solutions for go, java and rust
- Loading branch information
Showing
34 changed files
with
1,075 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to compile your program on CodeCrafters | ||
# | ||
# This runs before .codecrafters/run.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
go build -o /tmp/codecrafters-build-redis-go app/*.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to run your program on CodeCrafters | ||
# | ||
# This runs after .codecrafters/compile.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
exec /tmp/codecrafters-build-redis-go "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png) | ||
|
||
This is a starting point for Go solutions to the | ||
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis). | ||
|
||
In this challenge, you'll build a toy Redis clone that's capable of handling | ||
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about | ||
event loops, the Redis protocol and more. | ||
|
||
**Note**: If you're viewing this repo on GitHub, head over to | ||
[codecrafters.io](https://codecrafters.io) to try the challenge. | ||
|
||
# Passing the first stage | ||
|
||
The entry point for your Redis implementation is in `app/server.go`. Study and | ||
uncomment the relevant code, and push your changes to pass the first stage: | ||
|
||
```sh | ||
git commit -am "pass 1st stage" # any msg | ||
git push origin master | ||
``` | ||
|
||
That's all! | ||
|
||
# Stage 2 & beyond | ||
|
||
Note: This section is for stages 2 and beyond. | ||
|
||
1. Ensure you have `go (1.19)` installed locally | ||
1. Run `./your_program.sh` to run your Redis server, which is implemented in | ||
`app/server.go`. | ||
1. Commit your changes and run `git push origin master` to submit your solution | ||
to CodeCrafters. Test output will be streamed to your terminal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
l, err := net.Listen("tcp", "0.0.0.0:6379") | ||
if err != nil { | ||
fmt.Println("Failed to bind to port 6379") | ||
os.Exit(1) | ||
} | ||
conn, err := l.Accept() | ||
if err != nil { | ||
fmt.Println("Error accepting connection: ", err.Error()) | ||
os.Exit(1) | ||
} | ||
conn.Write([]byte("+PONG\r\n")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Set this to true if you want debug logs. | ||
# | ||
# These can be VERY verbose, so we suggest turning them off | ||
# unless you really need them. | ||
debug: false | ||
|
||
# Use this to change the Go version used to run your code | ||
# on Codecrafters. | ||
# | ||
# Available versions: go-1.22 | ||
language_pack: go-1.22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// DON'T EDIT THIS! | ||
// | ||
// Codecrafters relies on this file being intact to run tests successfully. Any changes | ||
// here will not reflect when CodeCrafters tests your code, and might even cause build | ||
// failures. | ||
// | ||
// DON'T EDIT THIS! | ||
|
||
module github.com/codecrafters-io/redis-starter-go | ||
|
||
go 1.22 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
# | ||
# Use this script to run your program LOCALLY. | ||
# | ||
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit early if any commands fail | ||
|
||
# Copied from .codecrafters/compile.sh | ||
# | ||
# - Edit this to change how your program compiles locally | ||
# - Edit .codecrafters/compile.sh to change how your program compiles remotely | ||
( | ||
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory | ||
go build -o /tmp/codecrafters-build-redis-go app/*.go | ||
) | ||
|
||
# Copied from .codecrafters/run.sh | ||
# | ||
# - Edit this to change how your program runs locally | ||
# - Edit .codecrafters/run.sh to change how your program runs remotely | ||
exec /tmp/codecrafters-build-redis-go "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@@ -1,24 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
-// Ensures gofmt doesn't remove the "net" and "os" imports in stage 1 (feel free to remove this!) | ||
-var _ = net.Listen | ||
-var _ = os.Exit | ||
- | ||
func main() { | ||
l, err := net.Listen("tcp", "0.0.0.0:6379") | ||
if err != nil { | ||
fmt.Println("Failed to bind to port 6379") | ||
os.Exit(1) | ||
} | ||
- _, err = l.Accept() | ||
+ conn, err := l.Accept() | ||
if err != nil { | ||
fmt.Println("Error accepting connection: ", err.Error()) | ||
os.Exit(1) | ||
} | ||
+ conn.Write([]byte("+PONG\r\n")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
The entry point for your Redis implementation is in `app/server.go`. | ||
|
||
Study and uncomment the relevant code: | ||
|
||
```go | ||
// Uncomment this block to pass the first stage | ||
|
||
l, err := net.Listen("tcp", "0.0.0.0:6379") | ||
if err != nil { | ||
fmt.Println("Failed to bind to port 6379") | ||
os.Exit(1) | ||
} | ||
_, err = l.Accept() | ||
if err != nil { | ||
fmt.Println("Error accepting connection: ", err.Error()) | ||
os.Exit(1) | ||
} | ||
``` | ||
|
||
Push your changes to pass the first stage: | ||
|
||
``` | ||
git add . | ||
git commit -m "pass 1st stage" # any msg | ||
git push origin master | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to compile your program on CodeCrafters | ||
# | ||
# This runs before .codecrafters/run.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
mvn -B package -Ddir=/tmp/codecrafters-build-redis-java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to run your program on CodeCrafters | ||
# | ||
# This runs after .codecrafters/compile.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
exec java -jar /tmp/codecrafters-build-redis-java/codecrafters-redis.jar "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.jar | ||
target/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png) | ||
|
||
This is a starting point for Java solutions to the | ||
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis). | ||
|
||
In this challenge, you'll build a toy Redis clone that's capable of handling | ||
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about | ||
event loops, the Redis protocol and more. | ||
|
||
**Note**: If you're viewing this repo on GitHub, head over to | ||
[codecrafters.io](https://codecrafters.io) to try the challenge. | ||
|
||
# Passing the first stage | ||
|
||
The entry point for your Redis implementation is in `src/main/java/Main.java`. | ||
Study and uncomment the relevant code, and push your changes to pass the first | ||
stage: | ||
|
||
```sh | ||
git commit -am "pass 1st stage" # any msg | ||
git push origin master | ||
``` | ||
|
||
That's all! | ||
|
||
# Stage 2 & beyond | ||
|
||
Note: This section is for stages 2 and beyond. | ||
|
||
1. Ensure you have `mvn` installed locally | ||
1. Run `./your_program.sh` to run your Redis server, which is implemented in | ||
`src/main/java/Main.java`. | ||
1. Commit your changes and run `git push origin master` to submit your solution | ||
to CodeCrafters. Test output will be streamed to your terminal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Set this to true if you want debug logs. | ||
# | ||
# These can be VERY verbose, so we suggest turning them off | ||
# unless you really need them. | ||
debug: false | ||
|
||
# Use this to change the Java version used to run your code | ||
# on Codecrafters. | ||
# | ||
# Available versions: java-23 | ||
language_pack: java-23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.codecrafters</groupId> | ||
<artifactId>codecrafters-redis</artifactId> | ||
<version>1.0</version> | ||
|
||
<properties> | ||
<maven.compiler.source>23</maven.compiler.source> | ||
<maven.compiler.target>23</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>23</java.version> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<configuration> | ||
<finalName>codecrafters-redis</finalName> <!-- Please do not change this final artifact name--> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
<appendAssemblyId>false</appendAssemblyId> | ||
<archive> | ||
<manifest> | ||
<addClasspath>true</addClasspath> | ||
<!-- This is the main class of your program which will be executed--> | ||
<mainClass>Main</mainClass> | ||
</manifest> | ||
</archive> | ||
<outputDirectory>${dir}</outputDirectory> | ||
</configuration> | ||
|
||
<executions> | ||
<execution> | ||
<id>make-assembly</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
public class Main { | ||
public static void main(String[] args){ | ||
ServerSocket serverSocket = null; | ||
Socket clientSocket = null; | ||
int port = 6379; | ||
try { | ||
serverSocket = new ServerSocket(port); | ||
// Since the tester restarts your program quite often, setting SO_REUSEADDR | ||
// ensures that we don't run into 'Address already in use' errors | ||
serverSocket.setReuseAddress(true); | ||
// Wait for connection from client. | ||
clientSocket = serverSocket.accept(); | ||
OutputStream outputStream = clientSocket.getOutputStream(); | ||
outputStream.write("+PONG\r\n".getBytes()); | ||
} catch (IOException e) { | ||
System.out.println("IOException: " + e.getMessage()); | ||
} finally { | ||
try { | ||
if (clientSocket != null) { | ||
clientSocket.close(); | ||
} | ||
} catch (IOException e) { | ||
System.out.println("IOException: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
# | ||
# Use this script to run your program LOCALLY. | ||
# | ||
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit early if any commands fail | ||
|
||
# Copied from .codecrafters/compile.sh | ||
# | ||
# - Edit this to change how your program compiles locally | ||
# - Edit .codecrafters/compile.sh to change how your program compiles remotely | ||
( | ||
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory | ||
mvn -B package -Ddir=/tmp/codecrafters-build-redis-java | ||
) | ||
|
||
# Copied from .codecrafters/run.sh | ||
# | ||
# - Edit this to change how your program runs locally | ||
# - Edit .codecrafters/run.sh to change how your program runs remotely | ||
exec java -jar /tmp/codecrafters-build-redis-java/codecrafters-redis.jar "$@" |
Oops, something went wrong.