-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschema.go
206 lines (166 loc) · 4.92 KB
/
schema.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
package gitdb
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/bouggo/log"
)
//Schema holds functions for generating a model id
type Schema struct {
dataset string
block string
record string
indexes map[string]interface{}
internal bool
}
//NewSchema constructs a *Schema
func NewSchema(name, block, record string, indexes map[string]interface{}) *Schema {
return &Schema{dataset: name, block: block, record: record, indexes: indexes}
}
func newSchema(name, block, record string, indexes map[string]interface{}) *Schema {
return &Schema{dataset: name, block: block, record: record, indexes: indexes, internal: true}
}
//name returns name of schema
func (a *Schema) name() string {
return a.dataset
}
//blockID retuns block id of schema
func (a *Schema) blockID() string {
return a.dataset + "/" + a.block
}
//recordID returns record id of schema
func (a *Schema) recordID() string {
return a.blockID() + "/" + a.record
}
//Validate ensures *Schema is valid
func (a *Schema) Validate() error {
if len(a.dataset) == 0 {
return errors.New("Invalid Schema Name")
}
if !a.internal && !a.validDatasetName(a.dataset) {
return fmt.Errorf("%s is a reserved Schema Name", a.dataset)
}
if !a.validName(a.block) {
return errors.New("Invalid Schema Block ID")
}
if !a.validName(a.record) {
return errors.New("Invalid Schema Record ID")
}
if _, ok := a.indexes["id"]; ok && !a.internal {
return fmt.Errorf("%s is a reserved index name", "id")
}
return nil
}
func (a *Schema) validDatasetName(name string) bool {
reservedName := []string{"gitdb", "bucket", "upload"}
lcname := strings.ToLower(name)
for _, rname := range reservedName {
if lcname == rname {
return false
}
}
return a.validName(name)
}
func (a *Schema) validName(name string) bool {
if len(name) < 1 {
return false
}
allowedChars := `abcdefghijklmnopqrstuvwxyz0123456789_-.`
return strings.ContainsAny(strings.ToLower(name), allowedChars)
}
//Indexes returns the index map of a given Model
func Indexes(m Model) map[string]interface{} {
return m.GetSchema().indexes
}
//ID returns the id of a given Model
func ID(m Model) string {
return m.GetSchema().recordID()
}
//ParseID parses a record id and returns it's metadata
func ParseID(id string) (dataset string, block string, record string, err error) {
recordMeta := strings.Split(id, "/")
if len(recordMeta) != 3 {
err = ErrInvalidRecordID
} else {
dataset = recordMeta[0]
block = recordMeta[1]
record = recordMeta[2]
}
return dataset, block, record, err
}
//BlockMethod type of method to use with AutoBlock
type BlockMethod string
var (
//BlockBySize generates a new block when current block has reached a specified size
BlockBySize BlockMethod = "size"
//BlockByCount generates a new block when the number of records has reached a specified count
BlockByCount BlockMethod = "count"
)
//AutoBlock automatically generates block id for a given Model depending on a BlockMethod
func AutoBlock(dbPath string, m Model, method BlockMethod, n int64) string {
var currentBlock int
var currentBlockFile os.FileInfo
var currentBlockrecords map[string]interface{}
//being sensible
if n <= 0 {
n = 1000
}
dataset := m.GetSchema().name()
fullPath := filepath.Join(dbPath, "data", dataset)
if _, err := os.Stat(fullPath); err != nil {
return fmt.Sprintf("b%d", currentBlock)
}
files, err := ioutil.ReadDir(fullPath)
if err != nil {
log.Error(err.Error())
log.Test("AutoBlock: " + err.Error())
return ""
}
if len(files) == 0 {
log.Test("AutoBlock: no blocks found at " + fullPath)
return fmt.Sprintf("b%d", currentBlock)
}
currentBlock = -1
for _, currentBlockFile = range files {
currentBlockFileName := filepath.Join(fullPath, currentBlockFile.Name())
if filepath.Ext(currentBlockFileName) != ".json" {
continue
}
currentBlock++
//TODO OPTIMIZE read file
b, err := ioutil.ReadFile(currentBlockFileName)
if err != nil {
log.Test("AutoBlock: " + err.Error())
log.Error(err.Error())
continue
}
currentBlockrecords = make(map[string]interface{})
if err := json.Unmarshal(b, ¤tBlockrecords); err != nil {
log.Error(err.Error())
continue
}
block := strings.Replace(filepath.Base(currentBlockFileName), filepath.Ext(currentBlockFileName), "", 1)
id := fmt.Sprintf("%s/%s/%s", dataset, block, m.GetSchema().record)
log.Test("AutoBlock: searching for - " + id)
//model already exists return its block
if _, ok := currentBlockrecords[id]; ok {
log.Test("AutoBlock: found - " + id)
return block
}
}
//is current block at it's size limit?
if method == BlockBySize && currentBlockFile.Size() >= n {
currentBlock++
return fmt.Sprintf("b%d", currentBlock)
}
//record size check
log.Test(fmt.Sprintf("AutoBlock: current block count - %d", len(currentBlockrecords)))
if method == BlockByCount && len(currentBlockrecords) >= int(n) {
currentBlock++
}
return fmt.Sprintf("b%d", currentBlock)
}