gifs/api

Paddy 2014-10-17 Parent:96aac3ae74e6

5:b5d88d57d587 Go to Latest

gifs/api/memstore.go

Simplify upload. Simplify the upload code by not running the hashing async, which requires fewer copy operations and less channel synchronization. Also, take advantage of the fact that PipeWriters and PipeReaders will return an error to the PipeReaders/PipeWriters (respectively) when read/write is called (respectively) to avoid passing back errors through channels.

History
1 package api
3 import (
4 "time"
6 "code.secondbit.org/uuid.hg"
7 )
9 type Memstore struct {
10 collections map[string]Collection
11 domains map[string]Domain
12 items map[string]Item
13 }
15 func NewMemstore() Memstore {
16 return Memstore{
17 collections: map[string]Collection{},
18 domains: map[string]Domain{},
19 items: map[string]Item{},
20 }
21 }
23 func (m Memstore) CreateCollection(c Collection) error {
24 m.collections[c.ID.String()] = c
25 return nil
26 }
28 func (m Memstore) UpdateCollection(id uuid.ID, change CollectionChange) error {
29 c, ok := m.collections[id.String()]
30 if !ok {
31 return ErrCollectionNotFound
32 }
33 c.ApplyChange(change)
34 m.collections[id.String()] = c
35 return nil
36 }
38 func (m Memstore) GetCollectionByDomain(domain string) (Collection, error) {
39 d, ok := m.domains[domain]
40 if !ok {
41 return Collection{}, ErrCollectionNotFound
42 }
43 return m.GetCollectionByID(d.CollectionID)
44 }
46 func (m Memstore) GetCollectionByID(id uuid.ID) (Collection, error) {
47 if c, ok := m.collections[id.String()]; ok {
48 return c, nil
49 }
50 return Collection{}, ErrCollectionNotFound
51 }
53 func (m Memstore) GetCollectionsByUser(id uuid.ID) ([]Collection, error) {
54 collections := []Collection{}
55 for _, c := range m.collections {
56 if c.Owner.Equal(id) {
57 collections = append(collections, c)
58 }
59 }
60 return collections, nil
61 }
63 func (m Memstore) AddDomainToCollection(id uuid.ID, domain string) error {
64 if _, ok := m.domains[domain]; ok {
65 return ErrDomainAlreadyExists
66 }
67 m.domains[domain] = Domain{
68 Domain: domain,
69 CollectionID: id,
70 Created: time.Now(),
71 }
72 return nil
73 }
75 func (m Memstore) RemoveDomainFromCollection(id uuid.ID, domain string) error {
76 if _, ok := m.domains[domain]; !ok {
77 return ErrDomainNotFound
78 }
79 delete(m.domains, domain)
80 return nil
81 }
83 func (m Memstore) GetDomainsByCollection(id uuid.ID) ([]Domain, error) {
84 domains := []Domain{}
85 for _, d := range m.domains {
86 if d.CollectionID.Equal(id) {
87 domains = append(domains, d)
88 }
89 }
90 return domains, nil
91 }
93 func (m Memstore) DeleteCollection(c Collection) error {
94 if _, ok := m.collections[c.ID.String()]; !ok {
95 return ErrCollectionNotFound
96 }
97 delete(m.collections, c.ID.String())
98 return nil
99 }
101 func (m Memstore) GetItemsByCollectionDomain(domain string, num, offset int) ([]Item, error) {
102 collection, err := m.GetCollectionByDomain(domain)
103 if err != nil {
104 return []Item{}, err
105 }
106 return m.GetItemsByCollectionID(collection.ID, num, offset)
107 }
109 func (m Memstore) GetItemsByCollectionID(id uuid.ID, num, offset int) ([]Item, error) {
110 if _, ok := m.collections[id.String()]; !ok {
111 return []Item{}, ErrCollectionNotFound
112 }
113 items := []Item{}
114 for _, item := range m.items {
115 if item.CollectionID.Equal(id) {
116 items = append(items, item)
117 }
118 }
119 if len(items) < offset {
120 return []Item{}, nil
121 }
122 end := offset + num
123 if len(items) < end {
124 end = len(items)
125 }
126 return items[offset:end], nil
127 }
129 func (m Memstore) AddItemToCollection(id uuid.ID, item Item) error {
130 if _, ok := m.collections[id.String()]; !ok {
131 return ErrCollectionNotFound
132 }
133 if _, ok := m.items[id.String()+"/"+item.Name]; ok {
134 return ErrItemAlreadyExists
135 }
136 item.CollectionID = id
137 m.items[id.String()+"/"+item.Name] = item
138 return nil
139 }
141 func (m Memstore) GetItemByName(collectionID uuid.ID, name string) (Item, error) {
142 if _, ok := m.collections[collectionID.String()]; !ok {
143 return Item{}, ErrCollectionNotFound
144 }
145 if item, ok := m.items[collectionID.String()+"/"+name]; ok {
146 return item, nil
147 }
148 return Item{}, ErrItemNotFound
149 }
151 func (m Memstore) DeleteItem(item Item) error {
152 if _, ok := m.items[item.CollectionID.String()+"/"+item.Name]; !ok {
153 return ErrItemNotFound
154 }
155 delete(m.items, item.CollectionID.String()+"/"+item.Name)
156 return nil
157 }