Skip to content

Commit 27bc0fb

Browse files
authored
Merge pull request #52 from urielsalis/java-support
Add java support
2 parents 3a78e2f + 0a16c5d commit 27bc0fb

File tree

36 files changed

+723
-44
lines changed

36 files changed

+723
-44
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/java/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
java_sqlite.jar
2+
target/*
3+
target/
4+
.idea/

compiled_starters/java/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png)
2+
3+
This is a starting point for Java solutions to the
4+
["Build Your Own SQLite" Challenge](https://codecrafters.io/challenges/sqlite).
5+
6+
In this challenge, you'll build a barebones SQLite implementation that supports
7+
basic SQL queries like `SELECT`. Along the way we'll learn about
8+
[SQLite's file format](https://www.sqlite.org/fileformat.html), how indexed data
9+
is
10+
[stored in B-trees](https://jvns.ca/blog/2014/10/02/how-does-sqlite-work-part-2-btrees/)
11+
and more.
12+
13+
**Note**: If you're viewing this repo on GitHub, head over to
14+
[codecrafters.io](https://codecrafters.io) to try the challenge.
15+
16+
# Passing the first stage
17+
18+
The entry point for your SQLite implementation is in `src/main/java/Main.java`.
19+
Study and uncomment the relevant code, and push your changes to pass the first
20+
stage:
21+
22+
```sh
23+
git add .
24+
git commit -m "pass 1st stage" # any msg
25+
git push origin master
26+
```
27+
28+
Time to move on to the next stage!
29+
30+
# Stage 2 & beyond
31+
32+
Note: This section is for stages 2 and beyond.
33+
34+
1. Ensure you have `java (21)` installed locally
35+
1. Run `./your_sqlite3.sh` to run your program, which is implemented in
36+
`src/main/java/Main.java`.
37+
1. Commit your changes and run `git push origin master` to submit your solution
38+
to CodeCrafters. Test output will be streamed to your terminal.
39+
40+
# Sample Databases
41+
42+
To make it easy to test queries locally, we've added a sample database in the
43+
root of this repository: `sample.db`.
44+
45+
This contains two tables: `apples` & `oranges`. You can use this to test your
46+
implementation for the first 6 stages.
47+
48+
You can explore this database by running queries against it like this:
49+
50+
```sh
51+
$ sqlite3 sample.db "select id, name from apples"
52+
1|Granny Smith
53+
2|Fuji
54+
3|Honeycrisp
55+
4|Golden Delicious
56+
```
57+
58+
There are two other databases that you can use:
59+
60+
1. `superheroes.db`:
61+
- This is a small version of the test database used in the table-scan stage.
62+
- It contains one table: `superheroes`.
63+
- It is ~1MB in size.
64+
1. `companies.db`:
65+
- This is a small version of the test database used in the index-scan stage.
66+
- It contains one table: `companies`, and one index: `idx_companies_country`
67+
- It is ~7MB in size.
68+
69+
These aren't included in the repository because they're large in size. You can
70+
download them by running this script:
71+
72+
```sh
73+
./download_sample_databases.sh
74+
```
75+
76+
If the script doesn't work for some reason, you can download the databases
77+
directly from
78+
[codecrafters-io/sample-sqlite-databases](https://github.com/codecrafters-io/sample-sqlite-databases).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the Java version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: java-21
11+
language_pack: java-21
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
echo "Downloading superheroes.db: ~1MB (used in stage 7)"
4+
curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db
5+
6+
echo "Downloading companies.db: ~7MB (used in stage 8)"
7+
curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db
8+
9+
echo "Sample databases downloaded."

compiled_starters/java/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.codecrafters</groupId>
8+
<artifactId>build-your-own-sqlite</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<java.version>21</java.version>
16+
</properties>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-assembly-plugin</artifactId>
23+
<configuration>
24+
<finalName>java_sqlite</finalName> <!-- Please do not change this final artifact name-->
25+
<descriptorRefs>
26+
<descriptorRef>jar-with-dependencies</descriptorRef>
27+
</descriptorRefs>
28+
<appendAssemblyId>false</appendAssemblyId>
29+
<archive>
30+
<manifest>
31+
<addClasspath>true</addClasspath>
32+
<!-- This is the main class of your program which will be executed-->
33+
<mainClass>Main</mainClass>
34+
</manifest>
35+
</archive>
36+
<outputDirectory>${dir}</outputDirectory>
37+
</configuration>
38+
39+
<executions>
40+
<execution>
41+
<id>make-assembly</id>
42+
<phase>package</phase>
43+
<goals>
44+
<goal>single</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>

compiled_starters/java/sample.db

16 KB
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.io.IOException;
2+
import java.nio.ByteBuffer;
3+
import java.nio.ByteOrder;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
7+
public class Main {
8+
public static void main(String[] args){
9+
if (args.length < 2) {
10+
System.out.println("Missing <database path> and <command>");
11+
return;
12+
}
13+
14+
String databaseFilePath = args[0];
15+
String command = args[1];
16+
17+
switch (command) {
18+
case ".dbinfo" -> {
19+
try {
20+
byte[] header = Files.readAllBytes(Path.of(databaseFilePath));
21+
22+
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order.
23+
// '& 0xFFFF' is used to convert the signed short to an unsigned int.
24+
int pageSize = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN).position(16).getShort() & 0xFFFF;
25+
26+
// You can use print statements as follows for debugging, they'll be visible when running tests.
27+
System.out.println("Logs from your program will appear here!");
28+
29+
// Uncomment this block to pass the first stage
30+
// System.out.println("database page size: " + pageSize);
31+
} catch (IOException e) {
32+
System.out.println("Error reading file: " + e.getMessage());
33+
}
34+
}
35+
default -> System.out.println("Missing or invalid command passed: " + command);
36+
}
37+
}
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
set -e
9+
mvn -B --quiet package -Ddir=/tmp/codecrafters-sqlite-target
10+
exec java -jar /tmp/codecrafters-sqlite-target/java_sqlite.jar "$@"

compiled_starters/javascript/app/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (command === ".dbinfo") {
99
const { buffer } = await databaseFileHandler.read({
1010
length: 100,
1111
position: 0,
12-
buffer: Buffer.alloc(100)
12+
buffer: Buffer.alloc(100),
1313
});
1414

1515
// You can use print statements as follows for debugging, they'll be visible when running tests.

0 commit comments

Comments
 (0)