-
Notifications
You must be signed in to change notification settings - Fork 5
/
read.go
222 lines (183 loc) · 5.41 KB
/
read.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
package gitdb
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/bouggo/log"
"github.com/gogitdb/gitdb/v2/internal/db"
)
func (g *gitdb) loadBlock(blockFile string) (*db.Block, error) {
if g.loadedBlocks == nil {
g.loadedBlocks = map[string]*db.Block{}
}
//if block file is not cached, load into cache
if _, ok := g.loadedBlocks[blockFile]; !ok {
g.loadedBlocks[blockFile] = db.LoadBlock(blockFile, g.config.EncryptionKey)
}
return g.loadedBlocks[blockFile], nil
}
func (g *gitdb) doGet(id string) (*db.Record, error) {
dataset, block, _, err := ParseID(id)
if err != nil {
return nil, err
}
if !g.isRegistered(dataset) {
return nil, ErrInvalidDataset
}
blockFilePath := filepath.Join(g.dbDir(), dataset, block+".json")
if _, err := os.Stat(blockFilePath); err != nil {
return nil, ErrNoRecords
}
//we used to to a doGetByIndex here but it doesn't work properly
//TODO revisit doGetByIndex
dataBlock := db.NewEmptyBlock(g.config.EncryptionKey)
if err := dataBlock.Hydrate(blockFilePath); err != nil {
return nil, err
}
return dataBlock.Get(id)
}
//func (g *gitdb) doGetByIndex(id, dataset string) (*db.Record, error) {
// //read id index
// indexFile := filepath.Join(g.indexDir(), dataset, "id.json")
// if _, ok := g.indexCache[indexFile]; !ok {
// g.buildIndexTargeted(dataset)
// }
//
// iv, ok := g.indexCache[indexFile][id]
// if ok {
// dataBlock := db.NewEmptyBlock(g.config.EncryptionKey)
// err = dataBlock.HydrateByPositions(blockFilePath, []int{iv.Offset, iv.Len})
// if err != nil {
// log.Error(err.Error())
// return nil, fmt.Errorf("Record %s not found in %s", id, dataset)
// }
//
// record, err := dataBlock.Get(id)
// if err != nil {
// log.Error(err.Error())
// return nil, fmt.Errorf("Record %s not found in %s", id, dataset)
// }
//
// return record, nil
// }
//
// return nil, fmt.Errorf("Record %s not found in %s", id, dataset)
//}
//Get hydrates a model with specified id into result Model
func (g *gitdb) Get(id string, result Model) error {
record, err := g.doGet(id)
if err != nil {
return err
}
g.events <- newReadEvent("...", id)
return record.Hydrate(result)
}
func (g *gitdb) Exists(id string) error {
_, err := g.doGet(id)
if err == nil {
g.events <- newReadEvent("...", id)
}
return err
}
func (g *gitdb) Fetch(dataset string, blocks ...string) ([]*db.Record, error) {
if !g.isRegistered(dataset) {
return nil, ErrInvalidDataset
}
dataBlock := db.NewEmptyBlock(g.config.EncryptionKey)
if len(blocks) > 0 {
fullPath := filepath.Join(g.dbDir(), dataset)
for _, block := range blocks {
blockFile := filepath.Join(fullPath, block+".json")
log.Test("Fetching BLOCK records from - " + blockFile)
if err := dataBlock.Hydrate(blockFile); err != nil {
return nil, err
}
}
return dataBlock.Records(), nil
}
if err := g.doFetch(dataset, dataBlock); err != nil {
return nil, err
}
log.Info(fmt.Sprintf("%d records found in %s", dataBlock.Len(), dataset))
return dataBlock.Records(), nil
}
func (g *gitdb) doFetch(dataset string, dataBlock *db.EmptyBlock) error {
fullPath := filepath.Join(g.dbDir(), dataset)
//events <- newReadEvent("...", fullPath)
log.Info("Fetching records from - " + fullPath)
files, err := ioutil.ReadDir(fullPath)
if err != nil && !os.IsNotExist(err) {
return err
}
if os.IsNotExist(err) {
return ErrNoRecords
}
var fileName string
for _, file := range files {
fileName = filepath.Join(fullPath, file.Name())
if filepath.Ext(fileName) == ".json" {
if err := dataBlock.Hydrate(fileName); err != nil {
return err
}
}
}
return nil
}
func (g *gitdb) Search(dataset string, searchParams []*SearchParam, searchMode SearchMode) ([]*db.Record, error) {
if !g.isRegistered(dataset) {
return nil, ErrInvalidDataset
}
//searchBlocks return the position of the record in the block
//searchBlocks := map[string][][]int{} //index based
searchBlocks := map[string]bool{}
matchingRecords := map[string]string{}
for _, searchParam := range searchParams {
indexFile := filepath.Join(g.indexDir(), dataset, searchParam.Index+".json")
if _, ok := g.indexCache[indexFile]; !ok {
g.buildIndexTargeted(dataset)
}
g.events <- newReadEvent("...", indexFile)
queryValue := strings.ToLower(searchParam.Value)
for recordID, iv := range g.indexCache[indexFile] {
addResult := false
dbValue := strings.ToLower(iv.(string))
switch searchMode {
case SearchEquals:
addResult = dbValue == queryValue
case SearchContains:
addResult = strings.Contains(dbValue, queryValue)
case SearchStartsWith:
addResult = strings.HasPrefix(dbValue, queryValue)
case SearchEndsWith:
addResult = strings.HasSuffix(dbValue, queryValue)
}
if addResult {
dataset, block, _, err := ParseID(recordID)
if err != nil {
return nil, err
}
matchingRecords[recordID] = recordID
searchBlocks[g.blockFilePath(dataset, block)] = true
}
}
}
resultBlock := db.NewEmptyBlock(g.config.EncryptionKey)
//TODO revisit index based search
//for block, pos := range searchBlocks {
// blockFile := filepath.Join(g.dbDir(), dataset, block+".json")
// err := resultBlock.HydrateByPositions(blockFile, pos...)
// if err != nil {
// return nil, err
// }
//}
for block := range searchBlocks {
if err := resultBlock.Hydrate(block); err != nil {
log.Error(err.Error())
continue
}
}
resultBlock.Filter(matchingRecords)
return resultBlock.Records(), nil
}