Skip to content

Commit 4a8c154

Browse files
committed
Initial commit
0 parents  commit 4a8c154

File tree

12 files changed

+281
-0
lines changed

12 files changed

+281
-0
lines changed

.codecrafters/compile.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
cargo build --release --target-dir=/tmp/codecrafters-build-sqlite-rust --manifest-path Cargo.toml

.codecrafters/run.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
exec /tmp/codecrafters-build-sqlite-rust/release/codecrafters-sqlite "$@"

.gitattributes

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

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Database files used for testing
2+
*.db
3+
4+
# Generated by Cargo
5+
# will have compiled files and executables
6+
debug/
7+
target/
8+
9+
# These are backup files generated by rustfmt
10+
**/*.rs.bk
11+
12+
# MSVC Windows builds of rustc generate these, which store debugging information
13+
*.pdb

Cargo.lock

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "codecrafters-sqlite"
3+
version = "0.1.0"
4+
authors = ["Codecrafters <hello@codecrafters.io>"]
5+
edition = "2021"
6+
rust-version = "1.80"
7+
8+
[dependencies]
9+
anyhow = "1.0.68" # error handling
10+
bytes = "1.3.0" # helps manage buffers
11+
thiserror = "1.0.38" # error handling

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![progress-banner](https://backend.codecrafters.io/progress/sqlite/b3b12e89-a536-4604-be71-ddd7dab3056e)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF)
2+
3+
This is a starting point for Rust 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.rs`. Study and
19+
uncomment the relevant code, and push your changes to pass the first stage:
20+
21+
```sh
22+
git commit -am "pass 1st stage" # any msg
23+
git push origin master
24+
```
25+
26+
Time to move on to the next stage!
27+
28+
# Stage 2 & beyond
29+
30+
Note: This section is for stages 2 and beyond.
31+
32+
1. Ensure you have `cargo (1.82)` installed locally
33+
1. Run `./your_program.sh` to run your program, which is implemented in
34+
`src/main.rs`. This command compiles your Rust project, so it might be slow
35+
the first time you run it. Subsequent runs will be fast.
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).

codecrafters.yml

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 Rust version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: rust-1.82
11+
language_pack: rust-1.82

download_sample_databases.sh

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."

sample.db

16 KB
Binary file not shown.

src/main.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use anyhow::{bail, Result};
2+
use std::fs::File;
3+
use std::io::prelude::*;
4+
5+
fn main() -> Result<()> {
6+
// Parse arguments
7+
let args = std::env::args().collect::<Vec<_>>();
8+
match args.len() {
9+
0 | 1 => bail!("Missing <database path> and <command>"),
10+
2 => bail!("Missing <command>"),
11+
_ => {}
12+
}
13+
14+
// Parse command and act accordingly
15+
let command = &args[2];
16+
match command.as_str() {
17+
".dbinfo" => {
18+
let mut file = File::open(&args[1])?;
19+
let mut header = [0; 100];
20+
file.read_exact(&mut header)?;
21+
22+
// The page size is stored at the 16th byte offset, using 2 bytes in big-endian order
23+
#[allow(unused_variables)]
24+
let page_size = u16::from_be_bytes([header[16], header[17]]);
25+
26+
// You can use print statements as follows for debugging, they'll be visible when running tests.
27+
eprintln!("Logs from your program will appear here!");
28+
29+
// Uncomment this block to pass the first stage
30+
// println!("database page size: {}", page_size);
31+
}
32+
_ => bail!("Missing or invalid command passed: {}", command),
33+
}
34+
35+
Ok(())
36+
}

your_program.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# Use this script to run your program LOCALLY.
4+
#
5+
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit early if any commands fail
10+
11+
# Copied from .codecrafters/compile.sh
12+
#
13+
# - Edit this to change how your program compiles locally
14+
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
15+
(
16+
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
17+
cargo build --release --target-dir=/tmp/codecrafters-build-sqlite-rust --manifest-path Cargo.toml
18+
)
19+
20+
# Copied from .codecrafters/run.sh
21+
#
22+
# - Edit this to change how your program runs locally
23+
# - Edit .codecrafters/run.sh to change how your program runs remotely
24+
exec /tmp/codecrafters-build-sqlite-rust/release/codecrafters-sqlite "$@"

0 commit comments

Comments
 (0)