From 3f6ef832e3ac25afff421eb68acd3c163898ad26 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Fri, 14 Jun 2024 14:38:54 +0530 Subject: [PATCH 1/2] fix allocation terms --- .../blobbercore/allocation/protocol.go | 20 ++++++++++--------- .../blobbercore/allocation/repository.go | 7 ++++++- goose/migrations/1718355236_terms_index.sql | 6 ++++++ 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 goose/migrations/1718355236_terms_index.sql diff --git a/code/go/0chain.net/blobbercore/allocation/protocol.go b/code/go/0chain.net/blobbercore/allocation/protocol.go index 717370cf1..30d4ec1be 100644 --- a/code/go/0chain.net/blobbercore/allocation/protocol.go +++ b/code/go/0chain.net/blobbercore/allocation/protocol.go @@ -52,16 +52,18 @@ func FetchAllocationFromEventsDB(ctx context.Context, allocationID string, alloc } if err == nil { - // load related terms - var terms []*Terms - err = tx.Model(terms). - Where("allocation_id = ?", a.ID). - Find(&terms).Error - if err != nil { - return nil, common.NewError("bad_db_operation", err.Error()) // unexpected DB error + if len(a.Terms) == 0 { + // load related terms + var terms []*Terms + err = tx.Model(terms). + Where("allocation_id = ?", a.ID). + Find(&terms).Error + if err != nil { + return nil, common.NewError("bad_db_operation", err.Error()) // unexpected DB error + } + a.Terms = terms // set field } - a.Terms = terms // set field - return // found in DB + return // found in DB } sa, err := requestAllocation(allocationID) diff --git a/code/go/0chain.net/blobbercore/allocation/repository.go b/code/go/0chain.net/blobbercore/allocation/repository.go index e793b3a13..5e6ebf99e 100644 --- a/code/go/0chain.net/blobbercore/allocation/repository.go +++ b/code/go/0chain.net/blobbercore/allocation/repository.go @@ -16,7 +16,7 @@ import ( const ( SQLWhereGetById = "allocations.id = ?" SQLWhereGetByTx = "allocations.tx = ?" - lruSize = 100 + lruSize = 500 ) var ( @@ -141,6 +141,11 @@ func (r *Repository) GetByTx(ctx context.Context, allocationID, txHash string) ( cache[allocationID] = AllocationCache{ Allocation: alloc, } + //get allocation terms + err = alloc.LoadTerms(ctx) + if err != nil { + return alloc, err + } r.setAllocToGlobalCache(alloc) return alloc, err } diff --git a/goose/migrations/1718355236_terms_index.sql b/goose/migrations/1718355236_terms_index.sql new file mode 100644 index 000000000..d671b0e91 --- /dev/null +++ b/goose/migrations/1718355236_terms_index.sql @@ -0,0 +1,6 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE ONLY terms DROP CONSTRAINT fk_terms_allocation; +ALTER TABLE ONLY terms ADD CONSTRAINT fk_terms_allocation foreign key (allocation_id) references allocations(id) ON DELETE CASCADE; +CREATE INDEX idx_terms_allocation_id ON terms USING btree(allocation_id); +-- +goose StatementEnd \ No newline at end of file From 713bddd5eafb421d24ae500c1732f6348ef5df56 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sat, 15 Jun 2024 01:53:04 +0530 Subject: [PATCH 2/2] add delete from terms --- goose/migrations/1718355236_terms_index.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/goose/migrations/1718355236_terms_index.sql b/goose/migrations/1718355236_terms_index.sql index d671b0e91..323bf9989 100644 --- a/goose/migrations/1718355236_terms_index.sql +++ b/goose/migrations/1718355236_terms_index.sql @@ -1,6 +1,7 @@ -- +goose Up -- +goose StatementBegin +DELETE FROM terms WHERE allocation_id NOT IN (SELECT id FROM allocations); ALTER TABLE ONLY terms DROP CONSTRAINT fk_terms_allocation; ALTER TABLE ONLY terms ADD CONSTRAINT fk_terms_allocation foreign key (allocation_id) references allocations(id) ON DELETE CASCADE; -CREATE INDEX idx_terms_allocation_id ON terms USING btree(allocation_id); +CREATE INDEX idx_terms_allocation_id ON terms USING HASH(allocation_id); -- +goose StatementEnd \ No newline at end of file