private <T> T protect(Callable<T> block) { try { return block.call(); } catch (Exception ex) { ex.printStackTrace(); throw new Failure("Ledger operation failed: " + ex.getMessage(), ex); } }
private <T> T protect(Callable<T> block) { try { return block.call(); } catch (Exception ex) { throw new Ledger.Failure("Ledger operation failed: " + ex.getMessage(), ex); } }
@Override public NNameRecord getNameByAddress (String address) { return getNameBy("WHERE name_storage.id=(SELECT name_storage_id FROM name_entry WHERE short_addr=? OR long_addr=? LIMIT 1) ", (statement)-> { try { statement.setString(1, address); statement.setString(2, address); } catch (SQLException e) { throw new Failure("getNameByAddress failed: " + e); } }); }
@Override public void clearExpiredSubscriptions() { try (PooledDb db = dbPool.db()) { ZonedDateTime now = ZonedDateTime.now(); try ( PreparedStatement statement = db.statement( "DELETE FROM contract_subscription WHERE expires_at < ?" ) ) { statement.setLong(1, StateRecord.unixTime(now)); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("clearExpiredSubscriptions failed: " + se); } catch (Exception e) { e.printStackTrace(); } }
@Override public void removeNameRecord(final String nameReduced) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM name_storage WHERE name_reduced=?" ) ) { statement.setString(1, nameReduced); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("removeNameRecord failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("removeNameRecord failed: " + e); } }
@Override public void removeEnvironmentStorage(long storageId) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM contract_storage WHERE id = ?" ) ) { statement.setLong(1, storageId); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("removeEnvironmentStorage failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("removeEnvironmentStorage failed: " + e); } }
@Override public void removeEnvironmentSubscription(long subscriptionId) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM contract_subscription WHERE id = ?" ) ) { statement.setLong(1, subscriptionId); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("removeEnvironmentSubscription failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("removeEnvironmentSubscription failed: " + e); } }
@Override public void clearExpiredStorages() { try (PooledDb db = dbPool.db()) { ZonedDateTime now = ZonedDateTime.now(); try ( PreparedStatement statement = db.statement( "DELETE FROM contract_storage WHERE expires_at < ?" ) ) { statement.setLong(1, StateRecord.unixTime(now)); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("clearExpiredStorages failed: " + se); } catch (Exception e) { e.printStackTrace(); } }
private void removeNameRecordEntries(final long nameStorageId) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM name_entry WHERE name_storage_id=?" ) ) { statement.setLong(1, nameStorageId); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("removeNameRecordEntries failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("removeNameRecordEntries failed: " + e); } }
@Override public void removeFollowerCallback(HashId id) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM follower_callbacks WHERE id = ?" ) ) { statement.setBytes(1, id.getDigest()); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("follower callback delete failed:" + se); } catch (Exception e) { e.printStackTrace(); } }
@Override public void removeNode(NodeInfo nodeInfo) { try (PooledDb db = dbPool.db()) { String sqlText = "delete from config where node_number = ?;"; try ( PreparedStatement statement = db.statementReturningKeys(sqlText, nodeInfo.getNumber()) ) { db.updateWithStatement(statement); } catch (Exception e) { e.printStackTrace(); throw e; } } catch (SQLException se) { se.printStackTrace(); throw new Failure("remove node failed:" + se); } catch (Exception e) { e.printStackTrace(); } }
public void savePayment(int amount, ZonedDateTime date) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "insert into payments_summary (amount,date) VALUES (?,?) ON CONFLICT (date) DO UPDATE SET amount = payments_summary.amount + excluded.amount" ) ) { statement.setInt(1, amount); statement.setInt(2, (int) date.truncatedTo(ChronoUnit.DAYS).toEpochSecond()); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("payment save failed:" + se); } catch (Exception e) { e.printStackTrace(); } }
@Override public void markTestRecord(HashId hash) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "insert into ledger_testrecords(hash) values(?) on conflict do nothing;" ) ) { statement.setBytes(1, hash.getDigest()); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("StateRecord markTest failed:" + se); } catch (Exception e) { e.printStackTrace(); } }
public void removeSubscriptionsByEnvId(long environmentId) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM contract_subscription WHERE environment_id = ?" ) ) { statement.setLong(1, environmentId); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("removeSubscriptionsByEnvId failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("removeSubscriptionsByEnvId failed: " + e); } }
public void removeStorageContractsByEnvId(long environmentId) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM contract_storage WHERE environment_id = ?" ) ) { statement.setLong(1, environmentId); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("removeStorageContractsByEnvId failed: " + se); } catch (Exception e) { e.printStackTrace(); } }
@Override public void clearExpiredStorageContractBinaries() { //TODO: add trigger for delete expired contracts after deleting all subscriptions, and remove this function try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "DELETE FROM contract_binary WHERE hash_id NOT IN (SELECT hash_id FROM contract_storage GROUP BY hash_id)" ) ) { db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("clearExpiredStorageContractBinaries failed: " + se); } catch (Exception e) { e.printStackTrace(); } }
public void cleanup(boolean isPermanetMode) { try (PooledDb db = dbPool.db()) { long now = Instant.now().getEpochSecond(); String sqlText = "delete from items where id in (select id from ledger where expires_at < ?);"; db.update(sqlText, now); if (!isPermanetMode) { sqlText = "delete from ledger where expires_at < ?;"; db.update(sqlText, now); } sqlText = "delete from items where keepTill < ?;"; db.update(sqlText, now); sqlText = "delete from follower_callbacks where stored_until < ?;"; db.update(sqlText, now); } catch (SQLException se) { se.printStackTrace(); throw new Failure("cleanup failed:" + se); } }
@Override public void addNameRecord(final NNameRecord nameRecord) { long nameStorageId = addNameStorage(nameRecord); if (nameStorageId != 0) { nameRecord.setId(nameStorageId); removeNameRecordEntries(nameStorageId); for (NameRecordEntry nameRecordEntry : nameRecord.getEntries()) { ((NNameRecordEntry) nameRecordEntry).setNameRecordId(nameStorageId); addNameEntry((NNameRecordEntry) nameRecordEntry); } } else { throw new Failure("addNameRecord failed"); } }
@Override public NNameRecord getNameByOrigin (byte[] origin) { return getNameBy("WHERE name_storage.id=(SELECT name_storage_id FROM name_entry WHERE origin=?) ", (statement)-> { try { statement.setBytes(1, origin); } catch (SQLException e) { throw new Failure("getNameByOrigin failed: " + e); } }); }
@Override public NNameRecord getNameRecord(final String nameReduced) { return getNameBy("WHERE name_storage.name_reduced=? ", (statement)-> { try { statement.setString(1, nameReduced); } catch (SQLException e) { throw new Failure("getNameRecord failed: " + e); } }); }