| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package memory | |
| 6 | |
| 7 import ( | |
| 8 "bytes" | |
| 9 "sync" | |
| 10 | |
| 11 "github.com/luci/gkvlite" | |
| 12 ) | |
| 13 | |
| 14 func gkvCollide(o, n *memCollection, f func(k, ov, nv []byte)) { | |
| 15 oldItems, newItems := make(chan *gkvlite.Item), make(chan *gkvlite.Item) | |
| 16 walker := func(c *memCollection, ch chan<- *gkvlite.Item, wg *sync.WaitG
roup) { | |
| 17 defer close(ch) | |
| 18 defer wg.Done() | |
| 19 if c != nil { | |
| 20 c.VisitItemsAscend(nil, true, func(i *gkvlite.Item) bool
{ | |
| 21 ch <- i | |
| 22 return true | |
| 23 }) | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 wg := &sync.WaitGroup{} | |
| 28 wg.Add(2) | |
| 29 go walker(o, oldItems, wg) | |
| 30 go walker(n, newItems, wg) | |
| 31 | |
| 32 l, r := <-oldItems, <-newItems | |
| 33 for { | |
| 34 if l == nil && r == nil { | |
| 35 break | |
| 36 } | |
| 37 | |
| 38 if l == nil { | |
| 39 f(r.Key, nil, r.Val) | |
| 40 r = <-newItems | |
| 41 } else if r == nil { | |
| 42 f(l.Key, l.Val, nil) | |
| 43 l = <-oldItems | |
| 44 } else { | |
| 45 switch bytes.Compare(l.Key, r.Key) { | |
| 46 case -1: // l < r | |
| 47 f(l.Key, l.Val, nil) | |
| 48 l = <-oldItems | |
| 49 case 0: // l == r | |
| 50 f(l.Key, l.Val, r.Val) | |
| 51 l, r = <-oldItems, <-newItems | |
| 52 case 1: // l > r | |
| 53 f(r.Key, nil, r.Val) | |
| 54 r = <-newItems | |
| 55 } | |
| 56 } | |
| 57 } | |
| 58 wg.Wait() | |
| 59 } | |
| 60 | |
| 61 // memStore is a gkvlite.Store which will panic for anything which might | |
| 62 // otherwise return an error. | |
| 63 // | |
| 64 // This is reasonable for in-memory Store objects, since the only errors that | |
| 65 // should occur happen with file IO on the underlying file (which of course | |
| 66 // doesn't exist). | |
| 67 type memStore gkvlite.Store | |
| 68 | |
| 69 func newMemStore() *memStore { | |
| 70 ret, err := gkvlite.NewStore(nil) | |
| 71 if err != nil { | |
| 72 panic(err) | |
| 73 } | |
| 74 return (*memStore)(ret) | |
| 75 } | |
| 76 | |
| 77 func (ms *memStore) Snapshot() *memStore { | |
| 78 return (*memStore)((*gkvlite.Store)(ms).Snapshot()) | |
| 79 } | |
| 80 | |
| 81 func (ms *memStore) MakePrivateCollection(cmp gkvlite.KeyCompare) *memCollection
{ | |
| 82 return (*memCollection)((*gkvlite.Store)(ms).MakePrivateCollection(cmp)) | |
| 83 } | |
| 84 | |
| 85 func (ms *memStore) GetCollection(name string) *memCollection { | |
| 86 return (*memCollection)((*gkvlite.Store)(ms).GetCollection(name)) | |
| 87 } | |
| 88 | |
| 89 func (ms *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle
ction { | |
| 90 return (*memCollection)((*gkvlite.Store)(ms).SetCollection(name, cmp)) | |
| 91 } | |
| 92 | |
| 93 func (ms *memStore) RemoveCollection(name string) { | |
| 94 (*gkvlite.Store)(ms).RemoveCollection(name) | |
| 95 } | |
| 96 | |
| 97 func (ms *memStore) GetCollectionNames() []string { | |
| 98 return (*gkvlite.Store)(ms).GetCollectionNames() | |
| 99 } | |
| 100 | |
| 101 // memCollection is a gkvlite.Collection which will panic for anything which | |
| 102 // might otherwise return an error. | |
| 103 // | |
| 104 // This is reasonable for in-memory Store objects, since the only errors that | |
| 105 // should occur happen with file IO on the underlying file (which of course | |
| 106 // doesn't exist. | |
| 107 type memCollection gkvlite.Collection | |
| 108 | |
| 109 func (mc *memCollection) Get(k []byte) []byte { | |
| 110 ret, err := (*gkvlite.Collection)(mc).Get(k) | |
| 111 if err != nil { | |
| 112 panic(err) | |
| 113 } | |
| 114 return ret | |
| 115 } | |
| 116 | |
| 117 func (mc *memCollection) MinItem(withValue bool) *gkvlite.Item { | |
| 118 ret, err := (*gkvlite.Collection)(mc).MinItem(withValue) | |
| 119 if err != nil { | |
| 120 panic(err) | |
| 121 } | |
| 122 return ret | |
| 123 } | |
| 124 | |
| 125 func (mc *memCollection) Set(k, v []byte) { | |
| 126 if err := (*gkvlite.Collection)(mc).Set(k, v); err != nil { | |
| 127 panic(err) | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 func (mc *memCollection) Delete(k []byte) bool { | |
| 132 ret, err := (*gkvlite.Collection)(mc).Delete(k) | |
| 133 if err != nil { | |
| 134 panic(err) | |
| 135 } | |
| 136 return ret | |
| 137 } | |
| 138 | |
| 139 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor
gkvlite.ItemVisitor) { | |
| 140 if err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue,
visitor); err != nil { | |
| 141 panic(err) | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 func (mc *memCollection) GetTotals() (numItems, numBytes uint64) { | |
| 146 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals() | |
| 147 if err != nil { | |
| 148 panic(err) | |
| 149 } | |
| 150 return | |
| 151 } | |
| OLD | NEW |