-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrinzler_search.go
203 lines (190 loc) · 5.27 KB
/
rinzler_search.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
package rinzler
import (
"os"
"github.com/hashicorp/golang-lru"
"strings"
"encoding/binary"
_"fmt"
)
type BinarySearch struct {
fp *os.File
cache *lru.Cache
recordSize uint64
fieldSize uint64
numRecords uint64
cacheHits uint64
cacheMisses uint64
}
func (b *BinarySearch) CachePerformance() float64 {
return float64(b.cacheHits) / float64(b.cacheMisses) * 100
}
func (r *Rinzler) SearchLeftB(target int) int64 {
iStart := r.FileDescription.IndexStartPos
//iEnd := r.FileDescription.IndexEndPos
//fmt.Println("Index Start and End positions: ",iStart,iEnd)
//value,_ := b.cache.Get(target)
//if value != nil {
// b.cacheHits++
// return value.(int64)
//}
//b.cacheMisses++
var recordLength uint64 = 13
//record := make([]byte,b.fieldSize)
record := make([]byte,recordLength)
var min uint64 = 0
max := uint64((r.FileDescription.IndexEndPos - r.FileDescription.IndexStartPos) / uint64(recordLength))
//fmt.Println("Index Min and Max Records: ",min,max)
//max := int64(b.numRecords)
//target = strings.ToLower(target)
//var str string
var match bool
for min < max {
mean := uint64((min + max) / 2)
r.DataFile.Seek(int64(iStart + (mean*recordLength)),os.SEEK_SET)
_,err := r.DataFile.Read(record)
val := binary.LittleEndian.Uint64(record[5:])
zz := append(record[:5],[]byte{0,0,0}...)
//fmt.Println(zz)
pos := binary.LittleEndian.Uint64(zz)
_ = pos
if err != nil {
panic(err)
}
//fmt.Println(pos,val)
//str = strings.TrimRight(string(record),"\x00")
//str = strings.ToLower(str)
if target == int(val) {
match = true
max = mean
} else if target < int(val) {
max = mean
} else {
min = mean + 1
}
}
if match {
//b.cache.Add(target,min)
return int64(min)
}
//b.cache.Add(target,-1)
return -1
}
func (r *Rinzler) NewBinarySearch(filename string, recordSize uint64, fieldSize uint64) *BinarySearch {
var search BinarySearch
search.fp,_ = os.OpenFile(filename, os.O_RDONLY, 0644)
search.recordSize = recordSize
search.fieldSize = fieldSize
search.cache,_ = lru.New(100000)
fi, _ := search.fp.Stat()
fileSize := fi.Size()
search.numRecords = uint64(fileSize) / search.recordSize
return &search
}
func (b *BinarySearch) Search(target string) int64 {
value,_ := b.cache.Get(target)
if value != nil {
b.cacheHits++
return value.(int64)
}
b.cacheMisses++
record := make([]byte,b.fieldSize)
var min int64 = 0
max := int64(b.numRecords)
target = strings.ToLower(target)
for min <= max {
mean := int64((min + max) / 2)
b.fp.Seek(int64(uint64(mean) * b.recordSize),os.SEEK_SET)
_,err := b.fp.Read(record)
if err != nil {
panic(err)
}
str := strings.TrimRight(string(record),"\x00")
str = strings.ToLower(str)
if target == str {
b.cache.Add(target,mean)
return int64(mean)
} else if target > str {
min = mean + 1
} else if target < str {
max = mean - 1
}
}
b.cache.Add(target,-1)
return -1
}
func (b *BinarySearch) SearchLeft(target string) int64 {
value,_ := b.cache.Get(target)
if value != nil {
b.cacheHits++
return value.(int64)
}
b.cacheMisses++
record := make([]byte,b.fieldSize)
var min int64 = 0
max := int64(b.numRecords)
target = strings.ToLower(target)
var str string
var match bool
for min < max {
mean := int64((min + max) / 2)
b.fp.Seek(int64(uint64(mean) * b.recordSize),os.SEEK_SET)
_,err := b.fp.Read(record)
if err != nil {
panic(err)
}
str = strings.TrimRight(string(record),"\x00")
str = strings.ToLower(str)
if target == str {
match = true
max = mean
} else if target < str {
max = mean
} else {
min = mean + 1
}
}
if match {
b.cache.Add(target,min)
return min
}
b.cache.Add(target,-1)
return -1
}
func (b *BinarySearch) SearchRight(target string) int64 {
value,_ := b.cache.Get(target)
if value != nil {
b.cacheHits++
return value.(int64)
}
b.cacheMisses++
record := make([]byte,b.fieldSize)
var min int64 = 0
max := int64(b.numRecords)
target = strings.ToLower(target)
var str string
var match bool
for min < max {
mean := int64((min + max + 1) / 2)
b.fp.Seek(int64(uint64(mean) * b.recordSize),os.SEEK_SET)
_,err := b.fp.Read(record)
if err != nil {
panic(err)
}
str = strings.TrimRight(string(record),"\x00")
str = strings.ToLower(str)
if target == str {
match = true
min = mean
} else if target > str {
min = mean
} else {
max = mean - 1
}
}
if match {
b.cache.Add(target,max)
return max
}
b.cache.Add(target,-1)
return -1
}