forked from dgraph-io/dgraph
/
schema.go
349 lines (310 loc) · 8.56 KB
/
schema.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package schema
import (
"bytes"
"fmt"
"sync"
"github.com/dgraph-io/badger"
"golang.org/x/net/trace"
"github.com/dgraph-io/dgraph/protos"
"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
const (
syncChCapacity = 10000
)
var (
pstate *stateGroup
pstore *badger.KV
syncCh chan SyncEntry
)
type stateGroup struct {
// Can have fine grained locking later if necessary, per group or predicate
sync.RWMutex // x.SafeMutex is slow.
// Map containing predicate to type information.
predicate map[string]*protos.SchemaUpdate
elog trace.EventLog
}
func (s *stateGroup) init() {
s.predicate = make(map[string]*protos.SchemaUpdate)
s.elog = trace.NewEventLog("Dgraph", "Schema")
}
type state struct {
sync.RWMutex
m map[uint32]*stateGroup
elog trace.EventLog
}
// SateFor returns the schema for given group
func State() *stateGroup {
return pstate
}
func (s *stateGroup) DeleteAll() {
s.Lock()
defer s.Unlock()
s.predicate = map[string]*protos.SchemaUpdate{
"_predicate_": &protos.SchemaUpdate{
ValueType: uint32(types.StringID),
List: true,
},
}
}
// Update updates the schema in memory and sends an entry to syncCh so that it can be
// committed later
func (s *stateGroup) Update(se SyncEntry) {
s.Lock()
defer s.Unlock()
s.predicate[se.Attr] = &se.Schema
se.Water.Begin(se.Index)
syncCh <- se
s.elog.Printf(logUpdate(se.Schema, se.Attr))
x.Printf(logUpdate(se.Schema, se.Attr))
}
// Delete updates the schema in memory and sends an entry to syncCh so that it can be
// committed later
func (s *stateGroup) Delete(se SyncEntry) {
s.Lock()
defer s.Unlock()
delete(s.predicate, se.Attr)
se.Water.Begin(se.Index)
syncCh <- se
s.elog.Printf("Deleting schema for attr: %s", se.Attr)
x.Printf("Deleting schema for attr: %s", se.Attr)
}
// Remove deletes the schema from memory and disk. Used after predicate move to do
// cleanup
func (s *stateGroup) Remove(predicate string) error {
s.Lock()
defer s.Unlock()
delete(s.predicate, predicate)
return pstore.Delete(x.SchemaKey(predicate))
}
func logUpdate(schema protos.SchemaUpdate, pred string) string {
typ := types.TypeID(schema.ValueType).Name()
if schema.List {
typ = fmt.Sprintf("[%s]", typ)
}
return fmt.Sprintf("Setting schema for attr %s: %v, tokenizer: %v, directive: %v, count: %v\n",
pred, typ, schema.Tokenizer, schema.Directive, schema.Count)
}
// Set sets the schema for given predicate in memory
// schema mutations must flow through update function, which are
// synced to db
func (s *stateGroup) Set(pred string, schema protos.SchemaUpdate) {
s.Lock()
defer s.Unlock()
s.predicate[pred] = &schema
s.elog.Printf(logUpdate(schema, pred))
}
// Get gets the schema for given predicate
func (s *stateGroup) Get(pred string) (protos.SchemaUpdate, bool) {
s.Lock()
defer s.Unlock()
schema, has := s.predicate[pred]
if !has {
return protos.SchemaUpdate{}, false
}
return *schema, true
}
// TypeOf returns the schema type of predicate
func (s *stateGroup) TypeOf(pred string) (types.TypeID, error) {
s.RLock()
defer s.RUnlock()
if schema, ok := s.predicate[pred]; ok {
return types.TypeID(schema.ValueType), nil
}
return types.TypeID(100), x.Errorf("Schema not defined for predicate: %v.", pred)
}
// IsIndexed returns whether the predicate is indexed or not
func (s *stateGroup) IsIndexed(pred string) bool {
s.RLock()
defer s.RUnlock()
if schema, ok := s.predicate[pred]; ok {
return len(schema.Tokenizer) > 0
}
return false
}
// IndexedFields returns the list of indexed fields
func (s *stateGroup) IndexedFields() []string {
s.RLock()
defer s.RUnlock()
var out []string
for k, v := range s.predicate {
if len(v.Tokenizer) > 0 {
out = append(out, k)
}
}
return out
}
// Predicates returns the list of predicates for given group
func (s *stateGroup) Predicates() []string {
s.RLock()
defer s.RUnlock()
out := make([]string, 0, len(s.predicate))
for k := range s.predicate {
out = append(out, k)
}
return out
}
// Tokenizer returns the tokenizer for given predicate
func (s *stateGroup) Tokenizer(pred string) []tok.Tokenizer {
s.RLock()
defer s.RUnlock()
schema, ok := s.predicate[pred]
x.AssertTruef(ok, "schema state not found for %s", pred)
var tokenizers []tok.Tokenizer
for _, it := range schema.Tokenizer {
t, has := tok.GetTokenizer(it)
x.AssertTruef(has, "Invalid tokenizer %s", it)
tokenizers = append(tokenizers, t)
}
return tokenizers
}
// TokenizerNames returns the tokenizer names for given predicate
func (s *stateGroup) TokenizerNames(pred string) []string {
s.RLock()
defer s.RUnlock()
schema, ok := s.predicate[pred]
x.AssertTruef(ok, "schema state not found for %s", pred)
var tokenizers []string
for _, it := range schema.Tokenizer {
t, found := tok.GetTokenizer(it)
x.AssertTruef(found, "Tokenizer not found for %s", it)
tokenizers = append(tokenizers, t.Name())
}
return tokenizers
}
// IsReversed returns whether the predicate has reverse edge or not
func (s *stateGroup) IsReversed(pred string) bool {
s.RLock()
defer s.RUnlock()
if schema, ok := s.predicate[pred]; ok {
return schema.Directive == protos.SchemaUpdate_REVERSE
}
return false
}
// HasCount returns whether we want to mantain a count index for the given predicate or not.
func (s *stateGroup) HasCount(pred string) bool {
s.RLock()
defer s.RUnlock()
if schema, ok := s.predicate[pred]; ok {
return schema.Count
}
return false
}
// IsList returns whether the predicate is of list type.
func (s *stateGroup) IsList(pred string) bool {
s.RLock()
defer s.RUnlock()
if schema, ok := s.predicate[pred]; ok {
return schema.List
}
return false
}
func Init(ps *badger.KV) {
pstore = ps
syncCh = make(chan SyncEntry, syncChCapacity)
reset()
go batchSync()
}
// LoadFromDb reads schema information from db and stores it in memory
func LoadFromDb() error {
prefix := x.SchemaPrefix()
itr := pstore.NewIterator(badger.DefaultIteratorOptions) // Need values, reversed=false.
defer itr.Close()
for itr.Seek(prefix); itr.Valid(); itr.Next() {
item := itr.Item()
key := item.Key()
if !bytes.HasPrefix(key, prefix) {
break
}
attr := x.Parse(key).Attr
var s protos.SchemaUpdate
err := item.Value(func(val []byte) error {
x.Checkf(s.Unmarshal(val), "Error while loading schema from db")
return nil
})
if err != nil {
return err
}
State().Set(attr, s)
}
State().Set("_predicate_", protos.SchemaUpdate{
ValueType: uint32(types.StringID),
List: true,
})
return nil
}
func reset() {
pstate = new(stateGroup)
pstate.init()
}
// SyncEntry stores the schema mutation information
type SyncEntry struct {
Attr string
Schema protos.SchemaUpdate
Water *x.WaterMark
Index uint64
}
func addToEntriesMap(entriesMap map[*x.WaterMark][]uint64, entries []SyncEntry) {
for _, entry := range entries {
if entry.Water != nil {
entriesMap[entry.Water] = append(entriesMap[entry.Water], entry.Index)
}
}
}
func batchSync() {
var entries []SyncEntry
var loop uint64
wb := make([]*badger.Entry, 0, 100)
for {
ent := <-syncCh
slurpLoop:
for {
entries = append(entries, ent)
if len(entries) == syncChCapacity {
// Avoid making infinite batch, push back against syncCh.
break
}
select {
case ent = <-syncCh:
default:
break slurpLoop
}
}
loop++
State().elog.Printf("[%4d] Writing schema batch of size: %v\n", loop, len(entries))
for _, e := range entries {
if e.Schema.Directive == protos.SchemaUpdate_DELETE {
wb = badger.EntriesDelete(wb, x.SchemaKey(e.Attr))
continue
}
val, err := e.Schema.Marshal()
x.Checkf(err, "Error while marshalling schema description")
wb = badger.EntriesSet(wb, x.SchemaKey(e.Attr), val)
}
pstore.BatchSet(wb)
wb = wb[:0]
entriesMap := make(map[*x.WaterMark][]uint64)
addToEntriesMap(entriesMap, entries)
for wm, indices := range entriesMap {
wm.DoneMany(indices)
}
entries = entries[:0]
}
}