/// <summary> /// 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. /// </summary> /// <param name="label"> to describe the counter. </param> /// <param name="typeId"> for the type of counter. </param> /// <param name="keyFunc"> for setting the key value for the counter. </param> /// <returns> the id allocated for the counter. </returns> public int Allocate(string label, int typeId, Action <IMutableDirectBuffer> keyFunc) { var counterId = NextCounterId(); CheckCountersCapacity(counterId); var recordOffset = MetaDataOffset(counterId); CheckMetaDataCapacity(recordOffset); try { MetaDataBuffer.PutInt(recordOffset + TYPE_ID_OFFSET, typeId); keyFunc(new UnsafeBuffer(MetaDataBuffer, recordOffset + KEY_OFFSET, MAX_KEY_LENGTH)); MetaDataBuffer.PutLong(recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, NOT_FREE_TO_REUSE); PutLabel(recordOffset, label); MetaDataBuffer.PutIntOrdered(recordOffset, RECORD_ALLOCATED); } catch (Exception) { _freeList.Add(counterId); throw; } return(counterId); }
/// <summary> /// Allocate a new counter with a given label and type. /// </summary> /// <param name="label"> to describe the counter. </param> /// <param name="typeId"> for the type of counter. </param> /// <returns> the id allocated for the counter. </returns> public int Allocate(string label, int typeId = DEFAULT_TYPE_ID) { int counterId = NextCounterId(); CheckCountersCapacity(counterId); int recordOffset = MetaDataOffset(counterId); CheckMetaDataCapacity(recordOffset); try { MetaDataBuffer.PutInt(recordOffset + TYPE_ID_OFFSET, typeId); MetaDataBuffer.PutLong(recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, NOT_FREE_TO_REUSE); PutLabel(recordOffset, label); MetaDataBuffer.PutIntOrdered(recordOffset, RECORD_ALLOCATED); } catch (Exception) { _freeList.Add(counterId); throw; } return(counterId); }
/// <summary> /// Free the counter identified by counterId. /// </summary> /// <param name="counterId"> the counter to freed </param> public void Free(int counterId) { int recordOffset = MetaDataOffset(counterId); MetaDataBuffer.PutLong(recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, _epochClock.Time() + _freeToReuseTimeoutMs); MetaDataBuffer.PutIntOrdered(recordOffset, RECORD_RECLAIMED); _freeList.Add(counterId); }
/// <summary> /// Allocate a counter with the minimum of allocation by allowing the label an key to be provided and copied. /// <para> /// If the keyBuffer is null then a copy of the key is not attempted. /// /// </para> /// </summary> /// <param name="typeId"> for the counter. </param> /// <param name="keyBuffer"> containing the optional key for the counter. </param> /// <param name="keyOffset"> within the keyBuffer at which the key begins. </param> /// <param name="keyLength"> of the key in the keyBuffer. </param> /// <param name="labelBuffer"> containing the mandatory label for the counter. </param> /// <param name="labelOffset"> within the labelBuffer at which the label begins. </param> /// <param name="labelLength"> of the label in the labelBuffer. </param> /// <returns> the id allocated for the counter. </returns> public int Allocate(int typeId, IDirectBuffer keyBuffer, int keyOffset, int keyLength, IDirectBuffer labelBuffer, int labelOffset, int labelLength) { int counterId = NextCounterId(); CheckCountersCapacity(counterId); int recordOffset = MetaDataOffset(counterId); CheckMetaDataCapacity(recordOffset); try { MetaDataBuffer.PutInt(recordOffset + TYPE_ID_OFFSET, typeId); MetaDataBuffer.PutLong(recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, NOT_FREE_TO_REUSE); int length; if (null != keyBuffer) { length = Math.Min(keyLength, MAX_KEY_LENGTH); MetaDataBuffer.PutBytes(recordOffset + KEY_OFFSET, keyBuffer, keyOffset, length); } length = Math.Min(labelLength, MAX_LABEL_LENGTH); MetaDataBuffer.PutInt(recordOffset + LABEL_OFFSET, length); MetaDataBuffer.PutBytes(recordOffset + LABEL_OFFSET + BitUtil.SIZE_OF_INT, labelBuffer, labelOffset, length); MetaDataBuffer.PutIntOrdered(recordOffset, RECORD_ALLOCATED); } catch (Exception) { _freeList.Add(counterId); throw; } return(counterId); }