Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiled_starters/java/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions compiled_starters/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java_sqlite.jar
target/*
target/
.idea/
78 changes: 78 additions & 0 deletions compiled_starters/java/README.md
Original file line number Diff line number Diff line change
@@ -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`.
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).
11 changes: 11 additions & 0 deletions compiled_starters/java/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Java version used to run your code
# on Codecrafters.
#
# Available versions: java-21
language_pack: java-21
9 changes: 9 additions & 0 deletions compiled_starters/java/download_sample_databases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

echo "Downloading superheroes.db: ~1MB (used in stage 7)"
curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db

echo "Downloading companies.db: ~7MB (used in stage 8)"
curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db

echo "Sample databases downloaded."
52 changes: 52 additions & 0 deletions compiled_starters/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.codecrafters</groupId>
<artifactId>build-your-own-sqlite</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>java_sqlite</finalName> <!-- Please do not change this final artifact name-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the main class of your program which will be executed-->
<mainClass>Main</mainClass>
</manifest>
</archive>
<outputDirectory>${dir}</outputDirectory>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Binary file added compiled_starters/java/sample.db
Binary file not shown.
38 changes: 38 additions & 0 deletions compiled_starters/java/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 <database path> and <command>");
return;
}

String databaseFilePath = args[0];
String command = args[1];

switch (command) {
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.
// '& 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!");

// 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: " + command);
}
}
}
10 changes: 10 additions & 0 deletions compiled_starters/java/your_sqlite3.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
2 changes: 1 addition & 1 deletion compiled_starters/javascript/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ languages:
release_status: "alpha"
alpha_tester_usernames: ["Terky"]
- slug: "zig"
- slug: "java"
release_status: "beta"

marketing:
difficulty: hard
Expand Down
16 changes: 16 additions & 0 deletions dockerfiles/java-21.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions solutions/cpp/01-init/diff/src/Server.cpp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

- // 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<unsigned char>(buffer[1]) | (static_cast<unsigned char>(buffer[0]) << 8));
- //
- //
- // std::cout << "database page size: " << page_size << std::endl;
+ database_file.seekg(16); // Skip the first 16 bytes of the header
+
Expand Down
23 changes: 23 additions & 0 deletions solutions/cpp/01-init/explanation.md
Original file line number Diff line number Diff line change
@@ -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<unsigned char>(buffer[1]) | (static_cast<unsigned char>(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
```
68 changes: 34 additions & 34 deletions solutions/csharp/01-init/diff/src/Program.cs.diff
Original file line number Diff line number Diff line change
@@ -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 <database path> and <command>"),
1 => throw new InvalidOperationException("Missing <command>"),
_ => (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 <database path> and <command>"),
1 => throw new InvalidOperationException("Missing <command>"),
_ => (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}");
}
1 change: 1 addition & 0 deletions solutions/java/01-init/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions solutions/java/01-init/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java_sqlite.jar
target/*
target/
.idea/
Loading