-
Notifications
You must be signed in to change notification settings - Fork 8
/
record_index.go
195 lines (156 loc) · 3.53 KB
/
record_index.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
package file
import (
"bufio"
"bytes"
"encoding/gob"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"time"
"github.com/delving/hub3/ikuzo/service/x/oaipmh"
)
type metrics struct {
Records int64
Deleted int64
EarliestDate time.Time
LatestDate time.Time
}
type index struct {
OrgID string
DatasetID string
DataSets []oaipmh.Set // TODO(kiivihal): maybe one
AllowedFormats []oaipmh.MetadataFormat
SourceFormat SourceFormat
SourceExtension string
lookUp map[string]*recordPointer
records []*recordPointer // sorted by last modified
files map[string]*os.File
m *metrics
}
/*
use generic bucket (gocloud) to store all data to disk
this makes it possible to run this in a loadbalanced mode possible
*/
func newIndex(orgID, datasetID string, format SourceFormat) (*index, error) {
idx := &index{
OrgID: orgID,
DatasetID: datasetID,
SourceFormat: format,
SourceExtension: "",
lookUp: map[string]*recordPointer{},
files: map[string]*os.File{},
}
switch format {
case FormatRaw:
case FormatNTriples:
case FormatEAD:
default:
return nil, fmt.Errorf("unsupported source format: %s", format)
}
return idx, nil
}
func (idx *index) Close() error {
for _, f := range idx.files {
if err := f.Close(); err != nil {
return err
}
}
return nil
}
func (idx *index) sortRecords() {
sort.Slice(idx.records, func(i, j int) bool {
return idx.records[i].LastModified.Before(idx.records[j].LastModified)
})
}
func (idx *index) ParseNarthexNtriples(r io.Reader, fname, fhash string) error {
var (
lines int
offset int
recordOffset int
recordLength int
recordStartLine int
records int
)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines++
line := scanner.Bytes()
if len(line) == 0 {
offset++ // empty line still has \n
continue
}
if IsRecordSeparator(line) {
records++
sep, err := NewRecordSeparator(line)
if err != nil {
return err
}
rp := sep.newRecordPointer(fname, fhash)
rp.Offset = int64(recordOffset)
rp.Length = int64(recordLength)
rp.StartLine = int64(recordStartLine)
rp.Lines = int64(lines - recordStartLine)
idx.records = append(idx.records, rp)
idx.lookUp[sep.HubID] = rp
recordOffset += (recordLength + 1 + len(line))
recordStartLine = lines + 1
recordLength = 0
continue
}
offset += len(line) + 1
recordLength += len(line) + 1
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func readIndex(orgID, datasetID string) (*index, error) {
path := filepath.Join(orgID, datasetID, configDirFname, indexFname)
f, err := os.Open(path)
if err != nil {
return nil, err
}
idx, err := decodeIndex(f)
if err != nil {
return nil, err
}
return idx, nil
}
func (idx *index) Write() error {
dirPath := filepath.Join(idx.OrgID, idx.DatasetID, configDirFname)
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return err
}
var buf bytes.Buffer
err = idx.encode(&buf)
if err != nil {
return err
}
path := filepath.Join(dirPath, indexFname)
return os.WriteFile(
path,
buf.Bytes(),
os.ModePerm,
)
}
func (idx *index) encode(w io.Writer) error {
e := gob.NewEncoder(w)
err := e.Encode(idx)
if err != nil {
return fmt.Errorf("unable to marshal file.index; %w", err)
}
return nil
}
func decodeIndex(r io.Reader) (*index, error) {
var idx index
d := gob.NewDecoder(r)
err := d.Decode(&idx)
if err != nil {
return nil, err
}
return &idx, nil
}