public ValueStore(File dataDir, boolean forceSync, int valueCacheSize, int valueIDCacheSize, int namespaceCacheSize, int namespaceIDCacheSize) throws IOException { super(); dataStore = new DataStore(dataDir, FILENAME_PREFIX, forceSync); valueCache = new ConcurrentCache<Integer, NativeValue>(valueCacheSize); valueIDCache = new ConcurrentCache<NativeValue, Integer>(valueIDCacheSize); namespaceCache = new ConcurrentCache<Integer, String>(namespaceCacheSize); namespaceIDCache = new ConcurrentCache<String, Integer>(namespaceIDCacheSize); setNewRevision(); }
public V put(K key, V value) { cleanUp(); return cache.put(key, value); }
private String getNamespace(int id) throws IOException { Integer cacheID = new Integer(id); String namespace = namespaceCache.get(cacheID); if (namespace == null) { byte[] namespaceData = dataStore.getData(id); namespace = data2namespace(namespaceData); namespaceCache.put(cacheID, namespace); } return namespace; }
protected void cleanUp() { // This is not thread-safe, but the worst that can happen is that we may (rarely) get slightly longer // cleanup intervals or run cleanUp twice cleanupTick++; if (cleanupTick <= CLEANUP_INTERVAL) return; cleanupTick %= CLEANUP_INTERVAL; synchronized (cache) { final int size = cache.size(); if (size < capacity + CLEANUP_INTERVAL / 2) return; Iterator<K> iter = cache.keySet().iterator(); float removeEachTh = (float)size / (size - capacity); for (int i = 0; iter.hasNext(); i++) { K key = iter.next(); if (i % removeEachTh < 1) cache.computeIfPresent(key, (k, v) -> onEntryRemoval(k) ? null : v); } } } }
/** * Removes all values from the ValueStore. * * @exception IOException * If an I/O error occurred. */ public void clear() throws IOException { try { Lock writeLock = lockManager.getWriteLock(); try { dataStore.clear(); valueCache.clear(); valueIDCache.clear(); namespaceCache.clear(); namespaceIDCache.clear(); initBNodeParams(); setNewRevision(); } finally { writeLock.release(); } } catch (InterruptedException e) { IOException ioe = new IOException("Failed to acquire write lock"); ioe.initCause(e); throw ioe; } }
protected void cleanUp() { // This is not thread-safe, but the worst that can happen is that we may (rarely) get slightly longer // cleanup intervals or run cleanUp twice cleanupTick++; if (cleanupTick <= CLEANUP_INTERVAL) return; cleanupTick %= CLEANUP_INTERVAL; synchronized (cache) { final int size = cache.size(); if (size < capacity + CLEANUP_INTERVAL / 2) return; Iterator<K> iter = cache.keySet().iterator(); float removeEachTh = (float)size / (size - capacity); for (int i = 0; iter.hasNext(); i++) { K key = iter.next(); if (i % removeEachTh < 1) cache.computeIfPresent(key, (k, v) -> onEntryRemoval(k) ? null : v); } } } }
/** * Removes all values from the ValueStore. * * @exception IOException * If an I/O error occurred. */ public void clear() throws IOException { try { Lock writeLock = lockManager.getWriteLock(); try { dataStore.clear(); valueCache.clear(); valueIDCache.clear(); namespaceCache.clear(); namespaceIDCache.clear(); initBNodeParams(); setNewRevision(); } finally { writeLock.release(); } } catch (InterruptedException e) { IOException ioe = new IOException("Failed to acquire write lock"); ioe.initCause(e); throw ioe; } }
private String getNamespace(int id) throws IOException { Integer cacheID = new Integer(id); String namespace = namespaceCache.get(cacheID); if (namespace == null) { byte[] namespaceData = dataStore.getData(id); namespace = data2namespace(namespaceData); namespaceCache.put(cacheID, namespace); } return namespace; }
public ValueStore(File dataDir, boolean forceSync, int valueCacheSize, int valueIDCacheSize, int namespaceCacheSize, int namespaceIDCacheSize) throws IOException { super(); dataStore = new DataStore(dataDir, FILENAME_PREFIX, forceSync); valueCache = new ConcurrentCache<Integer, NativeValue>(valueCacheSize); valueIDCache = new ConcurrentCache<NativeValue, Integer>(valueIDCacheSize); namespaceCache = new ConcurrentCache<Integer, String>(namespaceCacheSize); namespaceIDCache = new ConcurrentCache<String, Integer>(namespaceIDCacheSize); setNewRevision(); }
public V put(K key, V value) { cleanUp(); return cache.put(key, value); }
private int getNamespaceID(String namespace, boolean create) throws IOException { Integer cacheID = namespaceIDCache.get(namespace); if (cacheID != null) { return cacheID.intValue(); } byte[] namespaceData = namespace.getBytes("UTF-8"); int id; if (create) { id = dataStore.storeData(namespaceData); } else { id = dataStore.getID(namespaceData); } if (id != -1) { namespaceIDCache.put(namespace, new Integer(id)); } return id; }
private int getNamespaceID(String namespace, boolean create) throws IOException { Integer cacheID = namespaceIDCache.get(namespace); if (cacheID != null) { return cacheID.intValue(); } byte[] namespaceData = namespace.getBytes("UTF-8"); int id; if (create) { id = dataStore.storeData(namespaceData); } else { id = dataStore.getID(namespaceData); } if (id != -1) { namespaceIDCache.put(namespace, new Integer(id)); } return id; }
/** * Gets the value for the specified ID. * * @param id * A value ID. * @return The value for the ID, or <tt>null</tt> no such value could be found. * @exception IOException * If an I/O error occurred. */ public NativeValue getValue(int id) throws IOException { // Check value cache Integer cacheID = new Integer(id); NativeValue resultValue = valueCache.get(cacheID); if (resultValue == null) { // Value not in cache, fetch it from file byte[] data = dataStore.getData(id); if (data != null) { resultValue = data2value(id, data); // Store value in cache valueCache.put(cacheID, resultValue); } } return resultValue; }
/** * Gets the value for the specified ID. * * @param id * A value ID. * @return The value for the ID, or <tt>null</tt> no such value could be found. * @exception IOException * If an I/O error occurred. */ public NativeValue getValue(int id) throws IOException { // Check value cache Integer cacheID = new Integer(id); NativeValue resultValue = valueCache.get(cacheID); if (resultValue == null) { // Value not in cache, fetch it from file byte[] data = dataStore.getData(id); if (data != null) { resultValue = data2value(id, data); // Store value in cache valueCache.put(cacheID, resultValue); } } return resultValue; }