-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwrite.go
177 lines (140 loc) · 3.85 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
package gitdb
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/bouggo/log"
"github.com/gogitdb/gitdb/v2/internal/crypto"
"github.com/gogitdb/gitdb/v2/internal/db"
)
func (g *gitdb) Insert(mo Model) error {
m := wrap(mo)
if err := m.Validate(); err != nil {
return err
}
if err := m.BeforeInsert(); err != nil {
return fmt.Errorf("Model.BeforeInsert failed: %s", err)
}
if err := m.GetSchema().Validate(); err != nil {
return err
}
return g.insert(m)
}
func (g *gitdb) InsertMany(models []Model) error {
tx := g.StartTransaction("InsertMany")
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) insert(m Model) error {
if !g.isRegistered(m.GetSchema().dataset) {
return ErrInvalidDataset
}
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.block)
dataBlock, err := g.loadBlock(blockFilePath)
if err != nil {
return err
}
log.Test(fmt.Sprintf("Size of block before write - %d", dataBlock.Len()))
//...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
if _, err := dataBlock.Get(mID); err == nil {
commitMsg = "Updating " + mID
}
newRecordStr := string(newRecordBytes)
//encrypt data if need be
if m.ShouldEncrypt() {
newRecordStr = crypto.Encrypt(g.config.EncryptionKey, newRecordStr)
}
dataBlock.Add(mID, newRecordStr)
g.events <- newWriteBeforeEvent("...", mID)
if err := g.writeBlock(blockFilePath, dataBlock); err != nil {
return err
}
log.Info(fmt.Sprintf("autoCommit: %v", g.autoCommit))
g.commit.Add(1)
g.events <- newWriteEvent(commitMsg, blockFilePath, g.autoCommit)
log.Test("sent write event to loop")
g.updateIndexes(dataBlock)
//block here until write has been committed
g.waitForCommit()
return nil
}
func (g *gitdb) waitForCommit() {
if g.autoCommit {
log.Test("waiting for gitdb to commit changes")
g.commit.Wait()
}
}
func (g *gitdb) writeBlock(blockFile string, block *db.Block) error {
g.writeMu.Lock()
defer g.writeMu.Unlock()
blockBytes, fmtErr := json.MarshalIndent(block, "", "\t")
if fmtErr != nil {
return fmtErr
}
//update cache
if g.loadedBlocks != nil {
g.loadedBlocks[blockFile] = block
}
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, blockFilePath, failNotFound)
if err == nil {
log.Test("sending delete event to loop")
g.commit.Add(1)
g.events <- newDeleteEvent(fmt.Sprintf("Deleting %s", id), blockFilePath, g.autoCommit)
g.waitForCommit()
}
return err
}
func (g *gitdb) delByID(id 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 := db.LoadBlock(blockFile, g.config.EncryptionKey)
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)
}