@@ -193,13 +193,13 @@ func (Terms) TableName() string {
193193return TableNameTerms
194194}
195195
196+ // ReadPool represents new trimmed down readPool consisting of two balances,
197+ // one for the allocations that the client (client_id) owns
198+ // and the other for the allocations that the client (client_id) doesn't own
196199type ReadPool struct {
197- PoolID string `gorm:"column:pool_id;size:64;primaryKey"`
198- ClientID string `gorm:"column:client_id;size:64;not null;index:idx_read_pools_cab,priority:1"`
199- AllocationID string `gorm:"column:allocation_id;size:64;not null;index:idx_read_pools_cab,priority:2"`
200- // Cached balance in read pool. Might need update when balance - pending is less than 0
201- Balance int64 `gorm:"column:balance;not null"`
202- ExpireAt common.Timestamp `gorm:"column:expire_at;not null"`
200+ ClientID string `gorm:"column:client_id;size:64;primaryKey" json:"client_id"`
201+ OwnerBalance int64 `gorm:"column:owner_balance;not null" json:"owner_balance"`
202+ VisitorBalance int64 `gorm:"column:visitor_balance;not null" json:"visitor_balance"`
203203}
204204
205205func (ReadPool ) TableName () string {
@@ -218,57 +218,42 @@ func (WritePool) TableName() string {
218218return "write_pools"
219219}
220220
221- func GetReadPools (db * gorm.DB , allocationID , clientID string , until common.Timestamp ) (rps []* ReadPool , err error ) {
222- err = db .Model (& ReadPool {}).Select ("pool_id" , "balance" , "expire_at" ).
223- Where (
224- "allocation_id = ? AND " +
225- "client_id = ? AND " +
226- "expire_at > ?" , allocationID , clientID , until ).Find (& rps ).Error
227-
228- if err != nil {
229- return nil , err
230- }
231- return
221+ func GetReadPool (db * gorm.DB , clientID string ) (* ReadPool , error ) {
222+ var rp ReadPool
223+ return & rp , db .Model (& ReadPool {}).Where ("client_id = ?" , clientID ).Scan (& rp ).Error
232224}
233225
234- func GetReadPoolsBalance (db * gorm.DB , allocationID , clientID string , until common.Timestamp ) (balance int64 , err error ) {
235- var b * int64 // pointer to int64 for possible total sum as null
236- err = db .Model (& ReadPool {}).Select ("sum(balance) as tot_balance" ).Where (
237- "client_id = ? AND " +
238- "allocation_id = ? AND " +
239- "expire_at > ?" , clientID , allocationID , until ).Scan (& b ).Error
240-
226+ func GetReadPoolsBalance (db * gorm.DB , clientID string , isOwner bool ) (int64 , error ) {
227+ rp , err := GetReadPool (db , clientID )
241228if err != nil {
242229return 0 , err
243230}
244- if b == nil {
245- return 0 , nil
231+
232+ if isOwner {
233+ return rp .OwnerBalance , nil
246234}
247- return * b , nil
235+ return rp . VisitorBalance , nil
248236}
249237
250- func SetReadPools (db * gorm.DB , clientID , allocationID string , rps []* ReadPool ) (err error ) {
251- // cleanup and batch insert (remove old pools, add / update new)
252- const query = `client_id = ? AND
253- allocation_id = ?`
254-
255- var stub []* ReadPool
256- err = db .Model (& ReadPool {}).
257- Where (query , clientID , allocationID ).
258- Delete (& stub ).Error
238+ func SetReadPool (db * gorm.DB , rp * ReadPool ) error {
239+ var erp ReadPool //find existing read pool
240+ err := db .Model (& ReadPool {}).Where ("client_id = ?" , rp .ClientID ).FirstOrCreate (& erp , rp ).Error
259241if err != nil {
260- return
242+ return err
261243}
262244
263- if len ( rps ) == 0 {
264- return
245+ if erp . OwnerBalance == rp . OwnerBalance && erp . VisitorBalance == rp . VisitorBalance {
246+ return nil
265247}
248+ // update existing
249+ return UpdateReadPool (db , rp )
250+ }
266251
267- err = db . Model ( & ReadPool {}). Clauses (clause. OnConflict {
268- Columns : []clause. Column {{ Name : "pool_id" }},
269- DoUpdates : clause . AssignmentColumns ([] string { "balance" }) ,
270- }). Create ( rps ). Error
271- return
252+ func UpdateReadPool ( db * gorm. DB , rp * ReadPool ) error {
253+ return db . Model ( & ReadPool {}). Where ( "client_id = ?" , rp . ClientID ). Updates ( map [ string ] interface {}{
254+ "owner_balance" : rp . OwnerBalance ,
255+ "visitor_balance" : rp . VisitorBalance ,
256+ }). Error
272257}
273258
274259func SetWritePools (db * gorm.DB , clientID , allocationID string , wps []* WritePool ) (err error ) {
@@ -302,18 +287,3 @@ type ReadPoolRedeem struct {
302287PoolID string `json:"pool_id"` // read pool ID
303288Balance int64 `json:"balance"` // balance reduction
304289}
305-
306- // SubReadRedeemed subtracts tokens redeemed from read pools.
307- func SubReadRedeemed (rps []* ReadPool , redeems []ReadPoolRedeem ) {
308- var rm = make (map [string ]int64 )
309-
310- for _ , rd := range redeems {
311- rm [rd .PoolID ] += rd .Balance
312- }
313-
314- for _ , rp := range rps {
315- if sub , ok := rm [rp .PoolID ]; ok {
316- rp .Balance -= sub
317- }
318- }
319- }
0 commit comments