-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
100 lines (88 loc) · 2.69 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package application
import (
"time"
"github.com/TheThingsNetwork/ttn/core/handler/application/migrate"
"github.com/TheThingsNetwork/ttn/core/storage"
"github.com/TheThingsNetwork/ttn/utils/errors"
"gopkg.in/redis.v5"
)
// Store interface for Applications
type Store interface {
Count() (int, error)
List(opts *storage.ListOptions) ([]*Application, error)
Get(appID string) (*Application, error)
Set(new *Application, properties ...string) (err error)
Delete(appID string) error
}
const defaultRedisPrefix = "handler"
const redisApplicationPrefix = "application"
// NewRedisApplicationStore creates a new Redis-based Application store
// if an empty prefix is passed, a default prefix will be used.
func NewRedisApplicationStore(client *redis.Client, prefix string) Store {
if prefix == "" {
prefix = defaultRedisPrefix
}
store := storage.NewRedisMapStore(client, prefix+":"+redisApplicationPrefix)
store.SetBase(Application{}, "")
for v, f := range migrate.ApplicationMigrations(prefix) {
store.AddMigration(v, f)
}
s := &RedisApplicationStore{
store: store,
}
countStore(s)
return s
}
// RedisApplicationStore stores Applications in Redis.
// - Applications are stored as a Hash
type RedisApplicationStore struct {
store *storage.RedisMapStore
}
// Count all applications in the store
func (s *RedisApplicationStore) Count() (int, error) {
return s.store.Count("", nil)
}
// List all Applications
func (s *RedisApplicationStore) List(opts *storage.ListOptions) ([]*Application, error) {
applicationsI, err := s.store.List("", opts)
if err != nil {
return nil, err
}
applications := make([]*Application, len(applicationsI))
for i, applicationI := range applicationsI {
if application, ok := applicationI.(Application); ok {
applications[i] = &application
}
}
return applications, nil
}
// Get a specific Application
func (s *RedisApplicationStore) Get(appID string) (*Application, error) {
applicationI, err := s.store.Get(appID)
if err != nil {
return nil, err
}
if application, ok := applicationI.(Application); ok {
return &application, nil
}
return nil, errors.New("Database did not return a Application")
}
// Set a new Application or update an existing one
func (s *RedisApplicationStore) Set(new *Application, properties ...string) (err error) {
now := time.Now()
new.UpdatedAt = now
if new.old == nil {
new.CreatedAt = now
}
err = s.store.Set(new.AppID, *new, properties...)
if err != nil {
return
}
return nil
}
// Delete an Application
func (s *RedisApplicationStore) Delete(appID string) error {
return s.store.Delete(appID)
}