-
Notifications
You must be signed in to change notification settings - Fork 0
/
racorpus.go
150 lines (135 loc) · 2.94 KB
/
racorpus.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
package compactcorpus
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
)
type RaCorpus struct {
opened bool
name string
names []string
values [][2]int64
idx map[string]int
fp *os.File
dz *reader
}
type RaRange struct {
c *RaCorpus
idx int
}
// Return an iterator for the corpus
func (c *RaCorpus) NewRange() (r *RaRange, err error) {
if !c.opened {
err = fmt.Errorf("Corpus '%v' is closed", c.name)
return
}
r = &RaRange{
c: c,
idx: 0,
}
return
}
// Return true if there are more items available
func (r *RaRange) HasNext() bool {
return r.idx < len(r.c.idx)
}
// Return the next available item
func (r *RaRange) Next() (name string, xml []byte, err error) {
if r.idx == len(r.c.idx) {
return
}
name = r.c.names[r.idx]
v := r.c.values[r.idx]
xml, err = r.c.dz.get(v[0], v[1])
r.idx++
return
}
// Get a named item from the corpus
func (c *RaCorpus) Get(name string) (xml []byte, err error) {
if !c.opened {
err = fmt.Errorf("Corpus '%v' is closed", c.name)
return
}
i, ok := c.idx[name]
if !ok {
err = fmt.Errorf("Item '%v' not found in corpus '%v'", c.name, name)
return
}
v := c.values[i]
xml, err = c.dz.get(v[0], v[1])
return
}
// Close the corpus. This is necessary for freeing files
func (c *RaCorpus) Close() {
if c.opened {
c.opened = false
c.fp.Close()
}
}
// Open a compact corpus for reading in random access mode.
//
// You need to call Close() when you are done.
//
// Use this version if you want to retrieve only a few items from the
// corpus, or if you don't have enough memory to unpack the complete corpus
// into memory.
// Otherwise, use: Open()
func RaOpen(name string) (corpus *RaCorpus, err error) {
corpus = &RaCorpus{
names: make([]string, 0),
values: make([][2]int64, 0),
idx: make(map[string]int),
}
name = root(name)
corpus.name = name
var fp *os.File
curfile := name + ".index"
fp, err = os.Open(curfile)
if err != nil {
return
}
defer fp.Close()
lineno := 0
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
var offset, size uint64
line := scanner.Text()
lineno++
a := strings.Fields(line)
if len(a) != 3 {
err = fmt.Errorf("Invalid number of fields in file '%s.index', line %d", curfile, lineno)
return
}
corpus.idx[a[0]] = len(corpus.names)
corpus.names = append(corpus.names, a[0])
offset, err = decode(a[1])
if err != nil {
err = fmt.Errorf("%v in file '%s.index', line %d", curfile, lineno)
return
}
size, err = decode(a[2])
if err != nil {
err = fmt.Errorf("%v in file '%s.index', line %d", curfile, lineno)
return
}
corpus.values = append(corpus.values, [2]int64{int64(offset), int64(size)})
}
if err = scanner.Err(); err != nil {
return
}
curfile = name + ".data.dz"
corpus.fp, err = os.Open(curfile)
if err != nil {
return
}
corpus.dz, err = newReader(corpus.fp)
if err != nil {
corpus.fp.Close()
return
}
corpus.opened = true
runtime.SetFinalizer(corpus, (*RaCorpus).Close)
return
}