001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.kv;
020
021import java.util.Arrays;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Objects;
025import java.util.concurrent.locks.Lock;
026import java.util.concurrent.locks.ReadWriteLock;
027import java.util.concurrent.locks.ReentrantReadWriteLock;
028
029/**
030 * Memory-based implementation of a Key/Value store.
031 *
032 * @since 9.1
033 */
034public class MemKeyValueStore implements KeyValueStoreProvider {
035
036    protected final Map<String, byte[]> map;
037
038    protected final Lock readLock;
039
040    protected final Lock writeLock;
041
042    public MemKeyValueStore() {
043        ReadWriteLock rwLock = new ReentrantReadWriteLock();
044        readLock = rwLock.readLock();
045        writeLock = rwLock.writeLock();
046        // We can't use ConcurrentHashMap because compareAndSet needs to compare by value
047        // and ConcurrentHashMap doesn't know how to do that. Instead we use full locking;
048        // this is ok as this class isn't expected to be used in a high write rate scenario.
049        map = new HashMap<>();
050    }
051
052    @Override
053    public void initialize(Map<String, String> properties) {
054    }
055
056    @Override
057    public void close() {
058    }
059
060    @Override
061    public void clear() {
062        writeLock.lock();
063        try {
064            map.clear();
065        } finally {
066            writeLock.unlock();
067        }
068    }
069
070    protected static byte[] clone(byte[] value) {
071        return value == null ? null : value.clone();
072    }
073
074    @Override
075    public void put(String key, byte[] value) {
076        Objects.requireNonNull(key);
077        value = clone(value);
078        writeLock.lock();
079        try {
080            map.put(key, value);
081        } finally {
082            writeLock.unlock();
083        }
084    }
085
086    @Override
087    public byte[] get(String key) {
088        Objects.requireNonNull(key);
089        byte[] value;
090        readLock.lock();
091        try {
092            value = map.get(key);
093        } finally {
094            readLock.unlock();
095        }
096        return clone(value);
097    }
098
099    @Override
100    public boolean compareAndSet(String key, byte[] expected, byte[] value) {
101        Objects.requireNonNull(key);
102        // clone is not needed if the comparison fails
103        // but we are optimistic and prefer to do the clone outside the lock
104        value = clone(value);
105        writeLock.lock();
106        try {
107            byte[] current = map.get(key);
108            boolean equal = Arrays.equals(expected, current);
109            if (equal) {
110                map.put(key, value);
111            }
112            return equal;
113        } finally {
114            writeLock.unlock();
115        }
116    }
117
118}