qbolt/MemoryStore.go

53 lines
998 B
Go

package main
import "C"
import (
"errors"
"sync"
)
type ObjectReference int64
var NullObjectReference error = errors.New("Null object reference")
// GoMemoryStore is a int->interface storage structure so that Go pointers are
// never exposed to C code.
type GoMemoryStore struct {
mtx sync.RWMutex
items map[int64]interface{}
next int64
}
func NewGoMemoryStore() *GoMemoryStore {
ret := GoMemoryStore{}
ret.items = make(map[int64]interface{})
return &ret
}
func (this *GoMemoryStore) Put(itm interface{}) ObjectReference {
this.mtx.Lock()
defer this.mtx.Unlock()
key := this.next
this.items[key] = itm
this.next++
return ObjectReference(key)
}
func (this *GoMemoryStore) Get(i ObjectReference) (interface{}, bool) {
this.mtx.RLock()
defer this.mtx.RUnlock()
ret, ok := this.items[int64(i)]
return ret, ok
}
func (this *GoMemoryStore) Delete(i ObjectReference) {
this.mtx.Lock()
defer this.mtx.Unlock()
delete(this.items, int64(i))
}
var gms = NewGoMemoryStore()