-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
180 lines (139 loc) · 3.53 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package firestorm
import (
"reflect"
"sync"
"cloud.google.com/go/datastore"
)
var mapper = &IndexMapper{
Mutex: &sync.Mutex{},
Cache: make(map[reflect.Type]*IndexTree),
}
// Indexer represents an entity indexer
type Indexer interface {
Index(tx *datastore.Transaction) error
}
// IndexerFunc represents a checker func
type IndexerFunc func(tx *datastore.Transaction) error
// Index indexes the record
func (fn IndexerFunc) Index(tx *datastore.Transaction) error {
return fn(tx)
}
// NewInsertIndexer represents an insert indexer
func NewInsertIndexer(key *datastore.Key, input interface{}) Indexer {
var (
tree = mapper.Tree(reflect.TypeOf(input))
entity = reflect.ValueOf(input)
)
fn := func(tx *datastore.Transaction) error {
treeNext, err := tree.Keys(key, entity)
if err != nil || len(treeNext) == 0 {
return err
}
ops := []*datastore.Mutation{}
for _, next := range treeNext {
ops = append(ops, datastore.NewInsert(next.Key, next))
}
_, err = tx.Mutate(ops...)
return err
}
return IndexerFunc(fn)
}
// NewUpdateIndexer represents an update indexer
func NewUpdateIndexer(key *datastore.Key, input interface{}) Indexer {
var (
kind = reflect.TypeOf(input)
tree = mapper.Tree(kind)
empty = reflect.New(kind.Elem())
entity = reflect.ValueOf(input)
)
fn := func(tx *datastore.Transaction) error {
treeNext, err := tree.Keys(key, entity)
if err != nil || len(treeNext) == 0 {
return err
}
if err = tx.Get(key, empty.Interface()); err != nil {
return err
}
treePrev, err := tree.Keys(key, empty)
if err != nil {
return err
}
ops := []*datastore.Mutation{}
for index, next := range treeNext {
prev := treePrev[index]
if prev.Hash == next.Hash {
continue
}
ops = append(ops, datastore.NewDelete(prev.Key))
ops = append(ops, datastore.NewInsert(next.Key, next))
}
_, err = tx.Mutate(ops...)
return err
}
return IndexerFunc(fn)
}
// NewUpsertIndexer represents an update indexer
func NewUpsertIndexer(key *datastore.Key, input interface{}) Indexer {
var (
kind = reflect.TypeOf(input)
tree = mapper.Tree(kind)
empty = reflect.New(kind.Elem())
entity = reflect.ValueOf(input)
)
fn := func(tx *datastore.Transaction) error {
treeNext, err := tree.Keys(key, entity)
if err != nil || len(treeNext) == 0 {
return err
}
err = tx.Get(key, empty.Interface())
switch {
case err == datastore.ErrNoSuchEntity:
case err != nil:
return err
}
treePrev, err := tree.Keys(key, empty)
if err != nil {
return err
}
ops := []*datastore.Mutation{}
for index, next := range treeNext {
prev := treePrev[index]
if prev.Hash == next.Hash {
continue
}
ops = append(ops, datastore.NewDelete(prev.Key))
ops = append(ops, datastore.NewInsert(next.Key, next))
}
_, err = tx.Mutate(ops...)
return err
}
return IndexerFunc(fn)
}
// NewDeleteIndexer represents an upsert check
func NewDeleteIndexer(key *datastore.Key, input interface{}) Indexer {
var (
kind = reflect.TypeOf(input)
tree = mapper.Tree(kind)
entity = reflect.ValueOf(input)
)
fn := func(tx *datastore.Transaction) error {
err := tx.Get(key, input)
switch {
case err == datastore.ErrNoSuchEntity:
return nil
case err != nil:
return err
}
treePrev, err := tree.Keys(key, entity)
if err != nil || len(treePrev) == 0 {
return err
}
ops := []*datastore.Mutation{}
for _, prev := range treePrev {
ops = append(ops, datastore.NewDelete(prev.Key))
}
_, err = tx.Mutate(ops...)
return err
}
return IndexerFunc(fn)
}