7 changed files with 171 additions and 79 deletions
@ -0,0 +1,47 @@ |
|||
package com.xsn.explorer.data |
|||
|
|||
import com.alexitc.playsonify.core.ApplicationResult |
|||
import com.xsn.explorer.models.Transaction |
|||
import com.xsn.explorer.models.rpc.Block |
|||
|
|||
import scala.language.higherKinds |
|||
|
|||
trait DatabaseSeeder[F[_]] { |
|||
|
|||
import DatabaseSeeder._ |
|||
|
|||
/** |
|||
* There are no blocks, we are adding the first one which could possibly |
|||
* not be the genesis block but the first one on our database. |
|||
*/ |
|||
def firstBlock(command: CreateBlockCommand): F[Unit] |
|||
|
|||
/** |
|||
* The database has some blocks, we are appending a block to our latest block. |
|||
*/ |
|||
def newLatestBlock(command: CreateBlockCommand): F[Unit] |
|||
|
|||
/** |
|||
* The database has some blocks but there is a rechain happening, we need to |
|||
* replace our current latest block with the new latest block. |
|||
*/ |
|||
def replaceLatestBlock(command: ReplaceBlockCommand): F[Unit] |
|||
|
|||
/** |
|||
* The database has some blocks but the chain is not complete, we are inserting |
|||
* a previous block that's missing in our chain. |
|||
*/ |
|||
def insertPendingBlock(command: CreateBlockCommand): F[Unit] |
|||
} |
|||
|
|||
object DatabaseSeeder { |
|||
|
|||
case class CreateBlockCommand(block: Block, transactions: List[Transaction]) |
|||
case class DeleteBlockCommand(block: Block, transactions: List[Transaction]) |
|||
case class ReplaceBlockCommand( |
|||
orphanBlock: Block, orphanTransactions: List[Transaction], |
|||
newBlock: Block, newTransactions: List[Transaction]) |
|||
|
|||
} |
|||
|
|||
trait DatabaseBlockingSeeder extends DatabaseSeeder[ApplicationResult] |
@ -0,0 +1,31 @@ |
|||
package com.xsn.explorer.data.async |
|||
|
|||
import javax.inject.Inject |
|||
|
|||
import com.alexitc.playsonify.core.FutureApplicationResult |
|||
import com.xsn.explorer.data.{DatabaseBlockingSeeder, DatabaseSeeder} |
|||
import com.xsn.explorer.executors.DatabaseExecutionContext |
|||
|
|||
import scala.concurrent.Future |
|||
|
|||
class DatabaseFutureSeeder @Inject() ( |
|||
blockingSeeder: DatabaseBlockingSeeder)( |
|||
implicit ec: DatabaseExecutionContext) |
|||
extends DatabaseSeeder[FutureApplicationResult] { |
|||
|
|||
override def firstBlock(command: DatabaseSeeder.CreateBlockCommand): FutureApplicationResult[Unit] = Future { |
|||
blockingSeeder.firstBlock(command) |
|||
} |
|||
|
|||
override def newLatestBlock(command: DatabaseSeeder.CreateBlockCommand): FutureApplicationResult[Unit] = Future { |
|||
blockingSeeder.newLatestBlock(command) |
|||
} |
|||
|
|||
override def replaceLatestBlock(command: DatabaseSeeder.ReplaceBlockCommand): FutureApplicationResult[Unit] = Future { |
|||
blockingSeeder.replaceLatestBlock(command) |
|||
} |
|||
|
|||
override def insertPendingBlock(command: DatabaseSeeder.CreateBlockCommand): FutureApplicationResult[Unit] = Future { |
|||
blockingSeeder.insertPendingBlock(command) |
|||
} |
|||
} |
Loading…
Reference in new issue