Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: go/src/infra/gae/libs/wrapper/memory/gkvlite_utils.go

Issue 1240573002: Reland: Refactor current GAE abstraction library to be free of the SDK* (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: expand coverage range to fit 32bit test expectations Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 func (ms *memStore) RemoveCollection(name string) {
99 (*gkvlite.Store)(ms).RemoveCollection(name)
100 }
101
102 func (ms *memStore) GetCollectionNames() []string {
103 return (*gkvlite.Store)(ms).GetCollectionNames()
104 }
105
106 // memCollection is a gkvlite.Collection which will panic for anything which
107 // might otherwise return an error.
108 //
109 // This is reasonable for in-memory Store objects, since the only errors that
110 // should occur happen with file IO on the underlying file (which of course
111 // doesn't exist.
112 type memCollection gkvlite.Collection
113
114 func (mc *memCollection) Get(k []byte) []byte {
115 ret, err := (*gkvlite.Collection)(mc).Get(k)
116 if err != nil {
117 panic(err)
118 }
119 return ret
120 }
121
122 func (mc *memCollection) MinItem(withValue bool) *gkvlite.Item {
123 ret, err := (*gkvlite.Collection)(mc).MinItem(withValue)
124 if err != nil {
125 panic(err)
126 }
127 return ret
128 }
129
130 func (mc *memCollection) Set(k, v []byte) {
131 if err := (*gkvlite.Collection)(mc).Set(k, v); err != nil {
132 panic(err)
133 }
134 }
135
136 func (mc *memCollection) Delete(k []byte) bool {
137 ret, err := (*gkvlite.Collection)(mc).Delete(k)
138 if err != nil {
139 panic(err)
140 }
141 return ret
142 }
143
144 func (mc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor gkvlite.ItemVisitor) {
145 if err := (*gkvlite.Collection)(mc).VisitItemsAscend(target, withValue, visitor); err != nil {
146 panic(err)
147 }
148 }
149
150 func (mc *memCollection) GetTotals() (numItems, numBytes uint64) {
151 numItems, numBytes, err := (*gkvlite.Collection)(mc).GetTotals()
152 if err != nil {
153 panic(err)
154 }
155 return
156 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/libs/wrapper/memory/doc.go ('k') | go/src/infra/gae/libs/wrapper/memory/gkvlite_utils_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698