public boolean deReferenceOrDeleteValue(final Txn<ByteBuffer> writeTxn,
                                            final ByteBuffer keyBuffer,
                                            final KeyConsumer onDeleteAction) {
        LAMBDA_LOGGER.trace(() -> LogUtil.message("deReferenceValue({}, {})",
                writeTxn, ByteBufferUtils.byteBufferInfo(keyBuffer)));

        try (Cursor<ByteBuffer> cursor = getLmdbDbi().openCursor(writeTxn)) {
            boolean isFound = cursor.get(keyBuffer, GetOp.MDB_SET_KEY);
            if (isFound) {
                final ByteBuffer valueBuffer = cursor.val();

                // We run LMDB in its default mode of read only mmaps so we cannot mutate the key/value
                // bytebuffers.  Instead, we must copy the content and put the replacement entry.
                // We could run LMDB in MDB_WRITEMAP mode which allows mutation of the buffers (and
                // thus avoids the buffer copy cost) but adds more risk of DB corruption. As we are not
                // doing a high volume of value mutations read-only mode is a safer bet.
                boolean isLastReference = valueSerde.isLastReference(valueBuffer);

                if (isLastReference) {
                    // we have the last ref to this value, so we can delete it
                    LAMBDA_LOGGER.trace(() -> LogUtil.message(
                            "Ref count is zero, deleting entry for key {}",
                            ByteBufferUtils.byteBufferInfo(keyBuffer)));
                    cursor.delete();

                    // perform any post delete actions
                    onDeleteAction.accept(writeTxn, keyBuffer);
                    return true;

                } else {
                    // other people have a ref to it so just decrement the ref count
                    try (PooledByteBuffer pooledNewValueBuffer = getPooledValueBuffer()) {
                        final ByteBuffer newValueBuf = pooledNewValueBuffer.getByteBuffer();
                        valueSerde.cloneAndDecrementRefCount(
                                valueBuffer,
                                newValueBuf);

                        if (LAMBDA_LOGGER.isTraceEnabled()) {
                            int oldRefCount = valueSerde.extractReferenceCount(keyBuffer);
                            int newRefCount = valueSerde.extractReferenceCount(newValueBuf);
                            LAMBDA_LOGGER.trace(() -> LogUtil.message(
                                    "Updating entry ref count from {} to {} for key {}",
                                    oldRefCount,
                                    newRefCount,
                                    ByteBufferUtils.byteBufferInfo(keyBuffer)));
                        }
                        cursor.put(cursor.key(), newValueBuf, PutFlags.MDB_CURRENT);
                    }
                    return false;
                }
            } else {
                // Entry not found. This should not really happen as a value store entry should only be deleted
                // if its reference count drops to zero which should not be the case here as our KV entry
                // holds a ref to it. It indicates we have a problem somewhere.

                LAMBDA_LOGGER.warn(() -> "Expected to find a valueStoreMetaDb entry found with key: "
                        + ByteBufferUtils.byteBufferInfo(keyBuffer));
                // It is not there, which is what we want, so we have effectively deleted it, however
                // I think we have to assume if it is not there then the corresponding valueStoreDb entry is also
                // not there so don't call the onDeleteAction.
                return true;
            }
        }
    }
Decrements the reference count for this value. If the resulting reference count is zero or less
the value will be deleted as it is no longer required.

@return True if the value was deleted as a result of the reference count reaching zero, else false.
stroom-pipeline/src/main/java/stroom/pipeline/refdata/store/offheapstore/databases/ValueStoreMetaDb.java