summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/index/CompoundFile.cpp
blob: 5991b354214fc24433e3ae630320ba1a51098a23 (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
* 
* Distributable under the terms of either the Apache License (Version 2.0) or 
* the GNU Lesser General Public License, as specified in the COPYING file.
*
* Changes are Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
------------------------------------------------------------------------------*/
#include "CLucene/StdHeader.h"
#include "CompoundFile.h"
#include "CLucene/util/Misc.h"

CL_NS_USE(store)
CL_NS_USE(util)
CL_NS_DEF(index)

CompoundFileReader::CSIndexInput::CSIndexInput(CL_NS(store)::IndexInput* base,
    const int64_t fileOffset, const int64_t length)
{
    this->base = base;
    this->fileOffset = fileOffset;
    this->_length = length;
}

void CompoundFileReader::CSIndexInput::readInternal(uint8_t* b, const int32_t len)
{
    SCOPED_LOCK_MUTEX(base->THIS_LOCK)

    int64_t start = getFilePointer();
    if(start + len > _length)
        _CLTHROWA(CL_ERR_IO, "read past EOF");
    base->seek(fileOffset + start);
    base->readBytes(b, len);
}

CompoundFileReader::CSIndexInput::~CSIndexInput()
{
}

IndexInput* CompoundFileReader::CSIndexInput::clone() const
{
    return _CLNEW CSIndexInput(*this);
}

CompoundFileReader::CSIndexInput::CSIndexInput(const CSIndexInput& clone)
    : BufferedIndexInput(clone)
{
    this->base = clone.base; //no need to clone this..
    this->fileOffset = clone.fileOffset;
    this->_length = clone._length;
}

void CompoundFileReader::CSIndexInput::close()
{
}

CompoundFileReader::CompoundFileReader(Directory* dir, const QString& name)
    : entries(false, true)
{
    directory = dir;
    fileName = name;

    bool success = false;
    try {
        stream = dir->openInput(name);

        // read the directory and init files
        int32_t count = stream->readVInt();
        FileEntry* entry = NULL;
        TCHAR tid[CL_MAX_PATH];
        for (int32_t i = 0; i < count; i++) {
            int64_t offset = stream->readLong();
            int32_t read = stream->readString(tid, CL_MAX_PATH);
            QString aid(QString::fromWCharArray(tid, read));

            // set length of the previous entry
            if (entry != NULL)
                entry->length = offset - entry->offset;

            entry = _CLNEW FileEntry(offset);
            entries.put(aid, entry);
        }

        // set the length of the final entry
        if (entry != NULL)
            entry->length = stream->length() - entry->offset;
        success = true;
    } _CLFINALLY (
        if (!success && (stream != NULL)) {
            try {
                stream->close();
                _CLDELETE(stream);
            } catch (CLuceneError& err) {
                if (err.number() != CL_ERR_IO)
                    throw err;
            }
        }
    )
}

CompoundFileReader::~CompoundFileReader()
{
    close();
}

Directory* CompoundFileReader::getDirectory()
{
    return directory;
}

QString CompoundFileReader::getName() const
{
    return fileName;
}

void CompoundFileReader::close()
{
    SCOPED_LOCK_MUTEX(THIS_LOCK)

    if (stream != NULL) {
        entries.clear();
        stream->close();
        _CLDELETE(stream);
    }
}

IndexInput* CompoundFileReader::openInput(const QString& id)
{
    SCOPED_LOCK_MUTEX(THIS_LOCK)

    if (stream == NULL)
        _CLTHROWA(CL_ERR_IO, "Stream closed");

    const FileEntry* entry = entries.get(id);
    if (entry == NULL) {
        char buf[CL_MAX_PATH + 30];
        strcpy(buf,"No sub-file with id ");
        strncat(buf, id.toLocal8Bit().constData(), CL_MAX_PATH);
        strcat(buf, " found");
        _CLTHROWA(CL_ERR_IO,buf);
    }
    return _CLNEW CSIndexInput(stream, entry->offset, entry->length);
}

QStringList CompoundFileReader::list() const
{
    // for (EntriesType::const_iterator i=entries.begin();i!=entries.end();i++){
    //     names->push_back(i->first);
    //     ++i;
    // }

    QStringList names;
    EntriesType::const_iterator itr;
    // TODO: verify this, see old code above ???
    for (itr = entries.begin(); itr != entries.end(); ++itr)
        names.push_back(itr->first);

    return names;
}

bool CompoundFileReader::fileExists(const QString& name) const
{
    return entries.exists(name);
}

int64_t CompoundFileReader::fileModified(const QString& name) const
{
    return directory->fileModified(fileName);
}

void CompoundFileReader::touchFile(const QString& name)
{
    directory->touchFile(fileName);
}

bool CompoundFileReader::doDeleteFile(const QString& name)
{
    _CLTHROWA(CL_ERR_UnsupportedOperation,
        "UnsupportedOperationException: CompoundFileReader::doDeleteFile");
}

void CompoundFileReader::renameFile(const QString& from, const QString& to)
{
    _CLTHROWA(CL_ERR_UnsupportedOperation,
        "UnsupportedOperationException: CompoundFileReader::renameFile");
}

int64_t CompoundFileReader::fileLength(const QString& name) const
{
    FileEntry* e = entries.get(name);
    if (e == NULL) {
        char buf[CL_MAX_PATH + 30];
        strcpy(buf,"File ");
        strncat(buf, name.toLocal8Bit().constData(), CL_MAX_PATH);
        strcat(buf," does not exist");
        _CLTHROWA(CL_ERR_IO,buf);
    }
    return e->length;
}

IndexOutput* CompoundFileReader::createOutput(const QString& name)
{
    _CLTHROWA(CL_ERR_UnsupportedOperation,
        "UnsupportedOperationException: CompoundFileReader::createOutput");
}

LuceneLock* CompoundFileReader::makeLock(const QString& name)
{
    _CLTHROWA(CL_ERR_UnsupportedOperation,
        "UnsupportedOperationException: CompoundFileReader::makeLock");
}

QString CompoundFileReader::toString() const
{
    QString ret(QLatin1String("CompoundFileReader@"));
    return ret.append(fileName);
}

CompoundFileWriter::CompoundFileWriter(Directory* dir, const QString& name)
    : ids(false)
    , entries(true)
{
    if (dir == NULL)
        _CLTHROWA(CL_ERR_NullPointer, "directory cannot be null");

    if (name.isEmpty())
        _CLTHROWA(CL_ERR_NullPointer, "name cannot be null");

    merged = false;
    directory = dir;
    fileName = name;
}

CompoundFileWriter::~CompoundFileWriter()
{
}

Directory* CompoundFileWriter::getDirectory()
{
    return directory;
}

/** Returns the name of the compound file. */
QString CompoundFileWriter::getName() const
{
    return fileName;
}

void CompoundFileWriter::addFile(const QString& file)
{
    if (merged)
        _CLTHROWA(CL_ERR_IO, "Can't add extensions after merge has been called");

    if (file.isEmpty())
        _CLTHROWA(CL_ERR_NullPointer, "file cannot be null");

    if (ids.find(file) != ids.end()) {
        char buf[CL_MAX_PATH + 30];
        strcpy(buf, "File ");
        strncat(buf, file.toLocal8Bit().constData(), CL_MAX_PATH);
        strcat(buf," already added");
        _CLTHROWA(CL_ERR_IO,buf);
    }
    ids.insert(file);
    entries.push_back(_CLNEW WriterFileEntry(file));
}

void CompoundFileWriter::close()
{
    if (merged)
        _CLTHROWA(CL_ERR_IO, "Merge already performed");

    if (entries.size() == 0) // isEmpty()
        _CLTHROWA(CL_ERR_IO, "No entries to merge have been defined");

    merged = true;

    // open the compound stream
    IndexOutput* os = NULL;
    try {
        os = directory->createOutput(fileName);

        // Write the number of entries
        os->writeVInt(entries.size());

        // Write the directory with all offsets at 0.
        // Remember the positions of directory entries so that we can
        // adjust the offsets later
        { //msvc6 for scope fix
            TCHAR tfile[CL_MAX_PATH];
            for (CLLinkedList<WriterFileEntry*>::iterator i = entries.begin();
                i != entries.end(); i++) {
                WriterFileEntry* fe = *i;
                fe->directoryOffset = os->getFilePointer();
                os->writeLong(0);    // for now
                tfile[fe->file.toWCharArray(tfile)] = '\0';
                os->writeString(tfile, _tcslen(tfile));
            }
        }

        // Open the files and copy their data into the stream.
        // Remember the locations of each file's data section.
        { //msvc6 for scope fix
            int32_t bufferLength = 1024;
            uint8_t buffer[1024];
            for (CLLinkedList<WriterFileEntry*>::iterator i = entries.begin();
                i != entries.end(); i++) {
                WriterFileEntry* fe = *i;
                fe->dataOffset = os->getFilePointer();
                copyFile(fe, os, buffer, bufferLength);
            }
        }

        { //msvc6 for scope fix
            // Write the data offsets into the directory of the compound stream
            for (CLLinkedList<WriterFileEntry*>::iterator i = entries.begin();
                i != entries.end(); i++) {
                WriterFileEntry* fe = *i;
                os->seek(fe->directoryOffset);
                os->writeLong(fe->dataOffset);
            }
        }


    } _CLFINALLY (
        if (os != NULL) {
            try {
                os->close();
                _CLDELETE(os);
            } catch (...) { }
        }
    );
}

void CompoundFileWriter::copyFile(WriterFileEntry* source, IndexOutput* os,
    uint8_t* buffer, int32_t bufferLength)
{
    IndexInput* is = NULL;
    try {
        int64_t startPtr = os->getFilePointer();

        is = directory->openInput(source->file);
        int64_t length = is->length();
        int64_t remainder = length;
        int32_t chunk = bufferLength;

        while(remainder > 0) {
            int32_t len = (int32_t)min((int64_t)chunk, remainder);
            is->readBytes(buffer, len);
            os->writeBytes(buffer, len);
            remainder -= len;
        }

        // Verify that remainder is 0
        if (remainder != 0) {
            TCHAR buf[CL_MAX_PATH+100];
            _sntprintf(buf, CL_MAX_PATH + 100, _T("Non-zero remainder length ")
                _T("after copying: %d (id: %s, length: %d, buffer size: %d)"),
                remainder, source->file.toLocal8Bit().constData(), length, chunk);
            _CLTHROWT(CL_ERR_IO, buf);
        }

        // Verify that the output length diff is equal to original file
        int64_t endPtr = os->getFilePointer();
        int64_t diff = endPtr - startPtr;
        if (diff != length) {
            TCHAR buf[100];
            _sntprintf(buf, 100, _T("Difference in the output file offsets %d ")
                _T("does not match the original file length %d"), diff, length);
            _CLTHROWT(CL_ERR_IO,buf);
        }
    } _CLFINALLY (
        if (is != NULL) {
            is->close();
            _CLDELETE(is);
        }
    );
}

CL_NS_END