blob: 3a5ae6cfa5b156f1554f1fefd4e18a78853968a8 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Adam Sadovsky491fb2c2015-02-11 11:25:14 -08005// Package memstore provides a simple, in-memory implementation of server.Store.
6// Since it's a prototype implementation, it doesn't bother with entry-level
7// locking.
8package memstore
9
10import (
11 "strconv"
12 "sync"
13
Todd Wang2d85b152015-04-06 15:38:22 -070014 "v.io/x/ref/services/groups/internal/server"
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080015)
16
17type entry struct {
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070018 v interface{}
19 version int
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080020}
21
22type memstore struct {
23 mu sync.Mutex
24 data map[string]*entry
25}
26
27var _ server.Store = (*memstore)(nil)
28
29func New() server.Store {
30 return &memstore{data: map[string]*entry{}}
31}
32
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070033func (st *memstore) Get(k string) (v interface{}, version string, err error) {
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080034 st.mu.Lock()
35 defer st.mu.Unlock()
36 e, ok := st.data[k]
37 if !ok {
38 return nil, "", &server.ErrUnknownKey{Key: k}
39 }
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070040 return e.v, strconv.Itoa(e.version), nil
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080041}
42
43func (st *memstore) Insert(k string, v interface{}) error {
44 st.mu.Lock()
45 defer st.mu.Unlock()
46 if _, ok := st.data[k]; ok {
47 return &server.ErrKeyAlreadyExists{Key: k}
48 }
49 st.data[k] = &entry{v: v}
50 return nil
51}
52
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070053func (st *memstore) Update(k string, v interface{}, version string) error {
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080054 st.mu.Lock()
55 defer st.mu.Unlock()
56 e, ok := st.data[k]
57 if !ok {
58 return &server.ErrUnknownKey{Key: k}
59 }
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070060 if err := e.checkVersion(version); err != nil {
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080061 return err
62 }
63 e.v = v
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070064 e.version++
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080065 return nil
66}
67
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070068func (st *memstore) Delete(k string, version string) error {
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080069 st.mu.Lock()
70 defer st.mu.Unlock()
71 e, ok := st.data[k]
72 if !ok {
73 return &server.ErrUnknownKey{Key: k}
74 }
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070075 if err := e.checkVersion(version); err != nil {
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080076 return err
77 }
78 delete(st.data, k)
79 return nil
80}
81
Adam Sadovskyb1f9e3c2015-04-08 11:03:49 -070082func (e *entry) checkVersion(version string) error {
83 newVersion := strconv.Itoa(e.version)
84 if version != newVersion {
85 return &server.ErrBadVersion{}
Adam Sadovsky491fb2c2015-02-11 11:25:14 -080086 }
87 return nil
88}