-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocksdb.go
34 lines (30 loc) · 906 Bytes
/
blocksdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package nopfs
import "sync"
// BlocksDB is a key-value store of Entries. Keying may vary depending on
// whether we are indexing IPNS names, CIDs etc.
type BlocksDB struct {
// TODO: this will eventually need to be replaced by a database with
// its bloom filters etc. For a few million items this is just fine
// though.
blockDB sync.Map
}
// Load returns the Entries for a key.
func (b *BlocksDB) Load(key string) (Entries, bool) {
val, ok := b.blockDB.Load(key)
if !ok {
return nil, false
}
entries, ok := val.(Entries)
if !ok {
logger.Error("cannot convert to entries")
return nil, false
}
return entries, true
}
// Store stores a new entry with the given key. If there are existing Entries,
// the new Entry will be appended to them.
func (b *BlocksDB) Store(key string, entry Entry) {
entries, _ := b.Load(key)
entries = append(entries, entry)
b.blockDB.Store(key, entries)
}