ducky/devices

Paddy 2015-11-28 Parent:683050b4546b Child:1ae5bae472c1

12:03c49b4d3d9f Go to Latest

ducky/devices/memstore.go

Add doc comments to all our exported types. It makes golint happy, and it's a good thing to do. It's kind of shameful that we went so long without them. Oops.

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@4 72 func (m *Memstore) DeleteDevices(id []uuid.ID, c context.Context) error {
paddy@4 73 return nil
paddy@4 74 }
paddy@4 75
paddy@12 76 // CreateDevices stores the passed devices in the Memstore as a single
paddy@12 77 // transaction. If a Device's ID already exists in the Memstore, an
paddy@12 78 // ErrDeviceAlreadyExists error with that Device's ID is returned, and
paddy@12 79 // none of the passed Devices are stored.
paddy@4 80 func (m *Memstore) CreateDevices(devices []Device, c context.Context) error {
paddy@4 81 m.lock.Lock()
paddy@4 82 defer m.lock.Unlock()
paddy@4 83
paddy@4 84 for _, device := range devices {
paddy@5 85 if _, ok := m.devices[device.ID.String()]; ok {
paddy@5 86 return ErrDeviceAlreadyExists(device.ID)
paddy@5 87 }
paddy@5 88 }
paddy@5 89
paddy@5 90 for _, device := range devices {
paddy@4 91 m.devices[device.ID.String()] = device
paddy@4 92 }
paddy@4 93 return nil
paddy@4 94 }
paddy@4 95
paddy@12 96 // ListDevicesByOwner returns all the Devices in the Memstore that have an
paddy@12 97 // Owner property that matches the passed ID. If no Devices have an Owner
paddy@12 98 // property matching the passed ID, an empty slice is returned.
paddy@12 99 //
paddy@12 100 // ListDevicesByOwner does not guarantee any sort order for the Devices.
paddy@4 101 func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) {
paddy@8 102 var devices []Device
paddy@8 103 for _, device := range m.devices {
paddy@8 104 if !device.Owner.Equal(user) {
paddy@8 105 continue
paddy@8 106 }
paddy@8 107 devices = append(devices, device)
paddy@8 108 }
paddy@8 109 return devices, nil
paddy@4 110 }