forked from HouzuoGuo/tiedot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
213 lines (200 loc) · 6.08 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
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
/* Database is a collection of collections. */
package db
import (
"errors"
"fmt"
"github.com/HouzuoGuo/tiedot/tdlog"
"io/ioutil"
"os"
"path"
"strconv"
"sync"
"time"
)
const (
NUMCHUNKS_FILENAME = "numchunks"
)
type DB struct {
BaseDir string // Database directory path
StrCol map[string]*Col // Collection name to collection mapping
}
func OpenDB(baseDir string) (db *DB, err error) {
if err = os.MkdirAll(baseDir, 0700); err != nil {
return
}
db = &DB{BaseDir: baseDir, StrCol: make(map[string]*Col)}
files, err := ioutil.ReadDir(baseDir)
if err != nil {
return
}
// Try to open sub-directory as document collection
for _, f := range files {
if f.IsDir() {
// Figure out how many chunks there are in the collection
var numchunksFH *os.File
numchunksFH, err = os.OpenFile(path.Join(baseDir, f.Name(), NUMCHUNKS_FILENAME), os.O_CREATE|os.O_RDWR, 0600)
defer numchunksFH.Close()
if err != nil {
return
}
numchunksContent, err := ioutil.ReadAll(numchunksFH)
if err != nil {
panic(err)
}
numchunks, err := strconv.Atoi(string(numchunksContent))
if err != nil || numchunks < 1 {
panic(fmt.Sprintf("Cannot figure out number of chunks for collection %s, manually repair it maybe? %v", baseDir, err))
}
// Open the directory as a collection
if db.StrCol[f.Name()], err = OpenCol(path.Join(baseDir, f.Name()), numchunks); err != nil {
tdlog.Errorf("ERROR: Failed to open collection %s, error: %v", f.Name(), err)
} else {
tdlog.Printf("Successfully opened collection %s", f.Name())
}
}
}
return
}
// Create a collection.
func (db *DB) Create(name string, numChunks int) (err error) {
if numChunks < 1 {
return errors.New(fmt.Sprintf("Number of of partitions must be above 0"))
}
if _, nope := db.StrCol[name]; nope {
return errors.New(fmt.Sprintf("Collection %s already exists in %s", name, db.BaseDir))
}
if db.StrCol[name], err = OpenCol(path.Join(db.BaseDir, name), numChunks); err != nil {
return
}
if err = ioutil.WriteFile(path.Join(db.BaseDir, name, NUMCHUNKS_FILENAME), []byte(fmt.Sprint(numChunks)), 0600); err != nil {
return
}
return err
}
// Return collection reference by collection name. This function is safe for concurrent use.
func (db *DB) Use(name string) *Col {
if col, ok := db.StrCol[name]; ok {
return col
}
return nil
}
// Rename a collection.
func (db *DB) Rename(oldName, newName string) (err error) {
var numChunks int
col, ok := db.StrCol[oldName]
if ok {
numChunks = len(db.StrCol[oldName].Chunks)
} else {
return errors.New(fmt.Sprintf("Collection %s does not exists in %s", oldName, db.BaseDir))
}
if _, nope := db.StrCol[newName]; nope {
return errors.New(fmt.Sprintf("Collection name %s is already used in %s", newName, db.BaseDir))
}
col.Close()
delete(db.StrCol, oldName)
if err = os.Rename(path.Join(db.BaseDir, oldName), path.Join(db.BaseDir, newName)); err != nil {
return
}
db.StrCol[newName], err = OpenCol(path.Join(db.BaseDir, newName), numChunks)
return
}
// Drop (delete) a collection.
func (db *DB) Drop(name string) (err error) {
if col, ok := db.StrCol[name]; ok {
col.Close()
delete(db.StrCol, name)
return os.RemoveAll(path.Join(db.BaseDir, name))
} else {
return errors.New(fmt.Sprintf("Collection %s does not exists in %s", name, db.BaseDir))
}
}
// Compact and repair a collection.
func (db *DB) Scrub(name string) (counter uint64, err error) {
counterMutex := &sync.Mutex{}
target := db.Use(name)
if target == nil {
return 0, errors.New(fmt.Sprintf("Collection %s does not exist in %s", name, db.BaseDir))
}
// Create a temporary collection
numChunks := target.NumChunks
tempName := fmt.Sprintf("temp-%s-%v", name, time.Now().Unix())
db.Create(tempName, numChunks)
temp := db.Use(tempName)
// Recreate secondary indexes
for _, index := range target.SecIndexes {
temp.Index(index[0].Path)
}
// Reinsert documents
target.ForAll(func(id uint64, doc map[string]interface{}) bool {
if err := temp.InsertRecovery(id, doc); err == nil {
counterMutex.Lock()
counter += 1
counterMutex.Unlock()
} else {
tdlog.Errorf("Failed to recover document %v", doc)
}
return true
})
// Drop the old collection and rename the recovery collection
if err = db.Drop(name); err != nil {
tdlog.Errorf("Scrub operation failed to drop original collection %s: %v", name, err)
return
}
if err = db.Rename(tempName, name); err != nil {
tdlog.Errorf("Scrub operation failed to rename recovery collection %s: %v", tempName, err)
}
return
}
// Change the number of partitions in collection
func (db *DB) Repartition(name string, newNumber int) (counter uint64, err error) {
counterMutex := &sync.Mutex{}
target := db.Use(name)
if target == nil {
return 0, errors.New(fmt.Sprintf("Collection %s does not exist in %s", name, db.BaseDir))
}
if newNumber < 1 {
return 0, errors.New(fmt.Sprintf("New number of partitions must be above 0, %d given", newNumber))
}
// Create a temporary collection
tempName := fmt.Sprintf("temp-%s-%v", name, time.Now().Unix())
db.Create(tempName, newNumber)
temp := db.Use(tempName)
// Recreate secondary indexes
for _, index := range target.SecIndexes {
temp.Index(index[0].Path)
}
// Reinsert documents
target.ForAll(func(id uint64, doc map[string]interface{}) bool {
if err := temp.InsertRecovery(id, doc); err == nil {
counterMutex.Lock()
counter += 1
counterMutex.Unlock()
} else {
tdlog.Errorf("Failed to recover document %v", doc)
}
return true
})
// Drop the old collection and rename the recovery collection
if err = db.Drop(name); err != nil {
tdlog.Errorf("Scrub operation failed to drop original collection %s: %v", name, err)
return
}
if err = db.Rename(tempName, name); err != nil {
tdlog.Errorf("Scrub operation failed to rename recovery collection %s: %v", tempName, err)
}
return
}
// Flush all collection data and index files.
func (db *DB) Flush() {
for _, col := range db.StrCol {
if err := col.Flush(); err != nil {
tdlog.Errorf("Error during database flush: %v", err)
}
}
}
// Close all collections.
func (db *DB) Close() {
for _, col := range db.StrCol {
col.Close()
}
}