/** * Set an {@link AtomicCounter} value based on counterId. * * @param counterId to be set. * @param value to set for the counter. */ public void setCounterValue(final int counterId, final long value) { valuesBuffer.putLongOrdered(counterOffset(counterId), value); }
private void checkCountersCapacity(final int counterId) { if ((counterOffset(counterId) + COUNTER_LENGTH) > valuesBuffer.capacity()) { throw new IllegalStateException("Unable to allocate counter, values buffer is full"); } }
/** * Map a status indicator over a buffer. * * @param buffer containing the indicator. * @param counterId identifier of the indicator. */ public UnsafeBufferStatusIndicator(final AtomicBuffer buffer, final int counterId) { this.counterId = counterId; this.byteArray = buffer.byteArray(); this.byteBuffer = buffer.byteBuffer(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Map a counter over a buffer. This version will free the counter on close. * * @param buffer containing the counter. * @param counterId identifier for the counter. * @param countersManager to be called to free the counter on close. */ public AtomicCounter(final AtomicBuffer buffer, final int counterId, final CountersManager countersManager) { this.id = counterId; this.countersManager = countersManager; this.byteBuffer = buffer.byteBuffer(); this.byteArray = buffer.byteArray(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Map a position over a buffer and this indicator owns the counter for reclamation. * * @param buffer containing the counter. * @param counterId identifier of the counter. * @param countersManager to be used for freeing the counter when this is closed. */ public UnsafeBufferPosition(final UnsafeBuffer buffer, final int counterId, final CountersManager countersManager) { this.counterId = counterId; this.countersManager = countersManager; this.byteArray = buffer.byteArray(); this.byteBuffer = buffer.byteBuffer(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
private int nextCounterId() { final long nowMs = epochClock.time(); for (int i = 0, size = freeList.size(); i < size; i++) { final int counterId = freeList.getInt(i); final long deadlineMs = metaDataBuffer.getLongVolatile( metaDataOffset(counterId) + FREE_FOR_REUSE_DEADLINE_OFFSET); if (nowMs >= deadlineMs) { freeList.remove(i); valuesBuffer.putLongOrdered(counterOffset(counterId), 0L); return counterId; } } return ++idHighWaterMark; }
/** * Set an {@link AtomicCounter} value based on counterId. * * @param counterId to be set. * @param value to set for the counter. */ public void setCounterValue(final int counterId, final long value) { valuesBuffer.putLongOrdered(counterOffset(counterId), value); }
private void checkCountersCapacity(final int counterId) { if ((counterOffset(counterId) + COUNTER_LENGTH) > valuesBuffer.capacity()) { throw new IllegalStateException("Unable to allocate counter, values buffer is full"); } }
/** * Set an {@link AtomicCounter} value based on counterId. * * @param counterId to be set. * @param value to set for the counter. */ public void setCounterValue(final int counterId, final long value) { valuesBuffer.putLongOrdered(counterOffset(counterId), value); }
private int nextCounterId() { if (freeList.isEmpty()) { return ++idHighWaterMark; } final int counterId = freeList.popInt(); valuesBuffer.putLongOrdered(counterOffset(counterId), 0L); return counterId; } }
AtomicCounter(final AtomicBuffer buffer, final int counterId, final CountersManager countersManager) { this.counterId = counterId; this.countersManager = countersManager; this.buffer = buffer.byteArray(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Map a status indicator over a buffer. * * @param buffer containing the indicator. * @param counterId identifier of the indicator. */ public UnsafeBufferStatusIndicator(final AtomicBuffer buffer, final int counterId) { this.counterId = counterId; this.buffer = buffer.byteArray(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Map a position over a buffer and this indicator owns the counter for reclamation. * * @param buffer containing the counter. * @param counterId identifier of the counter. * @param countersManager to be used for freeing the counter when this is closed. */ public UnsafeBufferPosition(final UnsafeBuffer buffer, final int counterId, final CountersManager countersManager) { this.counterId = counterId; this.countersManager = countersManager; this.buffer = buffer.byteArray(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Map a status indicator over a buffer. * * @param buffer containing the indicator. * @param counterId identifier of the indicator. */ public UnsafeBufferStatusIndicator(final AtomicBuffer buffer, final int counterId) { this.counterId = counterId; this.byteArray = buffer.byteArray(); this.byteBuffer = buffer.byteBuffer(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Map a position over a buffer and this indicator owns the counter for reclamation. * * @param buffer containing the counter. * @param counterId identifier of the counter. * @param countersManager to be used for freeing the counter when this is closed. */ public UnsafeBufferPosition(final UnsafeBuffer buffer, final int counterId, final CountersManager countersManager) { this.counterId = counterId; this.countersManager = countersManager; this.byteArray = buffer.byteArray(); this.byteBuffer = buffer.byteBuffer(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Map a counter over a buffer. This version will free the counter on close. * * @param buffer containing the counter. * @param counterId identifier for the counter. * @param countersManager to be called to free the counter on close. */ public AtomicCounter(final AtomicBuffer buffer, final int counterId, final CountersManager countersManager) { this.id = counterId; this.countersManager = countersManager; this.byteBuffer = buffer.byteBuffer(); this.byteArray = buffer.byteArray(); final int counterOffset = CountersManager.counterOffset(counterId); buffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.addressOffset = buffer.addressOffset() + counterOffset; }
/** * Allocate a new counter with a given label. * * The key function will be called with a buffer with the exact length of available key space * in the record for the user to store what they want for the key. No offset is required. * * @param label to describe the counter. * @param typeId for the type of counter. * @param keyFunc for setting the key value for the counter. * @return the id allocated for the counter. */ public int allocate(final String label, final int typeId, final Consumer<MutableDirectBuffer> keyFunc) { final int counterId = nextCounterId(); if ((counterOffset(counterId) + COUNTER_LENGTH) > valuesBuffer.capacity()) { throw new IllegalArgumentException("Unable to allocated counter, values buffer is full"); } final int recordOffset = metaDataOffset(counterId); if ((recordOffset + METADATA_LENGTH) > metaDataBuffer.capacity()) { throw new IllegalArgumentException("Unable to allocate counter, labels buffer is full"); } metaDataBuffer.putInt(recordOffset + TYPE_ID_OFFSET, typeId); keyFunc.accept(new UnsafeBuffer(metaDataBuffer, recordOffset + KEY_OFFSET, MAX_KEY_LENGTH)); metaDataBuffer.putStringUtf8(recordOffset + LABEL_OFFSET, label, MAX_LABEL_LENGTH); metaDataBuffer.putIntOrdered(recordOffset, RECORD_ALLOCATED); return counterId; }
private int nextCounterId() { final long nowMs = epochClock.time(); for (int i = 0, size = freeList.size(); i < size; i++) { final int counterId = freeList.getInt(i); final long deadlineMs = metaDataBuffer.getLongVolatile( metaDataOffset(counterId) + FREE_FOR_REUSE_DEADLINE_OFFSET); if (nowMs >= deadlineMs) { freeList.remove(i); valuesBuffer.putLongOrdered(counterOffset(counterId), 0L); return counterId; } } return ++idHighWaterMark; }