public ResyncNotification(NodeInfo from, HashId itemId, ItemState itemState, Boolean hasEnvironment, boolean answerIsRequested) throws IOException { super(from, itemId, new ItemResult(new StateRecord(itemId)), answerIsRequested); this.itemState = itemState; this.hasEnvironment = hasEnvironment ? 1 : 0; }
public ResyncNotification(NodeInfo from, HashId itemId, boolean answerIsRequested) throws IOException { super(from, itemId, new ItemResult(new StateRecord(itemId)), answerIsRequested); }
@Override public Map<HashId,StateRecord> findUnfinished() { return protect(() -> { HashMap<HashId, StateRecord> map = new HashMap<>(); try (ResultSet rs = inPool(db -> db.queryRow("select * from sr_find_unfinished()"))) { if (rs != null) { do { StateRecord record = new StateRecord(this, rs); if (record.isExpired()) { record.destroy(); } else { map.put(record.getId(), record); } } while (rs.next()); } } catch (Exception e) { e.printStackTrace(); throw e; } return map; }); }
@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; } }); }
@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(() -> { StateRecord record = getFromCache(itemId); if (record == null) { try (ResultSet rs = inPool(db -> db.queryRow("select * from sr_find_or_create(?)", itemId.getDigest()))) { record = new StateRecord(this, rs); putToCache(record); } catch (Exception e) { e.printStackTrace(); throw e; } } return record; }); }
@Override public StateRecord getRecord(HashId itemId) { StateRecord sr = protect(() -> { StateRecord cached = getFromCache(itemId); if (cached != null) return cached; try (ResultSet rs = inPool(db -> db.queryRow("SELECT * FROM ledger WHERE hash = ? limit 1", itemId.getDigest()))) { if (rs != null) { StateRecord record = new StateRecord(this, rs); putToCache(record); return record; } } catch (Exception e) { e.printStackTrace(); throw e; } return null; }); if (sr != null && sr.isExpired()) { sr.destroy(); return null; } return sr; }
@Override public StateRecord getLockOwnerOf(StateRecord rc) { StateRecord cached = getFromCacheById(rc.getLockedByRecordId()); if (cached != null) { return cached; } StateRecord sr = protect(() -> dbPool.execute(db -> { try (ResultSet rs = db.queryRow("SELECT * FROM ledger WHERE id = ? limit 1", rc.getLockedByRecordId())) { if (rs == null) return null; StateRecord r = new StateRecord(this, rs); putToCache(r); return r; } catch (Exception e) { e.printStackTrace(); throw e; } }) ); if (sr != null && sr.isExpired()) { sr.destroy(); return null; } return sr; }
@Override public StateRecord getRecord(HashId itemId) { StateRecord sr = protect(() -> { StateRecord cached = getFromCache(itemId); if (cached != null) return cached; try (ResultSet rs = db.queryRow("SELECT * FROM ledger WHERE hash = ? limit 1", itemId.getDigest())) { if (rs != null) { StateRecord record = new StateRecord(this, rs); putToCache(record); return record; } } return null; }); if (sr != null && sr.isExpired()) { sr.destroy(); return null; } return sr; }