@Override public boolean containsKey(Object key) { return get(key) != null; }
private static long getRootPos(MVMap<String, String> map, int mapId) { String root = map.get(MVMap.getMapRootKey(mapId)); return root == null ? 0 : DataUtils.parseHexLong(root); }
/** * Get the name of the given map. * * @param id the map id * @return the name, or null if not found */ public synchronized String getMapName(int id) { checkOpen(); String m = meta.get(MVMap.getMapKey(id)); return m == null ? null : DataUtils.getMapName(m); }
while (logId >= toLogId) { Long undoKey = getOperationId(t.getId(), logId); Object[] op = undoLog.get(undoKey); logId--; if (op == null) {
@Override public T peek() { if (head.equals(tail)) { return null; } return this.queueMap.get(tail.get()); }
private VersionedValue getValue(K key, long maxLog) { transaction.store.rwLock.readLock().lock(); try { VersionedValue data = map.get(key); return getValue(key, maxLog, data); } finally { transaction.store.rwLock.readLock().unlock(); } }
/** * Add a key-value pair if it does not yet exist. * * @param key the key (may not be null) * @param value the new value * @return the old value if the key existed, or null otherwise */ @Override public synchronized V putIfAbsent(K key, V value) { V old = get(key); if (old == null) { put(key, value); } return old; }
/** * Replace a value for an existing key. * * @param key the key (may not be null) * @param value the new value * @return the old value, if the value was replaced, or null */ @Override public synchronized V replace(K key, V value) { V old = get(key); if (old != null) { put(key, value); return old; } return null; }
/** * Whether the entry for this key was added or removed from this * session. * * @param key the key * @return true if yes */ public boolean isSameTransaction(K key) { VersionedValue data = map.get(key); if (data == null) { // doesn't exist or deleted by a committed transaction return false; } int tx = getTransactionId(data.operationId); return tx == transaction.transactionId; }
@Override public T poll() { if (head.equals(tail)) { return null; } final long nextTail = tail.getAndIncrement(); final T tail = this.queueMap.get(nextTail); queueMap.remove(nextTail); this.metadataMap.put("tail", nextTail + 1); return tail; }
/** * Get the store version. The store version is usually used to upgrade the * structure of the store after upgrading the application. Initially the * store version is 0, until it is changed. * * @return the store version */ public int getStoreVersion() { checkOpen(); String x = meta.get("setting.storeVersion"); return x == null ? 0 : DataUtils.parseHexInt(x); }
private void loadChunkMeta() { // load the chunk metadata: we can load in any order, // because loading chunk metadata might recursively load another chunk for (Iterator<String> it = meta.keyIterator("chunk."); it.hasNext();) { String s = it.next(); if (!s.startsWith("chunk.")) { break; } s = meta.get(s); Chunk c = Chunk.fromString(s); if (chunks.putIfAbsent(c.id, c) == null) { if (c.block == Long.MAX_VALUE) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_FILE_CORRUPT, "Chunk {0} is invalid", c.id); } } } }
/** * Remove a key-value pair if the value matches the stored one. * * @param key the key (may not be null) * @param value the expected value * @return true if the item was removed */ @Override public synchronized boolean remove(Object key, Object value) { V old = get(key); if (areValuesEqual(old, value)) { remove(key); return true; } return false; }
/** * Replace a value for an existing key, if the value matches. * * @param key the key (may not be null) * @param oldValue the expected value * @param newValue the new value * @return true if the value was replaced */ @Override public synchronized boolean replace(K key, V oldValue, V newValue) { V old = get(key); if (areValuesEqual(old, oldValue)) { put(key, newValue); return true; } return false; }
H2PersistentQueue(MVStore store, String queueName) { if (queueName == null || queueName.isEmpty()) { throw new IllegalArgumentException("queueName parameter can't be empty or null"); } this.queueMap = store.openMap("queue_" + queueName); this.metadataMap = store.openMap("queue_" + queueName + "_meta"); //setup head index long headIdx = 0L; if (this.metadataMap.containsKey("head")) { headIdx = this.metadataMap.get("head"); } else { this.metadataMap.put("head", headIdx); } this.head = new AtomicLong(headIdx); //setup tail index long tailIdx = 0L; if (this.metadataMap.containsKey("tail")) { tailIdx = this.metadataMap.get("tail"); } else { this.metadataMap.put("tail", tailIdx); } this.tail = new AtomicLong(tailIdx); }
private Chunk getChunkIfFound(long pos) { int chunkId = DataUtils.getPageChunkId(pos); Chunk c = chunks.get(chunkId); if (c == null) { checkOpen(); if (!Thread.holdsLock(this)) { // it could also be unsynchronized metadata // access (if synchronization on this was forgotten) throw DataUtils.newIllegalStateException( DataUtils.ERROR_CHUNK_NOT_FOUND, "Chunk {0} no longer exists", chunkId); } String s = meta.get(Chunk.getMetaKey(chunkId)); if (s == null) { return null; } c = Chunk.fromString(s); if (c.block == Long.MAX_VALUE) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_FILE_CORRUPT, "Chunk {0} is invalid", chunkId); } chunks.put(c.id, c); } return c; }
key = undoLog.lowerKey(getOperationId(transactionId + 1, 0)); long logId = getLogId(key) + 1; Object[] data = preparedTransactions.get(transactionId); int status; String name;
/** * Remove a key-value pair, if the key exists. * * @param key the key (may not be null) * @return the old value if the key existed, or null otherwise */ @Override @SuppressWarnings("unchecked") public V remove(Object key) { beforeWrite(); V result = get(key); if (result == null) { return null; } long v = writeVersion; synchronized (this) { Page p = root.copy(v); result = (V) remove(p, v, key); if (!p.isLeaf() && p.getTotalCount() == 0) { p.removePage(); p = Page.createEmpty(this, p.getVersion()); } newRoot(p); } return result; }
@Override public InputStream getInputStream(ValueLobDb lob, byte[] hmac, long byteCount) throws IOException { init(); Object[] value = lobMap.get(lob.getLobId()); if (value == null) { if (lob.getTableId() == LobStorageFrontend.TABLE_RESULT || lob.getTableId() == LobStorageFrontend.TABLE_ID_SESSION_VARIABLE) { throw DbException.get( ErrorCode.LOB_CLOSED_ON_TIMEOUT_1, "" + lob.getLobId() + "/" + lob.getTableId()); } throw DbException.throwInternalError("Lob not found: " + lob.getLobId() + "/" + lob.getTableId()); } byte[] streamStoreId = (byte[]) value[0]; return streamStore.get(streamStoreId); }
@Override public ValueLobDb copyLob(ValueLobDb old, int tableId, long length) { init(); int type = old.getType(); long oldLobId = old.getLobId(); long oldLength = old.getPrecision(); if (oldLength != length) { throw DbException.throwInternalError("Length is different"); } Object[] value = lobMap.get(oldLobId); value = value.clone(); byte[] streamStoreId = (byte[]) value[0]; long lobId = generateLobId(); value[1] = tableId; lobMap.put(lobId, value); Object[] key = { streamStoreId, lobId }; refMap.put(key, Boolean.TRUE); ValueLobDb lob = ValueLobDb.create( type, database, tableId, lobId, null, length); if (TRACE) { trace("copy " + old.getTableId() + "/" + old.getLobId() + " > " + tableId + "/" + lobId); } return lob; }