Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlite: use repsertMany instead of deleteMany+putMany #347

Merged
merged 1 commit into from
Jun 4, 2019

Conversation

rvl
Copy link
Contributor

@rvl rvl commented Jun 4, 2019

Relates to #154

Overview

The function repsertMany does what I thought putMany did. When there is a conflict it updates the rows. This means that we don't need to delete before inserting.

Comments

Before:

λ> runExceptT $ putTxHistory db testPk testTxs
SELECT "wallet_id", "creation_time", "name", "passphrase_last_updated_at", "status", "delegation" FROM "wallet" WHERE ("wallet_id"=?) LIMIT 1; [PersistText "a34fc3b6d2cce8beb3216c2bbb5e55739e8121ed"]
INSERT INTO "tx_meta"("tx_id","wallet_id","status","direction","slot","amount") VALUES (?,?,?,?,?,?); [PersistText "747832",PersistText "a34fc3b6d2cce8beb3216c2bbb5e55739e8121ed",PersistText "in_ledger",PersistBool True,PersistInt64 302400,PersistInt64 1337144]
INSERT INTO "tx_in"("tx_id","order","source_id","source_index") VALUES (?,?,?,?); [PersistText "747832",PersistInt64 0,PersistText "747831",PersistInt64 0]
INSERT INTO "tx_out"("tx_id","index","address","amount") VALUES (?,?,?,?); [PersistText "747832",PersistInt64 0,PersistText "61646472",PersistInt64 1]
Right ()
λ> runExceptT $ putTxHistory db testPk testTxs
SELECT "wallet_id", "creation_time", "name", "passphrase_last_updated_at", "status", "delegation" FROM "wallet" WHERE ("wallet_id"=?) LIMIT 1; [PersistText "a34fc3b6d2cce8beb3216c2bbb5e55739e8121ed"]
INSERT INTO "tx_meta"("tx_id","wallet_id","status","direction","slot","amount") VALUES (?,?,?,?,?,?); [PersistText "747832",PersistText "a34fc3b6d2cce8beb3216c2bbb5e55739e8121ed",PersistText "in_ledger",PersistBool True,PersistInt64 302400,PersistInt64 1337144]
*** Exception: SQLite3 returned ErrorConstraint while attempting to perform step: UNIQUE constraint failed: tx_meta.tx_id, t

After:

λ> runExceptT $ putTxHistory db testPk testTxs
SELECT "wallet_id", "creation_time", "name", "passphrase_last_updated_at", "status", "delegation" FROM "wallet" WHERE ("wallet_id"=?) LIMIT 1; [PersistText "a34fc3b6d2cce8beb3216c2bbb5e55739e8121ed"]
INSERT INTO "tx_meta"("tx_id", "wallet_id", "status", "direction", "slot", "amount") VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT ("tx_id", "wallet_id") DO UPDATE SET "tx_id"=EXCLUDED."tx_id", "wallet_id"=EXCLUDED."wallet_id", "status"=EXCLUDED."status", "direction"=EXCLUDED."direction", "slot"=EXCLUDED."slot", "amount"=EXCLUDED."amount"; [PersistText "747832",PersistText "a34fc3b6d2cce8beb3216c2bbb5e55739e8121ed",PersistText "in_ledger",PersistBool True,PersistInt64 302400,PersistInt64 1337144]
INSERT INTO "tx_in"("tx_id", "order", "source_id", "source_index") VALUES (?, ?, ?, ?) ON CONFLICT ("tx_id", "source_id", "source_index") DO UPDATE SET "tx_id"=EXCLUDED."tx_id", "order"=EXCLUDED."order", "source_id"=EXCLUDED."source_id", "source_index"=EXCLUDED."source_index"; [PersistText "747832",PersistInt64 0,PersistText "747831",PersistInt64 0]
INSERT INTO "tx_out"("tx_id", "index", "address", "amount") VALUES (?, ?, ?, ?) ON CONFLICT ("tx_id", "index") DO UPDATE SET "tx_id"=EXCLUDED."tx_id", "index"=EXCLUDED."index", "address"=EXCLUDED."address", "amount"=EXCLUDED."amount"; [PersistText "747832",PersistInt64 0,PersistText "61646472",PersistInt64 1]

The function repsertMany does what I thought putMany did. When there
is a conflict it updates the rows. This means that we don't need to
delete before inserting.
@rvl rvl self-assigned this Jun 4, 2019
Copy link
Member

@KtorZ KtorZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet 👍

@KtorZ KtorZ merged commit 6c99e82 into rvl/154/db-bench Jun 4, 2019
@iohk-bors iohk-bors bot deleted the rvl/154/repsert branch June 4, 2019 07:31
@KtorZ KtorZ restored the rvl/154/repsert branch June 4, 2019 07:41
@KtorZ KtorZ removed this from the SQLite implementation for the DB Layer milestone Jun 5, 2019
@KtorZ KtorZ deleted the rvl/154/repsert branch June 7, 2019 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants