private final void setState(ItemState newState) { synchronized (mutex) { record.setState(newState); } }
/** * Set the record to the archiver (e.g. revoked) state, if it is allowed, and save. Trows {@link * IllegalStateException} if current state does not allow revocation. Note that the normal states graph allows * revokation only from {@link ItemState#LOCKED} state! */ public void revoke() { if (state == ItemState.LOCKED) { setState(ItemState.REVOKED); save(); } else { throw new IllegalStateException("can't archive record that is not in the locked state"); } }
/** * Set record to the approved state and saves it. Note that this process does not resolve any dependencies as for * now and does not revoke any records locked for revocation. This could be changed though. */ public void approve() { checkLedgerExists(); // check sanity if (state.isPending()) { setState(ItemState.APPROVED); save(); } else throw new IllegalStateException("attempt to approve record that is not pending: " + state); }
/** * Unlock the record if it was in a locked state, does nothing otherwise. * * @return self {@link StateRecord} */ public StateRecord unlock() { switch (state) { case LOCKED: setState(ItemState.APPROVED); setLockedByRecordId(0); break; case LOCKED_FOR_CREATION: destroy(); break; default: break; } return this; }
@Override public StateRecord createOutputLockRecord(long creatorRecordId, HashId newItemHashId) { StateRecord r = new StateRecord(this); r.setState(ItemState.LOCKED_FOR_CREATION); r.setLockedByRecordId(creatorRecordId); r.setId(newItemHashId); try { r.save(); return r; } catch (Ledger.Failure e) { return null; } }
@Override public StateRecord createOutputLockRecord(long creatorRecordId, HashId newItemHashId) { StateRecord r = new StateRecord(this); r.setState(ItemState.LOCKED_FOR_CREATION); r.setLockedByRecordId(creatorRecordId); r.setId(newItemHashId); try { r.save(); return r; } catch (Failure e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }
@Override public StateRecord findOrCreate(HashId itemId) { // This simple version requires that database is used exclusively by one localnode - the normal way. As nodes // are multithreaded, there is absolutely no use to share database between nodes. return protect(() -> { synchronized (writeLock) { StateRecord r = getRecord(itemId); if (r == null) { r = new StateRecord(this); r.setId(itemId); r.setState(ItemState.PENDING); r.save(); } if( r == null ) throw new RuntimeException("failure creating new stateReocrd"); return r; } }); }
private synchronized void addToAllLedgers(Contract c, ItemState state, Node exceptNode) { for( Node n: nodesMap_s.values() ) { if(n != exceptNode) { n.getLedger().findOrCreate(c.getId()).setState(state).save(); } } }
lockedRecord.setState(ItemState.LOCKED); lockedRecord.save();
private synchronized void addToAllLedgers(Contract c, ItemState state, Node exceptNode) { for( Node n: nodesMap_s.values() ) { if(n != exceptNode) { n.getLedger().findOrCreate(c.getId()).setState(state).save(); } } }
private void addToAllLedgers(Contract c, ItemState state, Node exceptNode) { for( Node n: nodes ) { if(n != exceptNode) { n.getLedger().findOrCreate(c.getId()).setState(state).save(); } } }
protected synchronized void addDetailsToAllLedgers(Contract contract) { HashId id; StateRecord orCreate; for (Approvable c : contract.getRevokingItems()) { id = c.getId(); for (Node nodeS : nodesMap.values()) { orCreate = nodeS.getLedger().findOrCreate(id); orCreate.setState(ItemState.APPROVED).save(); } } destroyFromAllNodesExistingNew(contract); destroyCurrentFromAllNodesIfExists(contract); }
StateRecord r2 = ledger.findOrCreate(HashId.createRandom()); int x = ledger.transaction(() -> { r1.setState(ItemState.APPROVED); r2.setState(ItemState.DECLINED); r1.save(); r2.save(); assertEquals(ItemState.DECLINED, r2.getState()); Object y = ledger.transaction(() -> { r1.setState(ItemState.REVOKED); r2.setState(ItemState.DISCARDED); r1.save(); r2.save();
StateRecord r2 = ledger.findOrCreate(HashId.createRandom()); int x = ledger.transaction(() -> { r1.setState(ItemState.APPROVED); r2.setState(ItemState.DECLINED); r1.save(); r2.save(); assertEquals(ItemState.DECLINED, r2.getState()); Object y = ledger.transaction(() -> { r1.setState(ItemState.REVOKED); r2.setState(ItemState.DISCARDED); r1.save(); r2.save();
@Test public void checkNegativeBytesInId() throws Exception { HashId id = HashId.withDigest(Do.randomNegativeBytes(64)); StateRecord r1 = ledger.findOrCreate(id); r1.setState(ItemState.DECLINED); r1.save(); StateRecord r2 = ledger.getRecord(id); assertNotNull(r2); assertNotSame(r1, r2); assertEquals(r1.getState(), r2.getState()); ledger.enableCache(true); StateRecord r3 = ledger.getRecord(id); StateRecord r4 = ledger.getRecord(id); assertEquals(r3.toString(), r4.toString()); // why? assertSame(r3, r4); }
@Test public void checkNegatoveBytesInId() throws Exception { HashId id = HashId.withDigest(Do.randomNegativeBytes(64)); StateRecord r1 = ledger.findOrCreate(id); r1.setState(ItemState.DECLINED); r1.save(); StateRecord r2 = ledger.getRecord(id); assertNotNull(r2); assertNotSame(r1, r2); assertEquals(r1.getState(), r2.getState()); ledger.enableCache(true); StateRecord r3 = ledger.getRecord(id); StateRecord r4 = ledger.getRecord(id); assertEquals(r3.toString(), r4.toString()); // why? assertSame(r3, r4); }
@Test public void revoke() throws Exception { StateRecord r1 = ledger.findOrCreate(HashId.createRandom()); assertFalse(r1.isApproved()); assertTrue(r1.isPending()); assertFalse(r1.isArchived()); r1.approve(); r1.reload(); assertTrue(r1.isApproved()); assertFalse(r1.isPending()); assertFalse(r1.isArchived()); r1.setState(ItemState.LOCKED); r1.revoke(); assertFalse(r1.isPending()); assertFalse(r1.isApproved()); assertTrue(r1.isArchived()); }
@Test public void revoke() throws Exception { StateRecord r1 = ledger.findOrCreate(HashId.createRandom()); assertFalse(r1.isApproved()); assertTrue(r1.isPending()); assertFalse(r1.isArchived()); r1.approve(); r1.reload(); assertTrue(r1.isApproved()); assertFalse(r1.isPending()); assertFalse(r1.isArchived()); r1.setState(ItemState.LOCKED); r1.revoke(); assertFalse(r1.isPending()); assertFalse(r1.isApproved()); assertTrue(r1.isArchived()); }