Skip to content

Commit

Permalink
Merge pull request #7 from manosriram/feat/segment_hierarchy
Browse files Browse the repository at this point in the history
add support for segments
  • Loading branch information
manosriram authored Dec 22, 2023
2 parents 07820fc + 0047cf6 commit 5eb127c
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 142 deletions.
105 changes: 103 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,109 @@
## nimbusdb

Persistent Key-Value store based on Bitcask paper.

Readme to be updated.
nimbusdb is a fast, lightweight, and scalable key-value store written in golang, based on bitcask.

nimbusdb maintains an active datafile to which data is written. When it crosses a threshold, the datafile is made inactive and new datafile is created.
As time passes, expired/deleted keys take up space which is not useful; Hence, a process called `merge` is done which removes all expired/deleted keys and frees up space.

## Features
<details>
<summary>
Thread-Safe
</summary>
All operations are thread-safe. Read and Write operations can handle multiple operations from multiple goroutines at the same time with consistency.
</details>

<details>
<summary>
Portable
</summary>
Data is extremely portable since it is only a bunch of files. All you have to do is move the folder and open an DB connection at that path.
</details>

<details>
<summary>
Custom Expiry
</summary>
Supports custom expiry for keys. Default expiry is 1 week.
</details>

<details>
<summary>
Supports Merge
</summary>
Supports `Sync` which can be called periodically to remove expired/deleted keys from disk and free-up more space.
</details>

<details>
<summary>
Single disk-seek write
</summary>
Writes are just one disk seek since we're appending to the file.
</details>

<details>
<summary>
Block cache for faster reads.
</summary>
Blocks are cached for faster reads. Default size of an Block is 32KB.
</details>

## Documentation
#### Open DB connection
```go
d, err := nimbusdb.Open(&nimbusdb.Options{Path: "/path/to/data/directory"})
if err != nil {
// handle error
}
```

#### Set
```go
kvPair := &nimbusdb.KeyValuePair{
Key: []byte("key"),
Value: []byte("value"),
Ttl: 5 * time.Minute, // Optional, default is 1 week
}
setValue, err := d.Set(kvPair)
if err != nil {
// handle error
}
```

#### Get

```go
value, err := d.Get([]byte("key"))
if err != nil {
// handle error
}
```

#### Delete

```go
value, err := d.Get([]byte("key"))
if err != nil {
// handle error
}
```

#### Sync
This does the merge process. This can be an expensive operation, hence it is better to run this periodically and whenever the traffic is low.

```go
err := d.Sync()
if err != nil {
// handle error
}
```

## Benchmarks
![Screenshot 2023-12-23 at 4 08 56 AM](https://github.com/manosriram/nimbusdb/assets/38112857/76720f68-ad16-44ee-a408-12b06e6c051a)




[Progress Board](https://trello.com/b/2eDSLLb3/nimbusdb) | [Streams](https://youtube.com/playlist?list=PLJALjJgNSDVo5veOf2apgMIE1QgN7IEfk) | [godoc](https://pkg.go.dev/github.com/manosriram/nimbusdb)

Expand Down
21 changes: 2 additions & 19 deletions btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@ import (
"github.com/manosriram/nimbusdb/utils"
)

type BlockOffsetPair struct {
startOffset int64
endOffset int64
filePath string
}

type BTree struct {
tree *btree.BTree
blockOffsets map[int64]BlockOffsetPair
mu sync.RWMutex
tree *btree.BTree
mu sync.RWMutex
}

type item struct {
Expand All @@ -39,16 +32,6 @@ func (b *BTree) Get(key []byte) *KeyDirValue {

func (b *BTree) Set(key []byte, value KeyDirValue) *KeyDirValue {
i := b.tree.ReplaceOrInsert(&item{key: key, v: value})
y, ok := b.blockOffsets[value.blockNumber]
if !ok {
y.startOffset = value.offset
y.endOffset = value.offset + value.size
y.filePath = value.path
b.blockOffsets[value.blockNumber] = y
} else {
y.endOffset = value.offset + value.size
b.blockOffsets[value.blockNumber] = y
}
if i != nil {
return &i.(*item).v
}
Expand Down
Loading

0 comments on commit 5eb127c

Please sign in to comment.