summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/index/TermInfosWriter.cpp
blob: 52829e25d7732716860ca4fa1adbfbf87e1d7f60 (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
/*
 * 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), all rights reserved.
*/
#include "CLucene/StdHeader.h"
#include "TermInfosWriter.h"

#include "CLucene/store/Directory.h"
#include "CLucene/util/Misc.h"
#include "FieldInfos.h"
#include "Term.h"
#include "TermInfo.h"
#include "IndexWriter.h"

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

TermInfosWriter::TermInfosWriter(Directory* directory, const QString& segment,
    FieldInfos* fis, int32_t interval)
    : fieldInfos(fis)
{
    //Func - Constructor
    //Pre  - directory contains a valid reference to a Directory
    //       segment != NULL
    //       fis contains a valid reference to a reference FieldInfos
    //Post - The instance has been created

    CND_PRECONDITION(!segment.isEmpty(), "segment is NULL");
    //Initialize instance
    initialise(directory, segment, interval, false);

    other = _CLNEW TermInfosWriter(directory, segment, fieldInfos, interval, true);

    CND_CONDITION(other != NULL, "other is NULL");

    other->other = this;
}

TermInfosWriter::TermInfosWriter(Directory* directory, const QString& segment,
    FieldInfos* fis, int32_t interval, bool isIndex)
    : fieldInfos(fis)
{
    //Func - Constructor
    //Pre  - directory contains a valid reference to a Directory
    //       segment != NULL
    //       fis contains a valid reference to a reference FieldInfos
    //       isIndex is true or false
    //Post - The instance has been created

    CND_PRECONDITION(!segment.isEmpty(), "segment is NULL");
    initialise(directory, segment, interval, isIndex);
}

void TermInfosWriter::initialise(Directory* directory, const QString& segment,
    int32_t interval, bool IsIndex)
{
    //Func - Helps constructors to initialize Instance
    //Pre  - directory contains a valid reference to a Directory
    //       segment != NULL
    //       fis contains a valid reference to a reference FieldInfos
    //Post - The instance has been initialized

    lastTerm = _CLNEW Term;

    CND_CONDITION(lastTerm != NULL, "Could not allocate memory for lastTerm");

    lastTi  = _CLNEW TermInfo();

    CND_CONDITION(lastTi != NULL, "Could not allocate memory for lastTi");

    lastIndexPointer = 0;
    size             = 0;
    isIndex          = IsIndex;
    indexInterval = interval;
    skipInterval = LUCENE_DEFAULT_TERMDOCS_SKIP_INTERVAL;

    QString buf = Misc::segmentname(segment, QLatin1String(isIndex ? ".tii" : ".tis"));
    output = directory->createOutput(buf);

    output->writeInt(FORMAT);                      // write format
    output->writeLong(0);                          // leave space for size
    output->writeInt(indexInterval);// write indexInterval
    output->writeInt(skipInterval); // write skipInterval

    //Set other to NULL by Default
    other = NULL;
}

TermInfosWriter::~TermInfosWriter()
{
    //Func - Destructor
    //Pre  - true
    //Post - de instance has been destroyed

    close();
}

void TermInfosWriter::add(Term* term, const TermInfo* ti)
{
    //Func - Writes a Term and TermInfo to the outputstream
    //Pre  - Term must be lexicographically greater than all previous Terms added.
    //       Pointers of TermInfo ti (freqPointer and proxPointer) must be
    //       positive and greater than all previous.

    CND_PRECONDITION(isIndex || (!isIndex  && term->compareTo(lastTerm) > 0),"term out of order");
    CND_PRECONDITION(ti->freqPointer >= lastTi->freqPointer,"freqPointer out of order");
    CND_PRECONDITION(ti->proxPointer >= lastTi->proxPointer,"proxPointer out of order");

    if (!isIndex && size % indexInterval == 0) {
        //add an index term
        other->add(lastTerm, lastTi);
    }

    //write term
    writeTerm(term);				      
    // write doc freq
    output->writeVInt(ti->docFreq);		  
    //write pointers
    output->writeVLong(ti->freqPointer - lastTi->freqPointer);
    output->writeVLong(ti->proxPointer - lastTi->proxPointer);
    if (ti->docFreq >= skipInterval) {
        output->writeVInt(ti->skipOffset);
    }

    if (isIndex){
        output->writeVLong(other->output->getFilePointer() - lastIndexPointer);
        lastIndexPointer = other->output->getFilePointer(); // write pointer
    }

    lastTi->set(ti);
    size++;
}

void TermInfosWriter::close() {
    //Func - Closes the TermInfosWriter
    //Pre  - true
    //Post - The TermInfosWriter has been closed

    if (output){
        //write size at start
        output->seek(4);          // write size after format
        output->writeLong(size);
        output->close();
        _CLDELETE(output);

        if (!isIndex){
            if(other){
                other->close();
                _CLDELETE( other );
            }
        }
        _CLDECDELETE(lastTerm);

        _CLDELETE(lastTi);
    }
}

void TermInfosWriter::writeTerm(Term* term)
{
    int32_t start = Misc::stringDifference(lastTerm->text(),lastTerm->textLength(), 
        term->text(),term->textLength());
    int32_t length = term->textLength() - start;

    output->writeVInt(start);			  // write shared prefix length
    output->writeVInt(length);			  // write delta length
    output->writeChars(term->text(), start, length);  // write delta chars

    int32_t fieldnum = fieldInfos->fieldNumber(term->field());
    CND_PRECONDITION(fieldnum>=-1&&fieldnum<fieldInfos->size(),"Fieldnum is out of range");
    output->writeVInt(fieldnum); // write field num

    if ( lastTerm->__cl_refcount == 1 ){
        lastTerm->set(term,term->text());
    }else{
        _CLDECDELETE(lastTerm);
        lastTerm = _CL_POINTER(term);
    }
}

CL_NS_END