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