-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
61 lines (52 loc) · 1.03 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
package main
// Schema represents one of the tables in the database, and plays a major
// role in abstracting away database operations.
import (
"github.com/akrylysov/pogreb"
)
type Schema struct {
db *Database
name string
store *pogreb.DB
}
func (s *Schema) Close() error {
if s.store == nil {
panic("double close")
}
if err := s.store.Close(); err != nil {
return err
}
s.store = nil
return nil
}
func (s Schema) Name() string {
return s.name
}
func (s Schema) Count() uint32 {
return s.store.Count()
}
func (s *Schema) Put(key []byte, value []byte) error {
return s.store.Put(key, value)
}
func (s *Schema) LoadData(loader *DataLoader) (int, error) {
defer func() { failOnError(s.Close()) }()
it := s.store.Items()
loaded := 0
for {
key, val, err := it.Next()
if err == nil {
err = loader.Load(val)
}
if err != nil {
if err == pogreb.ErrIterationDone {
break
}
if FilterError(err) != nil {
failOnError(s.store.Delete(key))
return loaded, err
}
}
loaded++
}
return loaded, nil
}