ducky/devices

Paddy 2015-11-12 Parent:8d40c0e5f4d9 Child:408abf6e48d3

4:7bc6a84ac906 Browse Files

Minimal Memstore implementation. Create an in-memory version of the Storer that meets the minimal requirements set by our Storer tests so far.

memstore.go memstore_test.go

     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memstore.go	Thu Nov 12 23:26:26 2015 -0800
     1.3 @@ -0,0 +1,66 @@
     1.4 +package devices
     1.5 +
     1.6 +import (
     1.7 +	"sync"
     1.8 +
     1.9 +	"code.secondbit.org/uuid.hg"
    1.10 +	"golang.org/x/net/context"
    1.11 +)
    1.12 +
    1.13 +type Memstore struct {
    1.14 +	devices map[string]Device
    1.15 +	lock    sync.RWMutex
    1.16 +}
    1.17 +
    1.18 +func NewMemstore() *Memstore {
    1.19 +	return &Memstore{
    1.20 +		devices: map[string]Device{},
    1.21 +	}
    1.22 +}
    1.23 +
    1.24 +func (m *Memstore) GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) {
    1.25 +	m.lock.RLock()
    1.26 +	defer m.lock.RUnlock()
    1.27 +
    1.28 +	results := map[string]Device{}
    1.29 +
    1.30 +	for _, id := range ids {
    1.31 +		device, ok := m.devices[id.String()]
    1.32 +		if !ok {
    1.33 +			continue
    1.34 +		}
    1.35 +		results[id.String()] = device
    1.36 +	}
    1.37 +	return results, nil
    1.38 +}
    1.39 +
    1.40 +func (m *Memstore) UpdateDevice(change DeviceChange, c context.Context) error {
    1.41 +	return nil
    1.42 +}
    1.43 +
    1.44 +func (m *Memstore) DeleteDevices(id []uuid.ID, c context.Context) error {
    1.45 +	return nil
    1.46 +}
    1.47 +
    1.48 +func (m *Memstore) CreateDevices(devices []Device, c context.Context) error {
    1.49 +	m.lock.Lock()
    1.50 +	defer m.lock.Unlock()
    1.51 +
    1.52 +	for _, device := range devices {
    1.53 +		m.devices[device.ID.String()] = device
    1.54 +	}
    1.55 +	return nil
    1.56 +}
    1.57 +
    1.58 +func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) {
    1.59 +	return []Device{}, nil
    1.60 +}
    1.61 +
    1.62 +func (m *Memstore) Factory(c context.Context) (Storer, error) {
    1.63 +	return NewMemstore(), nil
    1.64 +}
    1.65 +
    1.66 +func (m *Memstore) Destroy(c context.Context) error {
    1.67 +	m.devices = nil
    1.68 +	return nil
    1.69 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/memstore_test.go	Thu Nov 12 23:26:26 2015 -0800
     2.3 @@ -0,0 +1,5 @@
     2.4 +package devices
     2.5 +
     2.6 +func init() {
     2.7 +	storers = append(storers, NewMemstore())
     2.8 +}