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

org.apache.jackrabbit.oak.plugins.document.persistentCache.MultiGenerationMap Maven / Gradle / Ivy

There is a newer version: 1.62.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.jackrabbit.oak.plugins.document.persistentCache;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;

public class MultiGenerationMap implements Map {
    
    private volatile CacheMap write;
    private ConcurrentSkipListMap> read = 
            new ConcurrentSkipListMap>();
    
    MultiGenerationMap() {
    }
    
    public void setWriteMap(CacheMap m) {
        write = m;
    }

    public void addReadMap(int generation, CacheMap m) {
        read.put(generation, m);
    }
    
    public void removeReadMap(int generation) {
        read.remove(generation);
    }
    
    @Override
    public V put(K key, V value) {
        CacheMap m = write;
        if (m == null) {
            // closed concurrently
            return null;
        }
        return m.put(key, value);
    }

    @SuppressWarnings("unchecked")
    @Override
    public V get(Object key) {
        ValueWithGenerationInfo value = readValue(key);
        if (value == null) {
            return null;
        } else if (!value.isCurrentGeneration()) {
            put((K) key, value.value);
        }
        return value.getValue();
    }

    ValueWithGenerationInfo readValue(Object key) {
        for (int generation : read.descendingKeySet()) {
            CacheMap m = read.get(generation);
            if (m != null) {
                V value = m.get(key);
                if (value != null) {
                    return new ValueWithGenerationInfo(value, m == write);
                }
            }
        }
        return null;
    }
    
    @Override
    public boolean containsKey(Object key) {
        for (int generation : read.descendingKeySet()) {
            CacheMap m = read.get(generation);
            if (m != null) {
                if (m.containsKey(key)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public V remove(Object key) {
        return write.remove(key);
    }

    @Override
    public void clear() {
        write.clear();
    }
    
    @Override
    public int size() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean isEmpty() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean containsValue(Object value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map m) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set keySet() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection values() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set> entrySet() {
        throw new UnsupportedOperationException();
    }

    static class ValueWithGenerationInfo {

        private final V value;

        private final boolean isCurrentGeneration;

        private ValueWithGenerationInfo(V value, boolean isCurrentGeneration) {
            this.value = value;
            this.isCurrentGeneration = isCurrentGeneration;
        }

        V getValue() {
            return value;
        }

        boolean isCurrentGeneration() {
            return isCurrentGeneration;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy