-
Notifications
You must be signed in to change notification settings - Fork 868
/
OHashTable.java
executable file
·176 lines (134 loc) · 5.68 KB
/
OHashTable.java
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
*
* * Copyright 2010-2016 OrientDB LTD (http://orientdb.com)
* *
* * 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.
* *
* * For more information: http://orientdb.com
*
*/
package com.orientechnologies.orient.core.storage.index.hashindex.local;
import com.orientechnologies.common.comparator.ODefaultComparator;
import com.orientechnologies.common.serialization.types.OBinarySerializer;
import com.orientechnologies.orient.core.encryption.OEncryption;
import com.orientechnologies.orient.core.index.OIndexEngine;
import com.orientechnologies.orient.core.metadata.schema.OType;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Comparator;
/**
* Created by lomak_000 on 15.04.2015.
*/
public interface OHashTable<K, V> {
void create(OBinarySerializer<K> keySerializer, OBinarySerializer<V> valueSerializer, OType[] keyTypes, OEncryption encryption,
OHashFunction<K> keyHashFunction, boolean nullKeyIsSupported);
OBinarySerializer<K> getKeySerializer();
void setKeySerializer(OBinarySerializer<K> keySerializer);
OBinarySerializer<V> getValueSerializer();
void setValueSerializer(OBinarySerializer<V> valueSerializer);
V get(K key);
/**
* Puts the given value under the given key into this hash table. Validates the operation using the provided validator.
*
* @param key the key to put the value under.
* @param value the value to put.
* @param validator the operation validator.
*
* @return {@code true} if the validator allowed the put, {@code false} otherwise.
*
* @see OIndexEngine.Validator#validate(Object, Object, Object)
*/
boolean validatedPut(K key, V value, OIndexEngine.Validator<K, V> validator);
void put(K key, V value);
V remove(K key);
void clear();
OHashIndexBucket.Entry<K, V>[] higherEntries(K key);
OHashIndexBucket.Entry<K, V>[] higherEntries(K key, int limit);
void load(String name, OType[] keyTypes, boolean nullKeyIsSupported, OEncryption encryption, OHashFunction<K> keyHashFunction);
void deleteWithoutLoad(String name);
OHashIndexBucket.Entry<K, V>[] ceilingEntries(K key);
OHashIndexBucket.Entry<K, V> firstEntry();
OHashIndexBucket.Entry<K, V> lastEntry();
OHashIndexBucket.Entry<K, V>[] lowerEntries(K key);
OHashIndexBucket.Entry<K, V>[] floorEntries(K key);
long size();
void close();
void delete();
void flush();
boolean isNullKeyIsSupported();
/**
* Acquires exclusive lock in the active atomic operation running on the current thread for this hash table.
*/
void acquireAtomicExclusiveLock();
String getName();
public static final class BucketPath {
public final BucketPath parent;
public final int hashMapOffset;
public final int itemIndex;
public final int nodeIndex;
public final int nodeGlobalDepth;
public final int nodeLocalDepth;
public BucketPath(BucketPath parent, int hashMapOffset, int itemIndex, int nodeIndex, int nodeLocalDepth, int nodeGlobalDepth) {
this.parent = parent;
this.hashMapOffset = hashMapOffset;
this.itemIndex = itemIndex;
this.nodeIndex = nodeIndex;
this.nodeGlobalDepth = nodeGlobalDepth;
this.nodeLocalDepth = nodeLocalDepth;
}
}
public static final class BucketSplitResult {
public final long updatedBucketPointer;
public final long newBucketPointer;
public final int newDepth;
public BucketSplitResult(long updatedBucketPointer, long newBucketPointer, int newDepth) {
this.updatedBucketPointer = updatedBucketPointer;
this.newBucketPointer = newBucketPointer;
this.newDepth = newDepth;
}
}
public static final class NodeSplitResult {
public final long[] newNode;
public final boolean allLeftHashMapsEqual;
public final boolean allRightHashMapsEqual;
@SuppressFBWarnings("EI_EXPOSE_REP2")
public NodeSplitResult(long[] newNode, boolean allLeftHashMapsEqual, boolean allRightHashMapsEqual) {
this.newNode = newNode;
this.allLeftHashMapsEqual = allLeftHashMapsEqual;
this.allRightHashMapsEqual = allRightHashMapsEqual;
}
}
@SuppressFBWarnings(value = "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE")
final class KeyHashCodeComparator<K> implements Comparator<K> {
private final Comparator<? super K> comparator = ODefaultComparator.INSTANCE;
private final OHashFunction<K> keyHashFunction;
public KeyHashCodeComparator(OHashFunction<K> keyHashFunction) {
this.keyHashFunction = keyHashFunction;
}
@Override
public int compare(K keyOne, K keyTwo) {
final long hashCodeOne = keyHashFunction.hashCode(keyOne);
final long hashCodeTwo = keyHashFunction.hashCode(keyTwo);
if (greaterThanUnsigned(hashCodeOne, hashCodeTwo))
return 1;
if (lessThanUnsigned(hashCodeOne, hashCodeTwo))
return -1;
return comparator.compare(keyOne, keyTwo);
}
private static boolean lessThanUnsigned(long longOne, long longTwo) {
return (longOne + Long.MIN_VALUE) < (longTwo + Long.MIN_VALUE);
}
private static boolean greaterThanUnsigned(long longOne, long longTwo) {
return (longOne + Long.MIN_VALUE) > (longTwo + Long.MIN_VALUE);
}
}
}