Search in sources :

Example 1 with PooledDb

use of com.icodici.db.PooledDb in project universa by UniversaBlockchain.

the class PostgresLedger method loadConfig.

@Override
public Object[] loadConfig() {
    try {
        Object[] result = new Object[3];
        result[0] = null;
        result[2] = null;
        ArrayList<NodeInfo> nodeInfos = new ArrayList<>();
        try (PooledDb db = dbPool.db();
            ResultSet rs = db.queryRow("SELECT * FROM config;")) {
            if (rs == null)
                throw new Exception("config not found");
            do {
                NodeInfo nodeInfo = NodeInfo.initFrom(rs);
                nodeInfos.add(nodeInfo);
                byte[] packedKey = rs.getBytes("private_key");
                if (packedKey != null) {
                    result[0] = nodeInfo;
                    result[2] = new PrivateKey(packedKey);
                }
            } while (rs.next());
            if (nodeInfos.isEmpty())
                throw new Exception("config not found");
            result[1] = new NetConfig(nodeInfos);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failed to load config", e);
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PooledDb(com.icodici.db.PooledDb) NodeInfo(com.icodici.universa.node2.NodeInfo) ResultSet(java.sql.ResultSet) NetConfig(com.icodici.universa.node2.NetConfig) SQLException(java.sql.SQLException)

Example 2 with PooledDb

use of com.icodici.db.PooledDb in project universa by UniversaBlockchain.

the class PostgresLedger method save.

@Override
public void save(StateRecord stateRecord) {
    if (stateRecord.getLedger() == null) {
        stateRecord.setLedger(this);
    } else if (stateRecord.getLedger() != this)
        throw new IllegalStateException("can't save with a different ledger (make a copy!)");
    // TODO: probably, it should take a PooledDb as an argument and reuse it
    try (PooledDb db = dbPool.db()) {
        if (stateRecord.getRecordId() == 0) {
            try (PreparedStatement statement = db.statementReturningKeys("insert into ledger(hash,state,created_at, expires_at, locked_by_id) values(?,?,?,?,?);")) {
                statement.setBytes(1, stateRecord.getId().getDigest());
                statement.setInt(2, stateRecord.getState().ordinal());
                statement.setLong(3, StateRecord.unixTime(stateRecord.getCreatedAt()));
                statement.setLong(4, StateRecord.unixTime(stateRecord.getExpiresAt()));
                statement.setLong(5, stateRecord.getLockedByRecordId());
                db.updateWithStatement(statement);
                try (ResultSet keys = statement.getGeneratedKeys()) {
                    if (!keys.next())
                        throw new RuntimeException("generated keys are not supported");
                    long id = keys.getLong(1);
                    stateRecord.setRecordId(id);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
            putToCache(stateRecord);
        } else {
            db.update("update ledger set state=?, expires_at=?, locked_by_id=? where id=?", stateRecord.getState().ordinal(), StateRecord.unixTime(stateRecord.getExpiresAt()), stateRecord.getLockedByRecordId(), stateRecord.getRecordId());
        }
    } catch (SQLException se) {
        se.printStackTrace();
        throw new Failure("StateRecord save failed:" + se);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PooledDb(com.icodici.db.PooledDb) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 3 with PooledDb

use of com.icodici.db.PooledDb in project universa by UniversaBlockchain.

the class PostgresLedger method saveConfig.

@Override
public void saveConfig(NodeInfo myInfo, NetConfig netConfig, PrivateKey nodeKey) {
    try (PooledDb db = dbPool.db()) {
        try (PreparedStatement statement = db.statement("delete from config;")) {
            db.updateWithStatement(statement);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        for (NodeInfo nodeInfo : netConfig.toList()) {
            String sqlText;
            if (nodeInfo.getNumber() == myInfo.getNumber()) {
                sqlText = "insert into config(http_client_port,http_server_port,udp_server_port, node_number, node_name, public_host,host,public_key,private_key) values(?,?,?,?,?,?,?,?,?);";
            } else {
                sqlText = "insert into config(http_client_port,http_server_port,udp_server_port, node_number, node_name, public_host,host,public_key) values(?,?,?,?,?,?,?,?);";
            }
            try (PreparedStatement statement = db.statementReturningKeys(sqlText)) {
                statement.setInt(1, nodeInfo.getClientAddress().getPort());
                statement.setInt(2, nodeInfo.getServerAddress().getPort());
                statement.setInt(3, nodeInfo.getNodeAddress().getPort());
                statement.setInt(4, nodeInfo.getNumber());
                statement.setString(5, nodeInfo.getName());
                statement.setString(6, nodeInfo.getPublicHost());
                statement.setString(7, nodeInfo.getClientAddress().getHostName());
                statement.setBytes(8, nodeInfo.getPublicKey().pack());
                if (statement.getParameterMetaData().getParameterCount() > 8) {
                    statement.setBytes(9, nodeKey.pack());
                }
                db.updateWithStatement(statement);
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    } catch (SQLException se) {
        se.printStackTrace();
        throw new Failure("config save failed:" + se);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PooledDb(com.icodici.db.PooledDb) SQLException(java.sql.SQLException) NodeInfo(com.icodici.universa.node2.NodeInfo) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 4 with PooledDb

use of com.icodici.db.PooledDb in project universa by UniversaBlockchain.

the class PostgresLedger method putItem.

@Override
public void putItem(StateRecord record, Approvable item, Instant keepTill) {
    if (item instanceof Contract) {
        try (PooledDb db = dbPool.db()) {
            try (PreparedStatement statement = db.statement("insert into items(id,packed,keepTill) values(?,?,?);")) {
                statement.setLong(1, record.getRecordId());
                statement.setBytes(2, ((Contract) item).getPackedTransaction());
                statement.setLong(3, keepTill.getEpochSecond());
                db.updateWithStatement(statement);
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        } catch (SQLException se) {
            se.printStackTrace();
            throw new Failure("item save failed:" + se);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : PooledDb(com.icodici.db.PooledDb) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Contract(com.icodici.universa.contract.Contract) SQLException(java.sql.SQLException)

Example 5 with PooledDb

use of com.icodici.db.PooledDb in project universa by UniversaBlockchain.

the class PostgresLedger method cleanup.

public void cleanup() {
    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);
        sqlText = "delete from ledger where expires_at < ?;";
        db.update(sqlText, now);
        sqlText = "delete from items where keepTill < ?;";
        db.update(sqlText, now);
    } catch (SQLException se) {
        se.printStackTrace();
        throw new Failure("cleanup failed:" + se);
    }
}
Also used : PooledDb(com.icodici.db.PooledDb) SQLException(java.sql.SQLException)

Aggregations

PooledDb (com.icodici.db.PooledDb)6 SQLException (java.sql.SQLException)6 PreparedStatement (java.sql.PreparedStatement)4 NodeInfo (com.icodici.universa.node2.NodeInfo)2 ResultSet (java.sql.ResultSet)2 PrivateKey (com.icodici.crypto.PrivateKey)1 Contract (com.icodici.universa.contract.Contract)1 NetConfig (com.icodici.universa.node2.NetConfig)1