qbolt/MemoryStore.go

47 lines
869 B
Go
Raw Normal View History

2017-05-16 07:34:54 +00:00
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 (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 GoMemoryStore