Skip to content

Commit

Permalink
server: Attach the filter while retrieving block headers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Mar 19, 2019
1 parent 8f5f4a4 commit 5d1b366
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ class BlockFilterPostgresDAO {
'blockhash -> blockhash.string
).as(parseFilter.singleOpt)
}

def getBy(blockhash: Blockhash)(implicit conn: Connection): Option[GolombCodedSet] = {
SQL(
"""
|SELECT blockhash, m, n, p, hex
|FROM block_address_gcs
|WHERE blockhash = {blockhash}
""".stripMargin
).on(
'blockhash -> blockhash.string
).as(parseFilter.singleOpt)
}
}
22 changes: 19 additions & 3 deletions server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import com.xsn.explorer.models.persisted.{Block, BlockHeader}
import com.xsn.explorer.models.values.{Blockhash, Height}
import javax.inject.Inject

class BlockPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderingSQLInterpreter) {
class BlockPostgresDAO @Inject() (
blockFilterPostgresDAO: BlockFilterPostgresDAO,
fieldOrderingSQLInterpreter: FieldOrderingSQLInterpreter) {

def insert(block: Block)(implicit conn: Connection): Option[Block] = {
SQL(
Expand Down Expand Up @@ -154,7 +156,7 @@ class BlockPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderingSQLI
def getHeaders(limit: Limit, orderingCondition: OrderingCondition)(implicit conn: Connection): List[BlockHeader] = {
val order = toSQL(orderingCondition)

SQL(
val headers = SQL(
s"""
|SELECT blockhash, previous_blockhash, merkle_root, height, time
|FROM blocks
Expand All @@ -164,6 +166,13 @@ class BlockPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderingSQLI
).on(
'limit -> limit.int
).as(parseHeader.*)

for {
header <- headers
filterMaybe = blockFilterPostgresDAO.getBy(header.hash)
} yield filterMaybe
.map(header.withFilter)
.getOrElse(header)
}

def getHeaders(lastSeenHash: Blockhash, limit: Limit, orderingCondition: OrderingCondition)(implicit conn: Connection): List[BlockHeader] = {
Expand All @@ -173,7 +182,7 @@ class BlockPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderingSQLI
case OrderingCondition.AscendingOrder => ">"
}

SQL(
val headers = SQL(
s"""
|WITH CTE AS (
| SELECT height as lastSeenHeight
Expand All @@ -190,6 +199,13 @@ class BlockPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderingSQLI
'lastSeenHash -> lastSeenHash.string,
'limit -> limit.int
).as(parseHeader.*)

for {
header <- headers
filterMaybe = blockFilterPostgresDAO.getBy(header.hash)
} yield filterMaybe
.map(header.withFilter)
.getOrElse(header)
}

private def toSQL(condition: OrderingCondition): String = condition match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.xsn.explorer.models.values._
import io.scalaland.chimney.dsl._
import play.api.libs.json.{Json, Writes}

sealed trait BlockHeader {
sealed trait BlockHeader extends Product with Serializable {

def hash: Blockhash
def previousBlockhash: Option[Blockhash]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package com.xsn.explorer.data

import com.alexitc.playsonify.models.ordering.{FieldOrdering, OrderingCondition}
import com.alexitc.playsonify.models.pagination.{Count, Limit, Offset, PaginatedQuery}
import com.alexitc.playsonify.sql.FieldOrderingSQLInterpreter
import com.xsn.explorer.data.anorm.BlockPostgresDataHandler
import com.xsn.explorer.data.anorm.dao.BlockPostgresDAO
import com.xsn.explorer.data.common.PostgresDataHandlerSpec
import com.xsn.explorer.errors.BlockNotFoundError
import com.xsn.explorer.helpers.BlockLoader
import com.xsn.explorer.helpers.{BlockLoader, DataHandlerObjects}
import com.xsn.explorer.models.fields.BlockField
import com.xsn.explorer.models.persisted.Block
import com.xsn.explorer.models.values.Blockhash
Expand All @@ -16,12 +14,14 @@ import org.scalatest.BeforeAndAfter

class BlockPostgresDataHandlerSpec extends PostgresDataHandlerSpec with BeforeAndAfter {

import DataHandlerObjects._

before {
clearDatabase()
}

val dao = blockPostgresDAO
val defaultOrdering = FieldOrdering(BlockField.Height, OrderingCondition.AscendingOrder)
lazy val dao = new BlockPostgresDAO(new FieldOrderingSQLInterpreter)
lazy val dataHandler = new BlockPostgresDataHandler(database, dao)

def insert(block: Block) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ trait DataHandlerObjects {
transactionOutputDAO,
addressTransactionDetailsDAO,
fieldOrderingSQLInterpreter)
lazy val blockPostgresDAO = new BlockPostgresDAO(fieldOrderingSQLInterpreter)
lazy val blockFilterPostgresDAO = new BlockFilterPostgresDAO
lazy val blockPostgresDAO = new BlockPostgresDAO(blockFilterPostgresDAO, fieldOrderingSQLInterpreter)
lazy val balancePostgresDAO = new BalancePostgresDAO(fieldOrderingSQLInterpreter)
lazy val aggregatedAmountPostgresDAO = new AggregatedAmountPostgresDAO

Expand Down

0 comments on commit 5d1b366

Please sign in to comment.