All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.concurrent.lock.LockStoreContainer Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2013, 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.concurrent.lock;

import com.hazelcast.spi.ObjectNamespace;
import com.hazelcast.util.ConcurrencyUtil;
import com.hazelcast.util.ConstructorFunction;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author mdogan 2/12/13
 */
final class LockStoreContainer {

    private final LockServiceImpl lockService;
    private final int partitionId;
    private final ConcurrentMap lockStores = new ConcurrentHashMap();
    private final ConstructorFunction lockStoreConstructor = new ConstructorFunction() {
        public LockStoreImpl createNew(ObjectNamespace namespace) {
            final ConstructorFunction ctor = lockService.constructors.get(namespace.getServiceName());
            if (ctor != null) {
                final LockStoreInfo info = ctor.createNew(namespace);
                if (info != null) {
                    return new LockStoreImpl(lockService, namespace, info.getBackupCount(), info.getAsyncBackupCount());
                }
            }
            throw new IllegalArgumentException("No LockStore constructor is registered!");
        }
    };

    public LockStoreContainer(LockServiceImpl lockService, int partitionId) {
        this.lockService = lockService;
        this.partitionId = partitionId;
    }

    void clearLockStore(ObjectNamespace namespace) {
        final LockStoreImpl lockStore = lockStores.get(namespace);
        if (lockStore != null) {
            lockStore.clear();
        }
    }

    LockStoreImpl getOrCreateLockStore(ObjectNamespace namespace) {
        return ConcurrencyUtil.getOrPutIfAbsent(lockStores, namespace, lockStoreConstructor);
    }

    LockStoreImpl getLockStore(ObjectNamespace namespace) {
        return lockStores.get(namespace);
    }

    Collection getLockStores() {
        return Collections.unmodifiableCollection(lockStores.values());
    }

    void clear() {
        for (LockStoreImpl lockStore : lockStores.values()) {
            lockStore.clear();
        }
        lockStores.clear();
    }

    int getPartitionId() {
        return partitionId;
    }

    void put(LockStoreImpl ls) {
        ls.setLockService(lockService);
        lockStores.put(ls.getNamespace(), ls);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy