This repository was archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Add kotlin template for redis #118
Open
AleksandrSl
wants to merge
4
commits into
codecrafters-io:master
Choose a base branch
from
AleksandrSl:redis-kotlin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -13,12 +13,11 @@ jarFile="$HOME/.cache/codecrafters-git-kotlin.jar" | |
|
|
||
| # Kotlin doesn't do incremental compilation, so let's hack this in ourselves. | ||
| previousHash=$(cat "$cacheFile" 2>/dev/null || echo '') | ||
| currentHash=$(tar -cP "$(dirname "$0")/app" | openssl sha1) | ||
| currentHash=$(tar -c "$(dirname "$0")/app" | sha1sum) | ||
|
|
||
| if [ "$previousHash" != "$currentHash" ] | ||
| then | ||
| # The JAVA_OPTS here prevents a warning on 1.4, see: https://youtrack.jetbrains.com/issue/KT-43704#focus=Comments-27-4625141.0-0 | ||
| JAVA_OPTS="--add-opens java.base/java.util=ALL-UNNAMED" kotlinc "$(dirname $0)/app/main.kt" -include-runtime -d "$jarFile" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the new compiler, there is no longer such option, and the bug was fixed |
||
| kotlinc "$(dirname $0)/app/main.kt" -include-runtime -d "$jarFile" | ||
| echo "$currentHash" > "$cacheFile" | ||
| fi | ||
|
|
||
|
|
||
This file contains hidden or 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 @@ | ||
| This is a starting point for Kotlin 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 signup for early access. | ||
|
|
||
| # Passing the first stage | ||
|
|
||
| The entry point for your Redis implementation is in `src/main/kotlin/Main.kt`. | ||
| Study and uncomment the relevant code, and push your changes to pass the first | ||
| stage: | ||
|
|
||
| ```sh | ||
| git add . | ||
| git commit -m "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 `kotlin` installed locally | ||
| 1. Run `./spawn_redis_server.sh` to run your Redis server, which is implemented | ||
| in `src/main/kotlin/Main.kt`. | ||
| 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 hidden or 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 Kotlin version used to run your code | ||
| # on Codecrafters. | ||
| # | ||
| # Available versions: kotlin-1.4, kotlin-1.6.21 | ||
| language_pack: kotlin-1.6.21 |
22 changes: 22 additions & 0 deletions
22
compiled_starters/redis-starter-kotlin/spawn_redis_server.sh
This file contains hidden or 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,22 @@ | ||
| #!/bin/sh | ||
| # | ||
| # DON'T EDIT THIS! | ||
| # | ||
| # CodeCrafters uses this file to test your code. Don't make any changes here! | ||
| set -e | ||
|
|
||
| mkdir -p "$HOME/.cache" | ||
| cacheFile="$HOME/.cache/codecrafters-git-kotlin-hash" | ||
| jarFile="$HOME/.cache/codecrafters-git-kotlin.jar" | ||
|
|
||
| # Kotlin doesn't do incremental compilation, so let's hack this in ourselves. | ||
| previousHash=$(cat "$cacheFile" 2>/dev/null || echo '') | ||
| currentHash=$(tar -c "$(dirname "$0")/src" | sha1sum) | ||
|
|
||
| if [ "$previousHash" != "$currentHash" ] | ||
| then | ||
| kotlinc src/main/kotlin/Main.kt -include-runtime -d "$jarFile" | ||
| echo "$currentHash" > "$cacheFile" | ||
| fi | ||
|
|
||
| exec kotlin -classpath "$jarFile" MainKt "$@" |
16 changes: 16 additions & 0 deletions
16
compiled_starters/redis-starter-kotlin/src/main/kotlin/Main.kt
This file contains hidden or 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,16 @@ | ||
| import java.net.ServerSocket | ||
|
|
||
| const val PORT = 6379; | ||
|
|
||
| fun main() { | ||
| // You can use print statements as follows for debugging, they'll be visible when running tests. | ||
| println("Logs from your program will appear here!") | ||
|
|
||
| // Uncomment this block to pass the first stage | ||
| // ServerSocket(PORT).apply { | ||
| // reuseAddress = true | ||
| // }.use { serverSocket -> | ||
| // // Wait for connection from client. | ||
| // val clientSocket = serverSocket.accept() | ||
| // }; | ||
| } |
This file contains hidden or 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,13 @@ | ||
| FROM openjdk:17-jdk-alpine | ||
|
|
||
| WORKDIR /usr/lib | ||
|
|
||
| # kotlinc wants bash for some reason | ||
| RUN apk add --no-cache bash && \ | ||
| wget -q https://github.com/JetBrains/kotlin/releases/download/v1.6.21/kotlin-compiler-1.6.21.zip && \ | ||
| unzip kotlin-compiler-*.zip && \ | ||
| rm kotlin-compiler-*.zip | ||
|
|
||
| ENV PATH $PATH:/usr/lib/kotlinc/bin | ||
|
|
||
| CMD ["kotlinc"] |
This file contains hidden or 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,13 @@ | ||
| FROM openjdk:17-jdk-alpine | ||
|
|
||
| WORKDIR /usr/lib | ||
|
|
||
| # kotlinc wants bash for some reason | ||
| RUN apk add --no-cache bash && \ | ||
| wget -q https://github.com/JetBrains/kotlin/releases/download/v1.6.21/kotlin-compiler-1.6.21.zip && \ | ||
| unzip kotlin-compiler-*.zip && \ | ||
| rm kotlin-compiler-*.zip | ||
|
|
||
| ENV PATH $PATH:/usr/lib/kotlinc/bin | ||
|
|
||
| CMD ["kotlinc"] |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,22 @@ | ||
| #!/bin/sh | ||
| # | ||
| # DON'T EDIT THIS! | ||
| # | ||
| # CodeCrafters uses this file to test your code. Don't make any changes here! | ||
| set -e | ||
|
|
||
| mkdir -p "$HOME/.cache" | ||
| cacheFile="$HOME/.cache/codecrafters-git-kotlin-hash" | ||
| jarFile="$HOME/.cache/codecrafters-git-kotlin.jar" | ||
|
|
||
| # Kotlin doesn't do incremental compilation, so let's hack this in ourselves. | ||
| previousHash=$(cat "$cacheFile" 2>/dev/null || echo '') | ||
| currentHash=$(tar -c "$(dirname "$0")/src" | sha1sum) | ||
|
|
||
| if [ "$previousHash" != "$currentHash" ] | ||
| then | ||
| kotlinc src/main/kotlin/Main.kt -include-runtime -d "$jarFile" | ||
| echo "$currentHash" > "$cacheFile" | ||
| fi | ||
|
|
||
| exec kotlin -classpath "$jarFile" MainKt "$@" |
This file contains hidden or 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,16 @@ | ||
| import java.net.ServerSocket | ||
|
|
||
| const val PORT = 6379; | ||
|
|
||
| fun main() { | ||
| // You can use print statements as follows for debugging, they'll be visible when running tests. | ||
| println("Logs from your program will appear here!") | ||
|
|
||
| // Uncomment this block to pass the first stage | ||
| // ServerSocket(PORT).apply { | ||
| // reuseAddress = true | ||
| // }.use { serverSocket -> | ||
| // // Wait for connection from client. | ||
| // val clientSocket = serverSocket.accept() | ||
| // }; | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the commit
-Pisn't present in alpine tar and we don't need it (we can't silence the warning, though). OpenSSL is missing by default, but if it's really better we could install it, only ~1mb.