-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
151 lines (142 loc) · 4.05 KB
/
db.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
package catTrackslib
import (
"fmt"
"log"
"path/filepath"
bolt "go.etcd.io/bbolt"
)
var (
db *bolt.DB // master
devopDB *bolt.DB
edgeDB *bolt.DB
trackKey = "tracks"
statsKey = "stats"
statsDataKey = "storage" // use: bucket.Put(statsDataKey, value), bucket.Get(statsDataKey)
placesKey = "places"
googlefindnearby = "googlefindnearby"
googlefindnearbyphotos = "googlefindnearbyphotos"
placesByCoord = "placesByCoord"
catsnapsKey = "catsnaps"
catsnapsGeoJSONKey = "catsnaps-geojson"
allBuckets = []string{trackKey, statsKey, "names", "geohash", placesKey, googlefindnearby, googlefindnearbyphotos, placesByCoord, catsnapsKey}
)
// GetDB is db getter.
func GetDB(name string) *bolt.DB {
switch name {
case "master", "":
return db
case "devop":
return devopDB
case "edge":
return edgeDB
default:
panic("no db by that name")
}
}
func initBuckets(db *bolt.DB, buckets []string) error {
err := db.Update(func(tx *bolt.Tx) error {
var e error
for _, buck := range buckets {
_, e = tx.CreateBucketIfNotExists([]byte(buck))
if e != nil {
return e
}
}
return e
})
return err
}
// InitBoltDB sets up initial stuff, like the file and necesary buckets
func InitBoltDB() error {
var err error
// master
// if e := os.MkdirAll(filepath.Dir(masterdbpath), 0666); e != nil {
// log.Fatal(e)
// }
db, err = bolt.Open(masterdbpath, 0666, nil)
if err != nil {
log.Printf("Could not initialize Bolt database @master err=%v path=%v", err, masterdbpath)
abs, _ := filepath.Abs(masterdbpath)
fmt.Printf("that is abs: %s", abs)
return err
}
if err := initBuckets(GetDB("master"), allBuckets); err != nil {
fmt.Println("Err initing buckets @master.", err)
}
// devop and edge databases aren't actually necessary or required or used at all. just for symmetry, and maybe for something unknown
// devop
// os.MkdirAll(filepath.Dir(devopdbpath), 0666)
if devopdbpath != "" {
devopDB, err = bolt.Open(devopdbpath, 0666, nil)
if err != nil {
fmt.Println("Could not initialize Bolt database @devop. ", err)
return err
}
// only init track key for devop db
if err := initBuckets(GetDB("devop"), []string{trackKey}); err != nil {
return err
}
}
// edge
if edgedbpath != "" {
// os.MkdirAll(filepath.Dir(edgedbpath), 0666)
edgeDB, err = bolt.Open(edgedbpath, 0666, nil)
if err != nil {
fmt.Println("Could not initialize Bolt database @edge. ", err)
return err
}
// only init track key for edge db
if err := initBuckets(GetDB("edge"), []string{trackKey}); err != nil {
return err
}
}
return nil
}
// // BuildIndexBuckets populates name, lat, and long buckets from main "tracks" (time) bucket.
// func BuildIndexBuckets() error {
// err := db.View(func(tx *bolt.Tx) error {
// err := tx.Bucket([]byte(trackKey)).ForEach(func(key, val []byte) error {
//
// var tp *trackPoint.TrackPoint
// if err := json.Unmarshal(val, &tp); err != nil {
// return err
// }
//
// // update "name"
// if err := db.Update(func(txx *bolt.Tx) error {
// bname := txx.Bucket([]byte("names"))
//
// bByName, _ := bname.CreateBucketIfNotExists([]byte(tp.Name))
//
// err := bByName.Put(buildTrackpointKey(tp), val)
// return err
// }); err != nil {
// return err
// }
//
// // under geohasher keys
// if err := db.Update(func(txx *bolt.Tx) error {
// b := txx.Bucket([]byte("geohash"))
//
// // Compute the CellID for lat, lng
// c := s2.CellIDFromLatLng(s2.LatLngFromDegrees(tp.Lat, tp.Lng))
//
// // store the uint64 value of c to its bigendian binary form
// hashkey := make([]byte, 8)
// binary.BigEndian.PutUint64(hashkey, uint64(c))
//
// e := b.Put(hashkey, val)
// if e != nil {
// fmt.Println("shit geohash index err", e)
// return fmt.Errorf("shit geohash index err: %v", e)
// }
// return nil
// }); err != nil {
// return err
// }
// return nil
// })
// return err
// })
// return err
// }