/
compound.go
291 lines (262 loc) · 7.94 KB
/
compound.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package store
import (
"errors"
"fmt"
"github.com/rizwan-amir123/golucene/core/codec"
"github.com/rizwan-amir123/golucene/core/util"
"reflect"
"sync"
)
// store/CompoundFileDirectory.java
type FileSlice struct {
offset, length int64
}
const (
CFD_DATA_CODEC = "CompoundFileWriterData"
CFD_VERSION_START = 0
CFD_VERSION_CHECKSUM = 1
CFD_VERSION_CURRENT = CFD_VERSION_CHECKSUM
CFD_ENTRY_CODEC = "CompoundFileWriterEntries"
COMPOUND_FILE_EXTENSION = "cfs"
COMPOUND_FILE_ENTRIES_EXTENSION = "cfe"
)
var SENTINEL = make(map[string]FileSlice)
type CompoundFileDirectory struct {
*DirectoryImpl
*BaseDirectory
sync.Locker
directory Directory
fileName string
readBufferSize int
entries map[string]FileSlice
openForWrite bool
writer *CompoundFileWriter
handle IndexInput
version int
}
func NewCompoundFileDirectory(directory Directory, fileName string, context IOContext, openForWrite bool) (d *CompoundFileDirectory, err error) {
self := &CompoundFileDirectory{
Locker: &sync.Mutex{},
directory: directory,
fileName: fileName,
readBufferSize: bufferSize(context),
openForWrite: openForWrite}
self.DirectoryImpl = NewDirectoryImpl(self)
self.BaseDirectory = NewBaseDirectory(self)
if !openForWrite {
// log.Printf("Open for read.")
success := false
defer func() {
if !success {
util.CloseWhileSuppressingError(self.handle)
}
}()
self.handle, err = directory.OpenInput(fileName, context)
if err != nil {
return nil, err
}
self.entries, err = self.readEntries(self.handle, directory, fileName)
if err != nil {
return nil, err
}
if self.version >= CFD_VERSION_CHECKSUM {
if _, err = codec.CheckHeader(self.handle, CFD_DATA_CODEC,
int32(self.version), int32(self.version)); err != nil {
return nil, err
}
// NOTE: data file is too costly to verify checksum against all the
// bytes on open, but for now we at least verify proper structure
// of the checksum footer: which looks for FOOTER_MAGIC +
// algorithmID. This is cheap and can detect some forms of
// corruption such as file trucation.
if _, err = codec.RetrieveChecksum(self.handle); err != nil {
return nil, err
}
}
success = true
self.BaseDirectory.IsOpen = true
return self, nil
} else {
assert2(reflect.TypeOf(directory).Name() != "CompoundFileDirectory",
"compound file inside of compound file: %v", fileName)
self.entries = SENTINEL
self.IsOpen = true
self.writer = newCompoundFileWriter(directory, fileName)
self.handle = nil
return self, nil
}
}
func (d *CompoundFileDirectory) Close() error {
d.Lock() // syncronized
defer d.Unlock()
// fmt.Printf("Closing %v...\n", d)
if !d.IsOpen {
fmt.Println("CompoundFileDirectory is already closed.")
// allow double close - usually to be consistent with other closeables
return nil // already closed
}
d.IsOpen = false
if d.writer != nil {
assert(d.openForWrite)
return d.writer.Close()
} else {
return util.Close(d.handle)
}
}
func (d *CompoundFileDirectory) OpenInput(name string, context IOContext) (in IndexInput, err error) {
d.Lock() // synchronized
defer d.Unlock()
d.EnsureOpen()
assert(!d.openForWrite)
id := util.StripSegmentName(name)
if entry, ok := d.entries[id]; ok {
return d.handle.Slice(name, entry.offset, entry.length)
}
keys := make([]string, 0)
for k := range d.entries {
keys = append(keys, k)
}
panic(fmt.Sprintf("No sub-file with id %v found (fileName=%v files: %v)", id, name, keys))
}
func (d *CompoundFileDirectory) ListAll() (paths []string, err error) {
d.EnsureOpen()
// if self.writer != nil {
// return self.writer.ListAll()
// }
// Add the segment name
seg := util.ParseSegmentName(d.fileName)
keys := make([]string, 0, len(d.entries))
for k := range d.entries {
keys = append(keys, seg+k)
}
return keys, nil
}
func (d *CompoundFileDirectory) FileExists(name string) bool {
d.EnsureOpen()
// if d.writer != nil {
// return d.writer.FileExists(name)
// }
_, ok := d.entries[util.StripSegmentName(name)]
return ok
}
func (d *CompoundFileDirectory) DeleteFile(name string) error {
panic("not supported")
}
// Returns the length of a file in the directory.
func (d *CompoundFileDirectory) FileLength(name string) (n int64, err error) {
panic("not implemented yet")
}
func (d *CompoundFileDirectory) CreateOutput(name string, context IOContext) (out IndexOutput, err error) {
d.EnsureOpen()
return d.writer.createOutput(name, context)
}
func (d *CompoundFileDirectory) Sync(names []string) error {
panic("not supported")
}
func (d *CompoundFileDirectory) MakeLock(name string) Lock {
panic("not supported by CFS")
}
func (d *CompoundFileDirectory) String() string {
return fmt.Sprintf("CompoundFileDirectory(file='%v' in dir=%v)", d.fileName, d.directory)
}
const (
CODEC_MAGIC_BYTE1 = byte(uint32(codec.CODEC_MAGIC) >> 24 & 0xFF)
CODEC_MAGIC_BYTE2 = byte(uint32(codec.CODEC_MAGIC) >> 16 & 0xFF)
CODEC_MAGIC_BYTE3 = byte(uint32(codec.CODEC_MAGIC) >> 8 & 0xFF)
CODEC_MAGIC_BYTE4 = byte(codec.CODEC_MAGIC & 0xFF)
)
func (d *CompoundFileDirectory) readEntries(handle IndexInput, dir Directory, name string) (mapping map[string]FileSlice, err error) {
var stream IndexInput = nil
var entriesStream ChecksumIndexInput = nil
// read the first VInt. If it is negative, it's the version number
// otherwise it's the count (pre-3.1 indexes)
var success = false
defer func() {
if success {
err = util.Close(stream, entriesStream)
} else {
util.CloseWhileSuppressingError(stream, entriesStream)
}
}()
stream = handle.Clone()
// fmt.Printf("Reading from stream: %v\n", stream)
firstInt, err := stream.ReadVInt()
if err != nil {
return nil, err
}
// impossible for 3.0 to have 63 files in a .cfs, CFS writer was not visible
// and separate norms/etc are outside of cfs.
if firstInt == int32(CODEC_MAGIC_BYTE1) {
if secondByte, err := stream.ReadByte(); err == nil {
if thirdByte, err := stream.ReadByte(); err == nil {
if fourthByte, err := stream.ReadByte(); err == nil {
if secondByte != CODEC_MAGIC_BYTE2 ||
thirdByte != CODEC_MAGIC_BYTE3 ||
fourthByte != CODEC_MAGIC_BYTE4 {
return nil, errors.New(fmt.Sprintf(
"Illegal/impossible header for CFS file: %v,%v,%v",
secondByte, thirdByte, fourthByte))
}
}
}
}
if err != nil {
return nil, err
}
d.version, err = int32ToInt(codec.CheckHeaderNoMagic(stream, CFD_DATA_CODEC, CFD_VERSION_START, CFD_VERSION_CURRENT))
if err != nil {
return nil, err
}
entriesFileName := util.SegmentFileName(util.StripExtension(name), "", COMPOUND_FILE_ENTRIES_EXTENSION)
entriesStream, err = dir.OpenChecksumInput(entriesFileName, IO_CONTEXT_READONCE)
if err != nil {
return nil, err
}
_, err = codec.CheckHeader(entriesStream, CFD_ENTRY_CODEC, CFD_VERSION_START, CFD_VERSION_CURRENT)
if err != nil {
return nil, err
}
numEntries, err := entriesStream.ReadVInt()
if err != nil {
return nil, err
}
mapping = make(map[string]FileSlice)
// fmt.Printf("Entries number: %v\n", numEntries)
for i := int32(0); i < numEntries; i++ {
id, err := entriesStream.ReadString()
if err != nil {
return nil, err
}
if _, ok := mapping[id]; ok {
return nil, errors.New(fmt.Sprintf(
"Duplicate cfs entry id=%v in CFS: %v", id, entriesStream))
}
// log.Printf("Found entry: %v", id)
offset, err := entriesStream.ReadLong()
if err != nil {
return nil, err
}
length, err := entriesStream.ReadLong()
if err != nil {
return nil, err
}
mapping[id] = FileSlice{offset, length}
}
if d.version >= CFD_VERSION_CHECKSUM {
_, err = codec.CheckFooter(entriesStream)
} else {
err = codec.CheckEOF(entriesStream)
}
if err != nil {
return nil, err
}
} else {
// TODO remove once 3.x is not supported anymore
panic("not supported yet; will also be obsolete soon")
}
success = true
return mapping, nil
}
func int32ToInt(n int32, err error) (int, error) {
return int(n), err
}