public int extractSequence() { return Encoder.bytesToInt(array, array.length - 4); // - sequence }
public long extractTimestamp() { return Encoder.bytesToLong(array, array.length - 4 - 8); // - sequence - timestamp }
public void testLongEncodingDecoding() { byte[] longAsBytes; long result; longAsBytes = Encoder.longToBytes(Long.MAX_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MAX_VALUE, result); longAsBytes = Encoder.longToBytes(Long.MIN_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MIN_VALUE, result); longAsBytes = Encoder.longToBytes(-1L); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(-1L, result); long timestamp = System.currentTimeMillis(); longAsBytes = Encoder.longToBytes(timestamp); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(timestamp, result); byte[] intAsBytes = Encoder.intToBytes(-1); int resultAsInt = Encoder.bytesToInt(intAsBytes, 0); assertEquals(-1, resultAsInt); try { Encoder.bytesToLong(new byte[4], 0); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException ex) { assertEquals("a long can only be decoded from 8 bytes of an array (got a 4 byte(s) array, must start at position 0)", ex.getMessage()); } }
/** * Calculate the CRC32 value of this record. * @return the CRC32 value of this record. */ public int calculateCrc32() { CRC32 crc32 = new CRC32(); crc32.update(Encoder.intToBytes(status)); crc32.update(Encoder.intToBytes(recordLength)); crc32.update(Encoder.intToBytes(headerLength)); crc32.update(Encoder.longToBytes(time)); crc32.update(Encoder.intToBytes(sequenceNumber)); crc32.update(gtrid.getArray()); crc32.update(Encoder.intToBytes(uniqueNames.size())); for (String name : uniqueNames) { crc32.update(Encoder.shortToBytes((short) name.length())); try { crc32.update(name.getBytes("US-ASCII")); } catch (UnsupportedEncodingException ex) { log.error("unable to convert unique name bytes to US-ASCII", ex); } } crc32.update(Encoder.intToBytes(endRecord)); return (int) crc32.getValue(); }
/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
byte[] prependedBytes = Encoder.longToBytes(MonotonicClock.currentTimeMillis());
public void testLongEncodingDecoding() { byte[] longAsBytes; long result; longAsBytes = Encoder.longToBytes(Long.MAX_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MAX_VALUE, result); longAsBytes = Encoder.longToBytes(Long.MIN_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MIN_VALUE, result); longAsBytes = Encoder.longToBytes(-1L); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(-1L, result); long timestamp = System.currentTimeMillis(); longAsBytes = Encoder.longToBytes(timestamp); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(timestamp, result); byte[] intAsBytes = Encoder.intToBytes(-1); int resultAsInt = Encoder.bytesToInt(intAsBytes, 0); assertEquals(-1, resultAsInt); try { Encoder.bytesToLong(new byte[4], 0); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException ex) { assertEquals("a long can only be decoded from 8 bytes of an array (got a 4 byte(s) array, must start at position 0)", ex.getMessage()); } }
/** * Calculate the CRC32 value of this record. * @return the CRC32 value of this record. */ public int calculateCrc32() { CRC32 crc32 = new CRC32(); crc32.update(Encoder.intToBytes(status)); crc32.update(Encoder.intToBytes(recordLength)); crc32.update(Encoder.intToBytes(headerLength)); crc32.update(Encoder.longToBytes(time)); crc32.update(Encoder.intToBytes(sequenceNumber)); crc32.update(gtrid.getArray()); crc32.update(Encoder.intToBytes(uniqueNames.size())); for (String name : uniqueNames) { crc32.update(Encoder.shortToBytes((short) name.length())); try { crc32.update(name.getBytes("US-ASCII")); } catch (UnsupportedEncodingException ex) { log.error("unable to convert unique name bytes to US-ASCII", ex); } } crc32.update(Encoder.intToBytes(endRecord)); return (int) crc32.getValue(); }
/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
byte[] prependedBytes = Encoder.longToBytes(MonotonicClock.currentTimeMillis());
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
public void testHexaStringEncoder() throws Exception { byte[] result = Encoder.intToBytes(0x80); String hexString = new Uid(result).toString(); assertEquals("00000080", hexString); result = Encoder.longToBytes(0x81); hexString = new Uid(result).toString(); assertEquals("0000000000000081", hexString); result = Encoder.shortToBytes((short) 0xff); hexString = new Uid(result).toString(); assertEquals("00FF", hexString); }
/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
public long extractTimestamp() { return Encoder.bytesToLong(array, array.length - 4 - 8); // - sequence - timestamp }
public int extractSequence() { return Encoder.bytesToInt(array, array.length - 4); // - sequence }
byte[] prependedBytes = Encoder.longToBytes(MonotonicClock.currentTimeMillis());
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
public void testHexaStringEncoder() throws Exception { byte[] result = Encoder.intToBytes(0x80); String hexString = new Uid(result).toString(); assertEquals("00000080", hexString); result = Encoder.longToBytes(0x81); hexString = new Uid(result).toString(); assertEquals("0000000000000081", hexString); result = Encoder.shortToBytes((short) 0xff); hexString = new Uid(result).toString(); assertEquals("00FF", hexString); }
/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
public long extractTimestamp() { return Encoder.bytesToLong(array, array.length - 4 - 8); // - sequence - timestamp }