-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashstore.go
159 lines (141 loc) · 3.99 KB
/
hashstore.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package attorney
import (
"log/slog"
"path/filepath"
"github.com/freehandle/breeze/crypto"
"github.com/freehandle/papirus"
)
const (
remove byte = iota
exists
insert
)
func deleteOrInsert(found bool, hash crypto.Hash, b *papirus.Bucket, item int64, param []byte) papirus.OperationResult {
if len(param) < 1 {
slog.Error("deleteOrInsert called with zero param length")
return papirus.OperationResult{
Result: papirus.QueryResult{Ok: false},
}
}
if found {
if param[0] == remove { //Delete
return papirus.OperationResult{
Deleted: &papirus.Item{Bucket: b, Item: item},
Result: papirus.QueryResult{Ok: true},
}
} else if param[0] == exists { // exists?
return papirus.OperationResult{
Result: papirus.QueryResult{Ok: true},
}
} else { // insert
return papirus.OperationResult{
Result: papirus.QueryResult{Ok: false},
}
}
} else {
if param[0] == insert {
b.WriteItem(item, hash[:])
return papirus.OperationResult{
Added: &papirus.Item{Bucket: b, Item: item},
Result: papirus.QueryResult{Ok: true},
}
} else {
return papirus.OperationResult{
Result: papirus.QueryResult{Ok: false},
}
}
}
}
type hashVault struct {
hs *papirus.HashStore[crypto.Hash]
}
func (h *hashVault) Clone() *hashVault {
return &hashVault{
hs: h.hs.Clone(),
}
}
func (w *hashVault) ExistsHash(hash crypto.Hash) bool {
response := make(chan papirus.QueryResult)
ok, _ := w.hs.Query(papirus.Query[crypto.Hash]{Hash: hash, Param: []byte{exists}, Response: response})
return ok
}
func (w *hashVault) ExistsToken(token crypto.Token) bool {
hash := crypto.HashToken(token)
return w.ExistsHash(hash)
}
func (w *hashVault) InsertHash(hash crypto.Hash) bool {
response := make(chan papirus.QueryResult)
ok, _ := w.hs.Query(papirus.Query[crypto.Hash]{Hash: hash, Param: []byte{insert}, Response: response})
return ok
}
func (w *hashVault) InsertToken(token crypto.Token) bool {
hash := crypto.HashToken(token)
return w.InsertHash(hash)
}
func (w *hashVault) RemoveHash(hash crypto.Hash) bool {
response := make(chan papirus.QueryResult)
ok, _ := w.hs.Query(papirus.Query[crypto.Hash]{Hash: hash, Param: []byte{remove}, Response: response})
return ok
}
func (w *hashVault) RemoveToken(token crypto.Token) bool {
hash := crypto.HashToken(token)
return w.RemoveHash(hash)
}
func (w *hashVault) Close() bool {
defer func() {
if err := recover(); err != nil {
slog.Error("hashVault.Close", "msg", err)
}
}()
ok := make(chan bool)
w.hs.Stop <- ok
return <-ok
}
func NewHashVault(name string, epoch uint64, bitsForBucket int64, dataPath string) *hashVault {
nbytes := 56 + (32*6+8)*int64(1<<bitsForBucket)
var bytestore papirus.ByteStore
if dataPath == "" {
if store := papirus.NewMemoryStore(nbytes); store == nil {
slog.Error("NewHashVault: NewMemoryStore returned nil")
return nil
} else {
bytestore = store
}
} else {
if store := papirus.NewFileStore(filepath.Join(dataPath, name), nbytes); store == nil {
slog.Error("NewHashVault: NewFileStore returned nil")
return nil
} else {
bytestore = store
}
}
bucketstore := papirus.NewBucketStore(32, 6, bytestore)
if bucketstore == nil {
slog.Error("NewHashVault: NewBucketStore returned nil")
return nil
}
vault := &hashVault{
hs: papirus.NewHashStore(name, bucketstore, int(bitsForBucket), deleteOrInsert),
}
vault.hs.Start()
return vault
}
func NewFileHashVaultFromBytes(filePath, name string, data []byte) *hashVault {
bytestore := papirus.NewFileStore(filePath, 0)
return newHashVaultFromBytes(name, bytestore, data)
}
func NewMemoryHashVaultFromBytes(name string, data []byte) *hashVault {
bytestore := papirus.NewMemoryStore(0)
return newHashVaultFromBytes(name, bytestore, data)
}
func newHashVaultFromBytes(name string, store papirus.ByteStore, data []byte) *hashVault {
hs := papirus.NewHashStoreFromClonedBytes(name, store, deleteOrInsert, data)
vault := &hashVault{
hs: hs,
}
vault.hs.Start()
return vault
}
func (h *hashVault) Bytes() []byte {
return h.hs.Bytes()
}