1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 /** Remaps docIDs after a merge has completed, where the
21 * merged segments had at least one deletion. This is used
22 * to renumber the buffered deletes in IndexWriter when a
23 * merge of segments with deletions commits. */
25 final class MergeDocIDRemapper {
26 int[] starts; // used for binary search of mapped docID
27 int[] newStarts; // starts, minus the deletes
28 int[][] docMaps; // maps docIDs in the merged set
29 int minDocID; // minimum docID that needs renumbering
30 int maxDocID; // 1+ the max docID that needs renumbering
31 int docShift; // total # deleted docs that were compacted by this merge
33 public MergeDocIDRemapper(SegmentInfos infos, int[][] docMaps, int[] delCounts, MergePolicy.OneMerge merge, int mergedDocCount) {
34 this.docMaps = docMaps;
35 SegmentInfo firstSegment = merge.segments.get(0);
38 SegmentInfo info = infos.info(i);
39 if (info.equals(firstSegment))
41 minDocID += info.docCount;
46 for(int j=0;j<docMaps.length;i++,j++) {
47 numDocs += infos.info(i).docCount;
48 assert infos.info(i).equals(merge.segments.get(j));
50 maxDocID = minDocID + numDocs;
52 starts = new int[docMaps.length];
53 newStarts = new int[docMaps.length];
56 newStarts[0] = minDocID;
57 for(i=1;i<docMaps.length;i++) {
58 final int lastDocCount = merge.segments.get(i-1).docCount;
59 starts[i] = starts[i-1] + lastDocCount;
60 newStarts[i] = newStarts[i-1] + lastDocCount - delCounts[i-1];
62 docShift = numDocs - mergedDocCount;
64 // There are rare cases when docShift is 0. It happens
65 // if you try to delete a docID that's out of bounds,
66 // because the SegmentReader still allocates deletedDocs
67 // and pretends it has deletions ... so we can't make
69 // assert docShift > 0;
71 // Make sure it all adds up:
72 assert docShift == maxDocID - (newStarts[docMaps.length-1] + merge.segments.get(docMaps.length-1).docCount - delCounts[docMaps.length-1]);
75 public int remap(int oldDocID) {
76 if (oldDocID < minDocID)
77 // Unaffected by merge
79 else if (oldDocID >= maxDocID)
80 // This doc was "after" the merge, so simple shift
81 return oldDocID - docShift;
83 // Binary search to locate this document & find its new docID
84 int lo = 0; // search starts array
85 int hi = docMaps.length - 1; // for first element less
88 int mid = (lo + hi) >>> 1;
89 int midValue = starts[mid];
90 if (oldDocID < midValue)
92 else if (oldDocID > midValue)
94 else { // found a match
95 while (mid+1 < docMaps.length && starts[mid+1] == midValue) {
96 mid++; // scan to last match
98 if (docMaps[mid] != null)
99 return newStarts[mid] + docMaps[mid][oldDocID-starts[mid]];
101 return newStarts[mid] + oldDocID-starts[mid];
104 if (docMaps[hi] != null)
105 return newStarts[hi] + docMaps[hi][oldDocID-starts[hi]];
107 return newStarts[hi] + oldDocID-starts[hi];