LUCENE-2860: port to trunk

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1058155 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2011-01-12 14:29:33 +00:00
parent 84af9e5b5e
commit 466530addf
3 changed files with 77 additions and 18 deletions

View File

@ -379,7 +379,8 @@ Bug fixes
to a mutable reference to the IndexWriters SegmentInfos.
(Simon Willnauer, Earwin Burrfoot)
* LUCENE-2860: Fixed SegmentInfo.sizeInBytes to factor includeDocStores when it
decides whether to return the cached computed size or not. (Shai Erera)
======================= Lucene 3.x (not yet released) =======================

View File

@ -68,7 +68,8 @@ public final class SegmentInfo {
private List<String> files; // cached list of files that this segment uses
// in the Directory
long sizeInBytes = -1; // total byte size of all of our files (computed on demand)
private long sizeInBytesNoStore = -1; // total byte size of all but the store files (computed on demand)
private long sizeInBytesWithStore = -1; // total byte size of all of our files (computed on demand)
private int docStoreOffset; // if this segment shares stored fields & vectors, this
// offset is where in that file this segment's docs begin
@ -217,26 +218,34 @@ public final class SegmentInfo {
}
}
}
/** Returns total size in bytes of all of files used by
* this segment. */
/**
* Returns total size in bytes of all of files used by this segment (if
* {@code includeDocStores} is true), or the size of all files except the
* store files otherwise.
*/
public long sizeInBytes(boolean includeDocStores) throws IOException {
if (sizeInBytes == -1) {
List<String> files = files();
final int size = files.size();
sizeInBytes = 0;
for(int i=0;i<size;i++) {
final String fileName = files.get(i);
if (!includeDocStores && IndexFileNames.isDocStoreFile(fileName)) {
if (includeDocStores) {
if (sizeInBytesWithStore != -1) return sizeInBytesWithStore;
sizeInBytesWithStore = 0;
for (final String fileName : files()) {
// We don't count bytes used by a shared doc store against this segment
if (docStoreOffset == -1 || !IndexFileNames.isDocStoreFile(fileName)) {
sizeInBytesWithStore += dir.fileLength(fileName);
}
}
return sizeInBytesWithStore;
} else {
if (sizeInBytesNoStore != -1) return sizeInBytesNoStore;
sizeInBytesNoStore = 0;
for (final String fileName : files()) {
if (IndexFileNames.isDocStoreFile(fileName)) {
continue;
}
// We don't count bytes used by a shared doc store
// against this segment:
if (docStoreOffset == -1 || !IndexFileNames.isDocStoreFile(fileName))
sizeInBytes += dir.fileLength(fileName);
sizeInBytesNoStore += dir.fileLength(fileName);
}
return sizeInBytesNoStore;
}
return sizeInBytes;
}
public boolean getHasVectors() throws IOException {
@ -552,7 +561,8 @@ public final class SegmentInfo {
* files this segment has. */
private void clearFiles() {
files = null;
sizeInBytes = -1;
sizeInBytesNoStore = -1;
sizeInBytesWithStore = -1;
}
/** {@inheritDoc} */

View File

@ -0,0 +1,48 @@
package org.apache.lucene.index;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class TestSegmentInfo extends LuceneTestCase {
public void testSizeInBytesCache() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer());
IndexWriter writer = new IndexWriter(dir, conf);
Document doc = new Document();
doc.add(new Field("a", "value", Store.YES, Index.ANALYZED));
writer.addDocument(doc);
writer.close();
SegmentInfos sis = new SegmentInfos();
sis.read(dir);
SegmentInfo si = sis.info(0);
long sizeInBytesNoStore = si.sizeInBytes(false);
long sizeInBytesWithStore = si.sizeInBytes(true);
assertTrue("sizeInBytesNoStore=" + sizeInBytesNoStore + " sizeInBytesWithStore=" + sizeInBytesWithStore, sizeInBytesWithStore > sizeInBytesNoStore);
dir.close();
}
}