This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
forked from alpacahq/marketstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.go
636 lines (590 loc) · 17.8 KB
/
catalog.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package catalog
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/alpacahq/marketstore/utils/io"
)
type DMap map[string]*Directory // General purpose map for storing directories
type LevelFunc func(*Directory, interface{}) // Function for use in recursing into directories
type Directory struct {
sync.RWMutex
itemName, pathToItemName, category string
/*
itemName: instance of the category, e.g. itemName: "AAPL", category: "Symbol"
pathToItemName: directory path to this item, e.g. pathToItemName: "/project/data", itemName: "AAPL"
*/
directMap, subDirs DMap
/*
directMap[Key]: Key is the directory path, including the rootPath and excluding filename
subDirs[Key]: Key is the name of the directory, aka "ItemName" which is an instance of the category
*/
catList map[string]int8
datafile map[string]*io.TimeBucketInfo
/*
datafile[Key]: Key is the fully specified path to the datafile, including rootPath and filename
*/
}
func NewDirectory(rootpath string) *Directory {
d := &Directory{
// Directmap will point to each directory node using a composite key
directMap: make(DMap),
}
d.load(rootpath)
return d
}
func (dRoot *Directory) AddTimeBucket(tbk *io.TimeBucketKey, f *io.TimeBucketInfo) (err error) {
/*
Adds a (possibly) new data item to a rootpath. Takes an existing catalog directory and
adds the new data item to that data directory.
*/
dRoot.Lock()
defer dRoot.Unlock()
exists := func(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
writeCatName := func(catName, dirName string) error {
catNameFile := filepath.Join(dirName, "category_name")
if !exists(catNameFile) {
fp, err := os.OpenFile(catNameFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0770)
defer fp.Close()
if err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
if _, err = fp.WriteString(catName); err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
} else {
buffer, err := ioutil.ReadFile(catNameFile)
if err != nil {
return err
}
catNameFromFile := string(buffer)
if catNameFromFile != catName {
return fmt.Errorf("Category name does not match on-disk name")
}
}
return nil
}
catkeySplit := tbk.GetCategories()
datakeySplit := tbk.GetItems()
dirname := dRoot.GetPath()
for i, dataDirName := range datakeySplit {
subdirname := filepath.Join(dirname, dataDirName)
if !exists(subdirname) {
if err = os.Mkdir(subdirname, 0770); err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
}
if err = writeCatName(catkeySplit[i], dirname); err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
dirname = subdirname
}
// Write the last implied catName "Year"
if err = writeCatName("Year", dirname); err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
// Create a new data file using the TimeBucketInfo
if err = newTimeBucketInfoFromTemplate(f); err != nil {
return err
}
/*
Check to see if this is an empty top level directory, if so - we need to set
the top level category in the catalog entry
*/
if len(dRoot.category) == 0 {
dRoot.category = catkeySplit[0]
}
/*
Add this child directory tree to the parent top node's tree
*/
childNodeName := datakeySplit[0]
childNodePath := filepath.Join(dRoot.GetPath(), childNodeName)
childDirectory := NewDirectory(childNodePath)
dRoot.addSubdir(childDirectory, childNodeName)
return nil
}
func (dRoot *Directory) RemoveTimeBucket(tbk *io.TimeBucketKey) (err error) {
/*
Deletes the item at the last level specified in the dataItemKey
Also removes empty directories at the higher levels after the delete
*/
if dRoot == nil {
return fmt.Errorf(io.GetCallerFileContext(0) + ": Directory called from is nil")
}
datakeySplit := tbk.GetItems()
tree := make([]*Directory, len(datakeySplit))
current := dRoot
for i := 0; i < len(datakeySplit); i++ {
itemName := datakeySplit[i]
// Descend from the current directory to find the first directory with the item name
if tree[i] = current.GetSubDirWithItemName(itemName); tree[i] == nil {
return fmt.Errorf("Unable to find level item: " + itemName + " in directory")
}
current = tree[i]
}
deleteMap := make([]bool, len(datakeySplit))
end := len(datakeySplit) - 1
for i := end; i >= 0; i-- {
if i == end {
removeDirFiles(tree[i])
deleteMap[i] = true // This dir was deleted, we'll remove it from the parent's subdir list later
} else {
if deleteMap[i+1] {
tree[i].removeSubDir(tree[i+1].itemName, dRoot.directMap)
}
}
if !tree[i].DirHasSubDirs() {
removeDirFiles(tree[i])
deleteMap[i] = true // This dir was deleted, we'll remove it from the parent's subdir list later
}
}
if deleteMap[0] {
removeDirFiles(tree[0])
if dRoot != nil {
dRoot.removeSubDir(tree[0].itemName, dRoot.directMap)
}
}
return nil
}
func (d *Directory) GetTimeBucketInfoSlice() (tbinfolist []*io.TimeBucketInfo) {
// Returns a list of fileinfo for all datafiles in this directory or nil if there are none
d.RLock()
defer d.RUnlock()
if d.datafile == nil {
return nil
}
tbinfolist = make([]*io.TimeBucketInfo, 0)
for _, finfo_p := range d.datafile {
tbinfolist = append(tbinfolist, finfo_p)
}
return tbinfolist
}
func (d *Directory) GatherTimeBucketInfo() []*io.TimeBucketInfo {
// Locates a path in the directory and returns the TimeBucketInfo for that path or error if it isn't there
// Must be thread-safe for READ access
fileInfoFunc := func(d *Directory, i_list interface{}) {
p_list := i_list.(*([]*io.TimeBucketInfo))
if d.datafile != nil {
for _, dfile := range d.datafile {
*p_list = append(*p_list, dfile)
}
}
}
fileInfoList := make([]*io.TimeBucketInfo, 0)
d.recurse(&fileInfoList, fileInfoFunc)
return fileInfoList
}
func (d *Directory) GetLatestTimeBucketInfoFromKey(key *io.TimeBucketKey) (fi *io.TimeBucketInfo, err error) {
path := key.GetPathToYearFiles(d.pathToItemName)
fullFilePath := path + "/1970.bin" // Put a dummy file at the end of the path
subDir, err := d.GetOwningSubDirectory(fullFilePath)
if err != nil {
return nil, err
}
return subDir.getLatestYearFile()
}
func (d *Directory) PathToTimeBucketInfo(path string) (*io.TimeBucketInfo, error) {
/*
Finds the TimeBucketInfo file in this directory based on a full file path argument
*/
// Must be thread-safe for READ access
var tbinfo *io.TimeBucketInfo
findTimeBucketInfo := func(d *Directory, _ interface{}) {
if tbinfo != nil {
// We have already found our fileinfo match
return
}
if d.datafile != nil {
for _, dfile := range d.datafile {
if dfile.Path == path {
tbinfo = dfile.GetDeepCopy()
return
}
}
}
}
d.recurse(tbinfo, findTimeBucketInfo)
if tbinfo == nil {
return nil, NotFoundError("")
} else {
return tbinfo, nil
}
}
func (d *Directory) GetDataShapes(key *io.TimeBucketKey) (dsv []io.DataShape, err error) {
fi, err := d.GetLatestTimeBucketInfoFromKey(key)
if err != nil {
return nil, err
}
return fi.GetDataShapes(), nil
}
func (subDir *Directory) AddFile(newYear int16) (finfo_p *io.TimeBucketInfo, err error) {
// Must be thread-safe for WRITE access
/*
Adds a new primary storage file for the provided year to this directory
Returns an error if this directory does not already contain a primary storage file
!!! NOTE !!! This should be called from the subdirectory that "owns" the file
*/
subDir.RLock()
if subDir.datafile == nil {
subDir.RUnlock()
return nil, SubdirectoryDoesNotContainFiles(subDir.pathToItemName)
}
var finfoTemplate *io.TimeBucketInfo
for _, fi := range subDir.datafile {
finfoTemplate = fi
break
}
subDir.RUnlock()
newFileInfo := finfoTemplate.GetDeepCopy()
newFileInfo.Year = newYear
// Create a new filename for the new file
subDir.RLock()
newFileInfo.Path = path.Join(subDir.pathToItemName, strconv.Itoa(int(newYear))+".bin")
subDir.RUnlock()
if err = newTimeBucketInfoFromTemplate(newFileInfo); err != nil {
if _, ok := err.(FileAlreadyExists); ok {
return newFileInfo, nil
}
return nil, err
}
// Locate the directory in the catalog
subDir.Lock()
subDir.datafile[newFileInfo.Path] = newFileInfo
subDir.Unlock()
return newFileInfo, nil
}
func (d *Directory) DirHasDataFiles() bool {
d.RLock()
defer d.RUnlock()
return d.datafile != nil
}
func (d *Directory) GetName() string {
d.RLock()
defer d.RUnlock()
return d.itemName
}
func (d *Directory) GetPath() string {
return d.pathToItemName
}
func (d *Directory) GetSubDirectoryAndAddFile(fullFilePath string, year int16) (*io.TimeBucketInfo, error) {
d.Lock()
defer d.Unlock()
dirPath := path.Dir(fullFilePath)
if dir, ok := d.directMap[dirPath]; ok {
return dir.AddFile(year)
}
return nil, fmt.Errorf("Directory path %s not found in catalog", fullFilePath)
}
func (d *Directory) GetOwningSubDirectory(fullFilePath string) (subDir *Directory, err error) {
// Must be thread-safe for READ access
dirPath := path.Dir(fullFilePath)
d.RLock()
defer d.RUnlock()
if dir, ok := d.directMap[dirPath]; ok {
return dir, nil
}
return nil, fmt.Errorf("Directory path %s not found in catalog", fullFilePath)
}
func (d *Directory) GetListOfSubDirs() (subDirList []*Directory) {
// For a single directory, return a list of subdirectories it contains
d.RLock()
defer d.RUnlock()
if d.subDirs == nil {
return nil
}
subDirList = make([]*Directory, 0)
for _, subdir := range d.subDirs {
subDirList = append(subDirList, subdir)
}
return subDirList
}
func (d *Directory) GetSubDirWithItemName(itemName string) (subDir *Directory) {
// For a single directory, return a subdirectory that matches the name "itemName"
d.RLock()
defer d.RUnlock()
if d.subDirs == nil {
return nil
}
if _, ok := d.subDirs[itemName]; ok {
return d.subDirs[itemName]
}
return nil
}
func (d *Directory) DirHasSubDirs() bool {
// Returns true if this directory has subdirectories
d.RLock()
defer d.RUnlock()
if d.subDirs == nil {
return false
}
if len(d.subDirs) == 0 {
return false
}
return true
}
func (d *Directory) GetCategory() string {
d.RLock()
defer d.RUnlock()
return d.category
}
func (d *Directory) GatherCategoriesFromCache() (catList map[string]int8) {
// Must be thread-safe for WRITE access
// Provides a map of categories contained within and below this directory. Will create the list cache if nil.
d.RLock()
catList = d.catList
d.RUnlock()
if catList == nil {
return d.gatherCategoriesUpdateCache()
}
return catList
}
func (d *Directory) GatherCategoriesAndItems() map[string]map[string]int {
// Must be thread-safe for READ access
// Provides a map of categories and items within and below this directory
catListFunc := func(d *Directory, i_list interface{}) {
list := i_list.(map[string]map[string]int)
if list[d.category] == nil {
list[d.category] = make(map[string]int, 0)
}
if d.subDirs != nil {
for _, subdir := range d.subDirs {
list[d.category][subdir.itemName] = 0
}
}
if d.datafile != nil {
for _, file := range d.datafile {
list[d.category][strconv.Itoa(int(file.Year))] = 0
}
}
}
catList := make(map[string]map[string]int, 0)
d.recurse(catList, catListFunc)
return catList
}
func (d *Directory) String() string {
// Must be thread-safe for READ access
printstring := "Node: " + d.itemName
printstring += ", Category: " + d.category
printstring += ", Subdirs: "
d.RLock()
for _, subdir := range d.subDirs {
subdir.RLock()
printstring += subdir.itemName + ":"
subdir.RUnlock()
}
d.RUnlock()
return printstring[:len(printstring)-1]
}
func (d *Directory) gatherDirectories() []string {
// Must be thread-safe for READ access
dirListFunc := func(d *Directory, i_list interface{}) {
p_list := i_list.(*[]string)
*p_list = append(*p_list, d.itemName)
}
dirList := make([]string, 0)
d.recurse(&dirList, dirListFunc)
return dirList
}
func (d *Directory) gatherFilePaths() []string {
// Must be thread-safe for READ access
filePathListFunc := func(d *Directory, i_list interface{}) {
p_list := i_list.(*[]string)
if d.datafile != nil {
for _, dfile := range d.datafile {
*p_list = append(*p_list, dfile.Path)
}
}
}
filePathList := make([]string, 0)
d.recurse(&filePathList, filePathListFunc)
return filePathList
}
func (d *Directory) gatherCategoriesUpdateCache() map[string]int8 {
// Must be thread-safe for WRITE access
// Note that this should be called whenever catalog structure is modified to update the cache
catListFunc := func(d *Directory, i_list interface{}) {
list := i_list.(map[string]int8)
list[d.category] = 0
}
newCatList := make(map[string]int8, 0)
d.recurse(newCatList, catListFunc)
d.Lock()
d.catList = newCatList
d.Unlock()
return newCatList
}
func (d *Directory) getOwningSubDirectoryByRecursion(filePath string) (subDir *Directory, err error) {
// Locates the directory in the catalog that matches the path - note that this is O(N)
// Must be thread-safe for READ access
dirPath := path.Dir(filePath)
findDirectory := func(d *Directory, _ interface{}) {
if subDir != nil {
// We have already found our directory match
return
}
if d.pathToItemName == dirPath {
subDir = d
return
}
}
d.recurse(subDir, findDirectory)
if subDir == nil {
return nil, NotFoundError("")
} else {
return subDir, nil
}
}
func (d *Directory) getLatestYearFile() (latestFile *io.TimeBucketInfo, err error) {
// Must be thread-safe for READ access
d.RLock()
defer d.RUnlock()
if d.datafile == nil {
return nil, SubdirectoryDoesNotContainFiles("getLatestYearFile")
}
var year int16
for _, fp := range d.datafile {
if year < fp.Year || year == 0 {
year = fp.Year
latestFile = fp
}
}
return latestFile, nil
}
func (d *Directory) pathToKey(fullPath string) (key string) {
dirPath := path.Dir(fullPath)
key = strings.Replace(dirPath, d.pathToItemName, "", 1)
key = strings.TrimLeft(key, "/")
return key
}
func (d *Directory) addSubdir(subDir *Directory, subDirItemName string) {
subDir.itemName = subDirItemName
d.catList = nil // Reset the category list
if d.subDirs == nil {
d.subDirs = make(DMap)
}
d.subDirs[subDirItemName] = subDir
for key, val := range subDir.directMap {
d.directMap[key] = val
}
subDir.directMap = nil
}
func (d *Directory) removeSubDir(subDirItemName string, directMap DMap) {
d.Lock()
defer d.Unlock()
if _, ok := d.subDirs[subDirItemName]; ok {
// Note that this is a NoOp for all but the leaf node of the tree, but it's a harmless NoOp
subdir := d.subDirs[subDirItemName]
delete(directMap, subdir.pathToItemName)
}
delete(d.subDirs, subDirItemName)
if len(d.subDirs) == 0 {
d.subDirs = nil
}
}
func (d *Directory) recurse(elem interface{}, levelFunc LevelFunc) {
// Must be thread-safe for READ access
// Recurse will recurse through a directory, calling levelfunc. Elem is used to pass along a variable.
d.RLock()
defer d.RUnlock()
levelFunc(d, elem)
if d.subDirs != nil {
for _, pd := range d.subDirs {
pd.recurse(elem, levelFunc)
}
}
}
func (d *Directory) load(rootPath string) error {
// Load is single thread compatible - no concurrent access is anticipated
rootDmap := d.directMap
var loader func(d *Directory, subPath, rootPath string) error
loader = func(d *Directory, subPath, rootPath string) error {
relPath, _ := filepath.Rel(rootPath, subPath)
d.itemName = filepath.Base(relPath)
d.pathToItemName = filepath.Clean(subPath)
// Read the category name for the child directory items
catFilePath := subPath + "/" + "category_name"
catname, err := ioutil.ReadFile(catFilePath)
if err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
d.category = string(catname)
// Load up the child directories
d.subDirs = make(DMap)
dirlist, err := ioutil.ReadDir(subPath)
for _, dirname := range dirlist {
leafPath := path.Clean(subPath + "/" + dirname.Name())
if dirname.IsDir() && dirname.Name() != "metadata.db" {
itemName := dirname.Name()
d.subDirs[itemName] = new(Directory)
d.subDirs[itemName].itemName = itemName
d.subDirs[itemName].pathToItemName = subPath
d.datafile = nil
if err := loader(d.subDirs[itemName], leafPath, rootPath); err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
} else if filepath.Ext(leafPath) == ".bin" {
rootDmap[d.pathToItemName] = d
if d.datafile == nil {
d.datafile = make(map[string]*io.TimeBucketInfo)
}
// Mark this as a pending Fileinfo reference
d.datafile[leafPath] = new(io.TimeBucketInfo)
d.datafile[leafPath].IsRead = false
d.datafile[leafPath].Path = leafPath
yearFileBase := filepath.Base(leafPath)
yearString := yearFileBase[:len(yearFileBase)-4]
yearInt, err := strconv.Atoi(yearString)
if err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + err.Error())
}
d.datafile[leafPath].Year = int16(yearInt)
/*
if d.datafile[leafPath], err = ReadHeader(leafPath); err != nil {
return err
}
*/
}
}
return nil
}
return loader(d, rootPath, rootPath)
}
func removeDirFiles(td *Directory) {
os.RemoveAll(td.pathToItemName)
}
func newTimeBucketInfoFromTemplate(newTimeBucketInfo *io.TimeBucketInfo) (err error) {
if newTimeBucketInfo == nil {
return fmt.Errorf("Null fileinfo")
}
// If file already exists in this directory, return an error
if _, err := os.Stat(newTimeBucketInfo.Path); err == nil {
return FileAlreadyExists("Can not overwrite file")
}
// Create the file
fp, err := os.OpenFile(newTimeBucketInfo.Path, os.O_CREATE|os.O_RDWR, 0600)
defer fp.Close()
if err != nil {
return UnableToCreateFile(err.Error())
}
if err = io.WriteHeader(fp, newTimeBucketInfo); err != nil {
return UnableToWriteHeader(err.Error())
}
if err = fp.Truncate(io.FileSize(newTimeBucketInfo.GetTimeframe(), int(newTimeBucketInfo.Year), int(newTimeBucketInfo.GetRecordLength()))); err != nil {
return UnableToCreateFile(err.Error())
}
return nil
}