forked from HouzuoGuo/tiedot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
col.go
392 lines (368 loc) · 10.8 KB
/
col.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* Coordination between a collection of chunks. */
package db
import (
"errors"
"fmt"
"github.com/steveoc64/tiedot/chunk"
"github.com/steveoc64/tiedot/chunkfile"
"github.com/steveoc64/tiedot/tdlog"
"github.com/steveoc64/tiedot/uid"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
)
const (
HASHTABLE_DIRNAME_MAGIC = "ht_" // Hash table directory name prefix
CHUNK_DIRNAME_MAGIC = "chunk_" // Chunk directory name prefix
INDEX_PATH_SEP = "," // Separator between index path segments
)
type Col struct {
BaseDir string // Collection dir path
Chunks []*chunk.ChunkCol
ChunkMutexes []*sync.RWMutex // Synchronize access to chunks
NumChunks int // Total number of chunks
NumChunksI64 uint64 // Total number of chunks (uint64)
// Secondary indexes (hashtables)
SecIndexes map[string][]*chunkfile.HashTable
}
// Resolve the attribute(s) in the document structure along the given path.
func GetIn(doc interface{}, path []string) (ret []interface{}) {
docMap, ok := doc.(map[string]interface{})
if !ok {
tdlog.Printf("%v cannot be indexed because type conversation to map[string]interface{} failed", doc)
return
}
var thing interface{} = docMap
// Get into each path segment
for i, seg := range path {
if aMap, ok := thing.(map[string]interface{}); ok {
thing = aMap[seg]
} else if anArray, ok := thing.([]interface{}); ok {
for _, element := range anArray {
ret = append(ret, GetIn(element, path[i:])...)
}
return ret
} else {
return nil
}
}
switch thing.(type) {
case []interface{}:
return append(ret, thing.([]interface{})...)
default:
return append(ret, thing)
}
}
// Open a collection (made of chunks).
func OpenCol(baseDir string, numChunks int) (col *Col, err error) {
// Create the directory if it does not yet exist
if err = os.MkdirAll(baseDir, 0700); err != nil {
return
}
col = &Col{BaseDir: baseDir, NumChunks: numChunks, NumChunksI64: uint64(numChunks),
SecIndexes: make(map[string][]*chunkfile.HashTable),
Chunks: make([]*chunk.ChunkCol, numChunks), ChunkMutexes: make([]*sync.RWMutex, numChunks)}
// Open each chunk
for i := 0; i < numChunks; i++ {
col.Chunks[i], err = chunk.OpenChunk(i, path.Join(baseDir, CHUNK_DIRNAME_MAGIC+strconv.Itoa(int(i))))
if err != nil {
panic(err)
}
col.ChunkMutexes[i] = &sync.RWMutex{}
}
// Look for hash table directories
walker := func(currPath string, info os.FileInfo, err2 error) error {
if err2 != nil {
// log and skip the error
tdlog.Error(err)
return nil
}
if info.IsDir() {
switch {
case strings.HasPrefix(info.Name(), HASHTABLE_DIRNAME_MAGIC):
// Found a hashtable index
tdlog.Printf("Opening collection index hashtable %s", info.Name())
// Figure out indexed path
indexPath := strings.Split(info.Name()[len(HASHTABLE_DIRNAME_MAGIC):], INDEX_PATH_SEP)
// Open a hash table index and put it into collection structure
col.openIndex(indexPath, path.Join(baseDir, info.Name()))
}
}
return nil
}
err = filepath.Walk(baseDir, walker)
return
}
// Open a hash table index and put it into collection structure.
func (col *Col) openIndex(indexPath []string, baseDir string) {
jointPath := strings.Join(indexPath, INDEX_PATH_SEP)
tables := make([]*chunkfile.HashTable, col.NumChunks)
for i := 0; i < col.NumChunks; i++ {
if err := os.MkdirAll(baseDir, 0700); err != nil {
panic(err)
}
table, err := chunkfile.OpenHash(path.Join(baseDir, strconv.Itoa(int(i))), indexPath)
if err != nil {
panic(err)
}
tables[i] = table
}
col.SecIndexes[jointPath] = tables
}
// Create a new index.
func (col *Col) Index(indexPath []string) error {
jointPath := strings.Join(indexPath, INDEX_PATH_SEP)
// Check whether the index already exists
if _, alreadyExists := col.SecIndexes[jointPath]; alreadyExists {
return errors.New(fmt.Sprintf("Path %v is already indexed in collection %s", indexPath, col.BaseDir))
}
// Make the new index
indexBaseDir := path.Join(col.BaseDir, HASHTABLE_DIRNAME_MAGIC+jointPath)
col.openIndex(indexPath, indexBaseDir)
// Put all documents on the new index
newIndex := col.SecIndexes[jointPath]
col.ForAll(func(id uint64, doc map[string]interface{}) bool {
for _, toBeIndexed := range GetIn(doc, indexPath) {
if toBeIndexed != nil {
// Figure out where to put it
hash := chunk.StrHash(toBeIndexed)
dest := newIndex[hash%col.NumChunksI64]
lock := dest.Mutex
lock.Lock()
dest.Put(hash, id)
lock.Unlock()
}
}
return true
})
return nil
}
// Remove a secondary index.
func (col *Col) Unindex(indexPath []string) (err error) {
jointPath := strings.Join(indexPath, ",")
if _, found := col.SecIndexes[jointPath]; !found {
return errors.New(fmt.Sprintf("Path %v is not indexed in collection %s", indexPath, col.BaseDir))
}
// Close the index file
for _, indexFile := range col.SecIndexes[jointPath] {
indexFile.File.Close()
}
if err = os.RemoveAll(path.Join(col.BaseDir, HASHTABLE_DIRNAME_MAGIC+jointPath)); err != nil {
return
}
// Remove the index from collection structure
delete(col.SecIndexes, jointPath)
return nil
}
// Put the document on all secondary indexes.
func (col *Col) indexDoc(id uint64, doc interface{}) {
for _, index := range col.SecIndexes {
for _, toBeIndexed := range GetIn(doc, index[0].Path) {
if toBeIndexed != nil {
// Figure out where to put it
hashKey := chunk.StrHash(toBeIndexed)
num := hashKey % col.NumChunksI64
ht := index[num]
ht.Mutex.Lock()
index[num].Put(hashKey, id)
ht.Mutex.Unlock()
}
}
}
}
// Remove the document from all secondary indexes.
func (col *Col) unindexDoc(id uint64, doc interface{}) {
for _, index := range col.SecIndexes {
for _, toBeIndexed := range GetIn(doc, index[0].Path) {
if toBeIndexed != nil {
// Figure out where it was put
hashKey := chunk.StrHash(toBeIndexed)
num := hashKey % col.NumChunksI64
ht := index[num]
ht.Mutex.Lock()
index[num].Remove(hashKey, id)
ht.Mutex.Unlock()
}
}
}
}
// Insert a document, return its unique ID.
func (col *Col) Insert(doc map[string]interface{}) (id uint64, err error) {
// Allocate an ID to the document
id = uid.NextUID()
doc[uid.PK_NAME] = strconv.FormatUint(id, 10)
num := id % col.NumChunksI64
// Lock the chunk while inserting the document
lock := col.ChunkMutexes[num]
lock.Lock()
if _, err = col.Chunks[num].Insert(doc); err != nil {
lock.Unlock()
return
}
col.indexDoc(id, doc)
lock.Unlock()
return
}
// Insert a document without allocating a new ID to it. Only for collection recovery operation.
func (col *Col) InsertRecovery(knownID uint64, doc map[string]interface{}) (err error) {
doc[uid.PK_NAME] = strconv.FormatUint(knownID, 10)
num := knownID % col.NumChunksI64
// Lock the chunk while inserting the document
lock := col.ChunkMutexes[num]
lock.Lock()
_, err = col.Chunks[num].Insert(doc)
col.indexDoc(knownID, doc)
lock.Unlock()
return
}
// Read a document given its unique ID.
func (col *Col) Read(id uint64, doc interface{}) (physID uint64, err error) {
num := id % col.NumChunksI64
// Lock the chunk while reading the document
lock := col.ChunkMutexes[num]
dest := col.Chunks[num]
lock.RLock()
physID, err = dest.GetPhysicalID(id)
if err != nil {
lock.RUnlock()
return
}
err = dest.Read(physID, doc)
lock.RUnlock()
return
}
// Read a document given its unique ID (without placing any lock).
func (col *Col) ReadNoLock(id uint64, doc interface{}) (physID uint64, err error) {
num := id % col.NumChunksI64
// Lock the chunk while reading the document
dest := col.Chunks[num]
physID, err = dest.GetPhysicalID(id)
if err != nil {
return
}
err = dest.Read(physID, doc)
return
}
func (col *Col) HashScan(htPath string, key, limit uint64) (keys, vals []uint64) {
num := key % col.NumChunksI64
theIndex, exist := col.SecIndexes[htPath]
if !exist {
panic(fmt.Sprintf("Index %s does not exist", htPath))
}
theIndex[num].Mutex.RLock()
keys, vals = theIndex[num].Get(key, limit)
theIndex[num].Mutex.RUnlock()
return
}
// Update a document given its unique ID.
func (col *Col) Update(id uint64, newDoc map[string]interface{}) (err error) {
num := id % col.NumChunksI64
lock := col.ChunkMutexes[num]
dest := col.Chunks[num]
newDoc[uid.PK_NAME] = strconv.FormatUint(id, 10)
lock.Lock()
// Read back the original document
var oldDoc interface{}
physID, err := col.ReadNoLock(id, &oldDoc)
if err != nil {
physID, err = dest.GetPhysicalID(id)
if err != nil {
lock.Unlock()
return errors.New(fmt.Sprintf("Failed to update %d, cannot find its physical ID", id))
}
}
// Remove the original document from secondary indexes, and put new values into them
col.unindexDoc(id, oldDoc)
col.indexDoc(id, newDoc)
// Update document data file and return
_, err = dest.Update(physID, newDoc)
lock.Unlock()
return
}
// Delete a document given its unique ID.
func (col *Col) Delete(id uint64) {
num := id % col.NumChunksI64
lock := col.ChunkMutexes[num]
dest := col.Chunks[num]
lock.Lock()
// Read back the original document
var oldDoc interface{}
physID, err := col.ReadNoLock(id, &oldDoc)
if err != nil {
physID, err = dest.GetPhysicalID(id)
if err != nil {
lock.Unlock()
return
}
}
// Remove the original document from secondary indexes
col.unindexDoc(id, oldDoc)
dest.Delete(physID)
lock.Unlock()
return
}
/* Sequentially deserialize all documents and invoke the function on each document (Collection Scan).
The function must not write to this collection. */
func (col *Col) ForAll(fun func(id uint64, doc map[string]interface{}) bool) {
numChunks := col.NumChunks
wg := &sync.WaitGroup{}
wg.Add(numChunks)
for i := 0; i < numChunks; i++ {
go func(i int) {
dest := col.Chunks[i]
lock := col.ChunkMutexes[i]
lock.RLock()
dest.ForAll(fun)
lock.RUnlock()
wg.Done()
}(i)
}
wg.Wait()
}
/* Sequentially deserialize all documents into the template (pointer to struct) and invoke the function on each document (Collection Scan).
The function must not write to this collection. */
func (col *Col) DeserializeAll(template interface{}, fun func() bool) {
numChunks := col.NumChunks
for i := 0; i < numChunks; i++ {
dest := col.Chunks[i]
lock := col.ChunkMutexes[i]
lock.RLock()
dest.DeserializeAll(template, fun)
lock.RUnlock()
}
}
// Flush collection data and index files.
func (col *Col) Flush() error {
// Close chunks
for _, dest := range col.Chunks {
if err := dest.Flush(); err != nil {
return err
}
}
// Flush secondary indexes
for _, index := range col.SecIndexes {
for _, part := range index {
if err := part.File.Flush(); err != nil {
return err
}
}
}
return nil
}
// Close the collection.
func (col *Col) Close() {
// Close chunks
for _, dest := range col.Chunks {
dest.Close()
}
// Close secondary indexes
for _, index := range col.SecIndexes {
for _, part := range index {
part.File.Close()
}
}
tdlog.Printf("Collection %s is closed", col.BaseDir)
}