-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
204 lines (159 loc) · 4.31 KB
/
write.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
package gitdb
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
)
func (g *gitdb) Insert(mo Model) error {
m := wrapModel(mo)
m.SetBaseModel()
if err := m.Validate(); err != nil {
return fmt.Errorf("Model is not valid: %s", err)
}
if err := m.GetSchema().Validate(); err != nil {
return err
}
if err := g.flushQueue(); err != nil {
log(err.Error())
}
return g.write(m)
}
func (g *gitdb) InsertMany(models []Model) error {
//todo polish this up later
if len(models) > 100 {
return errors.New("max number of models InsertMany supports is 100")
}
tx := g.StartTransaction("InsertMany")
var model Model
for _, model = range models {
//create a new variable to pass to function to avoid
//passing pointer which will end up inserting the same
//model multiple times
m := model
f := func() error { return g.Insert(m) }
tx.AddOperation(f)
}
return tx.Commit()
}
func (g *gitdb) queue(m Model) error {
if len(g.writeQueue) == 0 {
g.writeQueue = map[string]Model{}
}
g.writeQueue[ID(m)] = m
return nil
}
func (g *gitdb) flushQueue() error {
for id, model := range g.writeQueue {
log("Flushing: " + id)
err := g.write(model)
if err != nil {
logError(err.Error())
return err
}
delete(g.writeQueue, id)
}
return nil
}
func (g *gitdb) write(m Model) error {
if _, err := os.Stat(g.fullPath(m)); err != nil {
err := os.MkdirAll(g.fullPath(m), 0755)
if err != nil {
return fmt.Errorf("failed to make dir %s: %w", g.fullPath(m), err)
}
}
schema := m.GetSchema()
blockFilePath := g.blockFilePath(schema.name(), schema.blockIDFunc())
dataBlock, err := g.loadBlock(blockFilePath, schema.name())
if err != nil {
return err
}
logTest(fmt.Sprintf("Size of block before write - %d", dataBlock.size()))
//...append new record to block
newRecordBytes, err := json.Marshal(m)
if err != nil {
return err
}
mID := ID(m)
//construct a commit message
commitMsg := "Inserting " + mID + " into " + schema.blockID()
if _, err := dataBlock.get(mID); err == nil {
commitMsg = "Updating " + mID + " in " + schema.blockID()
}
newRecordStr := string(newRecordBytes)
//encrypt data if need be
if m.ShouldEncrypt() {
newRecordStr = encrypt(g.config.EncryptionKey, newRecordStr)
}
dataBlock.add(m.GetSchema().recordID(), newRecordStr)
g.events <- newWriteBeforeEvent("...", mID)
if err := g.writeBlock(blockFilePath, dataBlock); err != nil {
return err
}
log(fmt.Sprintf("autoCommit: %v", g.autoCommit))
g.commit.Add(1)
g.events <- newWriteEvent(commitMsg, blockFilePath, g.autoCommit)
logTest("sent write event to loop")
g.updateIndexes(schema.name(), newRecord(mID, newRecordStr))
//block here until write has been committed
g.waitForCommit()
return nil
}
func (g *gitdb) waitForCommit() {
if g.autoCommit {
logTest("waiting for gitdb to commit changes")
g.commit.Wait()
}
}
func (g *gitdb) writeBlock(blockFile string, block *block) error {
g.writeMu.Lock()
defer g.writeMu.Unlock()
blockBytes, fmtErr := json.MarshalIndent(block, "", "\t")
if fmtErr != nil {
return fmtErr
}
return ioutil.WriteFile(blockFile, blockBytes, 0744)
}
func (g *gitdb) Delete(id string) error {
return g.dodelete(id, false)
}
func (g *gitdb) DeleteOrFail(id string) error {
return g.dodelete(id, true)
}
func (g *gitdb) dodelete(id string, failNotFound bool) error {
dataset, block, _, err := ParseID(id)
if err != nil {
return err
}
blockFilePath := g.blockFilePath(dataset, block)
err = g.delByID(id, dataset, blockFilePath, failNotFound)
if err == nil {
logTest("sending delete event to loop")
g.commit.Add(1)
g.events <- newDeleteEvent("Deleting "+id+" in "+blockFilePath, blockFilePath, g.autoCommit)
g.waitForCommit()
}
return err
}
func (g *gitdb) delByID(id string, dataset string, blockFile string, failIfNotFound bool) error {
if _, err := os.Stat(blockFile); err != nil {
if failIfNotFound {
return errors.New("Could not delete [" + id + "]: record does not exist")
}
return nil
}
dataBlock := newBlock(dataset)
err := g.readBlock(blockFile, dataBlock)
if err != nil {
return err
}
if err := dataBlock.delete(id); err != nil {
if failIfNotFound {
return errors.New("Could not delete [" + id + "]: record does not exist")
}
return nil
}
//write undeleted records back to block file
return g.writeBlock(blockFile, dataBlock)
}