From 08bcaf4e61a3c020563a105224c8df2edf7e0e24 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 16:45:31 -0400 Subject: [PATCH 01/12] Add scala dockerfile --- dockerfiles/scala-3.3.5.Dockerfile | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 dockerfiles/scala-3.3.5.Dockerfile diff --git a/dockerfiles/scala-3.3.5.Dockerfile b/dockerfiles/scala-3.3.5.Dockerfile new file mode 100644 index 0000000..6baf9ee --- /dev/null +++ b/dockerfiles/scala-3.3.5.Dockerfile @@ -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 + +# Install language-specific dependencies +RUN .codecrafters/compile.sh From 80c9e7182587ab23014b12aa723537dcef006094 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 16:45:39 -0400 Subject: [PATCH 02/12] Add scala solution --- .../01-dr6/code/.codecrafters/compile.sh | 11 +++ .../scala/01-dr6/code/.codecrafters/run.sh | 11 +++ solutions/scala/01-dr6/code/.gitattributes | 1 + solutions/scala/01-dr6/code/.gitignore | 7 ++ solutions/scala/01-dr6/code/README.md | 77 ++++++++++++++++++ solutions/scala/01-dr6/code/build.sbt | 14 ++++ solutions/scala/01-dr6/code/codecrafters.yml | 11 +++ .../01-dr6/code/download_sample_databases.sh | 9 ++ .../01-dr6/code/project/build.properties | 1 + .../scala/01-dr6/code/project/plugins.sbt | 3 + solutions/scala/01-dr6/code/sample.db | Bin 0 -> 16384 bytes .../01-dr6/code/src/main/scala/Main.scala | 26 ++++++ solutions/scala/01-dr6/code/your_program.sh | 24 ++++++ .../01-dr6/diff/src/main/java/Main.java.diff | 40 +++++++++ solutions/scala/01-dr6/explanation.md | 16 ++++ 15 files changed, 251 insertions(+) create mode 100755 solutions/scala/01-dr6/code/.codecrafters/compile.sh create mode 100755 solutions/scala/01-dr6/code/.codecrafters/run.sh create mode 100644 solutions/scala/01-dr6/code/.gitattributes create mode 100644 solutions/scala/01-dr6/code/.gitignore create mode 100644 solutions/scala/01-dr6/code/README.md create mode 100644 solutions/scala/01-dr6/code/build.sbt create mode 100644 solutions/scala/01-dr6/code/codecrafters.yml create mode 100755 solutions/scala/01-dr6/code/download_sample_databases.sh create mode 100644 solutions/scala/01-dr6/code/project/build.properties create mode 100644 solutions/scala/01-dr6/code/project/plugins.sbt create mode 100644 solutions/scala/01-dr6/code/sample.db create mode 100644 solutions/scala/01-dr6/code/src/main/scala/Main.scala create mode 100755 solutions/scala/01-dr6/code/your_program.sh create mode 100644 solutions/scala/01-dr6/diff/src/main/java/Main.java.diff create mode 100644 solutions/scala/01-dr6/explanation.md diff --git a/solutions/scala/01-dr6/code/.codecrafters/compile.sh b/solutions/scala/01-dr6/code/.codecrafters/compile.sh new file mode 100755 index 0000000..11cad90 --- /dev/null +++ b/solutions/scala/01-dr6/code/.codecrafters/compile.sh @@ -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 diff --git a/solutions/scala/01-dr6/code/.codecrafters/run.sh b/solutions/scala/01-dr6/code/.codecrafters/run.sh new file mode 100755 index 0000000..c0eb78a --- /dev/null +++ b/solutions/scala/01-dr6/code/.codecrafters/run.sh @@ -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 "$@" diff --git a/solutions/scala/01-dr6/code/.gitattributes b/solutions/scala/01-dr6/code/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/solutions/scala/01-dr6/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/scala/01-dr6/code/.gitignore b/solutions/scala/01-dr6/code/.gitignore new file mode 100644 index 0000000..5b4f849 --- /dev/null +++ b/solutions/scala/01-dr6/code/.gitignore @@ -0,0 +1,7 @@ +**/target +/.bloop/ +/.bsp/ +/.metals/ +/project/.bloop/ +metals.sbt +metals/project/ \ No newline at end of file diff --git a/solutions/scala/01-dr6/code/README.md b/solutions/scala/01-dr6/code/README.md new file mode 100644 index 0000000..f6fcb4a --- /dev/null +++ b/solutions/scala/01-dr6/code/README.md @@ -0,0 +1,77 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png) + +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). diff --git a/solutions/scala/01-dr6/code/build.sbt b/solutions/scala/01-dr6/code/build.sbt new file mode 100644 index 0000000..aad064d --- /dev/null +++ b/solutions/scala/01-dr6/code/build.sbt @@ -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( + ), + ) \ No newline at end of file diff --git a/solutions/scala/01-dr6/code/codecrafters.yml b/solutions/scala/01-dr6/code/codecrafters.yml new file mode 100644 index 0000000..b4ddb38 --- /dev/null +++ b/solutions/scala/01-dr6/code/codecrafters.yml @@ -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 diff --git a/solutions/scala/01-dr6/code/download_sample_databases.sh b/solutions/scala/01-dr6/code/download_sample_databases.sh new file mode 100755 index 0000000..03e0573 --- /dev/null +++ b/solutions/scala/01-dr6/code/download_sample_databases.sh @@ -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." diff --git a/solutions/scala/01-dr6/code/project/build.properties b/solutions/scala/01-dr6/code/project/build.properties new file mode 100644 index 0000000..73df629 --- /dev/null +++ b/solutions/scala/01-dr6/code/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.10.7 diff --git a/solutions/scala/01-dr6/code/project/plugins.sbt b/solutions/scala/01-dr6/code/project/plugins.sbt new file mode 100644 index 0000000..d8de595 --- /dev/null +++ b/solutions/scala/01-dr6/code/project/plugins.sbt @@ -0,0 +1,3 @@ + + +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") \ No newline at end of file diff --git a/solutions/scala/01-dr6/code/sample.db b/solutions/scala/01-dr6/code/sample.db new file mode 100644 index 0000000000000000000000000000000000000000..687673ecc5f29d50c7d12b9228829e47ce210eff GIT binary patch literal 16384 zcmeI(&rTCT90%~(b}6Am$HbUg8)XQZKqw~a$ruv>OHIUofF_*C(Ecm~)7i2!EA+?{ z=o=Ux#FOzAJezp%1$+R1+m-}4c;H}S{7$kvJG(o7_H&8)v#HG$OIB<<>;?uknx zio_$Ogb-tV#C#m50$-dgN5>ETkdKMmPd{AX7m63Z2>D(<Z(B-I?K0&z2`lTNxLqL#gVe zrmNGw+M~YGSxAg)rPL)C>g5TiyQBgg=GSfVt<>~_N!;L8dG2MQRGFWTHU}O1eakcH zEl>LyxfY!%7EcZ2%){Bdn>yE=OQP{gwHh7WA2g|ZJxhLgE}Uv$c<-Da(n@TSjy@ru zo5%8#kU!)%`I#?}AOHafKmY;|fB*y_009U<00I#B`vTYESS(bViEWcpkI9;rNix4l z)+Ps)Vl1Yoo+OU?{Aa@VmTcyC)Ec!X3$eIzc_rI5T&P8{xHZ${|01rh*1IO`teVbF zP|cLc<@5i7{3+xwJ|RH>0uX=z1Rwwb2tWV=5P$##An-2>Ocsi@XteyHRE~<(Xt;tP z&gcInc~!{o@~iwP_v8u}kRSj72tWV=5P$##AOHafKmY>&i9o%?LG#9Q{f?}9G2Bvc zKoryghtHL_g8=%r34u~a-p`E1Vt(O`w!HG+=uw4j@*&??uXZyI+PltOj^t0jUbch1 z1;$b4Gi_~Od7?Hi^Y?YtNr^Y%=T*GZtv#-DlOz074>s~G4JlVS@j^Zt-n@{vO;rx} h*X9Oq*~AaVsa;zIorW76{J(zZ85cD-aH9UGgWr)S@BRP) literal 0 HcmV?d00001 diff --git a/solutions/scala/01-dr6/code/src/main/scala/Main.scala b/solutions/scala/01-dr6/code/src/main/scala/Main.scala new file mode 100644 index 0000000..059d3c9 --- /dev/null +++ b/solutions/scala/01-dr6/code/src/main/scala/Main.scala @@ -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 and ") + 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) + } +} diff --git a/solutions/scala/01-dr6/code/your_program.sh b/solutions/scala/01-dr6/code/your_program.sh new file mode 100755 index 0000000..ad509ce --- /dev/null +++ b/solutions/scala/01-dr6/code/your_program.sh @@ -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 "$@" diff --git a/solutions/scala/01-dr6/diff/src/main/java/Main.java.diff b/solutions/scala/01-dr6/diff/src/main/java/Main.java.diff new file mode 100644 index 0000000..737356e --- /dev/null +++ b/solutions/scala/01-dr6/diff/src/main/java/Main.java.diff @@ -0,0 +1,40 @@ +@@ -2,38 +2,34 @@ + import java.io.FileInputStream; + import java.io.IOException; + import java.nio.ByteBuffer; + + public class Main { + public static void main(String[] args){ + if (args.length < 2) { + System.out.println("Missing and "); + return; + } + + String databaseFilePath = args[0]; + String command = args[1]; + + switch (command) { + case ".dbinfo" -> { + try { + FileInputStream databaseFile = new FileInputStream(new File(databaseFilePath)); + + databaseFile.skip(16); // Skip the first 16 bytes of the header + byte[] pageSizeBytes = new byte[2]; // The following 2 bytes are the page size + databaseFile.read(pageSizeBytes); + short pageSizeSigned = ByteBuffer.wrap(pageSizeBytes).getShort(); + int pageSize = Short.toUnsignedInt(pageSizeSigned); + +- // You can use print statements as follows for debugging, they'll be visible when running tests. +- System.err.println("Logs from your program will appear here!"); +- +- // Uncomment this block to pass the first stage +- // System.out.println("database page size: " + pageSize); ++ System.out.println("database page size: " + pageSize); + } catch (IOException e) { + System.out.println("Error reading file: " + e.getMessage()); + } + } + default -> System.out.println("Missing or invalid command passed: " + command); + } + } + } diff --git a/solutions/scala/01-dr6/explanation.md b/solutions/scala/01-dr6/explanation.md new file mode 100644 index 0000000..5db76e4 --- /dev/null +++ b/solutions/scala/01-dr6/explanation.md @@ -0,0 +1,16 @@ +The entry point for your SQLite implementation is in `src/main/scala/Main.scala`. + +Study and uncomment the relevant code: + +```java +// Uncomment this block to pass the first stage +println("database page size: " + pageSize); +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` From 042f7aabb4ccc5895cb3a74ccd50a084da5b72a4 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 16:58:17 -0400 Subject: [PATCH 03/12] Add scala starter template --- solutions/scala/01-dr6/explanation.md | 2 +- .../scala/code/.codecrafters/compile.sh | 11 ++++++++ .../scala/code/.codecrafters/run.sh | 11 ++++++++ starter_templates/scala/code/.gitattributes | 1 + starter_templates/scala/code/.gitignore | 7 +++++ starter_templates/scala/code/build.sbt | 14 ++++++++++ starter_templates/scala/code/codecrafters.yml | 11 ++++++++ .../scala/code/project/build.properties | 1 + .../scala/code/project/plugins.sbt | 3 +++ .../scala/code/src/main/scala/Main.scala | 26 +++++++++++++++++++ starter_templates/scala/config.yml | 3 +++ 11 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 starter_templates/scala/code/.codecrafters/compile.sh create mode 100755 starter_templates/scala/code/.codecrafters/run.sh create mode 100644 starter_templates/scala/code/.gitattributes create mode 100644 starter_templates/scala/code/.gitignore create mode 100644 starter_templates/scala/code/build.sbt create mode 100644 starter_templates/scala/code/codecrafters.yml create mode 100644 starter_templates/scala/code/project/build.properties create mode 100644 starter_templates/scala/code/project/plugins.sbt create mode 100644 starter_templates/scala/code/src/main/scala/Main.scala create mode 100644 starter_templates/scala/config.yml diff --git a/solutions/scala/01-dr6/explanation.md b/solutions/scala/01-dr6/explanation.md index 5db76e4..81b0f43 100644 --- a/solutions/scala/01-dr6/explanation.md +++ b/solutions/scala/01-dr6/explanation.md @@ -2,7 +2,7 @@ The entry point for your SQLite implementation is in `src/main/scala/Main.scala` Study and uncomment the relevant code: -```java +```scala // Uncomment this block to pass the first stage println("database page size: " + pageSize); ``` diff --git a/starter_templates/scala/code/.codecrafters/compile.sh b/starter_templates/scala/code/.codecrafters/compile.sh new file mode 100755 index 0000000..11cad90 --- /dev/null +++ b/starter_templates/scala/code/.codecrafters/compile.sh @@ -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 diff --git a/starter_templates/scala/code/.codecrafters/run.sh b/starter_templates/scala/code/.codecrafters/run.sh new file mode 100755 index 0000000..c0eb78a --- /dev/null +++ b/starter_templates/scala/code/.codecrafters/run.sh @@ -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 "$@" diff --git a/starter_templates/scala/code/.gitattributes b/starter_templates/scala/code/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/starter_templates/scala/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/starter_templates/scala/code/.gitignore b/starter_templates/scala/code/.gitignore new file mode 100644 index 0000000..5b4f849 --- /dev/null +++ b/starter_templates/scala/code/.gitignore @@ -0,0 +1,7 @@ +**/target +/.bloop/ +/.bsp/ +/.metals/ +/project/.bloop/ +metals.sbt +metals/project/ \ No newline at end of file diff --git a/starter_templates/scala/code/build.sbt b/starter_templates/scala/code/build.sbt new file mode 100644 index 0000000..aad064d --- /dev/null +++ b/starter_templates/scala/code/build.sbt @@ -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( + ), + ) \ No newline at end of file diff --git a/starter_templates/scala/code/codecrafters.yml b/starter_templates/scala/code/codecrafters.yml new file mode 100644 index 0000000..b4ddb38 --- /dev/null +++ b/starter_templates/scala/code/codecrafters.yml @@ -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 diff --git a/starter_templates/scala/code/project/build.properties b/starter_templates/scala/code/project/build.properties new file mode 100644 index 0000000..73df629 --- /dev/null +++ b/starter_templates/scala/code/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.10.7 diff --git a/starter_templates/scala/code/project/plugins.sbt b/starter_templates/scala/code/project/plugins.sbt new file mode 100644 index 0000000..d8de595 --- /dev/null +++ b/starter_templates/scala/code/project/plugins.sbt @@ -0,0 +1,3 @@ + + +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") \ No newline at end of file diff --git a/starter_templates/scala/code/src/main/scala/Main.scala b/starter_templates/scala/code/src/main/scala/Main.scala new file mode 100644 index 0000000..059d3c9 --- /dev/null +++ b/starter_templates/scala/code/src/main/scala/Main.scala @@ -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 and ") + 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) + } +} diff --git a/starter_templates/scala/config.yml b/starter_templates/scala/config.yml new file mode 100644 index 0000000..41a21c5 --- /dev/null +++ b/starter_templates/scala/config.yml @@ -0,0 +1,3 @@ +attributes: + required_executable: sbt (1.10.7) + user_editable_file: src/main/scala/Main.scala \ No newline at end of file From 59fb9bbbc816b625f71b47cbb034921213cc3f5a Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 19:45:35 -0400 Subject: [PATCH 04/12] Add compiled starter for scala --- .../scala/.codecrafters/compile.sh | 11 +++ compiled_starters/scala/.codecrafters/run.sh | 11 +++ compiled_starters/scala/.gitattributes | 1 + compiled_starters/scala/.gitignore | 7 ++ compiled_starters/scala/README.md | 77 ++++++++++++++++++ compiled_starters/scala/build.sbt | 14 ++++ compiled_starters/scala/codecrafters.yml | 11 +++ .../scala/download_sample_databases.sh | 9 ++ .../scala/project/build.properties | 1 + compiled_starters/scala/project/plugins.sbt | 3 + compiled_starters/scala/sample.db | Bin 0 -> 16384 bytes .../scala/src/main/scala/Main.scala | 26 ++++++ compiled_starters/scala/your_program.sh | 24 ++++++ .../01-dr6/diff/src/main/java/Main.java.diff | 40 --------- .../diff/src/main/scala/Main.scala.diff | 35 ++++++++ .../scala/code/src/main/scala/Main.scala | 7 +- 16 files changed, 236 insertions(+), 41 deletions(-) create mode 100755 compiled_starters/scala/.codecrafters/compile.sh create mode 100755 compiled_starters/scala/.codecrafters/run.sh create mode 100644 compiled_starters/scala/.gitattributes create mode 100644 compiled_starters/scala/.gitignore create mode 100644 compiled_starters/scala/README.md create mode 100644 compiled_starters/scala/build.sbt create mode 100644 compiled_starters/scala/codecrafters.yml create mode 100755 compiled_starters/scala/download_sample_databases.sh create mode 100644 compiled_starters/scala/project/build.properties create mode 100644 compiled_starters/scala/project/plugins.sbt create mode 100644 compiled_starters/scala/sample.db create mode 100644 compiled_starters/scala/src/main/scala/Main.scala create mode 100755 compiled_starters/scala/your_program.sh delete mode 100644 solutions/scala/01-dr6/diff/src/main/java/Main.java.diff create mode 100644 solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff diff --git a/compiled_starters/scala/.codecrafters/compile.sh b/compiled_starters/scala/.codecrafters/compile.sh new file mode 100755 index 0000000..11cad90 --- /dev/null +++ b/compiled_starters/scala/.codecrafters/compile.sh @@ -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 diff --git a/compiled_starters/scala/.codecrafters/run.sh b/compiled_starters/scala/.codecrafters/run.sh new file mode 100755 index 0000000..c0eb78a --- /dev/null +++ b/compiled_starters/scala/.codecrafters/run.sh @@ -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 "$@" diff --git a/compiled_starters/scala/.gitattributes b/compiled_starters/scala/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/compiled_starters/scala/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/compiled_starters/scala/.gitignore b/compiled_starters/scala/.gitignore new file mode 100644 index 0000000..5b4f849 --- /dev/null +++ b/compiled_starters/scala/.gitignore @@ -0,0 +1,7 @@ +**/target +/.bloop/ +/.bsp/ +/.metals/ +/project/.bloop/ +metals.sbt +metals/project/ \ No newline at end of file diff --git a/compiled_starters/scala/README.md b/compiled_starters/scala/README.md new file mode 100644 index 0000000..f6fcb4a --- /dev/null +++ b/compiled_starters/scala/README.md @@ -0,0 +1,77 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png) + +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). diff --git a/compiled_starters/scala/build.sbt b/compiled_starters/scala/build.sbt new file mode 100644 index 0000000..aad064d --- /dev/null +++ b/compiled_starters/scala/build.sbt @@ -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( + ), + ) \ No newline at end of file diff --git a/compiled_starters/scala/codecrafters.yml b/compiled_starters/scala/codecrafters.yml new file mode 100644 index 0000000..b4ddb38 --- /dev/null +++ b/compiled_starters/scala/codecrafters.yml @@ -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 diff --git a/compiled_starters/scala/download_sample_databases.sh b/compiled_starters/scala/download_sample_databases.sh new file mode 100755 index 0000000..03e0573 --- /dev/null +++ b/compiled_starters/scala/download_sample_databases.sh @@ -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." diff --git a/compiled_starters/scala/project/build.properties b/compiled_starters/scala/project/build.properties new file mode 100644 index 0000000..73df629 --- /dev/null +++ b/compiled_starters/scala/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.10.7 diff --git a/compiled_starters/scala/project/plugins.sbt b/compiled_starters/scala/project/plugins.sbt new file mode 100644 index 0000000..d8de595 --- /dev/null +++ b/compiled_starters/scala/project/plugins.sbt @@ -0,0 +1,3 @@ + + +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") \ No newline at end of file diff --git a/compiled_starters/scala/sample.db b/compiled_starters/scala/sample.db new file mode 100644 index 0000000000000000000000000000000000000000..687673ecc5f29d50c7d12b9228829e47ce210eff GIT binary patch literal 16384 zcmeI(&rTCT90%~(b}6Am$HbUg8)XQZKqw~a$ruv>OHIUofF_*C(Ecm~)7i2!EA+?{ z=o=Ux#FOzAJezp%1$+R1+m-}4c;H}S{7$kvJG(o7_H&8)v#HG$OIB<<>;?uknx zio_$Ogb-tV#C#m50$-dgN5>ETkdKMmPd{AX7m63Z2>D(<Z(B-I?K0&z2`lTNxLqL#gVe zrmNGw+M~YGSxAg)rPL)C>g5TiyQBgg=GSfVt<>~_N!;L8dG2MQRGFWTHU}O1eakcH zEl>LyxfY!%7EcZ2%){Bdn>yE=OQP{gwHh7WA2g|ZJxhLgE}Uv$c<-Da(n@TSjy@ru zo5%8#kU!)%`I#?}AOHafKmY;|fB*y_009U<00I#B`vTYESS(bViEWcpkI9;rNix4l z)+Ps)Vl1Yoo+OU?{Aa@VmTcyC)Ec!X3$eIzc_rI5T&P8{xHZ${|01rh*1IO`teVbF zP|cLc<@5i7{3+xwJ|RH>0uX=z1Rwwb2tWV=5P$##An-2>Ocsi@XteyHRE~<(Xt;tP z&gcInc~!{o@~iwP_v8u}kRSj72tWV=5P$##AOHafKmY>&i9o%?LG#9Q{f?}9G2Bvc zKoryghtHL_g8=%r34u~a-p`E1Vt(O`w!HG+=uw4j@*&??uXZyI+PltOj^t0jUbch1 z1;$b4Gi_~Od7?Hi^Y?YtNr^Y%=T*GZtv#-DlOz074>s~G4JlVS@j^Zt-n@{vO;rx} h*X9Oq*~AaVsa;zIorW76{J(zZ85cD-aH9UGgWr)S@BRP) literal 0 HcmV?d00001 diff --git a/compiled_starters/scala/src/main/scala/Main.scala b/compiled_starters/scala/src/main/scala/Main.scala new file mode 100644 index 0000000..059d3c9 --- /dev/null +++ b/compiled_starters/scala/src/main/scala/Main.scala @@ -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 and ") + 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) + } +} diff --git a/compiled_starters/scala/your_program.sh b/compiled_starters/scala/your_program.sh new file mode 100755 index 0000000..ad509ce --- /dev/null +++ b/compiled_starters/scala/your_program.sh @@ -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 "$@" diff --git a/solutions/scala/01-dr6/diff/src/main/java/Main.java.diff b/solutions/scala/01-dr6/diff/src/main/java/Main.java.diff deleted file mode 100644 index 737356e..0000000 --- a/solutions/scala/01-dr6/diff/src/main/java/Main.java.diff +++ /dev/null @@ -1,40 +0,0 @@ -@@ -2,38 +2,34 @@ - import java.io.FileInputStream; - import java.io.IOException; - import java.nio.ByteBuffer; - - public class Main { - public static void main(String[] args){ - if (args.length < 2) { - System.out.println("Missing and "); - return; - } - - String databaseFilePath = args[0]; - String command = args[1]; - - switch (command) { - case ".dbinfo" -> { - try { - FileInputStream databaseFile = new FileInputStream(new File(databaseFilePath)); - - databaseFile.skip(16); // Skip the first 16 bytes of the header - byte[] pageSizeBytes = new byte[2]; // The following 2 bytes are the page size - databaseFile.read(pageSizeBytes); - short pageSizeSigned = ByteBuffer.wrap(pageSizeBytes).getShort(); - int pageSize = Short.toUnsignedInt(pageSizeSigned); - -- // You can use print statements as follows for debugging, they'll be visible when running tests. -- System.err.println("Logs from your program will appear here!"); -- -- // Uncomment this block to pass the first stage -- // System.out.println("database page size: " + pageSize); -+ System.out.println("database page size: " + pageSize); - } catch (IOException e) { - System.out.println("Error reading file: " + e.getMessage()); - } - } - default -> System.out.println("Missing or invalid command passed: " + command); - } - } - } diff --git a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff new file mode 100644 index 0000000..dbccba1 --- /dev/null +++ b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff @@ -0,0 +1,35 @@ +--- starter_templates/scala/code/src/main/scala/Main.scala 2025-03-27 19:30:51.374671531 -0400 ++++ solutions/scala/01-dr6/code/src/main/scala/Main.scala 2025-03-27 19:22:20.930550947 -0400 +@@ -1,31 +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 and ") + 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 +- +- // You can use print statements as follows for debugging, they'll be visible when running tests. +- System.err.println("Logs from your program will appear here!") +- +- // Uncomment this block to pass the first stage +- // println("database page size: " + pageSize) ++ println("database page size: " + pageSize) + } + case _ => println("Missing or invalid command passed: " + command) + } + } diff --git a/starter_templates/scala/code/src/main/scala/Main.scala b/starter_templates/scala/code/src/main/scala/Main.scala index 059d3c9..afd7b89 100644 --- a/starter_templates/scala/code/src/main/scala/Main.scala +++ b/starter_templates/scala/code/src/main/scala/Main.scala @@ -19,7 +19,12 @@ object Main extends App { databaseFile.read(pageSizeBytes) val pageSizeSigned = ByteBuffer.wrap(pageSizeBytes).getShort() val pageSize = pageSizeSigned & 0xFFFF - println("database page size: " + pageSize) + + // You can use print statements as follows for debugging, they'll be visible when running tests. + System.err.println("Logs from your program will appear here!") + + // Uncomment this block to pass the first stage + // println("database page size: " + pageSize) } case _ => println("Missing or invalid command passed: " + command) } From 034bb576e5c9d3fa68e439e82534423fbc71b0c9 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 19:53:10 -0400 Subject: [PATCH 05/12] Remove overcoled codecrafters.yml in starter templates of scala --- starter_templates/scala/code/codecrafters.yml | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 starter_templates/scala/code/codecrafters.yml diff --git a/starter_templates/scala/code/codecrafters.yml b/starter_templates/scala/code/codecrafters.yml deleted file mode 100644 index b4ddb38..0000000 --- a/starter_templates/scala/code/codecrafters.yml +++ /dev/null @@ -1,11 +0,0 @@ -# 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 From 9241bf4bb84d9507b6a6f21203a5a49e7be25c35 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 19:54:10 -0400 Subject: [PATCH 06/12] Delete unncessary .gitattributes in scala --- starter_templates/scala/code/.gitattributes | 1 - 1 file changed, 1 deletion(-) delete mode 100644 starter_templates/scala/code/.gitattributes diff --git a/starter_templates/scala/code/.gitattributes b/starter_templates/scala/code/.gitattributes deleted file mode 100644 index 176a458..0000000 --- a/starter_templates/scala/code/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -* text=auto From 362f57c2e482b596809d2ce8812fa9d312b53422 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 20:00:25 -0400 Subject: [PATCH 07/12] Compile scala using course-sdk --- compiled_starters/scala/.gitignore | 3 +++ compiled_starters/scala/README.md | 8 ++++---- compiled_starters/scala/codecrafters.yml | 6 +++--- compiled_starters/scala/src/main/scala/Main.scala | 7 ++++++- solutions/scala/01-dr6/code/.gitignore | 3 +++ solutions/scala/01-dr6/code/README.md | 8 ++++---- solutions/scala/01-dr6/code/codecrafters.yml | 6 +++--- solutions/scala/01-dr6/code/src/main/scala/Main.scala | 1 + .../scala/01-dr6/diff/src/main/scala/Main.scala.diff | 8 +++----- solutions/scala/01-dr6/explanation.md | 2 +- 10 files changed, 31 insertions(+), 21 deletions(-) diff --git a/compiled_starters/scala/.gitignore b/compiled_starters/scala/.gitignore index 5b4f849..1adb3ca 100644 --- a/compiled_starters/scala/.gitignore +++ b/compiled_starters/scala/.gitignore @@ -1,3 +1,6 @@ +# Database files used for testing +*.db + **/target /.bloop/ /.bsp/ diff --git a/compiled_starters/scala/README.md b/compiled_starters/scala/README.md index f6fcb4a..cf4c638 100644 --- a/compiled_starters/scala/README.md +++ b/compiled_starters/scala/README.md @@ -15,9 +15,9 @@ and more. # 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: +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 @@ -30,7 +30,7 @@ Time to move on to the next stage! Note: This section is for stages 2 and beyond. -1. Ensure you have `mvn` installed locally +1. Ensure you have `sbt (1.10.7)` 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 diff --git a/compiled_starters/scala/codecrafters.yml b/compiled_starters/scala/codecrafters.yml index b4ddb38..28f6b84 100644 --- a/compiled_starters/scala/codecrafters.yml +++ b/compiled_starters/scala/codecrafters.yml @@ -4,8 +4,8 @@ # unless you really need them. debug: false -# Use this to change the Java version used to run your code +# Use this to change the Scala version used to run your code # on Codecrafters. # -# Available versions: java-23 -language_pack: java-17 +# Available versions: scala-3.3.5 +language_pack: scala-3.3.5 diff --git a/compiled_starters/scala/src/main/scala/Main.scala b/compiled_starters/scala/src/main/scala/Main.scala index 059d3c9..afd7b89 100644 --- a/compiled_starters/scala/src/main/scala/Main.scala +++ b/compiled_starters/scala/src/main/scala/Main.scala @@ -19,7 +19,12 @@ object Main extends App { databaseFile.read(pageSizeBytes) val pageSizeSigned = ByteBuffer.wrap(pageSizeBytes).getShort() val pageSize = pageSizeSigned & 0xFFFF - println("database page size: " + pageSize) + + // You can use print statements as follows for debugging, they'll be visible when running tests. + System.err.println("Logs from your program will appear here!") + + // Uncomment this block to pass the first stage + // println("database page size: " + pageSize) } case _ => println("Missing or invalid command passed: " + command) } diff --git a/solutions/scala/01-dr6/code/.gitignore b/solutions/scala/01-dr6/code/.gitignore index 5b4f849..1adb3ca 100644 --- a/solutions/scala/01-dr6/code/.gitignore +++ b/solutions/scala/01-dr6/code/.gitignore @@ -1,3 +1,6 @@ +# Database files used for testing +*.db + **/target /.bloop/ /.bsp/ diff --git a/solutions/scala/01-dr6/code/README.md b/solutions/scala/01-dr6/code/README.md index f6fcb4a..cf4c638 100644 --- a/solutions/scala/01-dr6/code/README.md +++ b/solutions/scala/01-dr6/code/README.md @@ -15,9 +15,9 @@ and more. # 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: +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 @@ -30,7 +30,7 @@ Time to move on to the next stage! Note: This section is for stages 2 and beyond. -1. Ensure you have `mvn` installed locally +1. Ensure you have `sbt (1.10.7)` 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 diff --git a/solutions/scala/01-dr6/code/codecrafters.yml b/solutions/scala/01-dr6/code/codecrafters.yml index b4ddb38..28f6b84 100644 --- a/solutions/scala/01-dr6/code/codecrafters.yml +++ b/solutions/scala/01-dr6/code/codecrafters.yml @@ -4,8 +4,8 @@ # unless you really need them. debug: false -# Use this to change the Java version used to run your code +# Use this to change the Scala version used to run your code # on Codecrafters. # -# Available versions: java-23 -language_pack: java-17 +# Available versions: scala-3.3.5 +language_pack: scala-3.3.5 diff --git a/solutions/scala/01-dr6/code/src/main/scala/Main.scala b/solutions/scala/01-dr6/code/src/main/scala/Main.scala index 059d3c9..67d507a 100644 --- a/solutions/scala/01-dr6/code/src/main/scala/Main.scala +++ b/solutions/scala/01-dr6/code/src/main/scala/Main.scala @@ -19,6 +19,7 @@ object Main extends App { 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) diff --git a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff index dbccba1..5e43a74 100644 --- a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff +++ b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff @@ -1,11 +1,9 @@ ---- starter_templates/scala/code/src/main/scala/Main.scala 2025-03-27 19:30:51.374671531 -0400 -+++ solutions/scala/01-dr6/code/src/main/scala/Main.scala 2025-03-27 19:22:20.930550947 -0400 -@@ -1,31 +1,26 @@ +@@ -1,31 +1,27 @@ 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 { @@ -22,7 +20,7 @@ databaseFile.read(pageSizeBytes) val pageSizeSigned = ByteBuffer.wrap(pageSizeBytes).getShort() val pageSize = pageSizeSigned & 0xFFFF -- + - // You can use print statements as follows for debugging, they'll be visible when running tests. - System.err.println("Logs from your program will appear here!") - diff --git a/solutions/scala/01-dr6/explanation.md b/solutions/scala/01-dr6/explanation.md index 81b0f43..1d87a1d 100644 --- a/solutions/scala/01-dr6/explanation.md +++ b/solutions/scala/01-dr6/explanation.md @@ -4,7 +4,7 @@ Study and uncomment the relevant code: ```scala // Uncomment this block to pass the first stage -println("database page size: " + pageSize); +println("database page size: " + pageSize) ``` Push your changes to pass the first stage: From c14d6065105ec21dafea85f0474f27917720b99c Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Thu, 27 Mar 2025 20:44:33 -0400 Subject: [PATCH 08/12] Fix oversight in scala dockerfile --- dockerfiles/scala-3.3.5.Dockerfile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dockerfiles/scala-3.3.5.Dockerfile b/dockerfiles/scala-3.3.5.Dockerfile index 6baf9ee..2df9e9b 100644 --- a/dockerfiles/scala-3.3.5.Dockerfile +++ b/dockerfiles/scala-3.3.5.Dockerfile @@ -1,5 +1,11 @@ # syntax=docker/dockerfile:1.7-labs -FROM maven:3.9.9-eclipse-temurin-17-alpine +FROM maven:3.9.9-eclipse-temurin-23-alpine + +RUN apk add --no-cache bash wget tar && \ + wget -O sbt.tgz https://github.com/sbt/sbt/releases/download/v1.10.11/sbt-1.10.11.tgz && \ + tar -xzf sbt.tgz -C /usr/local && \ + ln -s /usr/local/sbt/bin/sbt /usr/local/bin/sbt && \ + rm sbt.tgz # Ensures the container is re-built if dependency files change ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="build.sbt" From 0b0a78a21d00d5ba6b6a76d51242c5f4979f4cc6 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez <74797568+cheerio-pixel@users.noreply.github.com> Date: Fri, 28 Mar 2025 08:19:15 -0400 Subject: [PATCH 09/12] Update solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .../diff/src/main/scala/Main.scala.diff | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff index 5e43a74..ad5ca38 100644 --- a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff +++ b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff @@ -14,12 +14,28 @@ 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 + 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 ++ var databaseFile: FileInputStream = null ++ try { ++ 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) ++ } finally { ++ if (databaseFile != null) { ++ databaseFile.close() ++ } ++ } + } - // You can use print statements as follows for debugging, they'll be visible when running tests. - System.err.println("Logs from your program will appear here!") From a8d8e77c08b724ef1d982e11fcbab47b676c9755 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez <74797568+cheerio-pixel@users.noreply.github.com> Date: Fri, 28 Mar 2025 08:20:33 -0400 Subject: [PATCH 10/12] Update Main.scala.diff --- solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff | 1 - 1 file changed, 1 deletion(-) diff --git a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff index ad5ca38..dfa60f9 100644 --- a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff +++ b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff @@ -14,7 +14,6 @@ val command = args(1); command match { case ".dbinfo" => { - case ".dbinfo" => { - val databaseFile = new FileInputStream(new File(databaseFilePath)) - databaseFile.skip(16) - val pageSizeBytes = new Array[Byte](2) From b91f12f0950fd5a71bff5df075e3904716f5f967 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Fri, 28 Mar 2025 08:41:47 -0400 Subject: [PATCH 11/12] Use Using class for resource management --- .../scala/code/src/main/scala/Main.scala | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/starter_templates/scala/code/src/main/scala/Main.scala b/starter_templates/scala/code/src/main/scala/Main.scala index 059d3c9..545d772 100644 --- a/starter_templates/scala/code/src/main/scala/Main.scala +++ b/starter_templates/scala/code/src/main/scala/Main.scala @@ -2,6 +2,7 @@ import java.io.File import java.io.FileInputStream import java.io.IOException import java.nio.ByteBuffer +import scala.util.Using object Main extends App { if args.length < 2 @@ -13,13 +14,18 @@ object Main extends App { 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) + Using(new FileInputStream(new File(databaseFilePath))) { databaseFile => + 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) + }.recover { + case e: IOException => + println(s"Error reading database file: ${e.getMessage}") + System.exit(1) + } } case _ => println("Missing or invalid command passed: " + command) } From c12ccc1ad53dc70865e8c33ac47f3241670524b5 Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez Date: Fri, 28 Mar 2025 08:44:11 -0400 Subject: [PATCH 12/12] Compile scala course --- .../scala/src/main/scala/Main.scala | 8 ++++- .../01-dr6/code/src/main/scala/Main.scala | 8 ++++- .../diff/src/main/scala/Main.scala.diff | 35 +++++++------------ 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/compiled_starters/scala/src/main/scala/Main.scala b/compiled_starters/scala/src/main/scala/Main.scala index afd7b89..161e44e 100644 --- a/compiled_starters/scala/src/main/scala/Main.scala +++ b/compiled_starters/scala/src/main/scala/Main.scala @@ -2,6 +2,7 @@ import java.io.File import java.io.FileInputStream import java.io.IOException import java.nio.ByteBuffer +import scala.util.Using object Main extends App { if args.length < 2 @@ -13,7 +14,7 @@ object Main extends App { val command = args(1); command match { case ".dbinfo" => { - val databaseFile = new FileInputStream(new File(databaseFilePath)) + Using(new FileInputStream(new File(databaseFilePath))) { databaseFile => databaseFile.skip(16) val pageSizeBytes = new Array[Byte](2) databaseFile.read(pageSizeBytes) @@ -25,6 +26,11 @@ object Main extends App { // Uncomment this block to pass the first stage // println("database page size: " + pageSize) + }.recover { + case e: IOException => + println(s"Error reading database file: ${e.getMessage}") + System.exit(1) + } } case _ => println("Missing or invalid command passed: " + command) } diff --git a/solutions/scala/01-dr6/code/src/main/scala/Main.scala b/solutions/scala/01-dr6/code/src/main/scala/Main.scala index 67d507a..263a80d 100644 --- a/solutions/scala/01-dr6/code/src/main/scala/Main.scala +++ b/solutions/scala/01-dr6/code/src/main/scala/Main.scala @@ -2,6 +2,7 @@ import java.io.File import java.io.FileInputStream import java.io.IOException import java.nio.ByteBuffer +import scala.util.Using object Main extends App { if args.length < 2 @@ -13,7 +14,7 @@ object Main extends App { val command = args(1); command match { case ".dbinfo" => { - val databaseFile = new FileInputStream(new File(databaseFilePath)) + Using(new FileInputStream(new File(databaseFilePath))) { databaseFile => databaseFile.skip(16) val pageSizeBytes = new Array[Byte](2) databaseFile.read(pageSizeBytes) @@ -21,6 +22,11 @@ object Main extends App { val pageSize = pageSizeSigned & 0xFFFF println("database page size: " + pageSize) + }.recover { + case e: IOException => + println(s"Error reading database file: ${e.getMessage}") + System.exit(1) + } } case _ => println("Missing or invalid command passed: " + command) } diff --git a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff index dfa60f9..5d3e88a 100644 --- a/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff +++ b/solutions/scala/01-dr6/diff/src/main/scala/Main.scala.diff @@ -1,8 +1,9 @@ -@@ -1,31 +1,27 @@ +@@ -1,37 +1,33 @@ import java.io.File import java.io.FileInputStream import java.io.IOException import java.nio.ByteBuffer + import scala.util.Using object Main extends App { if args.length < 2 @@ -14,27 +15,12 @@ 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 -+ var databaseFile: FileInputStream = null -+ try { -+ 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) -+ } finally { -+ if (databaseFile != null) { -+ databaseFile.close() -+ } -+ } - } + Using(new FileInputStream(new File(databaseFilePath))) { databaseFile => + databaseFile.skip(16) + val pageSizeBytes = new Array[Byte](2) + databaseFile.read(pageSizeBytes) + val pageSizeSigned = ByteBuffer.wrap(pageSizeBytes).getShort() + val pageSize = pageSizeSigned & 0xFFFF - // You can use print statements as follows for debugging, they'll be visible when running tests. - System.err.println("Logs from your program will appear here!") @@ -42,6 +28,11 @@ - // Uncomment this block to pass the first stage - // println("database page size: " + pageSize) + println("database page size: " + pageSize) + }.recover { + case e: IOException => + println(s"Error reading database file: ${e.getMessage}") + System.exit(1) + } } case _ => println("Missing or invalid command passed: " + command) }