
org.sonar.batch.index.Cache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-scanner-engine Show documentation
Show all versions of sonar-scanner-engine Show documentation
Open source platform for continuous inspection of code quality
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.batch.index;
import com.google.common.collect.Sets;
import com.persistit.Exchange;
import com.persistit.Key;
import com.persistit.KeyFilter;
import com.persistit.exception.PersistitException;
import org.apache.commons.lang.builder.ToStringBuilder;
import javax.annotation.CheckForNull;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
/**
*
* This cache is not thread-safe, due to direct usage of {@link com.persistit.Exchange}
*
*/
public class Cache {
private final String name;
private final Exchange exchange;
Cache(String name, Exchange exchange) {
this.name = name;
this.exchange = exchange;
}
public Cache put(Object key, V value) {
resetKey(key);
return doPut(value);
}
public Cache put(Object firstKey, Object secondKey, V value) {
resetKey(firstKey, secondKey);
return doPut(value);
}
public Cache put(Object firstKey, Object secondKey, Object thirdKey, V value) {
resetKey(firstKey, secondKey, thirdKey);
return doPut(value);
}
public Cache put(Object[] key, V value) {
resetKey(key);
return doPut(value);
}
private Cache doPut(V value) {
try {
exchange.getValue().put(value);
exchange.store();
return this;
} catch (Exception e) {
throw new IllegalStateException("Fail to put element in the cache " + name, e);
}
}
/**
* Returns the value object associated with keys, or null if not found.
*/
public V get(Object key) {
resetKey(key);
return doGet();
}
/**
* Returns the value object associated with keys, or null if not found.
*/
@CheckForNull
public V get(Object firstKey, Object secondKey) {
resetKey(firstKey, secondKey);
return doGet();
}
/**
* Returns the value object associated with keys, or null if not found.
*/
@CheckForNull
public V get(Object firstKey, Object secondKey, Object thirdKey) {
resetKey(firstKey, secondKey, thirdKey);
return doGet();
}
/**
* Returns the value object associated with keys, or null if not found.
*/
@CheckForNull
public V get(Object[] key) {
resetKey(key);
return doGet();
}
@SuppressWarnings("unchecked")
@CheckForNull
private V doGet() {
try {
exchange.fetch();
if (!exchange.getValue().isDefined()) {
return null;
}
return (V) exchange.getValue().get();
} catch (Exception e) {
// TODO add parameters to message
throw new IllegalStateException("Fail to get element from cache " + name, e);
}
}
public boolean containsKey(Object key) {
resetKey(key);
return doContainsKey();
}
public boolean containsKey(Object firstKey, Object secondKey) {
resetKey(firstKey, secondKey);
return doContainsKey();
}
public boolean containsKey(Object firstKey, Object secondKey, Object thirdKey) {
resetKey(firstKey, secondKey, thirdKey);
return doContainsKey();
}
public boolean containsKey(Object[] key) {
resetKey(key);
return doContainsKey();
}
private boolean doContainsKey() {
try {
exchange.fetch();
return exchange.isValueDefined();
} catch (Exception e) {
// TODO add parameters to message
throw new IllegalStateException("Fail to check if element is in cache " + name, e);
}
}
public boolean remove(Object key) {
resetKey(key);
return doRemove();
}
public boolean remove(Object firstKey, Object secondKey) {
resetKey(firstKey, secondKey);
return doRemove();
}
public boolean remove(Object firstKey, Object secondKey, Object thirdKey) {
resetKey(firstKey, secondKey, thirdKey);
return doRemove();
}
public boolean remove(Object[] key) {
resetKey(key);
return doRemove();
}
private boolean doRemove() {
try {
return exchange.remove();
} catch (Exception e) {
// TODO add parameters to message
throw new IllegalStateException("Fail to get element from cache " + name, e);
}
}
/**
* Removes everything in the specified group.
*
* @param group The group name.
*/
public Cache clear(Object key) {
resetKey(key);
return doClear();
}
public Cache clear(Object firstKey, Object secondKey) {
resetKey(firstKey, secondKey);
return doClear();
}
public Cache clear(Object firstKey, Object secondKey, Object thirdKey) {
resetKey(firstKey, secondKey, thirdKey);
return doClear();
}
public Cache clear(Object[] key) {
resetKey(key);
return doClear();
}
private Cache doClear() {
try {
Key to = new Key(exchange.getKey());
to.append(Key.AFTER);
exchange.removeKeyRange(exchange.getKey(), to);
return this;
} catch (Exception e) {
throw new IllegalStateException("Fail to clear values from cache " + name, e);
}
}
/**
* Clears the default as well as all group caches.
*/
public void clear() {
try {
exchange.clear();
exchange.removeAll();
} catch (Exception e) {
throw new IllegalStateException("Fail to clear cache", e);
}
}
/**
* Returns the set of cache keys associated with this group.
* TODO implement a lazy-loading equivalent with Iterator/Iterable
*
* @param group The group.
* @return The set of cache keys for this group.
*/
@SuppressWarnings("rawtypes")
public Set keySet(Object key) {
try {
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy