-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck.go
105 lines (95 loc) · 2.48 KB
/
check.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
// Copyright (c) 2013 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package btree
import (
"os"
)
func (store *Store) check() bool {
wstore := store.wstore
freelist := wstore.freelist
rfd, _ := os.Open(wstore.Idxfile)
// Check whether configuration settings match.
fi, _ := rfd.Stat()
fpos_firstblock := (wstore.Sectorsize * 2) - (wstore.Flistsize * 2)
blksize := fi.Size() - fpos_firstblock
if fpos_firstblock != wstore.fpos_firstblock {
return false
}
if blksize%int64(wstore.Blocksize) != 0 {
return false
}
// Check freelist with btree.
root, _, _ := store.OpStart(false)
offs := root.listOffsets(store)
qsortOffsets(offs)
fulloffs := seq(wstore.fpos_firstblock, fi.Size(), int64(wstore.Blocksize))
offsets := make([]int64, 0, len(fulloffs))
i := 0
for _, x := range offs {
for i < len(fulloffs) {
if fulloffs[i] < x {
offsets = append(offsets, fulloffs[i])
} else if fulloffs[i] > x {
break
}
i++
}
}
for ; i < len(fulloffs); i++ {
offsets = append(offsets, fulloffs[i])
}
offsets = append(offsets, 0)
if len(offsets) != len(freelist.offsets) {
return false
}
count := 0
for _, offset := range offsets {
for _, floffset := range freelist.offsets {
if offset == floffset {
count += 1
break
}
}
}
if count != len(offsets) {
return false
}
return true
}
// Inplace quicksort
func qsortOffsets(arr []int64) {
if len(arr) <= 1 {
return
}
iPivot := qsort_partition(arr)
qsortOffsets(arr[:iPivot])
qsortOffsets(arr[iPivot-1:])
}
func qsort_partition(arr []int64) int {
swap := func(arr []int64, i1, i2 int) {
arr[i1], arr[i2] = arr[i2], arr[i1]
}
idx, lastidx := 0, len(arr)-1
pivot := arr[lastidx] // rightmost element
for i := 1; i < len(arr); i++ {
if arr[i] < pivot {
swap(arr, i, idx)
idx++
}
}
swap(arr, lastidx, idx)
return idx
}
func seq(start, end, step int64) []int64 {
out := make([]int64, 0, (end-start)/step)
for i := start; i < end; i += step {
out = append(out, i)
}
return out
}