@@ -27,60 +27,60 @@ namespace Lucene
27
27
entries = Collection<FileEntry>::newInstance ();
28
28
merged = false ;
29
29
}
30
-
30
+
31
31
CompoundFileWriter::~CompoundFileWriter ()
32
32
{
33
33
}
34
-
34
+
35
35
DirectoryPtr CompoundFileWriter::getDirectory ()
36
36
{
37
37
return DirectoryPtr (_directory);
38
38
}
39
-
39
+
40
40
String CompoundFileWriter::getName ()
41
41
{
42
42
return fileName;
43
43
}
44
-
44
+
45
45
void CompoundFileWriter::addFile (const String& file)
46
46
{
47
47
if (merged)
48
48
boost::throw_exception (IllegalStateException (L" Can't add extensions after merge has been called" ));
49
-
49
+
50
50
if (file.empty ())
51
51
boost::throw_exception (IllegalArgumentException (L" file cannot be empty" ));
52
-
52
+
53
53
if (!ids.add (file))
54
54
boost::throw_exception (IllegalArgumentException (L" File " + file + L" already added" ));
55
-
55
+
56
56
FileEntry entry;
57
57
entry.file = file;
58
58
entries.add (entry);
59
59
}
60
-
60
+
61
61
void CompoundFileWriter::close ()
62
62
{
63
63
if (merged)
64
64
boost::throw_exception (IllegalStateException (L" Merge already performed" ));
65
-
65
+
66
66
if (entries.empty ())
67
67
boost::throw_exception (IllegalStateException (L" No entries to merge have been defined" ));
68
-
68
+
69
69
merged = true ;
70
-
70
+
71
71
DirectoryPtr directory (_directory);
72
-
72
+
73
73
// open the compound stream
74
74
IndexOutputPtr os;
75
75
LuceneException finally;
76
76
try
77
77
{
78
78
os = directory->createOutput (fileName);
79
-
79
+
80
80
// Write the number of entries
81
81
os->writeVInt (entries.size ());
82
-
83
- // Write the directory with all offsets at 0. Remember the positions of directory entries so that we
82
+
83
+ // Write the directory with all offsets at 0. Remember the positions of directory entries so that we
84
84
// can adjust the offsets later
85
85
int64_t totalSize = 0 ;
86
86
for (Collection<FileEntry>::iterator fe = entries.begin (); fe != entries.end (); ++fe)
@@ -90,31 +90,31 @@ namespace Lucene
90
90
os->writeString (fe->file );
91
91
totalSize += directory->fileLength (fe->file );
92
92
}
93
-
94
- // Pre-allocate size of file as optimization - this can potentially help IO performance as we write the
95
- // file and also later during searching. It also uncovers a disk-full situation earlier and hopefully
93
+
94
+ // Pre-allocate size of file as optimization - this can potentially help IO performance as we write the
95
+ // file and also later during searching. It also uncovers a disk-full situation earlier and hopefully
96
96
// without actually filling disk to 100%
97
97
int64_t finalLength = totalSize + os->getFilePointer ();
98
98
os->setLength (finalLength);
99
-
99
+
100
100
// Open the files and copy their data into the stream. Remember the locations of each file's data section.
101
101
ByteArray buffer (ByteArray::newInstance (16384 ));
102
102
for (Collection<FileEntry>::iterator fe = entries.begin (); fe != entries.end (); ++fe)
103
103
{
104
104
fe->dataOffset = os->getFilePointer ();
105
105
copyFile (*fe, os, buffer);
106
106
}
107
-
107
+
108
108
// Write the data offsets into the directory of the compound stream
109
109
for (Collection<FileEntry>::iterator fe = entries.begin (); fe != entries.end (); ++fe)
110
110
{
111
111
os->seek (fe->directoryOffset );
112
112
os->writeLong (fe->dataOffset );
113
113
}
114
-
114
+
115
115
BOOST_ASSERT (finalLength == os->length ());
116
-
117
- // Close the output stream. Set the os to null before trying to close so that if an exception occurs during
116
+
117
+ // Close the output stream. Set the os to null before trying to close so that if an exception occurs during
118
118
// the close, the finally clause below will not attempt to close the stream the second time.
119
119
IndexOutputPtr tmp (os);
120
120
os.reset ();
@@ -124,10 +124,10 @@ namespace Lucene
124
124
{
125
125
finally = e;
126
126
}
127
-
127
+
128
128
if (os)
129
129
{
130
- try
130
+ try
131
131
{
132
132
os->close ();
133
133
}
@@ -137,7 +137,7 @@ namespace Lucene
137
137
}
138
138
finally.throwException ();
139
139
}
140
-
140
+
141
141
void CompoundFileWriter::copyFile (const FileEntry& source, IndexOutputPtr os, ByteArray buffer)
142
142
{
143
143
IndexInputPtr is;
@@ -146,15 +146,15 @@ namespace Lucene
146
146
try
147
147
{
148
148
int64_t startPtr = os->getFilePointer ();
149
-
149
+
150
150
is = directory->openInput (source.file );
151
151
int64_t length = is->length ();
152
152
int64_t remainder = length;
153
- int32_t chunk = buffer.size ();
154
-
153
+ int64_t chunk = buffer.size ();
154
+
155
155
while (remainder > 0 )
156
156
{
157
- int32_t len = std::min (chunk, ( int32_t ) remainder);
157
+ int32_t len = ( int32_t ) std::min (chunk, remainder);
158
158
is->readBytes (buffer.get (), 0 , len, false );
159
159
os->writeBytes (buffer.get (), len);
160
160
remainder -= len;
@@ -164,15 +164,15 @@ namespace Lucene
164
164
checkAbort->work (80 );
165
165
}
166
166
}
167
-
167
+
168
168
// Verify that remainder is 0
169
169
if (remainder != 0 )
170
170
{
171
- boost::throw_exception (IOException (L" Non-zero remainder length after copying: " + StringUtils::toString (remainder) +
171
+ boost::throw_exception (IOException (L" Non-zero remainder length after copying: " + StringUtils::toString (remainder) +
172
172
L" (id: " + source.file + L" , length: " + StringUtils::toString (length) +
173
173
L" , buffer size: " + StringUtils::toString (chunk) + L" )" ));
174
174
}
175
-
175
+
176
176
// Verify that the output length diff is equal to original file
177
177
int64_t endPtr = os->getFilePointer ();
178
178
int64_t diff = endPtr - startPtr;
@@ -186,7 +186,7 @@ namespace Lucene
186
186
{
187
187
finally = e;
188
188
}
189
-
189
+
190
190
if (is)
191
191
is->close ();
192
192
finally.throwException ();
0 commit comments