Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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;
private final IndexCopyBehavior copyQueryResultOn;
private volatile TypeConverter converter;
private final String attributeName;
private final boolean ordered;
private final Extractors extractors;
public IndexImpl(String attributeName, boolean ordered, InternalSerializationService ss, Extractors extractors,
IndexCopyBehavior copyQueryResultOn) {
this.attributeName = attributeName;
this.ordered = ordered;
this.ss = ss;
this.copyQueryResultOn = copyQueryResultOn;
this.indexStore = createIndexStore(ordered);
this.extractors = extractors;
}
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 getRecords(Comparable[] values) {
if (values.length == 1) {
return getRecords(values[0]);
} else {
if (converter != null) {
Set convertedValues = createHashSet(values.length);
for (Comparable value : values) {
convertedValues.add(convert(value));
}
return indexStore.getRecords(convertedValues);
}
return Collections.EMPTY_SET;
}
}
@Override
public Set getRecords(Comparable attributeValue) {
if (converter == null) {
return new SingleResultSet(null);
}
return indexStore.getRecords(convert(attributeValue));
}
@Override
public Set getSubRecords(ComparisonType comparisonType, Comparable searchedAttributeValue) {
if (converter == null) {
return Collections.EMPTY_SET;
}
return indexStore.getSubRecords(comparisonType, convert(searchedAttributeValue));
}
@Override
public Set 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;
}
}
}