update all downstream dependencies

no specific features I'm looking to add, just keeping thing up to date.
Unit tests and my manual testing seems like everything is still working
as expected.
This commit is contained in:
Will Norris 2017-06-01 08:37:05 -07:00
parent 17f19d612f
commit b5984d2822
25 changed files with 1661 additions and 486 deletions

View file

@ -114,11 +114,11 @@ with a RWMutex to provide safe concurrent access.
diskv is a key-value store and therefore inherently unordered. An ordering
system can be injected into the store by passing something which satisfies the
diskv.Index interface. (A default implementation, using Petar Maymounkov's
[LLRB tree][7], is provided.) Basically, diskv keeps an ordered (by a
diskv.Index interface. (A default implementation, using Google's
[btree][7] package, is provided.) Basically, diskv keeps an ordered (by a
user-provided Less function) index of the keys, which can be queried.
[7]: https://github.com/petar/GoLLRB
[7]: https://github.com/google/btree
## Adding compression

View file

@ -56,8 +56,8 @@ type Options struct {
// Diskv implements the Diskv interface. You shouldn't construct Diskv
// structures directly; instead, use the New constructor.
type Diskv struct {
sync.RWMutex
Options
mu sync.RWMutex
cache map[string][]byte
cacheSize uint64
}
@ -109,8 +109,8 @@ func (d *Diskv) WriteStream(key string, r io.Reader, sync bool) error {
return errEmptyKey
}
d.Lock()
defer d.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
return d.writeStreamWithLock(key, r, sync)
}
@ -181,8 +181,8 @@ func (d *Diskv) Import(srcFilename, dstKey string, move bool) (err error) {
return errImportDirectory
}
d.Lock()
defer d.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
if err := d.ensurePathWithLock(dstKey); err != nil {
return fmt.Errorf("ensure path: %s", err)
@ -234,8 +234,8 @@ func (d *Diskv) Read(key string) ([]byte, error) {
// If compression is enabled, ReadStream taps into the io.Reader stream prior
// to decompression, and caches the compressed data.
func (d *Diskv) ReadStream(key string, direct bool) (io.ReadCloser, error) {
d.RLock()
defer d.RUnlock()
d.mu.RLock()
defer d.mu.RUnlock()
if val, ok := d.cache[key]; ok {
if !direct {
@ -247,8 +247,8 @@ func (d *Diskv) ReadStream(key string, direct bool) (io.ReadCloser, error) {
}
go func() {
d.Lock()
defer d.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
d.uncacheWithLock(key, uint64(len(val)))
}()
}
@ -352,8 +352,8 @@ func (s *siphon) Read(p []byte) (int, error) {
// Erase synchronously erases the given key from the disk and the cache.
func (d *Diskv) Erase(key string) error {
d.Lock()
defer d.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
d.bustCacheWithLock(key)
@ -365,14 +365,15 @@ func (d *Diskv) Erase(key string) error {
// erase from disk
filename := d.completeFilename(key)
if s, err := os.Stat(filename); err == nil {
if !!s.IsDir() {
if s.IsDir() {
return errBadKey
}
if err = os.Remove(filename); err != nil {
return fmt.Errorf("remove: %s", err)
return err
}
} else {
return fmt.Errorf("stat: %s", err)
// Return err as-is so caller can do os.IsNotExist(err).
return err
}
// clean up and return
@ -385,8 +386,8 @@ func (d *Diskv) Erase(key string) error {
// diskv-related data. Care should be taken to always specify a diskv base
// directory that is exclusively for diskv data.
func (d *Diskv) EraseAll() error {
d.Lock()
defer d.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
d.cache = make(map[string][]byte)
d.cacheSize = 0
return os.RemoveAll(d.BasePath)
@ -394,8 +395,8 @@ func (d *Diskv) EraseAll() error {
// Has returns true if the given key exists.
func (d *Diskv) Has(key string) bool {
d.Lock()
defer d.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
if _, ok := d.cache[key]; ok {
return true
@ -498,8 +499,8 @@ func (d *Diskv) cacheWithLock(key string, val []byte) error {
// cacheWithoutLock acquires the store's (write) mutex and calls cacheWithLock.
func (d *Diskv) cacheWithoutLock(key string, val []byte) error {
d.Lock()
defer d.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
return d.cacheWithLock(key, val)
}

View file

@ -3,7 +3,7 @@ package diskv
import (
"sync"
"github.com/petar/GoLLRB/llrb"
"github.com/google/btree"
)
// Index is a generic interface for things that can
@ -18,85 +18,84 @@ type Index interface {
// LessFunction is used to initialize an Index of keys in a specific order.
type LessFunction func(string, string) bool
// llrbString is a custom data type that satisfies the LLRB Less interface,
// making the strings it wraps sortable by the LLRB package.
type llrbString struct {
// btreeString is a custom data type that satisfies the BTree Less interface,
// making the strings it wraps sortable by the BTree package.
type btreeString struct {
s string
l LessFunction
}
// Less satisfies the llrb.Less interface using the llrbString's LessFunction.
func (s llrbString) Less(i llrb.Item) bool {
return s.l(s.s, i.(llrbString).s)
// Less satisfies the BTree.Less interface using the btreeString's LessFunction.
func (s btreeString) Less(i btree.Item) bool {
return s.l(s.s, i.(btreeString).s)
}
// LLRBIndex is an implementation of the Index interface
// using Petar Maymounkov's LLRB tree.
type LLRBIndex struct {
// BTreeIndex is an implementation of the Index interface using google/btree.
type BTreeIndex struct {
sync.RWMutex
LessFunction
*llrb.LLRB
*btree.BTree
}
// Initialize populates the LLRB tree with data from the keys channel,
// according to the passed less function. It's destructive to the LLRBIndex.
func (i *LLRBIndex) Initialize(less LessFunction, keys <-chan string) {
// Initialize populates the BTree tree with data from the keys channel,
// according to the passed less function. It's destructive to the BTreeIndex.
func (i *BTreeIndex) Initialize(less LessFunction, keys <-chan string) {
i.Lock()
defer i.Unlock()
i.LessFunction = less
i.LLRB = rebuild(less, keys)
i.BTree = rebuild(less, keys)
}
// Insert inserts the given key (only) into the LLRB tree.
func (i *LLRBIndex) Insert(key string) {
// Insert inserts the given key (only) into the BTree tree.
func (i *BTreeIndex) Insert(key string) {
i.Lock()
defer i.Unlock()
if i.LLRB == nil || i.LessFunction == nil {
if i.BTree == nil || i.LessFunction == nil {
panic("uninitialized index")
}
i.LLRB.ReplaceOrInsert(llrbString{s: key, l: i.LessFunction})
i.BTree.ReplaceOrInsert(btreeString{s: key, l: i.LessFunction})
}
// Delete removes the given key (only) from the LLRB tree.
func (i *LLRBIndex) Delete(key string) {
// Delete removes the given key (only) from the BTree tree.
func (i *BTreeIndex) Delete(key string) {
i.Lock()
defer i.Unlock()
if i.LLRB == nil || i.LessFunction == nil {
if i.BTree == nil || i.LessFunction == nil {
panic("uninitialized index")
}
i.LLRB.Delete(llrbString{s: key, l: i.LessFunction})
i.BTree.Delete(btreeString{s: key, l: i.LessFunction})
}
// Keys yields a maximum of n keys in order. If the passed 'from' key is empty,
// Keys will return the first n keys. If the passed 'from' key is non-empty, the
// first key in the returned slice will be the key that immediately follows the
// passed key, in key order.
func (i *LLRBIndex) Keys(from string, n int) []string {
func (i *BTreeIndex) Keys(from string, n int) []string {
i.RLock()
defer i.RUnlock()
if i.LLRB == nil || i.LessFunction == nil {
if i.BTree == nil || i.LessFunction == nil {
panic("uninitialized index")
}
if i.LLRB.Len() <= 0 {
if i.BTree.Len() <= 0 {
return []string{}
}
llrbFrom := llrbString{s: from, l: i.LessFunction}
btreeFrom := btreeString{s: from, l: i.LessFunction}
skipFirst := true
if len(from) <= 0 || !i.LLRB.Has(llrbFrom) {
// no such key, so start at the top
llrbFrom = i.LLRB.Min().(llrbString)
if len(from) <= 0 || !i.BTree.Has(btreeFrom) {
// no such key, so fabricate an always-smallest item
btreeFrom = btreeString{s: "", l: func(string, string) bool { return true }}
skipFirst = false
}
keys := []string{}
iterator := func(i llrb.Item) bool {
keys = append(keys, i.(llrbString).s)
iterator := func(i btree.Item) bool {
keys = append(keys, i.(btreeString).s)
return len(keys) < n
}
i.LLRB.AscendGreaterOrEqual(llrbFrom, iterator)
i.BTree.AscendGreaterOrEqual(btreeFrom, iterator)
if skipFirst && len(keys) > 0 {
keys = keys[1:]
@ -107,10 +106,10 @@ func (i *LLRBIndex) Keys(from string, n int) []string {
// rebuildIndex does the work of regenerating the index
// with the given keys.
func rebuild(less LessFunction, keys <-chan string) *llrb.LLRB {
tree := llrb.New()
func rebuild(less LessFunction, keys <-chan string) *btree.BTree {
tree := btree.New(2)
for key := range keys {
tree.ReplaceOrInsert(llrbString{s: key, l: less})
tree.ReplaceOrInsert(btreeString{s: key, l: less})
}
return tree
}