-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from alephium/136-batch-write-blocks
Batch write blocks with SQL
- Loading branch information
Showing
28 changed files
with
1,252 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/src/main/scala/org/alephium/explorer/persistence/model/BlockDepEntity.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018 The Alephium Authors | ||
// This file is part of the alephium project. | ||
// | ||
// The library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package org.alephium.explorer.persistence.model | ||
|
||
import org.alephium.explorer.api.model.BlockEntry | ||
|
||
/** | ||
* Class for defining rows in table [[org.alephium.explorer.persistence.schema.BlockDepsSchema]] | ||
*/ | ||
final case class BlockDepEntity(hash: BlockEntry.Hash, dep: BlockEntry.Hash, order: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/main/scala/org/alephium/explorer/persistence/queries/BlockDepQueries.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2018 The Alephium Authors | ||
// This file is part of the alephium project. | ||
// | ||
// The library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package org.alephium.explorer.persistence.queries | ||
|
||
import slick.dbio.DBIOAction | ||
import slick.jdbc.{PositionedParameters, SetParameter, SQLActionBuilder} | ||
|
||
import org.alephium.explorer.persistence.DBActionW | ||
import org.alephium.explorer.persistence.model.BlockDepEntity | ||
import org.alephium.explorer.persistence.schema.CustomSetParameter._ | ||
|
||
object BlockDepQueries { | ||
|
||
/** | ||
* Insert block_deps or ignore if there is a primary key conflict. | ||
* | ||
* Slick creates the following `INSERT` using string interpolation. Here | ||
* the same is achieved by manually creating the [[SQLActionBuilder]] so | ||
* our inserts can write multiple rows within a single `INSERT` statement. | ||
* | ||
* <a href="https://scala-slick.org/doc/3.3.3/sql.html#splicing-literal-values">Splicing</a> | ||
* is not used to insert values so these queries are still cacheable prepared-statements. | ||
*/ | ||
def insertBlockDeps(deps: Iterable[BlockDepEntity]): DBActionW[Int] = | ||
if (deps.isEmpty) { | ||
DBIOAction.successful(0) | ||
} else { | ||
//generate '?' placeholders for the parameterised SQL query | ||
val placeholder = paramPlaceholder(rows = deps.size, columns = 3) | ||
val query = | ||
s""" | ||
|INSERT INTO block_deps ("hash", "dep", "order") | ||
|VALUES $placeholder | ||
|ON CONFLICT ON CONSTRAINT hash_deps_pk | ||
| DO NOTHING | ||
|""".stripMargin | ||
|
||
//set parameters following the insert order defined by the query above | ||
val parameters: SetParameter[Unit] = | ||
(_: Unit, params: PositionedParameters) => | ||
deps foreach { dep => | ||
params >> dep.hash | ||
params >> dep.dep | ||
params >> dep.order | ||
} | ||
|
||
//Return builder generated by Slick's string interpolation | ||
SQLActionBuilder( | ||
queryParts = query, | ||
unitPConv = parameters | ||
).asUpdate | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/src/main/scala/org/alephium/explorer/persistence/queries/InputQueries.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2018 The Alephium Authors | ||
// This file is part of the alephium project. | ||
// | ||
// The library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package org.alephium.explorer.persistence.queries | ||
|
||
import slick.dbio.DBIOAction | ||
import slick.jdbc.{PositionedParameters, SetParameter, SQLActionBuilder} | ||
|
||
import org.alephium.explorer.persistence.DBActionW | ||
import org.alephium.explorer.persistence.model.InputEntity | ||
import org.alephium.explorer.persistence.schema.CustomSetParameter._ | ||
|
||
object InputQueries { | ||
|
||
/** Inserts inputs or ignore rows with primary key conflict */ | ||
def insertInputs(inputs: Iterable[InputEntity]): DBActionW[Int] = | ||
if (inputs.isEmpty) { | ||
DBIOAction.successful(0) | ||
} else { | ||
val placeholder = paramPlaceholder(rows = inputs.size, columns = 8) | ||
|
||
val query = | ||
s""" | ||
|INSERT INTO inputs ("block_hash", | ||
| "tx_hash", | ||
| "timestamp", | ||
| "hint", | ||
| "output_ref_key", | ||
| "unlock_script", | ||
| "main_chain", | ||
| "order") | ||
|VALUES $placeholder | ||
|ON CONFLICT | ||
| ON CONSTRAINT inputs_pk | ||
| DO NOTHING | ||
|""".stripMargin | ||
|
||
val parameters: SetParameter[Unit] = | ||
(_: Unit, params: PositionedParameters) => | ||
inputs foreach { input => | ||
params >> input.blockHash | ||
params >> input.txHash | ||
params >> input.timestamp | ||
params >> input.hint | ||
params >> input.outputRefKey | ||
params >> input.unlockScript | ||
params >> input.mainChain | ||
params >> input.order | ||
} | ||
|
||
SQLActionBuilder( | ||
queryParts = query, | ||
unitPConv = parameters | ||
).asUpdate | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/src/main/scala/org/alephium/explorer/persistence/queries/OutputQueries.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2018 The Alephium Authors | ||
// This file is part of the alephium project. | ||
// | ||
// The library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package org.alephium.explorer.persistence.queries | ||
|
||
import slick.dbio.DBIOAction | ||
import slick.jdbc.{PositionedParameters, SetParameter, SQLActionBuilder} | ||
|
||
import org.alephium.explorer.persistence.DBActionW | ||
import org.alephium.explorer.persistence.model.OutputEntity | ||
import org.alephium.explorer.persistence.schema.CustomSetParameter._ | ||
|
||
object OutputQueries { | ||
|
||
/** Inserts outputs or ignore rows with primary key conflict */ | ||
def insertOutputs(outputs: Iterable[OutputEntity]): DBActionW[Int] = | ||
if (outputs.isEmpty) { | ||
DBIOAction.successful(0) | ||
} else { | ||
val placeholder = paramPlaceholder(rows = outputs.size, columns = 10) | ||
|
||
val query = | ||
s""" | ||
|INSERT INTO outputs ("block_hash", | ||
| "tx_hash", | ||
| "timestamp", | ||
| "hint", | ||
| "key", | ||
| "amount", | ||
| "address", | ||
| "main_chain", | ||
| "lock_time", | ||
| "order") | ||
|VALUES $placeholder | ||
|ON CONFLICT | ||
| ON CONSTRAINT outputs_pk | ||
| DO NOTHING | ||
|""".stripMargin | ||
|
||
val parameters: SetParameter[Unit] = | ||
(_: Unit, params: PositionedParameters) => | ||
outputs foreach { output => | ||
params >> output.blockHash | ||
params >> output.txHash | ||
params >> output.timestamp | ||
params >> output.hint | ||
params >> output.key | ||
params >> output.amount | ||
params >> output.address | ||
params >> output.mainChain | ||
params >> output.lockTime | ||
params >> output.order | ||
} | ||
|
||
SQLActionBuilder( | ||
queryParts = query, | ||
unitPConv = parameters | ||
).asUpdate | ||
} | ||
} |
Oops, something went wrong.