forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexMap.java
More file actions
108 lines (92 loc) · 3.29 KB
/
Copy pathIndexMap.java
File metadata and controls
108 lines (92 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed 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.
*/
package com.mongodb;
import java.util.HashMap;
import java.util.Map;
/**
* Efficiently maps each integer in a set to another integer in a set, useful for merging bulk write errors when a bulk write must be
* split into multiple batches. Has the ability to switch from a range-based to a hash-based map depending on the mappings that have been
* added.
*/
abstract class IndexMap {
/**
* Create an empty index map.
*/
static IndexMap create() {
return new RangeBased();
}
/**
* Create an index map that maps the integers 0..count to startIndex..startIndex + count.
*/
static IndexMap create(final int startIndex, final int count) {
return new RangeBased(startIndex, count);
}
abstract IndexMap add(int index, int originalIndex);
abstract int map(int index);
private static class HashBased extends IndexMap {
private final Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();
public HashBased(int startIndex, int count) {
for (int i = startIndex; i <= count; i++) {
indexMap.put(i - startIndex, i);
}
}
@Override
public IndexMap add(final int index, final int originalIndex) {
indexMap.put(index, originalIndex);
return this;
}
@Override
public int map(final int index) {
Integer originalIndex = indexMap.get(index);
if (originalIndex == null) {
throw new MongoInternalException("no mapping found for index " + index);
}
return originalIndex;
}
}
private static class RangeBased extends IndexMap {
private int startIndex;
private int count;
public RangeBased() {
}
public RangeBased(final int startIndex, final int count) {
this.startIndex = startIndex;
this.count = count;
}
@Override
public IndexMap add(final int index, final int originalIndex) {
if (count == 0) {
startIndex = originalIndex;
count = 1;
return this;
} else if (originalIndex == startIndex + count) {
count += 1;
return this;
} else {
IndexMap hashBasedMap = new HashBased(startIndex, count);
hashBasedMap.add(index, originalIndex);
return hashBasedMap;
}
}
@Override
public int map(final int index) {
if (index >= count) {
throw new MongoInternalException("index should not be greater than count");
}
return startIndex + index;
}
}
}