-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The addition of a locking interface to the blockstore allows us to perform atomic operations on the underlying datastore without having to worry about different operations happening in the background, such as garbage collection. License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent
16ea653
commit f008ce5
Showing
3 changed files
with
50 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,39 @@ | ||
package key | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type KeySet interface { | ||
Add(Key) | ||
Has(Key) bool | ||
Remove(Key) | ||
Keys() []Key | ||
} | ||
|
||
type ks struct { | ||
lock sync.RWMutex | ||
data map[Key]struct{} | ||
type keySet struct { | ||
keys map[Key]struct{} | ||
} | ||
|
||
func NewKeySet() KeySet { | ||
return &ks{ | ||
data: make(map[Key]struct{}), | ||
} | ||
return &keySet{make(map[Key]struct{})} | ||
} | ||
|
||
func (wl *ks) Add(k Key) { | ||
wl.lock.Lock() | ||
defer wl.lock.Unlock() | ||
|
||
wl.data[k] = struct{}{} | ||
func (gcs *keySet) Add(k Key) { | ||
gcs.keys[k] = struct{}{} | ||
} | ||
|
||
func (wl *ks) Remove(k Key) { | ||
wl.lock.Lock() | ||
defer wl.lock.Unlock() | ||
|
||
delete(wl.data, k) | ||
func (gcs *keySet) Has(k Key) bool { | ||
_, has := gcs.keys[k] | ||
return has | ||
} | ||
|
||
func (wl *ks) Keys() []Key { | ||
wl.lock.RLock() | ||
defer wl.lock.RUnlock() | ||
keys := make([]Key, 0) | ||
for k := range wl.data { | ||
keys = append(keys, k) | ||
func (ks *keySet) Keys() []Key { | ||
var out []Key | ||
for k, _ := range ks.keys { | ||
out = append(out, k) | ||
} | ||
return keys | ||
return out | ||
} | ||
|
||
func (ks *keySet) Remove(k Key) { | ||
delete(ks.keys, k) | ||
} | ||
|
||
// TODO: implement disk-backed keyset for working with massive DAGs |