https://github.com/krasun/lsmtree
Log-structured merge-tree
https://github.com/krasun/lsmtree
append append-only db embedded-database go go-library golang lsm lsm-tree
Last synced: 2 months ago
JSON representation
Log-structured merge-tree
- Host: GitHub
- URL: https://github.com/krasun/lsmtree
- Owner: krasun
- License: mit
- Created: 2021-04-21T17:56:52.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-02-26T08:38:51.000Z (over 2 years ago)
- Last Synced: 2025-07-15T07:35:34.083Z (3 months ago)
- Topics: append, append-only, db, embedded-database, go, go-library, golang, lsm, lsm-tree
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 31
- Watchers: 2
- Forks: 7
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lsmtree
[](https://github.com/krasun/lsmtree/actions/workflows/build.yml)
[](https://codecov.io/gh/krasun/lsmtree)
[](https://goreportcard.com/report/github.com/krasun/lsmtree)
[](https://godoc.org/github.com/krasun/lsmtree)lsmtree is a log-structured merge-tree implementation in Go.
**Attention!** lsmtree is **not** goroutine-safe - calling any methods on it from a different goroutine without synchronization is not safe and might lead to data corruption. Make sure that the access is synchronized if the tree is used from the separate goroutines.
## Installation
As simple as:
```
go get github.com/krasun/lsmtree
```## Quickstart
A fully-featured example:
```go
package lsmtree_testimport (
"fmt"
"io/ioutil"
"os""github.com/krasun/lsmtree"
)func Example() {
dbDir, err := ioutil.TempDir(os.TempDir(), "example")
if err != nil {
panic(fmt.Errorf("failed to create %s: %w", dbDir, err))
}
defer func() {
if err := os.RemoveAll(dbDir); err != nil {
panic(fmt.Errorf("failed to remove %s: %w", dbDir, err))
}
}()tree, err := lsmtree.Open(dbDir, lsmtree.SparseKeyDistance(64), lsmtree.MemTableThreshold(1000000))
if err != nil {
panic(fmt.Errorf("failed to open LSM tree %s: %w", dbDir, err))
}err = tree.Put([]byte("Hi!"), []byte("Hello world, LSMTree!"))
if err != nil {
panic(fmt.Errorf("failed to put: %w", err))
}err = tree.Put([]byte("Does it override key?"), []byte("No!"))
if err != nil {
panic(fmt.Errorf("failed to put: %w", err))
}err = tree.Put([]byte("Does it override key?"), []byte("Yes, absolutely! The key has been overridden."))
if err != nil {
panic(fmt.Errorf("failed to put: %w", err))
}if err := tree.Close(); err != nil {
panic(fmt.Errorf("failed to close: %w", err))
}tree, err = lsmtree.Open(dbDir)
if err != nil {
panic(fmt.Errorf("failed to open LSM tree %s: %w", dbDir, err))
}value, ok, err := tree.Get([]byte("Hi!"))
if err != nil {
panic(fmt.Errorf("failed to get value: %w", err))
}
if !ok {
fmt.Println("failed to find value")
}fmt.Println(string(value))
value, ok, err = tree.Get([]byte("Does it override key?"))
if err != nil {
panic(fmt.Errorf("failed to get value: %w", err))
}
if !ok {
fmt.Println("failed to find value")
}if err := tree.Close(); err != nil {
panic(fmt.Errorf("failed to close: %w", err))
}fmt.Println(string(value))
// Output:
// Hello world, LSMTree!
// Yes, absolutely! The key has been overridden.
}
```## Tests
To make sure that the code is fully tested and covered:
```
$ go test .
ok github.com/krasun/lsmtree 2.888s
```## Known Usages
1. [krasun/gosqldb](https://github.com/krasun/gosqldb) - my experimental implementation of a simple database.
## License
lsmtree is released under [the MIT license](LICENSE).