@@ -12,13 +12,16 @@ import (
12
12
"time"
13
13
)
14
14
15
- // DefaultDbFile is the default name of the database file that contains all the key-value pairs
16
- const DefaultDbFile string = "dump.scdb"
15
+ // defaultDbFile is the default name of the database file that contains all the key-value pairs
16
+ const defaultDbFile string = "dump.scdb"
17
17
18
- var ZeroU64 = internal .Uint64ToByteArray (0 )
18
+ var zeroU64 = internal .Uint64ToByteArray (0 )
19
19
20
- // Store is the public interface to the key-value store
21
- // that allows us to do operations like Set, Get, Delete, Clear and Compact
20
+ // Store is a key-value store that persists key-value pairs to disk
21
+ //
22
+ // Store behaves like a HashMap that saves keys and value as byte arrays
23
+ // on disk. It allows for specifying how long each key-value pair should be
24
+ // kept for i.e. the time-to-live in seconds. If None is provided, they last indefinitely.
22
25
type Store struct {
23
26
bufferPool * buffers.BufferPool
24
27
header * entries.DbFileHeader
@@ -27,14 +30,48 @@ type Store struct {
27
30
isClosed bool
28
31
}
29
32
30
- // New creates a new Store at the given path, with the given `compactionInterval`
33
+ // New creates a new Store at the given path
34
+ // The Store has a number of configurations that are passed into this New function
35
+ //
36
+ // - `storePath` - required:
37
+ // The path to a directory where scdb should store its data
38
+ //
39
+ // - `maxKeys` - default: 1 million:
40
+ // The maximum number of key-value pairs to store in store
41
+ //
42
+ // - `redundantBlocks` - default: 1:
43
+ // The store has an index to hold all the keys. This index is split
44
+ // into a fixed number of blocks basing on the virtual memory page size
45
+ // and the total number of keys to be held i.e. `max_keys`.
46
+ // Sometimes, there may be hash collision errors as the store's
47
+ // current stored keys approach `max_keys`. The closer it gets, the
48
+ // more it becomes likely see those errors. Adding redundant blocks
49
+ // helps mitigate this. Just be careful to not add too many (i.e. more than 2)
50
+ // since the higher the number of these blocks, the slower the store becomes.
51
+ //
52
+ // - `poolCapacity` - default: 5:
53
+ // The number of buffers to hold in memory as cache's for the store. Each buffer
54
+ // has the size equal to the virtual memory's page size, usually 4096 bytes.
55
+ // Increasing this number will speed this store up but of course, the machine
56
+ // has a limited RAM. When this number increases to a value that clogs the RAM, performance
57
+ // suddenly degrades, and keeps getting worse from there on.
58
+ //
59
+ // - `compactionInterval` - default 3600s (1 hour):
60
+ // The interval at which the store is compacted to remove dangling
61
+ // keys. Dangling keys result from either getting expired or being deleted.
62
+ // When a `delete` operation is done, the actual key-value pair
63
+ // is just marked as `deleted` but is not removed.
64
+ // Something similar happens when a key-value is updated.
65
+ // A new key-value pair is created and the old one is left unindexed.
66
+ // Compaction is important because it reclaims this space and reduces the size
67
+ // of the database file.
31
68
func New (path string , maxKeys * uint64 , redundantBlocks * uint16 , poolCapacity * uint64 , compactionInterval * uint32 ) (* Store , error ) {
32
69
err := os .MkdirAll (path , 0755 )
33
70
if err != nil {
34
71
return nil , err
35
72
}
36
73
37
- dbFilePath := filepath .Join (path , DefaultDbFile )
74
+ dbFilePath := filepath .Join (path , defaultDbFile )
38
75
bufferPool , err := buffers .NewBufferPool (poolCapacity , dbFilePath , maxKeys , redundantBlocks , nil )
39
76
if err != nil {
40
77
return nil , err
@@ -133,7 +170,7 @@ func (s *Store) Get(k []byte) ([]byte, error) {
133
170
return nil , err
134
171
}
135
172
136
- if bytes .Equal (kvOffsetInBytes , ZeroU64 ) {
173
+ if bytes .Equal (kvOffsetInBytes , zeroU64 ) {
137
174
continue
138
175
}
139
176
@@ -173,7 +210,7 @@ func (s *Store) Delete(k []byte) error {
173
210
return err
174
211
}
175
212
176
- if bytes .Equal (kvOffsetInBytes , ZeroU64 ) {
213
+ if bytes .Equal (kvOffsetInBytes , zeroU64 ) {
177
214
continue
178
215
}
179
216
0 commit comments