Skip to content

Commit ffa9c16

Browse files
update the readme
1 parent 3a4e40b commit ffa9c16

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

README.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,61 @@
22

33
[tiny-bitcask](https://github.com/elliotchenzichang/tiny-bitcask) is a small [Bitcask](https://riak.com/assets/bitcask-intro.pdf)-style key/value store in Go: one active append-only data file per directory, an in-memory **keydir** (hash map) pointing at the latest record per key, and optional **merge** to drop stale records and reclaim space.
44

5-
This repo is an educational reference and a playground for experiments; `master` carries ongoing work. For a smaller, teaching-oriented snapshot, use the **`demo`** branch:
5+
## Using the database
6+
7+
Add this module to your `go.mod`.
8+
```
9+
go get github.com/elliotchenzichang/tiny-bitcask@latest
10+
```
11+
From the repository root you can run the bundled example:
612

713
```shell
8-
git clone git@github.com:elliotchenzichang/tiny-bitcask.git
9-
cd tiny-bitcask
10-
git checkout demo
14+
go run ./cmd/demo
1115
```
1216

17+
Minimal program: open a store on a directory, `Set` / `Get` bytes, then `Close`.
18+
19+
```go
20+
package main
21+
22+
import (
23+
"fmt"
24+
"log"
25+
"os"
26+
"path/filepath"
27+
28+
"tiny-bitcask"
29+
)
30+
31+
func main() {
32+
parent, err := os.MkdirTemp("", "tb")
33+
if err != nil {
34+
log.Fatal(err)
35+
}
36+
defer os.RemoveAll(parent)
37+
dir := filepath.Join(parent, "data") // must not exist yet — see NewDB / recovery
38+
39+
o := *tiny_bitcask.DefaultOptions
40+
o.Dir = dir
41+
db, err := tiny_bitcask.NewDB(&o)
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
defer db.Close()
46+
47+
if err := db.Set([]byte("demo-key"), []byte("demo-value")); err != nil {
48+
log.Fatal(err)
49+
}
50+
v, err := db.Get([]byte("demo-key"))
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
fmt.Println(string(v))
55+
}
56+
```
57+
58+
The same source lives at [`cmd/demo/main.go`](cmd/demo/main.go). Could clone this repo and give it a try.
59+
1360
---
1461

1562
## Design (aligned with the paper)

cmd/demo/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
9+
"tiny-bitcask"
10+
)
11+
12+
func main() {
13+
parent, err := os.MkdirTemp("", "tb")
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
defer os.RemoveAll(parent)
18+
// NewDB creates the data directory when it does not exist; an empty temp dir
19+
// would otherwise be treated as an existing store and recovery would fail.
20+
dir := filepath.Join(parent, "data")
21+
22+
o := *tiny_bitcask.DefaultOptions
23+
o.Dir = dir
24+
db, err := tiny_bitcask.NewDB(&o)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
defer db.Close()
29+
30+
if err := db.Set([]byte("k"), []byte("v")); err != nil {
31+
log.Fatal(err)
32+
}
33+
v, err := db.Get([]byte("k"))
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
fmt.Println(string(v))
38+
}

0 commit comments

Comments
 (0)