|
2 | 2 |
|
3 | 3 | [](https://github.com/ckampfe/b4/actions/workflows/elixir.yml) |
4 | 4 |
|
5 | | -## Installation |
| 5 | +## What is this |
6 | 6 |
|
7 | | -If [available in Hex](https://hex.pm/docs/publish), the package can be installed |
8 | | -by adding `b4` to your list of dependencies in `mix.exs`: |
| 7 | +B4 is an implementation of [Bitcask](https://en.wikipedia.org/wiki/Bitcask). |
| 8 | + |
| 9 | +Bitcask is a fast, immutable, append-only log-structured hash table. It's a database that makes some interesting tradeoffs to achieve low read and write latency. |
| 10 | + |
| 11 | +This README is not a full technical reference for Bitcask. For that, see the wiki or [the paper](https://riak.com/assets/bitcask-intro.pdf). |
| 12 | + |
| 13 | +You can associate a key and a value. You can fetch the value for a key. You can delete that key. You can see all of the live keys. |
| 14 | + |
| 15 | +You can also merge (garbage collect) the on-disk database files, which is important because Bitcask is append only, meaning data is not overwritten on disk, but rather accumlates over time. Because of this, old tuples stick around on disk until they are "merged", which discards dead tuples and preserves live tuples in a new, minimal set of files on disk. |
| 16 | + |
| 17 | + |
| 18 | +Using it looks like this: |
9 | 19 |
|
10 | 20 | ```elixir |
11 | | -def deps do |
12 | | - [ |
13 | | - {:b4, "~> 0.1.0"} |
14 | | - ] |
15 | | -end |
| 21 | +$ iex -S mix |
| 22 | +iex(1)> B4.new(File.cwd!()) |
| 23 | +:ok |
| 24 | +iex(2)> B4.insert(File.cwd!(), "hello", "world") |
| 25 | +:ok |
| 26 | +iex(3)> B4.fetch(File.cwd!(), "hello") |
| 27 | +{:ok, "world"} |
| 28 | +iex(4)> B4.fetch(File.cwd!(), "does not exist") |
| 29 | +:not_found |
| 30 | +iex(5)> B4.delete(File.cwd!(), "hello") |
| 31 | +:ok |
| 32 | +iex(6)> B4.fetch(File.cwd!(), "hello") |
| 33 | +:not_found |
16 | 34 | ``` |
17 | 35 |
|
18 | | -Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) |
19 | | -and published on [HexDocs](https://hexdocs.pm). Once published, the docs can |
20 | | -be found at <https://hexdocs.pm/b4>. |
| 36 | +The public API looks like this: |
| 37 | + |
| 38 | +```elixir |
| 39 | +def new(directory, options \\ [target_file_size: 2 ** 31, sync_strategy: :every_write]) |
| 40 | +def insert(directory, key, value) |
| 41 | +def fetch(directory, key) |
| 42 | +def delete(directory, key) |
| 43 | +def keys(directory) |
| 44 | +def merge(directory, timeout \\ 15_000) |
| 45 | +def close(directory) |
| 46 | +``` |
21 | 47 |
|
0 commit comments