~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/index/MergeDocIDRemapper.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.index;
2
 
 
3
 
/**
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
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. */
24
 
 
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
32
 
 
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);
36
 
    int i = 0;
37
 
    while(true) {
38
 
      SegmentInfo info = infos.info(i);
39
 
      if (info.equals(firstSegment))
40
 
        break;
41
 
      minDocID += info.docCount;
42
 
      i++;
43
 
    }
44
 
 
45
 
    int numDocs = 0;
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));
49
 
    }
50
 
    maxDocID = minDocID + numDocs;
51
 
 
52
 
    starts = new int[docMaps.length];
53
 
    newStarts = new int[docMaps.length];
54
 
 
55
 
    starts[0] = minDocID;
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];
61
 
    }
62
 
    docShift = numDocs - mergedDocCount;
63
 
 
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
68
 
    // this assert here
69
 
    // assert docShift > 0;
70
 
 
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]);
73
 
  }
74
 
 
75
 
  public int remap(int oldDocID) {
76
 
    if (oldDocID < minDocID)
77
 
      // Unaffected by merge
78
 
      return oldDocID;
79
 
    else if (oldDocID >= maxDocID)
80
 
      // This doc was "after" the merge, so simple shift
81
 
      return oldDocID - docShift;
82
 
    else {
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
86
 
 
87
 
      while (hi >= lo) {
88
 
        int mid = (lo + hi) >>> 1;
89
 
        int midValue = starts[mid];
90
 
        if (oldDocID < midValue)
91
 
          hi = mid - 1;
92
 
        else if (oldDocID > midValue)
93
 
          lo = mid + 1;
94
 
        else {                                      // found a match
95
 
          while (mid+1 < docMaps.length && starts[mid+1] == midValue) {
96
 
            mid++;                                  // scan to last match
97
 
          }
98
 
          if (docMaps[mid] != null)
99
 
            return newStarts[mid] + docMaps[mid][oldDocID-starts[mid]];
100
 
          else
101
 
            return newStarts[mid] + oldDocID-starts[mid];
102
 
        }
103
 
      }
104
 
      if (docMaps[hi] != null)
105
 
        return newStarts[hi] + docMaps[hi][oldDocID-starts[hi]];
106
 
      else
107
 
        return newStarts[hi] + oldDocID-starts[hi];
108
 
    }
109
 
  }
110
 
}