Teja
Teja

Reputation: 37

Off Ledger Data Storing in Corda

I want to store off ledger data and query only from one node I have checked this. To store data in tables i have followed regular protocol of creating transaction builder as given in flow cookbook of Corda but struck at recording the transaction.

Can FinalityFlow can be used in order to record the transactions or has to record transactions using recordTransactions function in Service Hub?

What is preferred way to record the off ledger data?

Thanks in advance

Upvotes: 0

Views: 514

Answers (2)

Joel
Joel

Reputation: 23140

You should record the off-ledger data by writing to the node database directly within flows.

The Flow DB sample here is an example of a CorDapp that does this. It creates a DatabaseService that lives on the node and reads and writes to the node's database:

@CordaService
open class DatabaseService(private val services: ServiceHub) : SingletonSerializeAsToken() {

    companion object {
        val log = loggerFor<DatabaseService>()
    }

    /**
     * Executes a database update.
     *
     * @param query The query string with blanks for the parameters.
     * @param params The parameters to fill the blanks in the query string.
     */
    protected fun executeUpdate(query: String, params: Map<Int, Any>) {
        val preparedStatement = prepareStatement(query, params)

        try {
            preparedStatement.executeUpdate()
        } catch (e: SQLException) {
            log.error(e.message)
            throw e
        } finally {
            preparedStatement.close()
        }
    }

    /**
     * Executes a database query.
     *
     * @param query The query string with blanks for the parameters.
     * @param params The parameters to fill the blanks in the query string.
     * @param transformer A function for processing the query's ResultSet.
     *
     * @return The list of transformed query results.
     */
    protected fun <T : Any> executeQuery(
            query: String,
            params: Map<Int, Any>,
            transformer: (ResultSet) -> T
    ): List<T> {
        val preparedStatement = prepareStatement(query, params)
        val results = mutableListOf<T>()

        return try {
            val resultSet = preparedStatement.executeQuery()
            while (resultSet.next()) {
                results.add(transformer(resultSet))
            }
            results
        } catch (e: SQLException) {
            log.error(e.message)
            throw e
        } finally {
            preparedStatement.close()
        }
    }

    /**
     * Creates a PreparedStatement - a precompiled SQL statement to be
     * executed against the database.
     *
     * @param query The query string with blanks for the parameters.
     * @param params The parameters to fill the blanks in the query string.
     *
     * @return The query string and params compiled into a PreparedStatement
     */
    private fun prepareStatement(query: String, params: Map<Int, Any>): PreparedStatement {
        val session = services.jdbcSession()
        val preparedStatement = session.prepareStatement(query)

        params.forEach { (key, value) ->
            when (value) {
                is String -> preparedStatement.setString(key, value)
                is Int -> preparedStatement.setInt(key, value)
                is Long -> preparedStatement.setLong(key, value)
                else -> throw IllegalArgumentException("Unsupported type.")
            }
        }

        return preparedStatement
    }
}

Upvotes: 0

Abinay Bompally
Abinay Bompally

Reputation: 53

check the below example,

https://github.com/corda/flow-db

Here you can add a token to the node's database table by making a PUT request to:

Upvotes: 0

Related Questions