/
IndexImpl.java
221 lines (187 loc) · 7.17 KB
/
IndexImpl.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* 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.hazelcast.query.impl;
import com.hazelcast.core.TypeConverter;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.query.QueryException;
import com.hazelcast.query.impl.getters.Extractors;
import com.hazelcast.query.impl.predicates.PredicateDataSerializerHook;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import static com.hazelcast.query.impl.TypeConverters.NULL_CONVERTER;
import static com.hazelcast.util.SetUtil.createHashSet;
public class IndexImpl implements Index {
public static final NullObject NULL = new NullObject();
protected final InternalSerializationService ss;
protected final IndexStore indexStore;
protected final Extractors extractors;
private final IndexCopyBehavior copyQueryResultOn;
private volatile TypeConverter converter;
private final String attributeName;
private final boolean ordered;
public IndexImpl(String attributeName, boolean ordered, InternalSerializationService ss, Extractors extractors,
IndexCopyBehavior copyQueryResultOn) {
this.attributeName = attributeName;
this.ordered = ordered;
this.ss = ss;
this.extractors = extractors;
this.copyQueryResultOn = copyQueryResultOn;
this.indexStore = createIndexStore(ordered);
}
public IndexStore createIndexStore(boolean ordered) {
return ordered ? new SortedIndexStore(copyQueryResultOn) : new UnsortedIndexStore(copyQueryResultOn);
}
@Override
public void saveEntryIndex(QueryableEntry entry, Object oldRecordValue) throws QueryException {
/*
* At first, check if converter is not initialized, initialize it before saving an entry index
* Because, if entity index is saved before,
* that thread can be blocked before executing converter setting code block,
* another thread can query over indexes without knowing the converter and
* this causes to class cast exceptions.
*/
if (converter == null || converter == NULL_CONVERTER) {
converter = entry.getConverter(attributeName);
}
Object newAttributeValue = extractAttributeValue(entry.getKeyData(), entry.getTargetObject(false));
if (oldRecordValue == null) {
indexStore.newIndex(newAttributeValue, entry);
} else {
Object oldAttributeValue = extractAttributeValue(entry.getKeyData(), oldRecordValue);
indexStore.updateIndex(oldAttributeValue, newAttributeValue, entry);
}
}
@Override
public void removeEntryIndex(Data key, Object value) {
Object attributeValue = extractAttributeValue(key, value);
indexStore.removeIndex(attributeValue, key);
}
private Object extractAttributeValue(Data key, Object value) {
return QueryableEntry.extractAttributeValue(extractors, ss, attributeName, key, value);
}
@Override
public Set<QueryableEntry> getRecords(Comparable[] values) {
if (values.length == 1) {
return getRecords(values[0]);
} else {
if (converter != null) {
Set<Comparable> convertedValues = createHashSet(values.length);
for (Comparable value : values) {
convertedValues.add(convert(value));
}
return indexStore.getRecords(convertedValues);
}
return Collections.EMPTY_SET;
}
}
@Override
public Set<QueryableEntry> getRecords(Comparable attributeValue) {
if (converter == null) {
return new SingleResultSet(null);
}
return indexStore.getRecords(convert(attributeValue));
}
@Override
public Set<QueryableEntry> getSubRecords(ComparisonType comparisonType, Comparable searchedAttributeValue) {
if (converter == null) {
return Collections.EMPTY_SET;
}
return indexStore.getSubRecords(comparisonType, convert(searchedAttributeValue));
}
@Override
public Set<QueryableEntry> getSubRecordsBetween(Comparable fromAttributeValue, Comparable toAttributeValue) {
if (converter == null) {
return Collections.EMPTY_SET;
}
return indexStore.getSubRecordsBetween(convert(fromAttributeValue), convert(toAttributeValue));
}
/**
* Note: the fact that the given attributeValue is of type Comparable doesn't mean that this value is of the same
* type as the one that's stored in the index, thus the conversion is needed.
*
* @param attributeValue to be converted from given type to the type of the attribute that's stored in the index
* @return converted value that may be compared with the value that's stored in the index
*/
private Comparable convert(Comparable attributeValue) {
return converter.convert(attributeValue);
}
/**
* Provides comparable null object.
*/
@Override
public TypeConverter getConverter() {
return converter;
}
@Override
public void clear() {
indexStore.clear();
converter = null;
}
@Override
public void destroy() {
// NOOP
}
@Override
public String getAttributeName() {
return attributeName;
}
@Override
public boolean isOrdered() {
return ordered;
}
public static final class NullObject implements Comparable, IdentifiedDataSerializable {
@Override
public int compareTo(Object o) {
if (o == this || o instanceof NullObject) {
return 0;
}
return -1;
}
@Override
public int hashCode() {
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return true;
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
}
@Override
public void readData(ObjectDataInput in) throws IOException {
}
@Override
public int getFactoryId() {
return PredicateDataSerializerHook.F_ID;
}
@Override
public int getId() {
return PredicateDataSerializerHook.NULL_OBJECT;
}
}
}