forked from ghetzel/pivot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_backend.go
417 lines (343 loc) · 9.8 KB
/
file_backend.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package backends
import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"unicode"
"github.com/ghetzel/go-stockutil/fileutil"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/typeutil"
utilutil "github.com/ghetzel/go-stockutil/utils"
"github.com/ghetzel/pivot/v3/dal"
"github.com/ghetzel/pivot/v3/filter"
)
var fileBackendTypeAutodetectRows = 50
var fileBackendAutoindexField = `_index`
type FileBackend struct {
Backend
Indexer
conn dal.ConnectionString
recordsets sync.Map
collections sync.Map
}
func NewFileBackend(connection dal.ConnectionString) Backend {
return &FileBackend{
conn: connection,
}
}
func (self *FileBackend) filename() string {
filename := filepath.Join(self.conn.Host(), self.conn.Dataset())
filename = fileutil.MustExpandUser(filename)
return filename
}
func (self *FileBackend) normalize(filename string) string {
collection := strings.TrimSpace(filename)
collection = strings.TrimSuffix(filepath.Base(collection), filepath.Ext(collection))
return collection
}
func (self *FileBackend) normcol(name string) string {
name = strings.TrimSpace(name)
name = strings.ToLower(name)
name = strings.TrimFunc(name, func(r rune) bool {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
return false
} else {
return true
}
})
return name
}
func (self *FileBackend) rs(collection string) *dal.RecordSet {
if v, ok := self.recordsets.Load(collection); ok {
if rs, ok := v.(*dal.RecordSet); ok {
return rs
}
}
return nil
}
func (self *FileBackend) autoregisterCollections() error {
path := self.filename()
if fileutil.IsNonemptyFile(path) {
return self.updateCollectionFromFile(path)
} else if fileutil.DirExists(path) {
return fmt.Errorf("Not implemented")
} else {
return fmt.Errorf("Must specify a filename or directory")
}
}
func (self *FileBackend) updateCollectionFromFile(filename string) error {
protocol := self.conn.Protocol()
log.Debugf("%T: autoregister %s (protocol: %s)", self, filename, protocol)
switch protocol {
case `csv`, `tsv`:
if file, err := os.Open(filename); err == nil {
defer file.Close()
csvr := csv.NewReader(file)
csvr.Comment = '#'
switch protocol {
case `csv`:
csvr.Comma = ','
case `tsv`:
csvr.Comma = '\t'
}
if rc, err := csvr.ReadAll(); err == nil {
detectedTypes := make([]utilutil.ConvertType, 0)
for r, row := range rc {
if r == 0 {
continue
}
for c, col := range row {
col = strings.TrimSpace(col)
dtype := utilutil.DetectConvertType(col)
if c < len(detectedTypes) {
// bump the detected type up to less-specific types until we eventually
// settle on something or everything is just a string
if dtype.IsSupersetOf(detectedTypes[c]) {
detectedTypes[c] = dtype
}
} else if dtype.String() == `` {
detectedTypes = append(detectedTypes, utilutil.String)
} else {
detectedTypes = append(detectedTypes, dtype)
}
}
if r > fileBackendTypeAutodetectRows {
break
}
}
collection := dal.NewCollection(self.normalize(filename))
collection.SourceURI = filename
for _, row := range rc {
if len(row) > 0 {
collection.IdentityField = ``
idCol := -1
for c, col := range row {
if self.normcol(col) == `id` {
collection.IdentityField = col
idCol = c
break
}
}
if collection.IdentityField == `` {
collection.IdentityField = fileBackendAutoindexField
}
for c, col := range row {
if c == idCol {
collection.IdentityFieldType = dal.Type(detectedTypes[c].String())
collection.IdentityFieldIndex = c
continue
} else {
collection.AddFields(dal.Field{
Name: strings.TrimSpace(col),
Type: dal.Type(detectedTypes[c].String()),
Index: c,
})
}
}
}
break
}
self.collections.Store(collection.Name, collection)
return nil
} else {
return fmt.Errorf("read error: %v", err)
}
} else {
return err
}
default:
return fmt.Errorf("Unsupported file layout %q", protocol)
}
}
func (self *FileBackend) refresh(filename string) error {
recordset := dal.NewRecordSet()
if collection, err := self.GetCollection(filename); err == nil {
switch protocol := self.conn.Protocol(); protocol {
case `csv`, `tsv`:
if file, err := os.Open(filename); err == nil {
defer file.Close()
csvr := csv.NewReader(file)
csvr.Comment = '#'
switch protocol {
case `csv`:
csvr.Comma = ','
case `tsv`:
csvr.Comma = '\t'
}
if rc, err := csvr.ReadAll(); err == nil {
idColName := collection.GetIdentityFieldName()
hasExplicitIdColumn := (idColName != fileBackendAutoindexField)
NextRow:
for r, row := range rc {
record := dal.NewRecord(r)
NextColumn:
for c, col := range row {
col = strings.TrimSpace(col)
if col == `` {
continue NextColumn
}
if r == 0 {
continue NextRow
} else if hasExplicitIdColumn && c == collection.IdentityFieldIndex {
if v, err := collection.ValueForField(idColName, col, dal.RetrieveOperation); err == nil && v != `` {
record.ID = v
} else if v == `` {
log.Warningf("%T: failed to parse identity at R%dC%d: empty ID", self, r, c)
continue NextRow
} else {
log.Warningf("%T: failed to parse identity at R%dC%d: %v", self, r, c, err)
continue NextRow
}
} else if field, ok := collection.GetFieldByIndex(c); ok {
if v, err := field.ConvertValue(col); err == nil {
record.SetNested(field.Name, v)
} else {
log.Warningf("%T: failed to parse column R%dC%d: %v", self, r, c, err)
continue NextRow
}
}
}
if record.ID != nil {
recordset.Push(record)
}
}
self.recordsets.Store(self.normalize(collection.Name), recordset)
return nil
} else {
return fmt.Errorf("read error: %v", err)
}
} else {
return err
}
default:
return fmt.Errorf("Unsupported file layout %q", protocol)
}
} else {
return err
}
}
func (self *FileBackend) Initialize() error {
if scheme, protocol := self.conn.Scheme(); protocol == `` {
switch filepath.Ext(self.filename()) {
case `.csv`:
protocol = `csv`
case `.tsv`:
protocol = `tsv`
case `.xlsx`:
protocol = `xlsx`
default:
return fmt.Errorf("Could not determine protocol for file %q", self.filename())
}
if cs, err := dal.MakeConnectionString(
scheme+`+`+protocol,
self.conn.Host(),
self.conn.Dataset(),
self.conn.Options,
); err == nil {
self.conn = cs
} else {
return fmt.Errorf("protocol: %v", err)
}
}
if err := self.autoregisterCollections(); err != nil {
return fmt.Errorf("autoregister failed: %v", err)
}
return self.refresh(self.filename())
}
func (self *FileBackend) SetIndexer(dal.ConnectionString) error {
return nil
}
func (self *FileBackend) RegisterCollection(collection *dal.Collection) {
self.collections.Store(collection.Name, collection)
}
func (self *FileBackend) GetConnectionString() *dal.ConnectionString {
return &self.conn
}
func (self *FileBackend) Exists(name string, id interface{}) bool {
if collection, err := self.GetCollection(name); err == nil {
self.refresh(collection.SourceURI)
if rs := self.rs(collection.Name); rs != nil {
_, ok := rs.GetRecordByID(id)
return ok
}
}
return false
}
func (self *FileBackend) Retrieve(name string, id interface{}, fields ...string) (*dal.Record, error) {
if collection, err := self.GetCollection(name); err == nil {
if err := self.refresh(collection.SourceURI); err != nil {
return nil, err
}
if rs := self.rs(collection.Name); rs != nil {
if record, ok := rs.GetRecordByID(id); ok {
return record.OnlyFields(fields), nil
} else {
return nil, fmt.Errorf("Record %v does not exist", id)
}
}
}
return nil, dal.CollectionNotFound
}
func (self *FileBackend) Insert(collection string, records *dal.RecordSet) error {
return fmt.Errorf("File backends are read-only")
}
func (self *FileBackend) Update(collection string, records *dal.RecordSet, target ...string) error {
return fmt.Errorf("File backends are read-only")
}
func (self *FileBackend) Delete(collection string, ids ...interface{}) error {
return fmt.Errorf("File backends are read-only")
}
func (self *FileBackend) CreateCollection(definition *dal.Collection) error {
return fmt.Errorf("File backends are read-only")
}
func (self *FileBackend) DeleteCollection(collection string) error {
return fmt.Errorf("File backends are read-only")
}
func (self *FileBackend) ListCollections() ([]string, error) {
var names []string
self.collections.Range(func(key interface{}, value interface{}) bool {
names = append(names, typeutil.String(key))
return true
})
return names, nil
}
func (self *FileBackend) GetCollection(collection string) (*dal.Collection, error) {
if c, ok := self.collections.Load(self.normalize(collection)); ok {
if collection, ok := c.(*dal.Collection); ok {
return collection, nil
}
}
return nil, dal.CollectionNotFound
}
func (self *FileBackend) WithSearch(collection *dal.Collection, filters ...*filter.Filter) Indexer {
return nil
}
func (self *FileBackend) WithAggregator(collection *dal.Collection) Aggregator {
return nil
}
func (self *FileBackend) Flush() error {
return nil
}
func (self *FileBackend) Ping(_ time.Duration) error {
if fn := self.filename(); fileutil.IsNonemptyFile(fn) {
return nil
} else {
return fmt.Errorf("no such file: %s", fn)
}
}
func (self *FileBackend) String() string {
return `file`
}
func (self *FileBackend) Supports(features ...BackendFeature) bool {
for _, feat := range features {
switch feat {
default:
return false
}
}
return true
}