Skip to content

Commit 7cae5e8

Browse files
authored
Merge pull request #134 from codecrafters-io/andy/feat
Add Elixir to SQLite
2 parents 5105974 + 55aec5a commit 7cae5e8

33 files changed

+633
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
mix escript.build
12+
mv codecrafters_sqlite /tmp/codecrafters-build-sqlite-elixir
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 run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec /tmp/codecrafters-build-sqlite-elixir "$@"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/elixir/.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Database files used for testing
2+
*.db
3+
4+
# Ignore the compiled binary
5+
/codecrafters_sqlite
6+
7+
# The directory Mix will write compiled artifacts to.
8+
/_build/
9+
10+
# If you run "mix test --cover", coverage assets end up here.
11+
/cover/
12+
13+
# The directory Mix downloads your dependencies sources to.
14+
/deps/
15+
16+
# Where third-party dependencies like ExDoc output generated docs.
17+
/doc/
18+
19+
# Ignore .fetch files in case you like to edit your project deps locally.
20+
/.fetch
21+
22+
# If the VM crashes, it generates a dump, let's ignore it too.
23+
erl_crash.dump
24+
25+
# Also ignore archive artifacts (built via "mix archive.build").
26+
*.ez
27+
28+
# Ignore package tarball (built via "mix hex.build").
29+
app-*.tar
30+

compiled_starters/elixir/README.md

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule CLI do
2+
def main(args) do
3+
case args do
4+
[database_file_path, command] ->
5+
case command do
6+
".dbinfo" ->
7+
file = File.open!(database_file_path, [:read, :binary])
8+
9+
# You can use print statements as follows for debugging, they'll be visible when running tests.
10+
IO.puts(:stderr, "Logs from your program will appear here!")
11+
12+
# Uncomment this to pass the first stage
13+
# :file.position(file, 16) # Skip the first 16 bytes of the header
14+
# <<page_size::16>> = IO.binread(file, 2)
15+
# IO.puts("database page size: #{page_size}")
16+
end
17+
_ ->
18+
IO.puts("Usage: your_program <database_file> <command>")
19+
end
20+
end
21+
end

compiled_starters/elixir/mix.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule App.MixProject do
2+
# NOTE: You do not need to change anything in this file.
3+
use Mix.Project
4+
5+
def project do
6+
[
7+
app: :codecrafters_sqlite,
8+
version: "1.0.0",
9+
elixir: "~> 1.18",
10+
start_permanent: Mix.env() == :prod,
11+
deps: deps(),
12+
escript: [main_module: CLI]
13+
]
14+
end
15+
16+
# Run "mix help compile.app" to learn about applications.
17+
def application do
18+
[
19+
extra_applications: [:logger]
20+
]
21+
end
22+
23+
# Run "mix help deps" to learn about dependencies.
24+
defp deps do
25+
[
26+
# {:dep_from_hexpm, "~> 0.3.0"},
27+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
28+
]
29+
end
30+
end

0 commit comments

Comments
 (0)