forked from deepfabric/bkdtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bkdtree_insert.go
328 lines (303 loc) · 7.73 KB
/
bkdtree_insert.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
package bkdtree
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"unsafe"
"github.com/pkg/errors"
)
//Insert inserts given point. Fail if the tree is full.
func (bkd *BkdTree) Insert(point Point) (err error) {
bkd.rwlock.Lock()
defer bkd.rwlock.Unlock()
if !bkd.open {
err = errors.Errorf("(*BkdTree).Inset is not allowed at closed state")
return
}
//insert into in-memory buffer t0m. If t0m is not full, return.
bkd.insertT0M(point)
bkd.NumPoints++
if int(bkd.t0m.meta.NumPoints) < bkd.t0mCap {
return
}
//find the smallest index k in [0, len(trees)) at which trees[k] is empty, or its capacity is no less than the sum of size of t0m + trees[0:k+1]
k := bkd.getMinCompactPos()
if k == len(bkd.trees) {
kd := BkdSubTree{
meta: KdTreeExtMeta{
PointsOffEnd: 0,
RootOff: 0,
NumPoints: 0,
LeafCap: uint16(bkd.leafCap),
IntraCap: uint16(bkd.intraCap),
NumDims: uint8(bkd.NumDims),
BytesPerDim: uint8(bkd.BytesPerDim),
PointSize: uint8(bkd.pointSize),
FormatVer: 0,
},
}
bkd.trees = append(bkd.trees, kd)
}
err = bkd.compactTo(k)
return
}
//caclulate the min compoint position. Returns len(bkd.trees) if not found.
func (bkd *BkdTree) getMinCompactPos() (k int) {
//find the smallest index k in [0, len(trees)) at which trees[k] is empty, or its capacity is no less than the sum of size of t0m + trees[0:k+1]
sum := int(bkd.t0m.meta.NumPoints)
for k = 0; k < len(bkd.trees); k++ {
if bkd.trees[k].meta.NumPoints == 0 {
return
}
sum += int(bkd.trees[k].meta.NumPoints)
capK := bkd.t0mCap << uint(k)
if capK >= sum {
return
}
}
return
}
//compact T0M and trees[0:k+1] into tree[k]. Assumes write lock has been acquired.
func (bkd *BkdTree) compactTo(k int) (err error) {
//extract all points from t0m and trees[0:k+1] into a file F
fpK := bkd.TiPath(k)
tmpFpK := fpK + ".tmp"
tmpFK, err := os.OpenFile(tmpFpK, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
err = errors.Wrap(err, "")
return
}
defer tmpFK.Close()
err = bkd.extractT0M(tmpFK)
if err != nil {
return
}
for i := 0; i <= k; i++ {
err = bkd.extractTi(tmpFK, i)
if err != nil {
return
}
}
meta, err := bkd.bulkLoad(tmpFK)
if err != nil {
return
}
//empty T0M and Ti, 0<=i<k
bkd.clearT0M()
for i := 0; i <= k; i++ {
if bkd.trees[i].meta.NumPoints <= 0 {
continue
} else if err = FileMunmap(bkd.trees[i].data); err != nil {
return
} else if err = bkd.trees[i].f.Close(); err != nil {
err = errors.Wrap(err, "")
return
} else if err = os.Remove(bkd.trees[i].f.Name()); err != nil {
err = errors.Wrap(err, "")
return
}
bkd.trees[i].meta.NumPoints = 0
}
if err = os.Rename(tmpFpK, fpK); err != nil {
err = errors.Wrap(err, "")
return
}
fK, err := os.OpenFile(fpK, os.O_RDWR, 0600)
if err != nil {
err = errors.Wrap(err, "")
return
}
data, err := FileMmap(fK)
if err != nil {
return
}
bkd.trees[k] = BkdSubTree{
meta: *meta,
f: fK,
data: data,
}
return
}
func writeMetaNumPoints(data []byte, meta *KdTreeExtMeta) {
off := len(data) - KdTreeExtMetaSize
off += int(unsafe.Offsetof(meta.NumPoints))
binary.BigEndian.PutUint64(data[off:], meta.NumPoints)
}
func (bkd *BkdTree) insertT0M(point Point) {
pae := PointArrayExt{
data: bkd.t0m.data,
numPoints: int(bkd.t0m.meta.NumPoints),
byDim: 0, //not used
bytesPerDim: bkd.BytesPerDim,
numDims: bkd.NumDims,
pointSize: bkd.pointSize,
}
pae.Append(point)
bkd.t0m.meta.NumPoints++
writeMetaNumPoints(bkd.t0m.data, &bkd.t0m.meta)
}
func (bkd *BkdTree) clearT0M() {
bkd.t0m.meta.NumPoints = 0
writeMetaNumPoints(bkd.t0m.data, &bkd.t0m.meta)
}
func (bkd *BkdTree) extractT0M(tmpF *os.File) (err error) {
size := int(bkd.t0m.meta.NumPoints) * bkd.pointSize
_, err = tmpF.Write(bkd.t0m.data[:size])
if err != nil {
err = errors.Wrap(err, "")
return
}
return
}
func (bkd *BkdTree) extractTi(dstF *os.File, idx int) (err error) {
if bkd.trees[idx].meta.NumPoints <= 0 {
return
}
srcF, err := os.Open(bkd.TiPath(idx))
if err != nil {
err = errors.Wrap(err, "")
return
}
defer srcF.Close()
//depth-first extracting from the root node
meta := &bkd.trees[idx].meta
err = bkd.extractNode(dstF, bkd.trees[idx].data, meta, int(meta.RootOff))
return
}
func (bkd *BkdTree) extractNode(dstF *os.File, data []byte, meta *KdTreeExtMeta, nodeOffset int) (err error) {
var node KdTreeExtIntraNode
bf := bytes.NewReader(data[nodeOffset:])
err = node.Read(bf)
if err != nil {
err = errors.Wrap(err, "")
return
}
for _, child := range node.Children {
if child.Offset < meta.PointsOffEnd {
//leaf node
length := int(child.NumPoints) * int(meta.PointSize)
_, err = dstF.Write(data[int(child.Offset) : int(child.Offset)+length])
if err != nil {
err = errors.Wrap(err, "")
return
}
} else {
//intra node
err = bkd.extractNode(dstF, data, meta, int(child.Offset))
if err != nil {
return
}
}
}
return
}
func (bkd *BkdTree) bulkLoad(tmpF *os.File) (meta *KdTreeExtMeta, err error) {
pointsOffEnd, err := tmpF.Seek(0, 1) //get current position
if err != nil {
err = errors.Wrap(err, "")
return
}
var data []byte
if data, err = FileMmap(tmpF); err != nil {
return
}
defer FileMunmap(data)
numPoints := int(pointsOffEnd / int64(bkd.pointSize))
rootOff, err1 := bkd.createKdTreeExt(tmpF, data, 0, numPoints, 0)
if err1 != nil {
err = err1
return
}
//record meta info at end
meta = &KdTreeExtMeta{
PointsOffEnd: uint64(pointsOffEnd),
RootOff: uint64(rootOff),
NumPoints: uint64(numPoints),
LeafCap: uint16(bkd.leafCap),
IntraCap: uint16(bkd.intraCap),
NumDims: uint8(bkd.NumDims),
BytesPerDim: uint8(bkd.BytesPerDim),
PointSize: uint8(bkd.pointSize),
FormatVer: 0,
}
err = binary.Write(tmpF, binary.BigEndian, meta)
if err != nil {
err = errors.Wrap(err, "")
return
}
return
}
func getCurrentOffset(f *os.File) (offset int64, err error) {
offset, err = f.Seek(0, 1) //get current position
if err != nil {
err = errors.Wrap(err, "")
return
}
return
}
func (bkd *BkdTree) createKdTreeExt(tmpF *os.File, data []byte, begin, end, depth int) (offset int64, err error) {
if begin >= end {
err = errors.New(fmt.Sprintf("assertion begin>=end failed, begin %v, end %v", begin, end))
return
}
splitDim := depth % bkd.NumDims
numStrips := (end - begin + bkd.leafCap - 1) / bkd.leafCap
if numStrips > bkd.intraCap {
numStrips = bkd.intraCap
}
pae := PointArrayExt{
data: data[begin*bkd.pointSize:],
numPoints: end - begin,
byDim: splitDim,
bytesPerDim: bkd.BytesPerDim,
numDims: bkd.NumDims,
pointSize: bkd.pointSize,
}
splitValues, splitPoses := SplitPoints(&pae, numStrips)
children := make([]KdTreeExtNodeInfo, 0, numStrips)
var childOffset int64
for strip := 0; strip < numStrips; strip++ {
posBegin := begin
if strip != 0 {
posBegin = begin + splitPoses[strip-1]
}
posEnd := end
if strip != numStrips-1 {
posEnd = begin + splitPoses[strip]
}
if posEnd-posBegin <= bkd.leafCap {
info := KdTreeExtNodeInfo{
Offset: uint64(posBegin * bkd.pointSize),
NumPoints: uint64(posEnd - posBegin),
}
children = append(children, info)
} else {
childOffset, err = bkd.createKdTreeExt(tmpF, data, posBegin, posEnd, depth+1)
if err != nil {
return
}
info := KdTreeExtNodeInfo{
Offset: uint64(childOffset),
NumPoints: uint64(posEnd - posBegin),
}
children = append(children, info)
}
}
offset, err = getCurrentOffset(tmpF)
if err != nil {
return
}
node := &KdTreeExtIntraNode{
SplitDim: uint32(splitDim),
NumStrips: uint32(numStrips),
SplitValues: splitValues,
Children: children,
}
err = node.Write(tmpF)
if err != nil {
err = errors.Wrap(err, "")
return
}
return
}