-
Notifications
You must be signed in to change notification settings - Fork 24
Add scala support #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add scala support #129
Changes from 4 commits
08bcaf4
80c9e71
042f7aa
59fb9bb
034bb57
9241bf4
362f57c
c14d606
0b0a78a
a8d8e77
b91f12f
43cf712
c12ccc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
sbt assembly |
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 ./target/scala-3.3.5/sqlite.jar "$@" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
**/target | ||
/.bloop/ | ||
/.bsp/ | ||
/.metals/ | ||
/project/.bloop/ | ||
metals.sbt | ||
metals/project/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
 | ||
|
||
This is a starting point for Scala solutions to the | ||
["Build Your Own SQLite" Challenge](https://codecrafters.io/challenges/sqlite). | ||
|
||
In this challenge, you'll build a barebones SQLite implementation that supports | ||
basic SQL queries like `SELECT`. Along the way we'll learn about | ||
[SQLite's file format](https://www.sqlite.org/fileformat.html), how indexed data | ||
is | ||
[stored in B-trees](https://jvns.ca/blog/2014/10/02/how-does-sqlite-work-part-2-btrees/) | ||
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 SQLite implementation is in `src/main/scala/Main.scala`. | ||
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 | ||
``` | ||
|
||
Time to move on to the next stage! | ||
|
||
# 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 program, which is implemented in | ||
`src/main/scala/Main.scala`. | ||
1. Commit your changes and run `git push origin master` to submit your solution | ||
to CodeCrafters. Test output will be streamed to your terminal. | ||
|
||
# Sample Databases | ||
|
||
To make it easy to test queries locally, we've added a sample database in the | ||
root of this repository: `sample.db`. | ||
|
||
This contains two tables: `apples` & `oranges`. You can use this to test your | ||
implementation for the first 6 stages. | ||
|
||
You can explore this database by running queries against it like this: | ||
|
||
```sh | ||
$ sqlite3 sample.db "select id, name from apples" | ||
1|Granny Smith | ||
2|Fuji | ||
3|Honeycrisp | ||
4|Golden Delicious | ||
``` | ||
|
||
There are two other databases that you can use: | ||
|
||
1. `superheroes.db`: | ||
- This is a small version of the test database used in the table-scan stage. | ||
- It contains one table: `superheroes`. | ||
- It is ~1MB in size. | ||
1. `companies.db`: | ||
- This is a small version of the test database used in the index-scan stage. | ||
- It contains one table: `companies`, and one index: `idx_companies_country` | ||
- It is ~7MB in size. | ||
|
||
These aren't included in the repository because they're large in size. You can | ||
download them by running this script: | ||
|
||
```sh | ||
./download_sample_databases.sh | ||
``` | ||
|
||
If the script doesn't work for some reason, you can download the databases | ||
directly from | ||
[codecrafters-io/sample-sqlite-databases](https://github.com/codecrafters-io/sample-sqlite-databases). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
ThisBuild / scalaVersion := "3.3.5" | ||
ThisBuild / version := "0.1.0-SNAPSHOT" | ||
ThisBuild / organization := "com.CodeCrafters" | ||
ThisBuild / organizationName := "CodeCrafters" | ||
|
||
assembly / assemblyJarName := "sqlite" | ||
|
||
lazy val root = (project in file(".")) | ||
.settings( | ||
name := "codecrafter-sqlite", | ||
// List your dependencies here | ||
libraryDependencies ++= Seq( | ||
), | ||
) |
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-17 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
|
||
echo "Downloading superheroes.db: ~1MB (used in stage 7)" | ||
curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db | ||
|
||
echo "Downloading companies.db: ~7MB (used in stage 8)" | ||
curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db | ||
|
||
echo "Sample databases downloaded." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.10.7 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.IOException | ||
import java.nio.ByteBuffer | ||
|
||
object Main extends App { | ||
if args.length < 2 | ||
then { | ||
println("Missing <database path> and <command>") | ||
System.exit(0); | ||
} | ||
val databaseFilePath = args(0); | ||
val command = args(1); | ||
command match { | ||
case ".dbinfo" => { | ||
val databaseFile = new FileInputStream(new File(databaseFilePath)) | ||
databaseFile.skip(16) | ||
val pageSizeBytes = new Array[Byte](2) | ||
databaseFile.read(pageSizeBytes) | ||
val pageSizeSigned = ByteBuffer.wrap(pageSizeBytes).getShort() | ||
val pageSize = pageSizeSigned & 0xFFFF | ||
println("database page size: " + pageSize) | ||
} | ||
case _ => println("Missing or invalid command passed: " + command) | ||
} | ||
} | ||
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 | ||
sbt assembly | ||
) | ||
|
||
# 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 ./target/scala-3.3.5/sqlite.jar "$@" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# syntax=docker/dockerfile:1.7-labs | ||
FROM maven:3.9.9-eclipse-temurin-17-alpine | ||
|
||
# Ensures the container is re-built if dependency files change | ||
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="build.sbt" | ||
|
||
WORKDIR /app | ||
|
||
# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses | ||
COPY --exclude=.git --exclude=README.md . /app | ||
Comment on lines
+15
to
+16
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. 🛠️ Refactor suggestion Invalid Flag in COPY Command The .git
README.md Then modify the COPY command to simply copy the directory: - COPY --exclude=.git --exclude=README.md . /app
+ COPY . /app 🧰 Tools🪛 Hadolint (2.12.0)[error] 16-16: invalid flag: --exclude (DL1000) |
||
|
||
# Install language-specific dependencies | ||
RUN .codecrafters/compile.sh |
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 | ||
|
||
sbt assembly |
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 ./target/scala-3.3.5/sqlite.jar "$@" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
**/target | ||
/.bloop/ | ||
/.bsp/ | ||
/.metals/ | ||
/project/.bloop/ | ||
metals.sbt | ||
metals/project/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
 | ||
|
||
This is a starting point for Scala solutions to the | ||
["Build Your Own SQLite" Challenge](https://codecrafters.io/challenges/sqlite). | ||
|
||
In this challenge, you'll build a barebones SQLite implementation that supports | ||
basic SQL queries like `SELECT`. Along the way we'll learn about | ||
[SQLite's file format](https://www.sqlite.org/fileformat.html), how indexed data | ||
is | ||
[stored in B-trees](https://jvns.ca/blog/2014/10/02/how-does-sqlite-work-part-2-btrees/) | ||
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 SQLite implementation is in `src/main/scala/Main.scala`. | ||
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 | ||
``` | ||
|
||
Time to move on to the next stage! | ||
|
||
# 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 program, which is implemented in | ||
`src/main/scala/Main.scala`. | ||
1. Commit your changes and run `git push origin master` to submit your solution | ||
to CodeCrafters. Test output will be streamed to your terminal. | ||
|
||
# Sample Databases | ||
|
||
To make it easy to test queries locally, we've added a sample database in the | ||
root of this repository: `sample.db`. | ||
|
||
This contains two tables: `apples` & `oranges`. You can use this to test your | ||
implementation for the first 6 stages. | ||
|
||
You can explore this database by running queries against it like this: | ||
|
||
```sh | ||
$ sqlite3 sample.db "select id, name from apples" | ||
1|Granny Smith | ||
2|Fuji | ||
3|Honeycrisp | ||
4|Golden Delicious | ||
``` | ||
|
||
There are two other databases that you can use: | ||
|
||
1. `superheroes.db`: | ||
- This is a small version of the test database used in the table-scan stage. | ||
- It contains one table: `superheroes`. | ||
- It is ~1MB in size. | ||
1. `companies.db`: | ||
- This is a small version of the test database used in the index-scan stage. | ||
- It contains one table: `companies`, and one index: `idx_companies_country` | ||
- It is ~7MB in size. | ||
|
||
These aren't included in the repository because they're large in size. You can | ||
download them by running this script: | ||
|
||
```sh | ||
./download_sample_databases.sh | ||
``` | ||
|
||
If the script doesn't work for some reason, you can download the databases | ||
directly from | ||
[codecrafters-io/sample-sqlite-databases](https://github.com/codecrafters-io/sample-sqlite-databases). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
ThisBuild / scalaVersion := "3.3.5" | ||
ThisBuild / version := "0.1.0-SNAPSHOT" | ||
ThisBuild / organization := "com.CodeCrafters" | ||
ThisBuild / organizationName := "CodeCrafters" | ||
|
||
assembly / assemblyJarName := "sqlite" | ||
|
||
lazy val root = (project in file(".")) | ||
.settings( | ||
name := "codecrafter-sqlite", | ||
// List your dependencies here | ||
libraryDependencies ++= Seq( | ||
), | ||
) |
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-17 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
|
||
echo "Downloading superheroes.db: ~1MB (used in stage 7)" | ||
curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db | ||
|
||
echo "Downloading companies.db: ~7MB (used in stage 8)" | ||
curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db | ||
|
||
echo "Sample databases downloaded." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.10.7 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") |
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.
Fix resource management and add proper error handling.
The code doesn't close the FileInputStream and lacks proper exception handling for file operations.
Implement using Scala's resource management pattern:
📝 Committable suggestion