OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
| 8 "bytes" |
| 9 "sync" |
| 10 |
8 "github.com/luci/gkvlite" | 11 "github.com/luci/gkvlite" |
9 ) | 12 ) |
10 | 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 |
11 // memStore is a gkvlite.Store which will panic for anything which might | 61 // memStore is a gkvlite.Store which will panic for anything which might |
12 // otherwise return an error. | 62 // otherwise return an error. |
13 // | 63 // |
14 // This is reasonable for in-memory Store objects, since the only errors that | 64 // This is reasonable for in-memory Store objects, since the only errors that |
15 // should occur happen with file IO on the underlying file (which of course | 65 // should occur happen with file IO on the underlying file (which of course |
16 // doesn't exist). | 66 // doesn't exist). |
17 type memStore gkvlite.Store | 67 type memStore gkvlite.Store |
18 | 68 |
19 func newMemStore() *memStore { | 69 func newMemStore() *memStore { |
20 ret, err := gkvlite.NewStore(nil) | 70 ret, err := gkvlite.NewStore(nil) |
(...skipping 12 matching lines...) Expand all Loading... |
33 } | 83 } |
34 | 84 |
35 func (ms *memStore) GetCollection(name string) *memCollection { | 85 func (ms *memStore) GetCollection(name string) *memCollection { |
36 return (*memCollection)((*gkvlite.Store)(ms).GetCollection(name)) | 86 return (*memCollection)((*gkvlite.Store)(ms).GetCollection(name)) |
37 } | 87 } |
38 | 88 |
39 func (ms *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle
ction { | 89 func (ms *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle
ction { |
40 return (*memCollection)((*gkvlite.Store)(ms).SetCollection(name, cmp)) | 90 return (*memCollection)((*gkvlite.Store)(ms).SetCollection(name, cmp)) |
41 } | 91 } |
42 | 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 |
43 // memCollection is a gkvlite.Collection which will panic for anything which | 101 // memCollection is a gkvlite.Collection which will panic for anything which |
44 // might otherwise return an error. | 102 // might otherwise return an error. |
45 // | 103 // |
46 // This is reasonable for in-memory Store objects, since the only errors that | 104 // This is reasonable for in-memory Store objects, since the only errors that |
47 // should occur happen with file IO on the underlying file (which of course | 105 // should occur happen with file IO on the underlying file (which of course |
48 // doesn't exist. | 106 // doesn't exist. |
49 type memCollection gkvlite.Collection | 107 type memCollection gkvlite.Collection |
50 | 108 |
51 func (mc *memCollection) Get(k []byte) []byte { | 109 func (mc *memCollection) Get(k []byte) []byte { |
52 ret, err := (*gkvlite.Collection)(mc).Get(k) | 110 ret, err := (*gkvlite.Collection)(mc).Get(k) |
53 if err != nil { | 111 if err != nil { |
54 panic(err) | 112 panic(err) |
55 } | 113 } |
56 return ret | 114 return ret |
57 } | 115 } |
58 | 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 |
59 func (mc *memCollection) Set(k, v []byte) { | 125 func (mc *memCollection) Set(k, v []byte) { |
60 if err := (*gkvlite.Collection)(mc).Set(k, v); err != nil { | 126 if err := (*gkvlite.Collection)(mc).Set(k, v); err != nil { |
61 panic(err) | 127 panic(err) |
62 } | 128 } |
63 } | 129 } |
64 | 130 |
65 func (mc *memCollection) Delete(k []byte) bool { | 131 func (mc *memCollection) Delete(k []byte) bool { |
66 ret, err := (*gkvlite.Collection)(mc).Delete(k) | 132 ret, err := (*gkvlite.Collection)(mc).Delete(k) |
67 if err != nil { | 133 if err != nil { |
68 panic(err) | 134 panic(err) |
69 } | 135 } |
70 return ret | 136 return ret |
71 } | 137 } |
72 | 138 |
73 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor
gkvlite.ItemVisitor) { | 139 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor
gkvlite.ItemVisitor) { |
74 if err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue,
visitor); err != nil { | 140 if err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue,
visitor); err != nil { |
75 panic(err) | 141 panic(err) |
76 } | 142 } |
77 } | 143 } |
78 | 144 |
79 func (mc *memCollection) VisitItemsDescend(target []byte, withValue bool, visito
r gkvlite.ItemVisitor) { | |
80 if err := (*gkvlite.Collection)(mc).VisitItemsDescend(target, withValue,
visitor); err != nil { | |
81 panic(err) | |
82 } | |
83 } | |
84 | |
85 func (mc *memCollection) GetTotals() (numItems, numBytes uint64) { | 145 func (mc *memCollection) GetTotals() (numItems, numBytes uint64) { |
86 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals() | 146 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals() |
87 if err != nil { | 147 if err != nil { |
88 panic(err) | 148 panic(err) |
89 } | 149 } |
90 return | 150 return |
91 } | 151 } |
OLD | NEW |