Chromium Code Reviews| 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 wg := &sync.WaitGroup{} | |
| 17 wg.Add(2) | |
|
M-A Ruel
2015/05/31 23:03:15
Please do it right above the go statements, e.g. m
iannucci
2015/05/31 23:31:33
Done.
| |
| 18 | |
| 19 walker := func(c *memCollection, ch chan<- *gkvlite.Item) { | |
| 20 defer close(ch) | |
| 21 defer wg.Done() | |
| 22 if c != nil { | |
| 23 c.VisitItemsAscend(nil, true, func(i *gkvlite.Item) bool { | |
| 24 ch <- i | |
| 25 return true | |
| 26 }) | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 go walker(o, oldItems) | |
| 31 go walker(n, newItems) | |
| 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 | |
|
M-A Ruel
2015/05/31 23:03:16
remove
iannucci
2015/05/31 23:31:33
Done.
| |
| 59 } | |
| 60 wg.Wait() | |
|
M-A Ruel
2015/05/31 23:03:15
In theory if you synchronize with channels you don
iannucci
2015/05/31 23:31:33
yeah, but doesn't the WaitGroup use channels anywa
| |
| 61 } | |
| 62 | |
| 11 // memStore is a gkvlite.Store which will panic for anything which might | 63 // memStore is a gkvlite.Store which will panic for anything which might |
| 12 // otherwise return an error. | 64 // otherwise return an error. |
| 13 // | 65 // |
| 14 // This is reasonable for in-memory Store objects, since the only errors that | 66 // 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 | 67 // should occur happen with file IO on the underlying file (which of course |
| 16 // doesn't exist). | 68 // doesn't exist). |
| 17 type memStore gkvlite.Store | 69 type memStore gkvlite.Store |
| 18 | 70 |
| 19 func newMemStore() *memStore { | 71 func newMemStore() *memStore { |
| 20 ret, err := gkvlite.NewStore(nil) | 72 ret, err := gkvlite.NewStore(nil) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 33 } | 85 } |
| 34 | 86 |
| 35 func (ms *memStore) GetCollection(name string) *memCollection { | 87 func (ms *memStore) GetCollection(name string) *memCollection { |
| 36 return (*memCollection)((*gkvlite.Store)(ms).GetCollection(name)) | 88 return (*memCollection)((*gkvlite.Store)(ms).GetCollection(name)) |
| 37 } | 89 } |
| 38 | 90 |
| 39 func (ms *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle ction { | 91 func (ms *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle ction { |
| 40 return (*memCollection)((*gkvlite.Store)(ms).SetCollection(name, cmp)) | 92 return (*memCollection)((*gkvlite.Store)(ms).SetCollection(name, cmp)) |
| 41 } | 93 } |
| 42 | 94 |
| 95 func (ms *memStore) RemoveCollection(name string) { | |
| 96 (*gkvlite.Store)(ms).RemoveCollection(name) | |
| 97 } | |
| 98 | |
| 99 func (ms *memStore) GetCollectionNames() []string { | |
| 100 return (*gkvlite.Store)(ms).GetCollectionNames() | |
| 101 } | |
| 102 | |
| 43 // memCollection is a gkvlite.Collection which will panic for anything which | 103 // memCollection is a gkvlite.Collection which will panic for anything which |
| 44 // might otherwise return an error. | 104 // might otherwise return an error. |
| 45 // | 105 // |
| 46 // This is reasonable for in-memory Store objects, since the only errors that | 106 // 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 | 107 // should occur happen with file IO on the underlying file (which of course |
| 48 // doesn't exist. | 108 // doesn't exist. |
| 49 type memCollection gkvlite.Collection | 109 type memCollection gkvlite.Collection |
| 50 | 110 |
| 51 func (mc *memCollection) Get(k []byte) []byte { | 111 func (mc *memCollection) Get(k []byte) []byte { |
| 52 ret, err := (*gkvlite.Collection)(mc).Get(k) | 112 ret, err := (*gkvlite.Collection)(mc).Get(k) |
| 53 if err != nil { | 113 if err != nil { |
| 54 panic(err) | 114 panic(err) |
| 55 } | 115 } |
| 56 return ret | 116 return ret |
| 57 } | 117 } |
| 58 | 118 |
| 119 func (mc *memCollection) MinItem(withValue bool) *gkvlite.Item { | |
| 120 ret, err := (*gkvlite.Collection)(mc).MinItem(withValue) | |
| 121 if err != nil { | |
| 122 panic(err) | |
| 123 } | |
| 124 return ret | |
| 125 } | |
| 126 | |
| 59 func (mc *memCollection) Set(k, v []byte) { | 127 func (mc *memCollection) Set(k, v []byte) { |
| 60 if err := (*gkvlite.Collection)(mc).Set(k, v); err != nil { | 128 if err := (*gkvlite.Collection)(mc).Set(k, v); err != nil { |
| 61 panic(err) | 129 panic(err) |
| 62 } | 130 } |
| 63 } | 131 } |
| 64 | 132 |
| 65 func (mc *memCollection) Delete(k []byte) bool { | 133 func (mc *memCollection) Delete(k []byte) bool { |
| 66 ret, err := (*gkvlite.Collection)(mc).Delete(k) | 134 ret, err := (*gkvlite.Collection)(mc).Delete(k) |
| 67 if err != nil { | 135 if err != nil { |
| 68 panic(err) | 136 panic(err) |
| 69 } | 137 } |
| 70 return ret | 138 return ret |
| 71 } | 139 } |
| 72 | 140 |
| 73 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor gkvlite.ItemVisitor) { | 141 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor gkvlite.ItemVisitor) { |
| 74 if err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue, visitor); err != nil { | 142 if err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue, visitor); err != nil { |
| 75 panic(err) | 143 panic(err) |
| 76 } | 144 } |
| 77 } | 145 } |
| 78 | 146 |
| 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) { | 147 func (mc *memCollection) GetTotals() (numItems, numBytes uint64) { |
| 86 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals() | 148 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals() |
| 87 if err != nil { | 149 if err != nil { |
| 88 panic(err) | 150 panic(err) |
| 89 } | 151 } |
| 90 return | 152 return |
| 91 } | 153 } |
| OLD | NEW |