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
|
/*
* 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) 2009 Nokia Corporation and/or its subsidiary(-ies).
*/
#include "CLucene/StdHeader.h"
#include "SegmentInfos.h"
#include "CLucene/store/Directory.h"
#include "CLucene/util/Misc.h"
CL_NS_USE(store)
CL_NS_USE(util)
CL_NS_DEF(index)
SegmentInfo::SegmentInfo(const QString& Name, const int32_t DocCount,
CL_NS(store)::Directory* Dir)
: docCount(DocCount)
, dir(Dir)
{
//Func - Constructor. Initialises SegmentInfo.
//Pre - Name holds the unique name in the directory Dir
// DocCount holds the number of documents in the segment
// Dir holds the Directory where the segment resides
//Post - The instance has been created. name contains the duplicated string
// Name. docCount = DocCount and dir references Dir
name = Name;
}
SegmentInfo::~SegmentInfo()
{
}
SegmentInfos::SegmentInfos(bool _deleteMembers)
: deleteMembers(_deleteMembers)
{
//Func - Constructor
//Pre - deleteMembers indicates if the instance to be created must delete
// all SegmentInfo instances it manages when the instance is destroyed
// or not true -> must delete, false may not delete
//Post - An instance of SegmentInfos has been created.
//initialize counter to 0
counter = 0;
version = Misc::currentTimeMillis();
}
SegmentInfos::~SegmentInfos()
{
//Func - Destructor
//Pre - true
//Post - The instance has been destroyed. Depending on the constructor used
// the SegmentInfo instances that this instance managed have been
// deleted or not.
if (deleteMembers) {
segmentInfosType::iterator it;
for (it = infos.begin(); it != infos.end(); ++it)
_CLLDELETE(*it);
}
//Clear the list of SegmentInfo instances - make sure everything is deleted
infos.clear();
}
SegmentInfo* SegmentInfos::info(int32_t i) const
{
//Func - Returns a reference to the i-th SegmentInfo in the list.
//Pre - i >= 0
//Post - A reference to the i-th SegmentInfo instance has been returned
CND_PRECONDITION(i >= 0, "i contains negative number");
//Get the i-th SegmentInfo instance
SegmentInfo *ret = infos.value(i, 0);
//Condition check to see if the i-th SegmentInfo has been retrieved
CND_CONDITION(ret != NULL, "No SegmentInfo instance found");
return ret;
}
void SegmentInfos::clearto(size_t _min)
{
// Make sure we actually need to remove
if (infos.size() > _min) {
segmentInfosType::iterator itr;
segmentInfosType::iterator eitr = infos.end();
segmentInfosType::iterator bitr = infos.begin() + _min;
for(itr = bitr; itr != eitr; ++itr)
_CLLDELETE((*itr));
infos.erase(bitr, eitr);
}
}
void SegmentInfos::add(SegmentInfo* info)
{
infos.push_back(info);
}
int32_t SegmentInfos::size() const
{
return infos.size();
}
void SegmentInfos::read(Directory* directory)
{
//Func - Reads segments file that resides in directory.
//Pre - directory contains a valid reference
//Post - The segments file has been read and for each segment found
// a SegmentsInfo intance has been created and stored.
//Open an IndexInput to the segments file and check if valid
IndexInput* input = directory->openInput(QLatin1String("segments"));
if (input) {
try {
int32_t format = input->readInt();
// file contains explicit format info
if (format < 0) {
// check that it is a format we can understand
if (format < FORMAT) {
TCHAR err[30];
_sntprintf(err, 30, _T("Unknown format version: %d"), format);
_CLTHROWT(CL_ERR_Runtime, err);
}
// read version
version = input->readLong();
// read counter
counter = input->readInt();
} else {
// file is in old format without explicit format info
counter = format;
}
//Temporary variable for storing the name of the segment
char aname[CL_MAX_PATH] = { 0 };
TCHAR tname[CL_MAX_PATH] = { 0 };
//read segmentInfos
for (int32_t i = input->readInt(); i > 0; --i) {
// read the name of the segment
input->readString(tname, CL_MAX_PATH);
STRCPY_TtoA(aname, tname, CL_MAX_PATH);
//Instantiate a new SegmentInfo Instance
SegmentInfo* si = _CLNEW SegmentInfo(QLatin1String(aname),
input->readInt(), directory);
//Condition check to see if si points to an instance
CND_CONDITION(si != NULL, "Memory allocation for si failed") ;
//store SegmentInfo si
infos.push_back(si);
}
if (format >= 0) {
// in old format the version number may be at the end of the file
if (input->getFilePointer() >= input->length()) {
// old file format without version number
version = Misc::currentTimeMillis();
} else {
// read version
version = input->readLong();
}
}
} _CLFINALLY (
//destroy the inputStream input. The destructor of IndexInput will
//also close the Inputstream input
_CLDELETE(input);
);
}
}
void SegmentInfos::write(Directory* directory)
{
//Func - Writes a new segments file based upon the SegmentInfo instances it manages
//Pre - directory is a valid reference to a Directory
//Post - The new segment has been written to disk
//Open an IndexOutput to the segments file and check if valid
IndexOutput* output = directory->createOutput(QLatin1String("segments.new"));
if (output) {
try {
// write FORMAT
output->writeInt(FORMAT);
// every write changes the index
output->writeLong(++version);
// Write the counter
output->writeInt(counter);
// Write the number of SegmentInfo Instances which is equal to the number
// of segments in directory as each SegmentInfo manages a single segment
output->writeInt(infos.size());
//temporary value for wide segment name
TCHAR tname[CL_MAX_PATH];
//Iterate through all the SegmentInfo instances
for (uint32_t i = 0; i < infos.size(); ++i) {
//Retrieve the SegmentInfo
SegmentInfo *si = infos.value(i, 0);
//Condition check to see if si has been retrieved
CND_CONDITION(si != NULL, "No SegmentInfo instance found");
//Write the name of the current segment
int32_t count = si->name.toWCharArray(tname);
tname[count] = '\0';
output->writeString(tname, _tcslen(tname));
//Write the number of documents in the segment
output->writeInt(si->docCount);
}
} _CLFINALLY(
output->close();
_CLDELETE(output);
);
// install new segment info
directory->renameFile(QLatin1String("segments.new"),
QLatin1String("segments"));
}
}
int64_t SegmentInfos::readCurrentVersion(Directory* directory)
{
int32_t format = 0;
int64_t version = 0;
IndexInput* input = directory->openInput(QLatin1String("segments"));
try {
format = input->readInt();
if (format < 0){
if (format < FORMAT) {
TCHAR err[30];
_sntprintf(err, 30, _T("Unknown format version: %d"), format);
_CLTHROWT(CL_ERR_Runtime, err);
}
// read version
version = input->readLong();
}
} _CLFINALLY (
input->close();
_CLDELETE(input);
);
if (format < 0)
return version;
// We cannot be sure about the format of the file. Therefore we have to
// read the whole file and cannot simply seek to the version entry.
SegmentInfos segmentInfos;
segmentInfos.read(directory);
return segmentInfos.getVersion();
}
CL_NS_END
|