com.hazelcast.query.impl.BaseIndexStore Maven / Gradle / Ivy
/*
* 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.nio.serialization.Data;
import com.hazelcast.query.impl.getters.MultiResult;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Base class for concrete index store implementations
*/
public abstract class BaseIndexStore implements IndexStore {
static final float LOAD_FACTOR = 0.75F;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
private final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
private final CopyFunctor resultCopyFunctor;
private boolean multiResultHasToDetectDuplicates;
BaseIndexStore(IndexCopyBehavior copyOn) {
if (copyOn == IndexCopyBehavior.COPY_ON_WRITE || copyOn == IndexCopyBehavior.NEVER) {
resultCopyFunctor = new PassThroughFunctor();
} else {
resultCopyFunctor = new CopyInputFunctor();
}
}
abstract void newIndexInternal(Comparable newValue, QueryableEntry record);
abstract void removeIndexInternal(Comparable oldValue, Data indexKey);
@Override
public final void newIndex(Object newValue, QueryableEntry record) {
takeWriteLock();
try {
unwrapAndAddToIndex(newValue, record);
} finally {
releaseWriteLock();
}
}
private void unwrapAndAddToIndex(Object newValue, QueryableEntry record) {
if (newValue instanceof MultiResult) {
multiResultHasToDetectDuplicates = true;
List