forked from sokrypton/ColabFold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbreader.go
100 lines (85 loc) · 1.73 KB
/
dbreader.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
package main
import (
"bufio"
"math"
"os"
"sort"
)
type Entry struct {
Key string
Offset uint64
Length uint64
}
type Index []Entry
func (a Index) Len() int { return len(a) }
func (a Index) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Index) Less(i, j int) bool { return a[i].Key < a[j].Key }
type Reader struct {
Index Index
file *os.File
}
func (d *Reader) Make(data string, index string) {
file, err := os.Open(data)
if err != nil {
return
}
d.file = file
f, err := os.Open(index)
if err != nil {
return
}
defer f.Close()
d.Index = make([]Entry, 0)
entry := Entry{}
parser := NewTsvParser(bufio.NewReader(f), &entry)
for {
eof, err := parser.Next()
if eof {
break
}
if err != nil {
return
}
d.Index = append(d.Index, entry)
}
sort.Sort(Index(d.Index))
}
func (d *Reader) Delete() {
d.file.Close()
}
func (d *Reader) Id(key string) (int64, bool) {
i := int64(sort.Search(int(d.Size()), func(i int) bool {
return d.Index[i].Key >= key
}))
return i, i < d.Size() && d.Index[i].Key == key
}
func (d *Reader) Key(id int64) (string, bool) {
if id < 0 || id >= d.Size() {
return "", false
}
return d.Index[id].Key, true
}
func (d *Reader) Offset(id int64) uint64 {
if id < 0 || id >= d.Size() {
return math.MaxUint64
}
return d.Index[id].Offset
}
func (d *Reader) Length(id int64) uint64 {
if id < 0 || id >= d.Size() {
return math.MaxUint64
}
return d.Index[id].Length
}
func (d *Reader) Data(id int64) string {
if id < 0 || id >= d.Size() {
return ""
}
length := d.Index[id].Length - 1
buffer := make([]byte, length)
d.file.ReadAt(buffer, int64(d.Index[id].Offset))
return string(buffer[:length])
}
func (d *Reader) Size() int64 {
return int64(len(d.Index))
}