| Index: contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java |
| =================================================================== |
| --- contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java (revision 742818) |
| +++ contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java (working copy) |
| @@ -54,7 +54,30 @@ |
| // testEquals(fsdir, ii); |
| // } |
| |
| + public void testClone() throws Exception { |
| + RAMDirectory dir = new RAMDirectory(); |
| |
| + // create dir data |
| + IndexWriter indexWriter = new IndexWriter(dir, new StandardAnalyzer(), true); |
| + for (int i = 0; i < 20; i++) { |
| + Document document = new Document(); |
| + assembleDocument(document, i); |
| + indexWriter.addDocument(document); |
| + } |
| + indexWriter.close(); |
| + |
| + IndexReader ir = IndexReader.open(dir); |
| + InstantiatedIndex ii = new InstantiatedIndex(ir); |
| + |
| + InstantiatedIndexReader r1 = new InstantiatedIndexReader(ii); |
| + r1.deleteDocument(2); |
| + r1.deleteDocument(5); |
| + InstantiatedIndexReader r1cloned = (InstantiatedIndexReader)r1.clone(); |
| + assertEquals(18, r1cloned.numDocs()); |
| + // verify deleted docs are the same |
| + testEquals(r1, r1cloned); |
| + } |
| + |
| public void testLoadIndexReader() throws Exception { |
| RAMDirectory dir = new RAMDirectory(); |
| |
| @@ -77,8 +100,6 @@ |
| |
| |
| public void testInstantiatedIndexWriter() throws Exception { |
| - |
| - |
| RAMDirectory dir = new RAMDirectory(); |
| InstantiatedIndex ii = new InstantiatedIndex(); |
| |
| @@ -221,12 +242,14 @@ |
| // make sure they still equal |
| testEquals(aprioriIndex, testIndex); |
| } |
| - |
| + |
| protected void testEquals(Directory aprioriIndex, InstantiatedIndex testIndex) throws Exception { |
| - |
| IndexReader aprioriReader = IndexReader.open(aprioriIndex); |
| IndexReader testReader = testIndex.indexReaderFactory(); |
| - |
| + testEquals(aprioriReader, testReader); |
| + } |
| + |
| + protected void testEquals(IndexReader aprioriReader, IndexReader testReader) throws Exception { |
| assertEquals(aprioriReader.numDocs(), testReader.numDocs()); |
| |
| // assert field options |
| Index: contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexReader.java |
| =================================================================== |
| --- contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexReader.java (revision 742818) |
| +++ contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexReader.java (working copy) |
| @@ -47,15 +47,31 @@ |
| * Consider using InstantiatedIndex as if it was immutable. |
| */ |
| public class InstantiatedIndexReader extends IndexReader { |
| - |
| private final InstantiatedIndex index; |
| |
| public InstantiatedIndexReader(InstantiatedIndex index) { |
| super(); |
| this.index = index; |
| } |
| - |
| + |
| + public Object clone() { |
| + try { |
| + doCommit(); |
| + InstantiatedIndex clonedIndex = index.cloneWithDeletesNorms(); |
| + return new InstantiatedIndexReader(clonedIndex); |
| + } catch (IOException ioe) { |
| + throw new RuntimeException("", ioe); |
| + } |
| + } |
| + |
| /** |
| + * openReadOnly is ignored. |
| + */ |
| + public IndexReader clone(boolean openReadOnly) { |
| + return (IndexReader)clone(); |
| + } |
| + |
| + /** |
| * @return always true. |
| */ |
| public boolean isOptimized() { |
| @@ -116,7 +132,9 @@ |
| } |
| |
| public int numDocs() { |
| - return getIndex().getDocumentsByNumber().length - index.getDeletedDocuments().size() - deletedDocuments.size(); |
| + int idd = index.getDeletedDocuments().size(); |
| + int dds = deletedDocuments.size(); |
| + return getIndex().getDocumentsByNumber().length - idd - dds; |
| } |
| |
| public int maxDoc() { |
| Index: contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndex.java |
| =================================================================== |
| --- contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndex.java (revision 742818) |
| +++ contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndex.java (working copy) |
| @@ -77,6 +77,18 @@ |
| initialize(); |
| } |
| |
| + InstantiatedIndex cloneWithDeletesNorms() { |
| + InstantiatedIndex clone = new InstantiatedIndex(); |
| + clone.version = System.currentTimeMillis(); |
| + clone.documentsByNumber = documentsByNumber; |
| + clone.deletedDocuments = new HashSet<Integer>(deletedDocuments); |
| + clone.termsByFieldAndText = termsByFieldAndText; |
| + clone.orderedTerms = orderedTerms; |
| + clone.normsByFieldNameAndDocumentNumber = new HashMap<String, byte[]>(normsByFieldNameAndDocumentNumber); |
| + clone.fieldSettings = fieldSettings; |
| + return clone; |
| + } |
| + |
| void initialize() { |
| // todo: clear index without loosing memory (uncouple stuff) |
| termsByFieldAndText = new HashMap<String, Map<String, InstantiatedTerm>>(); |
| @@ -170,7 +182,7 @@ |
| } |
| |
| |
| - documentsByNumber = new InstantiatedDocument[sourceIndexReader.numDocs()]; |
| + documentsByNumber = new InstantiatedDocument[sourceIndexReader.maxDoc()]; |
| |
| // create documents |
| for (int i = 0; i < sourceIndexReader.numDocs(); i++) { |
| @@ -248,7 +260,7 @@ |
| InstantiatedTermDocumentInformation termDocumentInformation = new InstantiatedTermDocumentInformation(term, document, positions, payloads); |
| term.getAssociatedDocuments()[position++] = termDocumentInformation; |
| |
| - if (document.getVectorSpace() != null |
| + if (document != null && document.getVectorSpace() != null |
| && document.getVectorSpace().containsKey(term.field())) { |
| document.getVectorSpace().get(term.field()).add(termDocumentInformation); |
| } |
| @@ -258,7 +270,8 @@ |
| } |
| |
| // load offsets to term-document informations |
| - for (InstantiatedDocument document : getDocumentsByNumber()) { |
| + //for (InstantiatedDocument document : getDocumentsByNumber()) { |
| + for (InstantiatedDocument document : getDocumentsNotDeleted()) { |
| for (Field field : (List<Field>) document.getDocument().getFields()) { |
| if (field.isTermVectorStored() && field.isStoreOffsetWithTermVector()) { |
| TermPositionVector termPositionVector = (TermPositionVector) sourceIndexReader.getTermFreqVector(document.getDocumentNumber(), field.name()); |
| @@ -312,7 +325,15 @@ |
| public InstantiatedDocument[] getDocumentsByNumber() { |
| return documentsByNumber; |
| } |
| - |
| + |
| + public List<InstantiatedDocument> getDocumentsNotDeleted() { |
| + List<InstantiatedDocument> list = new ArrayList<InstantiatedDocument>(documentsByNumber.length-deletedDocuments.size()); |
| + for (int x=0; x < documentsByNumber.length; x++) { |
| + if (!deletedDocuments.contains(x)) list.add(documentsByNumber[x]); |
| + } |
| + return list; |
| + } |
| + |
| public Map<String, byte[]> getNormsByFieldNameAndDocumentNumber() { |
| return normsByFieldNameAndDocumentNumber; |
| } |
| Index: contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedTerm.java |
| =================================================================== |
| --- contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedTerm.java (revision 742818) |
| +++ contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedTerm.java (working copy) |
| @@ -165,7 +165,10 @@ |
| } |
| int min = offset, max = offset + length - 1; |
| int minVal = getAssociatedDocuments()[min].getDocument().getDocumentNumber(); |
| - int maxVal = getAssociatedDocuments()[max].getDocument().getDocumentNumber(); |
| + InstantiatedTermDocumentInformation itdi = getAssociatedDocuments()[max]; |
| + InstantiatedDocument id = itdi.getDocument(); |
| + int maxVal = id.getDocumentNumber(); |
| + //int maxVal = getAssociatedDocuments()[max].getDocument().getDocumentNumber(); |
| |
| |
| int nPreviousSteps = 0; |