From a47757344d5600b60b0c94d9e9463a6c564fb8df Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Sun, 19 Nov 2023 02:37:16 +0100 Subject: [PATCH 01/10] Add java support --- compiled_starters/java/.gitattributes | 1 + compiled_starters/java/.gitignore | 4 + compiled_starters/java/README.md | 78 ++++++++++++++++++ compiled_starters/java/codecrafters.yml | 11 +++ .../java/download_sample_databases.sh | 9 ++ compiled_starters/java/pom.xml | 52 ++++++++++++ compiled_starters/java/sample.db | Bin 0 -> 16384 bytes .../java/src/main/java/Main.java | 33 ++++++++ compiled_starters/java/your_sqlite3.sh | 10 +++ course-definition.yml | 2 + dockerfiles/java-21.Dockerfile | 16 ++++ solutions/java/01-init/code/.gitattributes | 1 + solutions/java/01-init/code/.gitignore | 4 + solutions/java/01-init/code/README.md | 78 ++++++++++++++++++ solutions/java/01-init/code/codecrafters.yml | 11 +++ .../01-init/code/download_sample_databases.sh | 9 ++ solutions/java/01-init/code/pom.xml | 52 ++++++++++++ solutions/java/01-init/code/sample.db | Bin 0 -> 16384 bytes .../java/01-init/code/src/main/java/Main.java | 29 +++++++ solutions/java/01-init/code/your_sqlite3.sh | 10 +++ .../01-init/diff/src/main/java/Main.java.diff | 15 ++++ starter-repository-definitions.yml | 26 +++++- starter_templates/java/.gitignore | 4 + starter_templates/java/pom.xml | 52 ++++++++++++ .../java/src/main/java/Main.java | 33 ++++++++ starter_templates/java/your_sqlite3.sh | 10 +++ 26 files changed, 549 insertions(+), 1 deletion(-) create mode 100644 compiled_starters/java/.gitattributes create mode 100644 compiled_starters/java/.gitignore create mode 100644 compiled_starters/java/README.md create mode 100644 compiled_starters/java/codecrafters.yml create mode 100755 compiled_starters/java/download_sample_databases.sh create mode 100644 compiled_starters/java/pom.xml create mode 100644 compiled_starters/java/sample.db create mode 100644 compiled_starters/java/src/main/java/Main.java create mode 100755 compiled_starters/java/your_sqlite3.sh create mode 100644 dockerfiles/java-21.Dockerfile create mode 100644 solutions/java/01-init/code/.gitattributes create mode 100644 solutions/java/01-init/code/.gitignore create mode 100644 solutions/java/01-init/code/README.md create mode 100644 solutions/java/01-init/code/codecrafters.yml create mode 100755 solutions/java/01-init/code/download_sample_databases.sh create mode 100644 solutions/java/01-init/code/pom.xml create mode 100644 solutions/java/01-init/code/sample.db create mode 100644 solutions/java/01-init/code/src/main/java/Main.java create mode 100755 solutions/java/01-init/code/your_sqlite3.sh create mode 100644 solutions/java/01-init/diff/src/main/java/Main.java.diff create mode 100644 starter_templates/java/.gitignore create mode 100644 starter_templates/java/pom.xml create mode 100644 starter_templates/java/src/main/java/Main.java create mode 100755 starter_templates/java/your_sqlite3.sh diff --git a/compiled_starters/java/.gitattributes b/compiled_starters/java/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/compiled_starters/java/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/compiled_starters/java/.gitignore b/compiled_starters/java/.gitignore new file mode 100644 index 0000000..74cc3e4 --- /dev/null +++ b/compiled_starters/java/.gitignore @@ -0,0 +1,4 @@ +java_sqlite.jar +target/* +target/ +.idea/ \ No newline at end of file diff --git a/compiled_starters/java/README.md b/compiled_starters/java/README.md new file mode 100644 index 0000000..e3ba8b5 --- /dev/null +++ b/compiled_starters/java/README.md @@ -0,0 +1,78 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png) + +This is a starting point for Java 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/java/Main.java`. 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 +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `java (21)` installed locally +1. Run `./your_sqlite3.sh` to run your program, which is implemented in + `src/main/java/Main.java`. This command compiles your Java project, so it might be slow + the first time you run it. Subsequent runs will be fast. +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/java/codecrafters.yml b/compiled_starters/java/codecrafters.yml new file mode 100644 index 0000000..9cc28db --- /dev/null +++ b/compiled_starters/java/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 Rust version used to run your code +# on Codecrafters. +# +# Available versions: java-21 +language_pack: java-21 diff --git a/compiled_starters/java/download_sample_databases.sh b/compiled_starters/java/download_sample_databases.sh new file mode 100755 index 0000000..03e0573 --- /dev/null +++ b/compiled_starters/java/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/java/pom.xml b/compiled_starters/java/pom.xml new file mode 100644 index 0000000..c483c23 --- /dev/null +++ b/compiled_starters/java/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + io.codecrafters + build-your-own-sqlite + 1.0 + + + 21 + 21 + UTF-8 + 21 + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + java_sqlite + + jar-with-dependencies + + false + + + true + + Main + + + ${dir} + + + + + make-assembly + package + + single + + + + + + + + \ No newline at end of file diff --git a/compiled_starters/java/sample.db b/compiled_starters/java/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/java/src/main/java/Main.java b/compiled_starters/java/src/main/java/Main.java new file mode 100644 index 0000000..690f96c --- /dev/null +++ b/compiled_starters/java/src/main/java/Main.java @@ -0,0 +1,33 @@ +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Files; +import java.nio.file.Path; + +public class Main { + public static void main(String[] args){ + if (args.length < 2) { + System.out.println("Missing and "); + return; + } + + switch (args[1]) { + case ".dbinfo" -> { + try { + byte[] header = Files.readAllBytes(Path.of(args[0])); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); + + // You can use print statements as follows for debugging, they'll be visible when running tests. + System.out.println("Logs from your program will appear here!"); + + // Uncomment this block to pass the first stage + // 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: " + args[1]); + } + } +} diff --git a/compiled_starters/java/your_sqlite3.sh b/compiled_starters/java/your_sqlite3.sh new file mode 100755 index 0000000..c0d5216 --- /dev/null +++ b/compiled_starters/java/your_sqlite3.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# +# DON'T EDIT THIS! +# +# CodeCrafters uses this file to test your code. Don't make any changes here! +# +# DON'T EDIT THIS! +set -e +mvn -B --quiet package -Ddir=/tmp/codecrafters-sqlite-target +exec java -jar /tmp/codecrafters-sqlite-target/java_sqlite.jar "$@" \ No newline at end of file diff --git a/course-definition.yml b/course-definition.yml index 1c9b10a..5f6753d 100644 --- a/course-definition.yml +++ b/course-definition.yml @@ -28,6 +28,8 @@ languages: release_status: "alpha" alpha_tester_usernames: ["Terky"] - slug: "zig" + - slug: "java" + release_status: "beta" marketing: difficulty: hard diff --git a/dockerfiles/java-21.Dockerfile b/dockerfiles/java-21.Dockerfile new file mode 100644 index 0000000..c4eed06 --- /dev/null +++ b/dockerfiles/java-21.Dockerfile @@ -0,0 +1,16 @@ +FROM maven:3.9.5-eclipse-temurin-21-alpine + +COPY pom.xml /app/pom.xml + +WORKDIR /app + +# Download the dependencies +RUN mvn -B package -Ddir=/tmp/codecrafters-sqlite-target + +# Cache Dependencies +RUN mkdir -p /app-cached +RUN mv /app/target /app-cached # Is this needed? + +# Pre-compile steps +RUN echo "cd \${CODECRAFTERS_SUBMISSION_DIR} && mvn -B package -Ddir=/tmp/codecrafters-sqlite-target && sed -i 's/^\(mvn .*\)/#\1/' ./your_sqlite3.sh" > /codecrafters-precompile.sh +RUN chmod +x /codecrafters-precompile.sh \ No newline at end of file diff --git a/solutions/java/01-init/code/.gitattributes b/solutions/java/01-init/code/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/solutions/java/01-init/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/java/01-init/code/.gitignore b/solutions/java/01-init/code/.gitignore new file mode 100644 index 0000000..74cc3e4 --- /dev/null +++ b/solutions/java/01-init/code/.gitignore @@ -0,0 +1,4 @@ +java_sqlite.jar +target/* +target/ +.idea/ \ No newline at end of file diff --git a/solutions/java/01-init/code/README.md b/solutions/java/01-init/code/README.md new file mode 100644 index 0000000..e3ba8b5 --- /dev/null +++ b/solutions/java/01-init/code/README.md @@ -0,0 +1,78 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png) + +This is a starting point for Java 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/java/Main.java`. 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 +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `java (21)` installed locally +1. Run `./your_sqlite3.sh` to run your program, which is implemented in + `src/main/java/Main.java`. This command compiles your Java project, so it might be slow + the first time you run it. Subsequent runs will be fast. +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/java/01-init/code/codecrafters.yml b/solutions/java/01-init/code/codecrafters.yml new file mode 100644 index 0000000..9cc28db --- /dev/null +++ b/solutions/java/01-init/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 Rust version used to run your code +# on Codecrafters. +# +# Available versions: java-21 +language_pack: java-21 diff --git a/solutions/java/01-init/code/download_sample_databases.sh b/solutions/java/01-init/code/download_sample_databases.sh new file mode 100755 index 0000000..03e0573 --- /dev/null +++ b/solutions/java/01-init/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/java/01-init/code/pom.xml b/solutions/java/01-init/code/pom.xml new file mode 100644 index 0000000..c483c23 --- /dev/null +++ b/solutions/java/01-init/code/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + io.codecrafters + build-your-own-sqlite + 1.0 + + + 21 + 21 + UTF-8 + 21 + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + java_sqlite + + jar-with-dependencies + + false + + + true + + Main + + + ${dir} + + + + + make-assembly + package + + single + + + + + + + + \ No newline at end of file diff --git a/solutions/java/01-init/code/sample.db b/solutions/java/01-init/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/java/01-init/code/src/main/java/Main.java b/solutions/java/01-init/code/src/main/java/Main.java new file mode 100644 index 0000000..114e67b --- /dev/null +++ b/solutions/java/01-init/code/src/main/java/Main.java @@ -0,0 +1,29 @@ +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Files; +import java.nio.file.Path; + +public class Main { + public static void main(String[] args){ + if (args.length < 2) { + System.out.println("Missing and "); + return; + } + + switch (args[1]) { + case ".dbinfo" -> { + try { + byte[] header = Files.readAllBytes(Path.of(args[0])); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); + + 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: " + args[1]); + } + } +} diff --git a/solutions/java/01-init/code/your_sqlite3.sh b/solutions/java/01-init/code/your_sqlite3.sh new file mode 100755 index 0000000..c0d5216 --- /dev/null +++ b/solutions/java/01-init/code/your_sqlite3.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# +# DON'T EDIT THIS! +# +# CodeCrafters uses this file to test your code. Don't make any changes here! +# +# DON'T EDIT THIS! +set -e +mvn -B --quiet package -Ddir=/tmp/codecrafters-sqlite-target +exec java -jar /tmp/codecrafters-sqlite-target/java_sqlite.jar "$@" \ No newline at end of file diff --git a/solutions/java/01-init/diff/src/main/java/Main.java.diff b/solutions/java/01-init/diff/src/main/java/Main.java.diff new file mode 100644 index 0000000..a452447 --- /dev/null +++ b/solutions/java/01-init/diff/src/main/java/Main.java.diff @@ -0,0 +1,15 @@ +@@ -17,12 +17,8 @@ + byte[] header = Files.readAllBytes(Path.of(args[0])); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); +- +- // You can use print statements as follows for debugging, they'll be visible when running tests. +- System.out.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()); + } \ No newline at end of file diff --git a/starter-repository-definitions.yml b/starter-repository-definitions.yml index aab47e5..addbd05 100644 --- a/starter-repository-definitions.yml +++ b/starter-repository-definitions.yml @@ -216,4 +216,28 @@ target: .gitattributes template_attributes: required_executable: "zig (0.11)" - user_editable_file: "app/main.zig" \ No newline at end of file + user_editable_file: "app/main.zig" + +- language: java + file_mappings: + - source: starter_templates/README.md + target: README.md + - source: starter_templates/codecrafters.yml + target: codecrafters.yml + - source: starter_templates/sample.db + target: sample.db + - source: starter_templates/download_sample_databases.sh + target: download_sample_databases.sh + - source: starter_templates/java/your_sqlite3.sh + target: your_sqlite3.sh + - source: starter_templates/java/.gitignore + target: .gitignore + - source: starter_templates/java/src/main/java/Main.java + target: src/main/java/Main.java + - source: starter_templates/java/pom.xml + target: pom.xml + - source: starter_templates/.gitattributes + target: .gitattributes + template_attributes: + required_executable: "java (21)" + user_editable_file: "src/main/java/Main.java" diff --git a/starter_templates/java/.gitignore b/starter_templates/java/.gitignore new file mode 100644 index 0000000..74cc3e4 --- /dev/null +++ b/starter_templates/java/.gitignore @@ -0,0 +1,4 @@ +java_sqlite.jar +target/* +target/ +.idea/ \ No newline at end of file diff --git a/starter_templates/java/pom.xml b/starter_templates/java/pom.xml new file mode 100644 index 0000000..c483c23 --- /dev/null +++ b/starter_templates/java/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + io.codecrafters + build-your-own-sqlite + 1.0 + + + 21 + 21 + UTF-8 + 21 + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + java_sqlite + + jar-with-dependencies + + false + + + true + + Main + + + ${dir} + + + + + make-assembly + package + + single + + + + + + + + \ No newline at end of file diff --git a/starter_templates/java/src/main/java/Main.java b/starter_templates/java/src/main/java/Main.java new file mode 100644 index 0000000..690f96c --- /dev/null +++ b/starter_templates/java/src/main/java/Main.java @@ -0,0 +1,33 @@ +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Files; +import java.nio.file.Path; + +public class Main { + public static void main(String[] args){ + if (args.length < 2) { + System.out.println("Missing and "); + return; + } + + switch (args[1]) { + case ".dbinfo" -> { + try { + byte[] header = Files.readAllBytes(Path.of(args[0])); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); + + // You can use print statements as follows for debugging, they'll be visible when running tests. + System.out.println("Logs from your program will appear here!"); + + // Uncomment this block to pass the first stage + // 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: " + args[1]); + } + } +} diff --git a/starter_templates/java/your_sqlite3.sh b/starter_templates/java/your_sqlite3.sh new file mode 100755 index 0000000..c0d5216 --- /dev/null +++ b/starter_templates/java/your_sqlite3.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# +# DON'T EDIT THIS! +# +# CodeCrafters uses this file to test your code. Don't make any changes here! +# +# DON'T EDIT THIS! +set -e +mvn -B --quiet package -Ddir=/tmp/codecrafters-sqlite-target +exec java -jar /tmp/codecrafters-sqlite-target/java_sqlite.jar "$@" \ No newline at end of file From cc8d17244dde9e1afc3e4b926d6b40c06bd9fe38 Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Sat, 25 Nov 2023 17:22:22 +0100 Subject: [PATCH 02/10] Changes from coursesdk --- compiled_starters/java/README.md | 8 +++--- compiled_starters/java/codecrafters.yml | 2 +- solutions/java/01-init/code/README.md | 8 +++--- solutions/java/01-init/code/codecrafters.yml | 2 +- .../01-init/diff/src/main/java/Main.java.diff | 28 ++++++++++++++++--- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/compiled_starters/java/README.md b/compiled_starters/java/README.md index e3ba8b5..a15dd6c 100644 --- a/compiled_starters/java/README.md +++ b/compiled_starters/java/README.md @@ -15,8 +15,9 @@ and more. # Passing the first stage -The entry point for your SQLite implementation is in `src/main/java/Main.java`. 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/java/Main.java`. +Study and uncomment the relevant code, and push your changes to pass the first +stage: ```sh git add . @@ -32,8 +33,7 @@ Note: This section is for stages 2 and beyond. 1. Ensure you have `java (21)` installed locally 1. Run `./your_sqlite3.sh` to run your program, which is implemented in - `src/main/java/Main.java`. This command compiles your Java project, so it might be slow - the first time you run it. Subsequent runs will be fast. + `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. diff --git a/compiled_starters/java/codecrafters.yml b/compiled_starters/java/codecrafters.yml index 9cc28db..7384f15 100644 --- a/compiled_starters/java/codecrafters.yml +++ b/compiled_starters/java/codecrafters.yml @@ -4,7 +4,7 @@ # unless you really need them. debug: false -# Use this to change the Rust version used to run your code +# Use this to change the Java version used to run your code # on Codecrafters. # # Available versions: java-21 diff --git a/solutions/java/01-init/code/README.md b/solutions/java/01-init/code/README.md index e3ba8b5..a15dd6c 100644 --- a/solutions/java/01-init/code/README.md +++ b/solutions/java/01-init/code/README.md @@ -15,8 +15,9 @@ and more. # Passing the first stage -The entry point for your SQLite implementation is in `src/main/java/Main.java`. 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/java/Main.java`. +Study and uncomment the relevant code, and push your changes to pass the first +stage: ```sh git add . @@ -32,8 +33,7 @@ Note: This section is for stages 2 and beyond. 1. Ensure you have `java (21)` installed locally 1. Run `./your_sqlite3.sh` to run your program, which is implemented in - `src/main/java/Main.java`. This command compiles your Java project, so it might be slow - the first time you run it. Subsequent runs will be fast. + `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. diff --git a/solutions/java/01-init/code/codecrafters.yml b/solutions/java/01-init/code/codecrafters.yml index 9cc28db..7384f15 100644 --- a/solutions/java/01-init/code/codecrafters.yml +++ b/solutions/java/01-init/code/codecrafters.yml @@ -4,7 +4,7 @@ # unless you really need them. debug: false -# Use this to change the Rust version used to run your code +# Use this to change the Java version used to run your code # on Codecrafters. # # Available versions: java-21 diff --git a/solutions/java/01-init/diff/src/main/java/Main.java.diff b/solutions/java/01-init/diff/src/main/java/Main.java.diff index a452447..507e5d0 100644 --- a/solutions/java/01-init/diff/src/main/java/Main.java.diff +++ b/solutions/java/01-init/diff/src/main/java/Main.java.diff @@ -1,15 +1,35 @@ -@@ -17,12 +17,8 @@ +@@ -1,33 +1,29 @@ + import java.io.IOException; + import java.nio.ByteBuffer; + import java.nio.ByteOrder; + import java.nio.file.Files; + import java.nio.file.Path; + + public class Main { + public static void main(String[] args){ + if (args.length < 2) { + System.out.println("Missing and "); + return; + } + + switch (args[1]) { + case ".dbinfo" -> { + try { byte[] header = Files.readAllBytes(Path.of(args[0])); // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); -- + - // You can use print statements as follows for debugging, they'll be visible when running tests. - System.out.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()); - } \ No newline at end of file + } + } + default -> System.out.println("Missing or invalid command passed: " + args[1]); + } + } + } From e30a40acf8e17a200cc8d56292a38d1d4603ddbe Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Sat, 25 Nov 2023 17:36:39 +0100 Subject: [PATCH 03/10] Changes from coursesdk --- starter_templates/codecrafters.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/starter_templates/codecrafters.yml b/starter_templates/codecrafters.yml index 27df7be..f990021 100644 --- a/starter_templates/codecrafters.yml +++ b/starter_templates/codecrafters.yml @@ -52,8 +52,8 @@ language_pack: elixir-1.10 language_pack: kotlin-1.4 {{/ language_is_kotlin }} {{# language_is_java }} -# Available versions: java-1.8 -language_pack: java-1.8 +# Available versions: java-21 +language_pack: java-21 {{/ language_is_java }} {{# language_is_nim }} # Available versions: nim-1.0 From 2217ec9d0469d595b4ccf1bdd962dd17757f3deb Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Tue, 28 Nov 2023 00:14:26 +0000 Subject: [PATCH 04/10] minor changes --- compiled_starters/java/sample.db | Bin 16384 -> 16419 bytes .../java/src/main/java/Main.java | 9 ++++++--- solutions/java/01-init/code/sample.db | Bin 16384 -> 16419 bytes .../java/01-init/code/src/main/java/Main.java | 9 ++++++--- .../01-init/diff/src/main/java/Main.java.diff | 11 +++++++---- solutions/java/01-init/explanation.md | 16 ++++++++++++++++ .../java/src/main/java/Main.java | 10 +++++++--- 7 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 solutions/java/01-init/explanation.md diff --git a/compiled_starters/java/sample.db b/compiled_starters/java/sample.db index 687673ecc5f29d50c7d12b9228829e47ce210eff..cb8778a0480e0746870dfc6170e73725403cd0f1 100644 GIT binary patch delta 134 zcmZo@U|ih5I3Y>$^n~~O_wq6@Ffj8084UbDg5MUz+$<;%$j4Q|%q}i1%?RXAKFxQU z8AMO#&ldsG5T!r{SPPKeET}M%fAT{4R32nmpy)OJi2Edjc;50|PVPVFvzwep|l7n+*j5`8X<=*~O)$85<{G<2wx`m+L2mD9*=WjMN_{0wY D&$A#; diff --git a/compiled_starters/java/src/main/java/Main.java b/compiled_starters/java/src/main/java/Main.java index 690f96c..e53ad72 100644 --- a/compiled_starters/java/src/main/java/Main.java +++ b/compiled_starters/java/src/main/java/Main.java @@ -11,10 +11,13 @@ public static void main(String[] args){ return; } - switch (args[1]) { + String databaseFilePath = args[0]; + String command = args[1]; + + switch (command) { case ".dbinfo" -> { try { - byte[] header = Files.readAllBytes(Path.of(args[0])); + byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); @@ -27,7 +30,7 @@ public static void main(String[] args){ System.out.println("Error reading file: " + e.getMessage()); } } - default -> System.out.println("Missing or invalid command passed: " + args[1]); + default -> System.out.println("Missing or invalid command passed: " + command); } } } diff --git a/solutions/java/01-init/code/sample.db b/solutions/java/01-init/code/sample.db index 687673ecc5f29d50c7d12b9228829e47ce210eff..cb8778a0480e0746870dfc6170e73725403cd0f1 100644 GIT binary patch delta 134 zcmZo@U|ih5I3Y>$^n~~O_wq6@Ffj8084UbDg5MUz+$<;%$j4Q|%q}i1%?RXAKFxQU z8AMO#&ldsG5T!r{SPPKeET}M%fAT{4R32nmpy)OJi2Edjc;50|PVPVFvzwep|l7n+*j5`8X<=*~O)$85<{G<2wx`m+L2mD9*=WjMN_{0wY D&$A#; diff --git a/solutions/java/01-init/code/src/main/java/Main.java b/solutions/java/01-init/code/src/main/java/Main.java index 114e67b..5ad2258 100644 --- a/solutions/java/01-init/code/src/main/java/Main.java +++ b/solutions/java/01-init/code/src/main/java/Main.java @@ -11,10 +11,13 @@ public static void main(String[] args){ return; } - switch (args[1]) { + String databaseFilePath = args[0]; + String command = args[1]; + + switch (command) { case ".dbinfo" -> { try { - byte[] header = Files.readAllBytes(Path.of(args[0])); + byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); @@ -23,7 +26,7 @@ public static void main(String[] args){ System.out.println("Error reading file: " + e.getMessage()); } } - default -> System.out.println("Missing or invalid command passed: " + args[1]); + default -> System.out.println("Missing or invalid command passed: " + command); } } } diff --git a/solutions/java/01-init/diff/src/main/java/Main.java.diff b/solutions/java/01-init/diff/src/main/java/Main.java.diff index 507e5d0..f2bfcc6 100644 --- a/solutions/java/01-init/diff/src/main/java/Main.java.diff +++ b/solutions/java/01-init/diff/src/main/java/Main.java.diff @@ -1,4 +1,4 @@ -@@ -1,33 +1,29 @@ +@@ -1,36 +1,32 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -12,10 +12,13 @@ return; } - switch (args[1]) { + String databaseFilePath = args[0]; + String command = args[1]; + + switch (command) { case ".dbinfo" -> { try { - byte[] header = Files.readAllBytes(Path.of(args[0])); + byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); @@ -29,7 +32,7 @@ System.out.println("Error reading file: " + e.getMessage()); } } - default -> System.out.println("Missing or invalid command passed: " + args[1]); + default -> System.out.println("Missing or invalid command passed: " + command); } } } diff --git a/solutions/java/01-init/explanation.md b/solutions/java/01-init/explanation.md new file mode 100644 index 0000000..7fbbf1a --- /dev/null +++ b/solutions/java/01-init/explanation.md @@ -0,0 +1,16 @@ +The entry point for your SQLite implementation is in `src/main/java/Main.java`. + +Study and uncomment the relevant code: + +```java +// Uncomment this block to pass the first stage +System.out.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 +``` diff --git a/starter_templates/java/src/main/java/Main.java b/starter_templates/java/src/main/java/Main.java index 690f96c..340cee0 100644 --- a/starter_templates/java/src/main/java/Main.java +++ b/starter_templates/java/src/main/java/Main.java @@ -11,10 +11,14 @@ public static void main(String[] args){ return; } - switch (args[1]) { + String databaseFilePath = args[0]; + String command = args[1]; + + switch (command) { case ".dbinfo" -> { try { - byte[] header = Files.readAllBytes(Path.of(args[0])); + byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); @@ -27,7 +31,7 @@ public static void main(String[] args){ System.out.println("Error reading file: " + e.getMessage()); } } - default -> System.out.println("Missing or invalid command passed: " + args[1]); + default -> System.out.println("Missing or invalid command passed: " + command); } } } From d76be0fcad73b606b2e1466ec7dd35c22343da5e Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 29 Nov 2023 15:49:39 +0000 Subject: [PATCH 05/10] update --- compiled_starters/java/src/main/java/Main.java | 1 + solutions/java/01-init/code/src/main/java/Main.java | 1 + solutions/java/01-init/diff/src/main/java/Main.java.diff | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/compiled_starters/java/src/main/java/Main.java b/compiled_starters/java/src/main/java/Main.java index e53ad72..340cee0 100644 --- a/compiled_starters/java/src/main/java/Main.java +++ b/compiled_starters/java/src/main/java/Main.java @@ -18,6 +18,7 @@ public static void main(String[] args){ case ".dbinfo" -> { try { byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); diff --git a/solutions/java/01-init/code/src/main/java/Main.java b/solutions/java/01-init/code/src/main/java/Main.java index 5ad2258..fd81f81 100644 --- a/solutions/java/01-init/code/src/main/java/Main.java +++ b/solutions/java/01-init/code/src/main/java/Main.java @@ -18,6 +18,7 @@ public static void main(String[] args){ case ".dbinfo" -> { try { byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); diff --git a/solutions/java/01-init/diff/src/main/java/Main.java.diff b/solutions/java/01-init/diff/src/main/java/Main.java.diff index f2bfcc6..04ed307 100644 --- a/solutions/java/01-init/diff/src/main/java/Main.java.diff +++ b/solutions/java/01-init/diff/src/main/java/Main.java.diff @@ -1,4 +1,4 @@ -@@ -1,36 +1,32 @@ +@@ -1,37 +1,33 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -19,6 +19,7 @@ case ".dbinfo" -> { try { byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); From 2745c0e6bfedf5419de00f758c552fd091cb5ec0 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 29 Nov 2023 16:04:53 +0000 Subject: [PATCH 06/10] add template eval skip --- starter-repository-definitions.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/starter-repository-definitions.yml b/starter-repository-definitions.yml index addbd05..ef48951 100644 --- a/starter-repository-definitions.yml +++ b/starter-repository-definitions.yml @@ -6,6 +6,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/python/app/main.py @@ -32,6 +33,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/go/app/main.go @@ -58,6 +60,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/javascript/app/main.js @@ -82,6 +85,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/rust/.gitignore @@ -108,6 +112,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/csharp/.gitignore @@ -134,6 +139,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/.gitignore @@ -156,6 +162,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/swift/.gitignore @@ -180,6 +187,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/cpp/.gitignore @@ -204,6 +212,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/zig/.gitignore @@ -226,6 +235,7 @@ target: codecrafters.yml - source: starter_templates/sample.db target: sample.db + should_skip_template_evaluation: true - source: starter_templates/download_sample_databases.sh target: download_sample_databases.sh - source: starter_templates/java/your_sqlite3.sh From bb14d5c11862d794e1b3a671760dbb3fda8f59f0 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 29 Nov 2023 16:13:01 +0000 Subject: [PATCH 07/10] Update database file handling and add debug statements --- compiled_starters/java/sample.db | Bin 16419 -> 16384 bytes compiled_starters/javascript/app/main.js | 2 +- .../cpp/01-init/diff/src/Server.cpp.diff | 6 +- solutions/cpp/01-init/explanation.md | 23 ++++++ .../csharp/01-init/diff/src/Program.cs.diff | 68 +++++++++--------- solutions/java/01-init/code/sample.db | Bin 16419 -> 16384 bytes solutions/javascript/01-init/code/app/main.js | 2 +- .../javascript/01-init/diff/app/main.js.diff | 2 +- solutions/zig/01-init/explanation.md | 42 +++++++++++ 9 files changed, 105 insertions(+), 40 deletions(-) create mode 100644 solutions/cpp/01-init/explanation.md create mode 100644 solutions/zig/01-init/explanation.md diff --git a/compiled_starters/java/sample.db b/compiled_starters/java/sample.db index cb8778a0480e0746870dfc6170e73725403cd0f1..687673ecc5f29d50c7d12b9228829e47ce210eff 100644 GIT binary patch delta 99 zcmZ47z}V2hI3Y>Edjc;50|PVPVFvzwep|l7n+*j5`8X<=*~O)$85<{G<2wx`m+L2mD9*=WjMN_{0wY D&$A#; delta 134 zcmZo@U|ih5I3Y>$^n~~O_wq6@Ffj8084UbDg5MUz+$<;%$j4Q|%q}i1%?RXAKFxQU z8AMO#&ldsG5T!r{SPPKeET}M%fAT{4R32nmpy)OJi2(buffer[1]) | (static_cast(buffer[0]) << 8)); -- // +- // - // std::cout << "database page size: " << page_size << std::endl; + database_file.seekg(16); // Skip the first 16 bytes of the header + diff --git a/solutions/cpp/01-init/explanation.md b/solutions/cpp/01-init/explanation.md new file mode 100644 index 0000000..9160aaa --- /dev/null +++ b/solutions/cpp/01-init/explanation.md @@ -0,0 +1,23 @@ +The entry point for your SQLite implementation is in `src/Server.cpp`. + +Study and uncomment the relevant code: + +```cpp +// Uncomment this to pass the first stage +database_file.seekg(16); // Skip the first 16 bytes of the header + +char buffer[2]; +database_file.read(buffer, 2); + +unsigned short page_size = (static_cast(buffer[1]) | (static_cast(buffer[0]) << 8)); + +std::cout << "database page size: " << page_size << std::endl; +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` diff --git a/solutions/csharp/01-init/diff/src/Program.cs.diff b/solutions/csharp/01-init/diff/src/Program.cs.diff index 8ec971e..a254409 100644 --- a/solutions/csharp/01-init/diff/src/Program.cs.diff +++ b/solutions/csharp/01-init/diff/src/Program.cs.diff @@ -1,35 +1,35 @@ @@ -1,29 +1,25 @@ - using static System.Buffers.Binary.BinaryPrimitives; - - // Parse arguments - var (path, command) = args.Length switch - { - 0 => throw new InvalidOperationException("Missing and "), - 1 => throw new InvalidOperationException("Missing "), - _ => (args[0], args[1]) - }; - - var databaseFile = File.OpenRead(path); - - // Parse command and act accordingly - if (command == ".dbinfo") - { -- // You can use print statements as follows for debugging, they'll be visible when running tests. -- Console.WriteLine("Logs from your program will appear here!"); -- -- // Uncomment this line to pass the first stage -- // databaseFile.Seek(16, SeekOrigin.Begin); // Skip the first 16 bytes -- // byte[] pageSizeBytes = new byte[2]; -- // databaseFile.Read(pageSizeBytes, 0, 2); -- // var pageSize = ReadUInt16BigEndian(pageSizeBytes); -- // Console.WriteLine($"database page size: {pageSize}"); -+ databaseFile.Seek(16, SeekOrigin.Begin); // Skip the first 16 bytes -+ byte[] pageSizeBytes = new byte[2]; -+ databaseFile.Read(pageSizeBytes, 0, 2); -+ var pageSize = ReadUInt16BigEndian(pageSizeBytes); -+ Console.WriteLine($"database page size: {pageSize}"); - } - else - { - throw new InvalidOperationException($"Invalid command: {command}"); - } + using static System.Buffers.Binary.BinaryPrimitives; + + // Parse arguments + var (path, command) = args.Length switch + { + 0 => throw new InvalidOperationException("Missing and "), + 1 => throw new InvalidOperationException("Missing "), + _ => (args[0], args[1]) + }; + + var databaseFile = File.OpenRead(path); + + // Parse command and act accordingly + if (command == ".dbinfo") + { +- // You can use print statements as follows for debugging, they'll be visible when running tests. +- Console.WriteLine("Logs from your program will appear here!"); +- +- // Uncomment this line to pass the first stage +- // databaseFile.Seek(16, SeekOrigin.Begin); // Skip the first 16 bytes +- // byte[] pageSizeBytes = new byte[2]; +- // databaseFile.Read(pageSizeBytes, 0, 2); +- // var pageSize = ReadUInt16BigEndian(pageSizeBytes); +- // Console.WriteLine($"database page size: {pageSize}"); ++ databaseFile.Seek(16, SeekOrigin.Begin); // Skip the first 16 bytes ++ byte[] pageSizeBytes = new byte[2]; ++ databaseFile.Read(pageSizeBytes, 0, 2); ++ var pageSize = ReadUInt16BigEndian(pageSizeBytes); ++ Console.WriteLine($"database page size: {pageSize}"); + } + else + { + throw new InvalidOperationException($"Invalid command: {command}"); + } diff --git a/solutions/java/01-init/code/sample.db b/solutions/java/01-init/code/sample.db index cb8778a0480e0746870dfc6170e73725403cd0f1..687673ecc5f29d50c7d12b9228829e47ce210eff 100644 GIT binary patch delta 99 zcmZ47z}V2hI3Y>Edjc;50|PVPVFvzwep|l7n+*j5`8X<=*~O)$85<{G<2wx`m+L2mD9*=WjMN_{0wY D&$A#; delta 134 zcmZo@U|ih5I3Y>$^n~~O_wq6@Ffj8084UbDg5MUz+$<;%$j4Q|%q}i1%?RXAKFxQU z8AMO#&ldsG5T!r{SPPKeET}M%fAT{4R32nmpy)OJi2 \n", .{args[0]}); + return; +} + +var database_file_path: []const u8 = args[1]; +var command: []const u8 = args[2]; + +if (std.mem.eql(u8, command, ".dbinfo")) { + var file = try std.fs.cwd().openFile(database_file_path, .{}); + defer file.close(); + + var buf: [2]u8 = undefined; + _ = try file.seekTo(16); + _ = try file.read(&buf); + const page_size = std.mem.readInt(u16, &buf, .Big); + try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size}); +} +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` From dddc74b43cd38c50bbac9ab31da81cde0cbdd71f Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 29 Nov 2023 16:21:36 +0000 Subject: [PATCH 08/10] Fix page size retrieval in Main.java --- compiled_starters/java/src/main/java/Main.java | 5 +++-- solutions/java/01-init/code/src/main/java/Main.java | 5 +++-- solutions/java/01-init/diff/src/main/java/Main.java.diff | 7 ++++--- starter_templates/java/src/main/java/Main.java | 5 +++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/compiled_starters/java/src/main/java/Main.java b/compiled_starters/java/src/main/java/Main.java index 340cee0..e4d6fe7 100644 --- a/compiled_starters/java/src/main/java/Main.java +++ b/compiled_starters/java/src/main/java/Main.java @@ -19,8 +19,9 @@ public static void main(String[] args){ try { byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); - // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order - int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order. + // '& 0xFFFF' is used to convert the signed short to an unsigned int. + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF; // You can use print statements as follows for debugging, they'll be visible when running tests. System.out.println("Logs from your program will appear here!"); diff --git a/solutions/java/01-init/code/src/main/java/Main.java b/solutions/java/01-init/code/src/main/java/Main.java index fd81f81..f0ca2e2 100644 --- a/solutions/java/01-init/code/src/main/java/Main.java +++ b/solutions/java/01-init/code/src/main/java/Main.java @@ -19,8 +19,9 @@ public static void main(String[] args){ try { byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); - // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order - int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order. + // '& 0xFFFF' is used to convert the signed short to an unsigned int. + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF; System.out.println("database page size: " + pageSize); } catch (IOException e) { diff --git a/solutions/java/01-init/diff/src/main/java/Main.java.diff b/solutions/java/01-init/diff/src/main/java/Main.java.diff index 04ed307..14cd5d2 100644 --- a/solutions/java/01-init/diff/src/main/java/Main.java.diff +++ b/solutions/java/01-init/diff/src/main/java/Main.java.diff @@ -1,4 +1,4 @@ -@@ -1,37 +1,33 @@ +@@ -1,38 +1,34 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -20,8 +20,9 @@ try { byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); - // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order - int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order. + // '& 0xFFFF' is used to convert the signed short to an unsigned int. + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF; - // You can use print statements as follows for debugging, they'll be visible when running tests. - System.out.println("Logs from your program will appear here!"); diff --git a/starter_templates/java/src/main/java/Main.java b/starter_templates/java/src/main/java/Main.java index 340cee0..e4d6fe7 100644 --- a/starter_templates/java/src/main/java/Main.java +++ b/starter_templates/java/src/main/java/Main.java @@ -19,8 +19,9 @@ public static void main(String[] args){ try { byte[] header = Files.readAllBytes(Path.of(databaseFilePath)); - // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order - int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort(); + // The page size is stored at the 16th byte offset, using 2 bytes in big-endian order. + // '& 0xFFFF' is used to convert the signed short to an unsigned int. + int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF; // You can use print statements as follows for debugging, they'll be visible when running tests. System.out.println("Logs from your program will appear here!"); From 1acac716b66878469c0104483a11152b82183e9c Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 29 Nov 2023 16:29:38 +0000 Subject: [PATCH 09/10] test From 0a16c5d128a98edf225ea520629ca68795c04127 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 29 Nov 2023 16:36:45 +0000 Subject: [PATCH 10/10] test --- starter_templates/javascript/app/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starter_templates/javascript/app/main.js b/starter_templates/javascript/app/main.js index 5fcdd28..4e5b751 100644 --- a/starter_templates/javascript/app/main.js +++ b/starter_templates/javascript/app/main.js @@ -9,7 +9,7 @@ if (command === ".dbinfo") { const { buffer } = await databaseFileHandler.read({ length: 100, position: 0, - buffer: Buffer.alloc(100) + buffer: Buffer.alloc(100), }); // You can use print statements as follows for debugging, they'll be visible when running tests.