gifs/api

Paddy 2014-10-17 Parent:d3ba1115bfd0

4:1bbbe113f599 Go to Latest

gifs/api/datastore.go

Upload is no longer async, memstorage is parallel-safe. Upload no longer needs to be run async (it can be run inside a goroutine), so it now returns stuff instead of taking a channel as an argument. This will make it easier to implement, as all the async stuff is an abstraction above, and therefore doesn't need to be worried about for each reimplementation. The memstorage type is no longer exported and can now be safely used by multiple goroutines, thanks to the sync package.

History
1 package api
3 import (
4 "errors"
5 "time"
6 "unicode"
8 "code.google.com/p/go.text/unicode/norm"
9 "code.secondbit.org/uuid.hg"
10 )
12 var (
13 ErrCollectionNotFound = errors.New("collection not found")
14 ErrDomainNotFound = errors.New("domain not found")
15 ErrDomainAlreadyExists = errors.New("domain already attached to a collection")
16 ErrItemNotFound = errors.New("item not found")
17 ErrItemAlreadyExists = errors.New("item already exists")
18 )
20 type Datastore interface {
21 CreateCollection(c Collection) error
22 UpdateCollection(id uuid.ID, change CollectionChange) error
23 GetCollectionByDomain(domain string) (Collection, error)
24 GetCollectionByID(id uuid.ID) (Collection, error)
25 GetCollectionsByUser(id uuid.ID) ([]Collection, error)
26 AddDomainToCollection(id uuid.ID, domain string) error
27 RemoveDomainFromCollection(id uuid.ID, domain string) error
28 GetDomainsByCollection(id uuid.ID) ([]Domain, error)
29 DeleteCollection(c Collection) error
31 GetItemsByCollectionDomain(domain string, num, offset int) ([]Item, error)
32 GetItemsByCollectionID(id uuid.ID, num, offset int) ([]Item, error)
33 AddItemToCollection(id uuid.ID, item Item) error
34 GetItemByName(collectionID uuid.ID, name string) (Item, error)
35 DeleteItem(item Item) error
36 }
38 func slugify(in string) string {
39 buf := make([]rune, 0, len(in))
40 needsDash := false
41 for _, r := range norm.NFKD.String(in) {
42 if unicode.IsSpace(r) || unicode.IsPunct(r) {
43 if needsDash {
44 buf = append(buf, '-')
45 needsDash = false
46 }
47 } else {
48 buf = append(buf, r)
49 needsDash = true
50 }
51 }
52 return string(buf)
53 }
55 type Collection struct {
56 ID uuid.ID
57 Name string
58 Owner uuid.ID
59 Created time.Time
60 }
62 func (c *Collection) ApplyChange(change CollectionChange) {
63 if change.Name != nil {
64 c.Name = *change.Name
65 }
66 }
68 type CollectionChange struct {
69 Name *string
70 }
72 type Domain struct {
73 Domain string
74 CollectionID uuid.ID
75 Created time.Time
76 }
78 type Item struct {
79 Blob string
80 Bucket string
81 CollectionID uuid.ID
82 Name string
83 }