Alexis Hernandez
6 years ago
5 changed files with 85 additions and 4 deletions
@ -0,0 +1,43 @@ |
|||||
|
package com.xsn.explorer.data.anorm.dao |
||||
|
|
||||
|
import java.sql.Connection |
||||
|
|
||||
|
import anorm._ |
||||
|
import com.xsn.explorer.data.anorm.parsers.BlockFilterParsers |
||||
|
import com.xsn.explorer.gcs.GolombCodedSet |
||||
|
import com.xsn.explorer.models.values.Blockhash |
||||
|
|
||||
|
class BlockFilterPostgresDAO { |
||||
|
|
||||
|
import BlockFilterParsers._ |
||||
|
|
||||
|
def insert(blockhash: Blockhash, filter: GolombCodedSet)(implicit conn: Connection): GolombCodedSet = { |
||||
|
SQL( |
||||
|
""" |
||||
|
|INSERT INTO block_address_gcs |
||||
|
| (blockhash, m, n, p, hex) |
||||
|
|VALUES |
||||
|
| ({blockhash}, {m}, {n}, {p}, {hex}) |
||||
|
|RETURNING blockhash, m, n, p, hex |
||||
|
""".stripMargin |
||||
|
).on( |
||||
|
'blockhash -> blockhash.string, |
||||
|
'm -> filter.m, |
||||
|
'n -> filter.n, |
||||
|
'p -> filter.p, |
||||
|
'hex -> filter.hex.string |
||||
|
).as(parseFilter.single) |
||||
|
} |
||||
|
|
||||
|
def delete(blockhash: Blockhash)(implicit conn: Connection): Option[GolombCodedSet] = { |
||||
|
SQL( |
||||
|
""" |
||||
|
|DELETE FROM block_address_gcs |
||||
|
|WHERE blockhash = {blockhash} |
||||
|
|RETURNING blockhash, m, n, p, hex |
||||
|
""".stripMargin |
||||
|
).on( |
||||
|
'blockhash -> blockhash.string |
||||
|
).as(parseFilter.singleOpt) |
||||
|
} |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
package com.xsn.explorer.data.anorm.parsers |
||||
|
|
||||
|
import anorm.SqlParser._ |
||||
|
import anorm._ |
||||
|
import com.xsn.explorer.gcs.GolombCodedSet |
||||
|
import com.xsn.explorer.models.values.HexString |
||||
|
|
||||
|
object BlockFilterParsers { |
||||
|
|
||||
|
val parseP = int("p") |
||||
|
val parseM = int("m") |
||||
|
val parseN = int("n") |
||||
|
val parseHex = str("hex") |
||||
|
.map(HexString.from) |
||||
|
.map { _.getOrElse(throw new RuntimeException("corrupted hex")) } |
||||
|
|
||||
|
val parseFilter = (parseN ~ parseM ~ parseP ~ parseHex).map { |
||||
|
case n ~ m ~ p ~ hex => |
||||
|
new GolombCodedSet( |
||||
|
n = n, |
||||
|
m = m, |
||||
|
p = p, |
||||
|
hex = hex |
||||
|
) |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue