forked from HouzuoGuo/tiedot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition.go
168 lines (142 loc) · 4.18 KB
/
partition.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
/*
(Collection) Partition is a collection data file accompanied by a hash table in order to allow addressing of a
document using an unchanging ID:
The hash table stores the unchanging ID as entry key and the physical document location as entry value.
*/
package data
import (
"sync"
"github.com/HouzuoGuo/tiedot/dberr"
"github.com/HouzuoGuo/tiedot/tdlog"
)
// Partition associates a hash table with collection documents, allowing addressing of a document using an unchanging ID.
type Partition struct {
col *Collection
lookup *HashTable
updating map[int]struct{}
Lock *sync.RWMutex
}
// Open a collection partition.
func OpenPartition(colPath, lookupPath string) (part *Partition, err error) {
part = &Partition{updating: make(map[int]struct{}), Lock: new(sync.RWMutex)}
if part.col, err = OpenCollection(colPath); err != nil {
return
} else if part.lookup, err = OpenHashTable(lookupPath); err != nil {
return
}
return
}
// Insert a document. The ID may be used to retrieve/update/delete the document later on.
func (part *Partition) Insert(id int, data []byte) (physID int, err error) {
physID, err = part.col.Insert(data)
if err != nil {
return
}
part.lookup.Put(id, physID)
return
}
// Find and retrieve a document by ID.
func (part *Partition) Read(id int) ([]byte, error) {
physID := part.lookup.Get(id, 1)
if len(physID) == 0 {
return nil, dberr.New(dberr.ErrorNoDoc, id)
}
data := part.col.Read(physID[0])
if data == nil {
return nil, dberr.New(dberr.ErrorNoDoc, id)
}
return data, nil
}
// Update a document.
func (part *Partition) Update(id int, data []byte) (err error) {
physID := part.lookup.Get(id, 1)
if len(physID) == 0 {
return dberr.New(dberr.ErrorNoDoc, id)
}
newID, err := part.col.Update(physID[0], data)
if err != nil {
return
}
if newID != physID[0] {
part.lookup.Remove(id, physID[0])
part.lookup.Put(id, newID)
}
return
}
// Lock a document for exclusive update.
func (part *Partition) LockUpdate(id int) (err error) {
if _, alreadyLocked := part.updating[id]; alreadyLocked {
return dberr.New(dberr.ErrorDocLocked, id)
}
part.updating[id] = struct{}{}
return
}
// Unlock a document to make it ready for the next update.
func (part *Partition) UnlockUpdate(id int) {
delete(part.updating, id)
}
// Delete a document.
func (part *Partition) Delete(id int) (err error) {
physID := part.lookup.Get(id, 1)
if len(physID) == 0 {
return dberr.New(dberr.ErrorNoDoc, id)
}
part.col.Delete(physID[0])
part.lookup.Remove(id, physID[0])
return
}
// Partition documents into roughly equally sized portions, and run the function on every document in the portion.
func (part *Partition) ForEachDoc(partNum, totalPart int, fun func(id int, doc []byte) bool) (moveOn bool) {
ids, physIDs := part.lookup.GetPartition(partNum, totalPart)
for i, id := range ids {
data := part.col.Read(physIDs[i])
if data != nil {
if !fun(id, data) {
return false
}
}
}
return true
}
// Return approximate number of documents in the partition.
func (part *Partition) ApproxDocCount() int {
totalPart := 24 // not magic; a larger number makes estimation less accurate, but improves performance
for {
keys, _ := part.lookup.GetPartition(0, totalPart)
if len(keys) == 0 {
if totalPart < 8 {
return 0 // the hash table is really really empty
}
// Try a larger partition size
totalPart = totalPart / 2
} else {
return int(float64(len(keys)) * float64(totalPart))
}
}
}
// Clear data file and lookup hash table.
func (part *Partition) Clear() error {
var err error
if e := part.col.Clear(); e != nil {
tdlog.CritNoRepeat("Failed to clear %s: %v", part.col.Path, e)
err = dberr.New(dberr.ErrorIO)
}
if e := part.lookup.Clear(); e != nil {
tdlog.CritNoRepeat("Failed to clear %s: %v", part.lookup.Path, e)
err = dberr.New(dberr.ErrorIO)
}
return err
}
// Close all file handles.
func (part *Partition) Close() error {
var err error
if e := part.col.Close(); e != nil {
tdlog.CritNoRepeat("Failed to close %s: %v", part.col.Path, e)
err = dberr.New(dberr.ErrorIO)
}
if e := part.lookup.Close(); e != nil {
tdlog.CritNoRepeat("Failed to close %s: %v", part.lookup.Path, e)
err = dberr.New(dberr.ErrorIO)
}
return err
}