/** * @param idString TxnId in YYYY-DDD-SSS-NN-TTTTT format * * <ul> * <li><code>CYY</code> Century Year Year</li> * <li><code>DDD</code> day of year</li> * <li><code>SSS</code> second of day</li> * <li><code>NNN</code> unique node number (000 to 999)</li> * <li><code>TTTTT</code> last 5 digits of transaction manager's transaction id</li> * </ul> */ public static TxnId parse (String idString) { Matcher matcher = pattern.matcher(idString); if (!matcher.matches() && matcher.groupCount() != 5) throw new IllegalArgumentException("Invalid idString '" + idString + "'"); return new TxnId().init( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), Integer.parseInt(matcher.group(5)) ); }
@Test public void test() { TxnId txnId = TxnId.create(DateTime.now(), 0, 1L); TxnId txnId1 = TxnId.parse(txnId.toString()); TxnId txnId2 = TxnId.parse(txnId.id()); TxnId txnId3 = TxnId.fromRrn(txnId.toRrn()); assertEquals(txnId1, txnId2); assertEquals(txnId1.toString(), txnId2.toString()); assertEquals(txnId2, txnId3); assertEquals(txnId2.toString(), txnId3.toString()); assertEquals(12, txnId.toRrn().length()); }
@Test public void testNegativeId() { try { TxnId.parse(-Long.MAX_VALUE); fail ("Should raise exception"); } catch (IllegalArgumentException ignored) { } }
/** * Parse TxnId from long * * @param id value * @return newly created TxnId */ public static TxnId parse (long id) { if (id <0 || id > MAX_VALUE) throw new IllegalArgumentException("Invalid id " + id); return new TxnId(id); }
@Test public void testInvalidRrn() { try { TxnId id = TxnId.fromRrn(Long.toString(Long.MAX_VALUE,36)); fail ("Should raise exception - TxnId=" + id); } catch (IllegalArgumentException e) { assertEquals("Invalid rrn 1y2p0ij32e8e7", e.getMessage()); } }
@Test public void testMaxValue() { TxnId txnId = TxnId.parse(4738381338321616895L); TxnId txnId1 = TxnId.parse(txnId.toString()); TxnId txnId2 = TxnId.parse(txnId.id()); TxnId txnId3 = TxnId.fromRrn(txnId.toRrn()); assertEquals(txnId1, txnId2); assertEquals(txnId1.toString(), txnId2.toString()); assertEquals(txnId2, txnId3); assertEquals(txnId2.toString(), txnId3.toString()); assertEquals(12, txnId.toRrn().length()); } }
@Test public void testBigId() { try { TxnId.parse(Long.MAX_VALUE); fail ("Should raise exception"); } catch (IllegalArgumentException ignored) { } }
/** * Parse TxnId from rrn * * @param rrn value * @return newly created TxnId */ public static TxnId fromRrn (String rrn) { long id = Long.parseLong(rrn, 36); if (id <0 || id > MAX_VALUE) throw new IllegalArgumentException("Invalid rrn " + rrn); return new TxnId(id); } }
/** * Creates new TxnId object * * @param dt Transaction's TIMESTAMP DateTime * @param node node id * @param transactionId TransactionManager's ID */ public static TxnId create (DateTime dt, int node, long transactionId) { TxnId id = new TxnId(); if (dt.getZone() != DateTimeZone.UTC) dt = dt.toDateTime(DateTimeZone.UTC); return id.init (dt.getYear()-2000, dt.getDayOfYear(), dt.getSecondOfDay(), node, transactionId); }