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 "github.com/luci/gkvlite" |
| 9 ) |
| 10 |
| 11 // memStore is a gkvlite.Store which will panic for anything which might |
| 12 // otherwise return an error. |
| 13 // |
| 14 // 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 |
| 16 // doesn't exist). |
| 17 type memStore gkvlite.Store |
| 18 |
| 19 func newMemStore() *memStore { |
| 20 ret, err := gkvlite.NewStore(nil) |
| 21 if err != nil { |
| 22 panic(err) |
| 23 } |
| 24 return (*memStore)(ret) |
| 25 } |
| 26 |
| 27 func (bs *memStore) Snapshot() *memStore { |
| 28 return (*memStore)((*gkvlite.Store)(bs).Snapshot()) |
| 29 } |
| 30 |
| 31 func (bs *memStore) MakePrivateCollection(cmp gkvlite.KeyCompare) *memCollection
{ |
| 32 return (*memCollection)((*gkvlite.Store)(bs).MakePrivateCollection(cmp)) |
| 33 } |
| 34 |
| 35 func (bs *memStore) GetCollection(name string) *memCollection { |
| 36 return (*memCollection)((*gkvlite.Store)(bs).GetCollection(name)) |
| 37 } |
| 38 |
| 39 func (bs *memStore) SetCollection(name string, cmp gkvlite.KeyCompare) *memColle
ction { |
| 40 return (*memCollection)((*gkvlite.Store)(bs).SetCollection(name, cmp)) |
| 41 } |
| 42 |
| 43 // memCollection is a gkvlite.Collection which will panic for anything which |
| 44 // might otherwise return an error. |
| 45 // |
| 46 // 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 |
| 48 // doesn't exist. |
| 49 type memCollection gkvlite.Collection |
| 50 |
| 51 func (bc *memCollection) Get(k []byte) []byte { |
| 52 ret, err := (*gkvlite.Collection)(bc).Get(k) |
| 53 if err != nil { |
| 54 panic(err) |
| 55 } |
| 56 return ret |
| 57 } |
| 58 |
| 59 func (bc *memCollection) Set(k, v []byte) { |
| 60 err := (*gkvlite.Collection)(bc).Set(k, v) |
| 61 if err != nil { |
| 62 panic(err) |
| 63 } |
| 64 } |
| 65 |
| 66 func (bc *memCollection) Delete(k []byte) bool { |
| 67 ret, err := (*gkvlite.Collection)(bc).Delete(k) |
| 68 if err != nil { |
| 69 panic(err) |
| 70 } |
| 71 return ret |
| 72 } |
| 73 |
| 74 func (bc *memCollection) VisitItemsAscend(target []byte, withValue bool, visitor
gkvlite.ItemVisitor) { |
| 75 err := (*gkvlite.Collection)(bc).VisitItemsAscend(target, withValue, vis
itor) |
| 76 if err != nil { |
| 77 panic(err) |
| 78 } |
| 79 } |
| 80 |
| 81 func (bc *memCollection) VisitItemsDescend(target []byte, withValue bool, visito
r gkvlite.ItemVisitor) { |
| 82 err := (*gkvlite.Collection)(bc).VisitItemsDescend(target, withValue, vi
sitor) |
| 83 if err != nil { |
| 84 panic(err) |
| 85 } |
| 86 } |
| 87 |
| 88 func (bc *memCollection) GetTotals() (numItems, numBytes uint64) { |
| 89 numItems, numBytes, err := (*gkvlite.Collection)(bc).GetTotals() |
| 90 if err != nil { |
| 91 panic(err) |
| 92 } |
| 93 return |
| 94 } |
OLD | NEW |