ducky/devices

Paddy 2015-11-29 Parent:03c49b4d3d9f

14:1ae5bae472c1 Go to Latest

ducky/devices/memstore.go

Set up DeleteDevices in Memstore. Implement the DeleteDevices method for our Memstore, removing the Devices specified by the passed IDs. Also, create a simple test that verifies that when Devices are deleted, only the Devices you intend to delete are actually deleted. Further tests should be written to verify that this extends to ListDevicesByOwner (e.g., deleted devices will not be returned when listing them), CreateDevices (e.g., will not result in an ErrDeviceAlreadyExists error), and UpdateDevice (e.g., will return an ErrDeviceNotFound error after the Device is deleted). But for now, we're confident that it works in the simplest possible case.

History
paddy@4 1 package devices
paddy@4 2
paddy@4 3 import (
paddy@4 4 "sync"
paddy@4 5
paddy@4 6 "code.secondbit.org/uuid.hg"
paddy@4 7 "golang.org/x/net/context"
paddy@4 8 )
paddy@4 9
paddy@12 10 // Memstore is an in-memory implementation of Storer, and should
paddy@12 11 // only be used for testing or for temporary local servers.
paddy@4 12 type Memstore struct {
paddy@4 13 devices map[string]Device
paddy@4 14 lock sync.RWMutex
paddy@4 15 }
paddy@4 16
paddy@12 17 // NewMemstore returns a Memstore that is ready to be used as a
paddy@12 18 // Storer implementation.
paddy@4 19 func NewMemstore() *Memstore {
paddy@4 20 return &Memstore{
paddy@4 21 devices: map[string]Device{},
paddy@4 22 }
paddy@4 23 }
paddy@4 24
paddy@12 25 // GetDevices returns any Devices in the Memstore that match the
paddy@12 26 // passed IDs. If an ID cannot be matched to a Device in the
paddy@12 27 // Memstore, it is ignored. The result is a map, with the values
paddy@12 28 // being the Devices that could be found, and the keys being the
paddy@12 29 // result of the String() method for each Device's ID.
paddy@12 30 //
paddy@12 31 // An empty map is a possible response, if none of the IDs could
paddy@12 32 // be found.
paddy@4 33 func (m *Memstore) GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) {
paddy@4 34 m.lock.RLock()
paddy@4 35 defer m.lock.RUnlock()
paddy@4 36
paddy@4 37 results := map[string]Device{}
paddy@4 38
paddy@4 39 for _, id := range ids {
paddy@4 40 device, ok := m.devices[id.String()]
paddy@4 41 if !ok {
paddy@4 42 continue
paddy@4 43 }
paddy@4 44 results[id.String()] = device
paddy@4 45 }
paddy@4 46 return results, nil
paddy@4 47 }
paddy@4 48
paddy@12 49 // UpdateDevice applies the passed DeviceChange to the Device
paddy@12 50 // in the Memstore specified by the DeviceChange's DeviceID
paddy@12 51 // property. If no Device in the Memstore matches the DeviceChange's
paddy@12 52 // DeviceID property, then an ErrDeviceNotFound error will be
paddy@12 53 // returned.
paddy@4 54 func (m *Memstore) UpdateDevice(change DeviceChange, c context.Context) error {
paddy@10 55 m.lock.Lock()
paddy@10 56 defer m.lock.Unlock()
paddy@10 57
paddy@10 58 device, ok := m.devices[change.DeviceID.String()]
paddy@10 59 if !ok {
paddy@11 60 return ErrDeviceNotFound
paddy@10 61 }
paddy@10 62
paddy@10 63 device = ApplyChange(device, change)
paddy@10 64 m.devices[change.DeviceID.String()] = device
paddy@10 65
paddy@4 66 return nil
paddy@4 67 }
paddy@4 68
paddy@12 69 // DeleteDevices will remove any Devices from the Memstore that match
paddy@12 70 // the IDs passed in. If an ID can't be matched to a Device in the
paddy@12 71 // Memstore, then it is ignored.
paddy@14 72 func (m *Memstore) DeleteDevices(ids []uuid.ID, c context.Context) error {
paddy@14 73 m.lock.Lock()
paddy@14 74 defer m.lock.Unlock()
paddy@14 75
paddy@14 76 for _, id := range ids {
paddy@14 77 delete(m.devices, id.String())
paddy@14 78 }
paddy@14 79
paddy@4 80 return nil
paddy@4 81 }
paddy@4 82
paddy@12 83 // CreateDevices stores the passed devices in the Memstore as a single
paddy@12 84 // transaction. If a Device's ID already exists in the Memstore, an
paddy@12 85 // ErrDeviceAlreadyExists error with that Device's ID is returned, and
paddy@12 86 // none of the passed Devices are stored.
paddy@4 87 func (m *Memstore) CreateDevices(devices []Device, c context.Context) error {
paddy@4 88 m.lock.Lock()
paddy@4 89 defer m.lock.Unlock()
paddy@4 90
paddy@4 91 for _, device := range devices {
paddy@5 92 if _, ok := m.devices[device.ID.String()]; ok {
paddy@5 93 return ErrDeviceAlreadyExists(device.ID)
paddy@5 94 }
paddy@5 95 }
paddy@5 96
paddy@5 97 for _, device := range devices {
paddy@4 98 m.devices[device.ID.String()] = device
paddy@4 99 }
paddy@4 100 return nil
paddy@4 101 }
paddy@4 102
paddy@12 103 // ListDevicesByOwner returns all the Devices in the Memstore that have an
paddy@12 104 // Owner property that matches the passed ID. If no Devices have an Owner
paddy@12 105 // property matching the passed ID, an empty slice is returned.
paddy@12 106 //
paddy@12 107 // ListDevicesByOwner does not guarantee any sort order for the Devices.
paddy@4 108 func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) {
paddy@8 109 var devices []Device
paddy@8 110 for _, device := range m.devices {
paddy@8 111 if !device.Owner.Equal(user) {
paddy@8 112 continue
paddy@8 113 }
paddy@8 114 devices = append(devices, device)
paddy@8 115 }
paddy@8 116 return devices, nil
paddy@4 117 }