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" | 8 "bytes" |
9 "runtime" | 9 "runtime" |
10 "sync" | 10 "sync" |
11 | 11 |
12 "github.com/luci/gkvlite" | 12 "github.com/luci/gkvlite" |
13 ) | 13 ) |
14 | 14 |
15 func gkvCollide(o, n *memCollection, f func(k, ov, nv []byte)) { | 15 func gkvCollide(o, n *memCollection, f func(k, ov, nv []byte)) { |
| 16 // TODO(riannucci): reimplement in terms of *iterator. |
16 oldItems, newItems := make(chan *gkvlite.Item), make(chan *gkvlite.Item) | 17 oldItems, newItems := make(chan *gkvlite.Item), make(chan *gkvlite.Item) |
17 walker := func(c *memCollection, ch chan<- *gkvlite.Item, wg *sync.WaitG
roup) { | 18 walker := func(c *memCollection, ch chan<- *gkvlite.Item, wg *sync.WaitG
roup) { |
18 defer close(ch) | 19 defer close(ch) |
19 defer wg.Done() | 20 defer wg.Done() |
20 if c != nil { | 21 if c != nil { |
21 c.VisitItemsAscend(nil, true, func(i *gkvlite.Item) bool
{ | 22 c.VisitItemsAscend(nil, true, func(i *gkvlite.Item) bool
{ |
22 ch <- i | 23 ch <- i |
23 return true | 24 return true |
24 }) | 25 }) |
25 } | 26 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 // 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 |
63 // otherwise return an error. | 64 // otherwise return an error. |
64 // | 65 // |
65 // 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 |
66 // 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 |
67 // doesn't exist). | 68 // doesn't exist). |
68 type memStore gkvlite.Store | 69 type memStore gkvlite.Store |
69 | 70 |
70 func newMemStore() *memStore { | 71 func newMemStore() *memStore { |
71 ret, err := gkvlite.NewStore(nil) | 72 ret, err := gkvlite.NewStore(nil) |
72 » if err != nil { | 73 » memoryCorruption(err) |
73 » » panic(err) | |
74 » } | |
75 return (*memStore)(ret) | 74 return (*memStore)(ret) |
76 } | 75 } |
77 | 76 |
78 func (ms *memStore) Snapshot() *memStore { | 77 func (ms *memStore) Snapshot() *memStore { |
79 ret := (*memStore)((*gkvlite.Store)(ms).Snapshot()) | 78 ret := (*memStore)((*gkvlite.Store)(ms).Snapshot()) |
80 runtime.SetFinalizer((*gkvlite.Store)(ret), func(s *gkvlite.Store) { | 79 runtime.SetFinalizer((*gkvlite.Store)(ret), func(s *gkvlite.Store) { |
81 go s.Close() | 80 go s.Close() |
82 }) | 81 }) |
83 return ret | 82 return ret |
84 } | 83 } |
85 | 84 |
86 func (ms *memStore) MakePrivateCollection(cmp gkvlite.KeyCompare) *memCollection
{ | 85 func (ms *memStore) MakePrivateCollection(cmp gkvlite.KeyCompare) *memCollection
{ |
87 return (*memCollection)((*gkvlite.Store)(ms).MakePrivateCollection(cmp)) | 86 return (*memCollection)((*gkvlite.Store)(ms).MakePrivateCollection(cmp)) |
88 } | 87 } |
89 | 88 |
90 func (ms *memStore) GetCollection(name string) *memCollection { | 89 func (ms *memStore) GetCollection(name string) *memCollection { |
91 return (*memCollection)((*gkvlite.Store)(ms).GetCollection(name)) | 90 return (*memCollection)((*gkvlite.Store)(ms).GetCollection(name)) |
92 } | 91 } |
93 | 92 |
94 func (ms *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle
ction { | 93 func (ms *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle
ction { |
95 return (*memCollection)((*gkvlite.Store)(ms).SetCollection(name, cmp)) | 94 return (*memCollection)((*gkvlite.Store)(ms).SetCollection(name, cmp)) |
96 } | 95 } |
97 | 96 |
| 97 func (ms *memStore) GetCollectionNames() []string { |
| 98 return (*gkvlite.Store)(ms).GetCollectionNames() |
| 99 } |
| 100 |
98 // memCollection is a gkvlite.Collection which will panic for anything which | 101 // memCollection is a gkvlite.Collection which will panic for anything which |
99 // might otherwise return an error. | 102 // might otherwise return an error. |
100 // | 103 // |
101 // 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 |
102 // 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 |
103 // doesn't exist. | 106 // doesn't exist. |
104 type memCollection gkvlite.Collection | 107 type memCollection gkvlite.Collection |
105 | 108 |
106 func (mc *memCollection) Get(k []byte) []byte { | 109 func (mc *memCollection) Get(k []byte) []byte { |
107 ret, err := (*gkvlite.Collection)(mc).Get(k) | 110 ret, err := (*gkvlite.Collection)(mc).Get(k) |
108 » if err != nil { | 111 » memoryCorruption(err) |
109 » » panic(err) | |
110 » } | |
111 return ret | 112 return ret |
112 } | 113 } |
113 | 114 |
114 func (mc *memCollection) MinItem(withValue bool) *gkvlite.Item { | 115 func (mc *memCollection) MinItem(withValue bool) *gkvlite.Item { |
115 ret, err := (*gkvlite.Collection)(mc).MinItem(withValue) | 116 ret, err := (*gkvlite.Collection)(mc).MinItem(withValue) |
116 » if err != nil { | 117 » memoryCorruption(err) |
117 » » panic(err) | |
118 » } | |
119 return ret | 118 return ret |
120 } | 119 } |
121 | 120 |
122 func (mc *memCollection) Set(k, v []byte) { | 121 func (mc *memCollection) Set(k, v []byte) { |
123 » if err := (*gkvlite.Collection)(mc).Set(k, v); err != nil { | 122 » err := (*gkvlite.Collection)(mc).Set(k, v) |
124 » » panic(err) | 123 » memoryCorruption(err) |
125 » } | |
126 } | 124 } |
127 | 125 |
128 func (mc *memCollection) Delete(k []byte) bool { | 126 func (mc *memCollection) Delete(k []byte) bool { |
129 ret, err := (*gkvlite.Collection)(mc).Delete(k) | 127 ret, err := (*gkvlite.Collection)(mc).Delete(k) |
130 » if err != nil { | 128 » memoryCorruption(err) |
131 » » panic(err) | |
132 » } | |
133 return ret | 129 return ret |
134 } | 130 } |
135 | 131 |
136 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor
gkvlite.ItemVisitor) { | 132 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor
gkvlite.ItemVisitor) { |
137 » if err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue,
visitor); err != nil { | 133 » err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue, vis
itor) |
138 » » panic(err) | 134 » memoryCorruption(err) |
139 » } | |
140 } | 135 } |
141 | 136 |
142 func (mc *memCollection) GetTotals() (numItems, numBytes uint64) { | 137 func (mc *memCollection) GetTotals() (numItems, numBytes uint64) { |
143 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals() | 138 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals() |
144 » if err != nil { | 139 » memoryCorruption(err) |
145 » » panic(err) | |
146 » } | |
147 return | 140 return |
148 } | 141 } |
OLD | NEW |