/** * Creates a new UnsafeMetricDefinition * * @param globalId The global id of the metric * @param timestamp The creation timestamp of this metric, or -1L if the metric is new, in which * case the currnt time will be substituted * @param name The metric name * @param opaqueKey The opaque key */ public UnsafeMetricDefinition(long globalId, long timestamp, String name, byte[] opaqueKey) { byte[] nameBytes = getBytes(name); byte[] opaqueBytes = getBytes(opaqueKey); int size = BASE_SIZE + nameBytes.length + opaqueBytes.length; address[0] = UnsafeAdapter.allocateAlignedMemory(size); UnsafeAdapter.registerForDeAlloc(this); UnsafeAdapter.putByte(address[0], DELETE_FLAG); UnsafeAdapter.putInt(address[0] + SIZE, size); UnsafeAdapter.putLong(address[0] + ID, globalId); UnsafeAdapter.putLong( address[0] + TIMESTAMP, timestamp != -1L ? timestamp : System.currentTimeMillis()); UnsafeAdapter.putInt(address[0] + NAME_SIZE, nameBytes.length); UnsafeAdapter.putInt(address[0] + OPAQUE_SIZE, opaqueBytes.length); if (nameBytes.length > 0) { UnsafeAdapter.copyMemory( nameBytes, UnsafeAdapter.BYTE_ARRAY_OFFSET, null, address[0] + NAME_BYTES, nameBytes.length); } if (opaqueBytes.length > 0) { UnsafeAdapter.copyMemory( opaqueBytes, UnsafeAdapter.BYTE_ARRAY_OFFSET, null, address[0] + NAME_BYTES + nameBytes.length, opaqueBytes.length); } }
/** * Returns the bytes for the name * * @return the bytes for the name */ public byte[] getNameBytes() { if (address[0] == -1L) return null; int size = getNameSize(); if (size == 0) return EMPTY_BYTE_ARR; byte[] bytes = new byte[size]; UnsafeAdapter.copyMemory( null, address[0] + NAME_BYTES, bytes, UnsafeAdapter.BYTE_ARRAY_OFFSET, size); return bytes; }
/** * {@inheritDoc} * * @see org.helios.rindle.metric.IMetricDefinition#getOpaqueKey() */ @Override public byte[] getOpaqueKey() { int size = getOpaqueSize(); if (size == 0) return null; byte[] bytes = new byte[size]; UnsafeAdapter.copyMemory( null, address[0] + NAME_BYTES + getNameSize(), bytes, UnsafeAdapter.BYTE_ARRAY_OFFSET, size); return bytes; }