/** * 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; }
/** * Update the store version. * * @param version the new store version */ public synchronized void setStoreVersion(int version) { checkOpen(); markMetaChanged(); meta.put("setting.storeVersion", Integer.toHexString(version)); }
@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; }
@Override public void addRowsToBuffer(List<Row> rows, String bufferName) { MVMap<ValueArray, Value> map = openMap(bufferName); for (Row row : rows) { ValueArray key = convertToKey(row); map.put(key, ValueNull.INSTANCE); } }
/** * Update the value for the given key, without adding an undo log entry. * * @param key the key * @param value the value * @return the old value */ @SuppressWarnings("unchecked") public V putCommitted(K key, V value) { DataUtils.checkArgument(value != null, "The value may not be null"); VersionedValue newValue = new VersionedValue(0L, value); VersionedValue oldValue = map.put(key, newValue); return (V) (oldValue == null ? null : oldValue.value); }
/** * 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; }
@Override public void addNewSubscription(Subscription subscription) { subscriptions.put(subscription.getTopicFilter() + "-" + subscription.getClientId(), subscription); }
private ValueLobDb createLob(InputStream in, int type) throws IOException { byte[] streamStoreId; try { streamStoreId = streamStore.put(in); } catch (Exception e) { throw DbException.convertToIOException(e); } long lobId = generateLobId(); long length = streamStore.length(streamStoreId); int tableId = LobStorageFrontend.TABLE_TEMP; Object[] value = { streamStoreId, tableId, length, 0 }; 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("create " + tableId + "/" + lobId); } return lob; }
/** * Log an entry. * * @param t the transaction * @param logId the log id * @param mapId the map id * @param key the key * @param oldValue the old value */ void log(Transaction t, long logId, int mapId, Object key, Object oldValue) { Long undoKey = getOperationId(t.getId(), logId); Object[] log = { mapId, key, oldValue }; rwLock.writeLock().lock(); try { if (logId == 0) { if (undoLog.containsKey(undoKey)) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_TOO_MANY_OPEN_TRANSACTIONS, "An old transaction with the same id " + "is still open: {0}", t.getId()); } } undoLog.put(undoKey, log); } finally { rwLock.writeLock().unlock(); } }
@Override public void retain(Topic topic, MqttPublishMessage msg) { final ByteBuf payload = msg.content(); byte[] rawPayload = new byte[payload.readableBytes()]; payload.getBytes(0, rawPayload); final RetainedMessage toStore = new RetainedMessage(msg.fixedHeader().qosLevel(), rawPayload); queueMap.put(topic, toStore); }
private synchronized void freeUnusedChunks() { if (lastChunk == null || !reuseSpace) { return; } Set<Integer> referenced = collectReferencedChunks(); long time = getTimeSinceCreation(); for (Iterator<Chunk> it = chunks.values().iterator(); it.hasNext(); ) { Chunk c = it.next(); if (!referenced.contains(c.id)) { if (canOverwriteChunk(c, time)) { it.remove(); markMetaChanged(); meta.remove(Chunk.getMetaKey(c.id)); long start = c.block * BLOCK_SIZE; int length = c.len * BLOCK_SIZE; fileStore.free(start, length); } else { if (c.unused == 0) { c.unused = time; meta.put(Chunk.getMetaKey(c.id), c.asString()); markMetaChanged(); } } } } }
/** * Store a transaction. * * @param t the transaction */ synchronized void storeTransaction(Transaction t) { if (t.getStatus() == Transaction.STATUS_PREPARED || t.getName() != null) { Object[] v = { t.getStatus(), t.getName() }; preparedTransactions.put(t.getId(), v); } }
/** * Rename a map. * * @param map the map * @param newName the new name */ public synchronized void renameMap(MVMap<?, ?> map, String newName) { checkOpen(); DataUtils.checkArgument(map != meta, "Renaming the meta map is not allowed"); int id = map.getId(); String oldName = getMapName(id); if (oldName.equals(newName)) { return; } DataUtils.checkArgument( !meta.containsKey("name." + newName), "A map named {0} already exists", newName); markMetaChanged(); String x = Integer.toHexString(id); meta.remove("name." + oldName); meta.put(MVMap.getMapKey(id), map.asString(newName)); meta.put("name." + newName, x); }
targetMeta.put(key, m.getValue());
@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; }
/** * Add or replace a key-value pair. * * @param key the key (may not be null) * @param value the value (may not be null) * @return the old value if the key existed, or null otherwise */ @Override @SuppressWarnings("unchecked") public synchronized V put(K key, V value) { DataUtils.checkArgument(value != null, "The value may not be null"); beforeWrite(); long v = writeVersion; Page p = root.copy(v); p = splitRootIfNeeded(p, v); Object result = put(p, v, key, value); newRoot(p); return (V) result; }
map.put(k, v); lastPage = p;