-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
128 lines (111 loc) · 2.9 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
package gitdb
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type gdbIndex map[string]interface{}
type gdbIndexCache map[string]gdbIndex
func (g *gitdb) updateIndexes(dataset string, records ...*record) {
g.indexUpdated = true
indexPath := g.indexPath(dataset)
for _, record := range records {
for name, value := range record.indexes(g.config.EncryptionKey) {
indexFile := filepath.Join(indexPath, name+".json")
if _, ok := g.indexCache[indexFile]; !ok {
g.indexCache[indexFile] = g.readIndex(indexFile)
}
g.indexCache[indexFile][record.id] = value
}
}
}
//for read-only backward compatibility with earlier versions of GitDB
func (g *gitdb) updateIndexesV1(dataset string, records ...*record) {
g.indexUpdated = true
indexPath := g.indexPath(dataset)
model := g.config.Factory(dataset)
for _, record := range records {
record.gHydrate(model, g.config.EncryptionKey)
for name, value := range model.GetSchema().indexes() {
indexFile := filepath.Join(indexPath, name+".json")
if _, ok := g.indexCache[indexFile]; !ok {
g.indexCache[indexFile] = g.readIndex(indexFile)
}
g.indexCache[indexFile][record.id] = value
}
}
}
func (g *gitdb) flushIndex() error {
if g.indexUpdated {
logTest("flushing index")
for indexFile, data := range g.indexCache {
indexPath := filepath.Dir(indexFile)
if _, err := os.Stat(indexPath); err != nil {
err = os.MkdirAll(indexPath, 0755)
if err != nil {
logError("Failed to write to index: " + indexFile)
return err
}
}
indexBytes, err := json.MarshalIndent(data, "", "\t")
if err != nil {
logError("Failed to write to index [" + indexFile + "]: " + err.Error())
return err
}
err = ioutil.WriteFile(indexFile, indexBytes, 0744)
if err != nil {
logError("Failed to write to index: " + indexFile)
return err
}
}
g.indexUpdated = false
}
return nil
}
func (g *gitdb) readIndex(indexFile string) gdbIndex {
rMap := make(gdbIndex)
if _, err := os.Stat(indexFile); err == nil {
data, err := ioutil.ReadFile(indexFile)
if err == nil {
err = json.Unmarshal(data, &rMap)
}
if err != nil {
logError(err.Error())
}
}
return rMap
}
func (g *gitdb) buildIndex() {
datasets := getDatasets(g.dbDir())
for _, dataset := range datasets {
log("Building index for Dataset: " + dataset)
records, err := g.Fetch(dataset)
if err != nil {
continue
}
if len(records) > 0 {
if records[0].version() == "v1" {
g.updateIndexesV1(dataset, records...)
continue
}
g.updateIndexes(dataset, records...)
}
}
log("Building index complete")
}
func getDatasets(dbDir string) []string {
var datasets []string
dirs, err := ioutil.ReadDir(dbDir)
if err != nil {
log(err.Error())
return datasets
}
for _, dir := range dirs {
if !strings.HasPrefix(dir.Name(), ".") && dir.IsDir() {
datasets = append(datasets, dir.Name())
}
}
return datasets
}