Skip to content

Commit 82375a2

Browse files
authored
Merge pull request #1448 from 0chain/fix/alloc-cache
Load terms in repo and add on delete cascade
2 parents c692dcb + 713bddd commit 82375a2

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

code/go/0chain.net/blobbercore/allocation/protocol.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,18 @@ func FetchAllocationFromEventsDB(ctx context.Context, allocationID string, alloc
5252
}
5353

5454
if err == nil {
55-
// load related terms
56-
var terms []*Terms
57-
err = tx.Model(terms).
58-
Where("allocation_id = ?", a.ID).
59-
Find(&terms).Error
60-
if err != nil {
61-
return nil, common.NewError("bad_db_operation", err.Error()) // unexpected DB error
55+
if len(a.Terms) == 0 {
56+
// load related terms
57+
var terms []*Terms
58+
err = tx.Model(terms).
59+
Where("allocation_id = ?", a.ID).
60+
Find(&terms).Error
61+
if err != nil {
62+
return nil, common.NewError("bad_db_operation", err.Error()) // unexpected DB error
63+
}
64+
a.Terms = terms // set field
6265
}
63-
a.Terms = terms // set field
64-
return // found in DB
66+
return // found in DB
6567
}
6668

6769
sa, err := requestAllocation(allocationID)

code/go/0chain.net/blobbercore/allocation/repository.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
const (
1717
SQLWhereGetById = "allocations.id = ?"
1818
SQLWhereGetByTx = "allocations.tx = ?"
19-
lruSize = 100
19+
lruSize = 500
2020
)
2121

2222
var (
@@ -141,6 +141,11 @@ func (r *Repository) GetByTx(ctx context.Context, allocationID, txHash string) (
141141
cache[allocationID] = AllocationCache{
142142
Allocation: alloc,
143143
}
144+
//get allocation terms
145+
err = alloc.LoadTerms(ctx)
146+
if err != nil {
147+
return alloc, err
148+
}
144149
r.setAllocToGlobalCache(alloc)
145150
return alloc, err
146151
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
DELETE FROM terms WHERE allocation_id NOT IN (SELECT id FROM allocations);
4+
ALTER TABLE ONLY terms DROP CONSTRAINT fk_terms_allocation;
5+
ALTER TABLE ONLY terms ADD CONSTRAINT fk_terms_allocation foreign key (allocation_id) references allocations(id) ON DELETE CASCADE;
6+
CREATE INDEX idx_terms_allocation_id ON terms USING HASH(allocation_id);
7+
-- +goose StatementEnd

0 commit comments

Comments
 (0)