Apache Lucene open-source search software
LUCENE-2860: port to trunk
Shai Erera466530addfc4
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 888ed11b991..c0573de7544 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -379,7 +379,8 @@ Bug fixesto 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) =======================diff --git a/lucene/src/java/org/apache/lucene/index/SegmentInfo.java b/lucene/src/java/org/apache/lucene/index/SegmentInfo.javaindex ff621277dd2..2c91efc636b 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentInfo.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentInfo.java@@ -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} */diff --git a/lucene/src/test/org/apache/lucene/index/TestSegmentInfo.java b/lucene/src/test/org/apache/lucene/index/TestSegmentInfo.javanew file mode 100644index 00000000000..58b7cb8d9a6--- /dev/null+++ b/lucene/src/test/org/apache/lucene/index/TestSegmentInfo.java@@ -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();+ }++}