Skip to content

Commit eb6b52c

Browse files
authored
Merge pull request #69 from codecrafters-io/CC-1158-gleam
CC-1158: Add Gleam support to SQLite
2 parents 093f5bd + 9314ecc commit eb6b52c

27 files changed

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

compiled_starters/gleam/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.beam
2+
*.ez
3+
/build
4+
erl_crash.dump

compiled_starters/gleam/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png)
2+
3+
This is a starting point for Gleam 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/sqlite.gleam`. Study
19+
and uncomment the relevant code, and push your changes to pass the first stage:
20+
21+
```sh
22+
git add .
23+
git commit -m "pass 1st stage" # any msg
24+
git push origin master
25+
```
26+
27+
Time to move on to the next stage!
28+
29+
# Stage 2 & beyond
30+
31+
Note: This section is for stages 2 and beyond.
32+
33+
1. Ensure you have `gleam (1.0)` installed locally
34+
1. Run `./your_sqlite3.sh` to run your program, which is implemented in
35+
`src/sqlite.gleam`.
36+
1. Commit your changes and run `git push origin master` to submit your solution
37+
to CodeCrafters. Test output will be streamed to your terminal.
38+
39+
# Sample Databases
40+
41+
To make it easy to test queries locally, we've added a sample database in the
42+
root of this repository: `sample.db`.
43+
44+
This contains two tables: `apples` & `oranges`. You can use this to test your
45+
implementation for the first 6 stages.
46+
47+
You can explore this database by running queries against it like this:
48+
49+
```sh
50+
$ sqlite3 sample.db "select id, name from apples"
51+
1|Granny Smith
52+
2|Fuji
53+
3|Honeycrisp
54+
4|Golden Delicious
55+
```
56+
57+
There are two other databases that you can use:
58+
59+
1. `superheroes.db`:
60+
- This is a small version of the test database used in the table-scan stage.
61+
- It contains one table: `superheroes`.
62+
- It is ~1MB in size.
63+
1. `companies.db`:
64+
- This is a small version of the test database used in the index-scan stage.
65+
- It contains one table: `companies`, and one index: `idx_companies_country`
66+
- It is ~7MB in size.
67+
68+
These aren't included in the repository because they're large in size. You can
69+
download them by running this script:
70+
71+
```sh
72+
./download_sample_databases.sh
73+
```
74+
75+
If the script doesn't work for some reason, you can download the databases
76+
directly from
77+
[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 Gleam version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: gleam-1.0
11+
language_pack: gleam-1.0

compiled_starters/gleam/gleam.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name = "sqlite"
2+
version = "1.0.0"
3+
4+
# Fill out these fields if you intend to generate HTML documentation or publish
5+
# your project to the Hex package manager.
6+
#
7+
# description = ""
8+
# licences = ["Apache-2.0"]
9+
# repository = { type = "github", user = "username", repo = "project" }
10+
# links = [{ title = "Website", href = "https://gleam.run" }]
11+
#
12+
# For a full reference of all the available options, you can have a look at
13+
# https://gleam.run/writing-gleam/gleam-toml/.
14+
15+
[dependencies]
16+
gleam_stdlib = "~> 0.34 or ~> 1.0"
17+
argv = ">= 1.0.2 and < 2.0.0"
18+
file_streams = ">= 0.4.2 and < 1.0.0"
19+
20+
[dev-dependencies]
21+
gleeunit = "~> 1.0"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6+
{ name = "file_streams", version = "0.4.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "file_streams", source = "hex", outer_checksum = "86775D2F36B74C54C64CA9080ACA9B9CF4655759E8D3E87E1A2EA9B0FA8D0804" },
7+
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
8+
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
9+
]
10+
11+
[requirements]
12+
argv = { version = ">= 1.0.2 and < 2.0.0" }
13+
file_streams = { version = ">= 0.4.2 and < 1.0.0" }
14+
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
15+
gleeunit = { version = "~> 1.0" }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import argv
2+
import file_streams/read_stream
3+
import gleam/int.{to_string}
4+
import gleam/io
5+
6+
pub fn main() {
7+
// You can use print statements as follows for debugging, they'll be visible when running tests.
8+
io.println("Logs from your program will appear here!")
9+
10+
let args = argv.load().arguments
11+
12+
// Uncomment this to pass the first stage
13+
// case args {
14+
// [database_file_path, ".dbinfo", ..] -> {
15+
// let assert Ok(rs) = read_stream.open(database_file_path)
16+
// // Get a file handle to the database file, and skip the first 16 bytes
17+
// let assert Ok(_bytes) = read_stream.read_bytes_exact(rs, 16)
18+
// // The next 2 bytes hold the page size in big-endian format
19+
// let assert Ok(page_size) = read_stream.read_int16_be(rs)
20+
//
21+
// io.print("database page size: ")
22+
// io.println(to_string(page_size))
23+
// }
24+
// _ -> {
25+
// io.println("Unknown command")
26+
// }
27+
// }
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
exec gleam run -- "$@"

course-definition.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ languages:
4444
- slug: "zig"
4545
- slug: "java"
4646
release_status: "beta"
47+
- slug: "gleam"
48+
release_status: "beta"
4749
- slug: "typescript"
4850
release_status: "beta"
4951

dockerfiles/gleam-1.0.Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM ghcr.io/gleam-lang/gleam:v1.0.0-erlang-alpine
2+
3+
# Pre-compile steps
4+
RUN printf "cd \${CODECRAFTERS_SUBMISSION_DIR} && gleam build" > /codecrafters-precompile.sh
5+
RUN chmod +x /codecrafters-precompile.sh

0 commit comments

Comments
 (0)