-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: Precompute the available coins
In order to speed up the available coins retrieval, the aggregated_amounts table is created, here we store the total available coins which can be retrieved fast.
- Loading branch information
Showing
6 changed files
with
65 additions
and
11 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
22 changes: 22 additions & 0 deletions
22
server/app/com/xsn/explorer/data/anorm/dao/AggregatedAmountPostgresDAO.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,22 @@ | ||
package com.xsn.explorer.data.anorm.dao | ||
|
||
import java.sql.Connection | ||
|
||
import anorm._ | ||
|
||
class AggregatedAmountPostgresDAO { | ||
|
||
def updateAvailableCoins(delta: BigDecimal)(implicit conn: Connection): Unit = { | ||
val affectedRows = SQL( | ||
""" | ||
|UPDATE aggregated_amounts | ||
|SET value = value + {delta} | ||
|WHERE name = 'available_coins' | ||
""".stripMargin | ||
).on( | ||
'delta -> delta | ||
).executeUpdate() | ||
|
||
require(affectedRows == 1) | ||
} | ||
} |
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,17 @@ | ||
|
||
# --- !Ups | ||
|
||
CREATE TABLE aggregated_amounts( | ||
name TEXT NOT NULL, | ||
value AMOUNT_TYPE NOT NULL, | ||
-- constraints | ||
CONSTRAINT aggregated_amounts_name_pk PRIMARY KEY (name) | ||
); | ||
|
||
INSERT INTO aggregated_amounts | ||
SELECT 'available_coins' AS name, COALESCE(SUM(received - spent), 0) AS value FROM balances; | ||
|
||
|
||
# --- !Downs | ||
|
||
DROP TABLE aggregated_amounts; |
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
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
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