Skip to content

Commit 9205fad

Browse files
committed
remove unused refpath index, use take and add txn options
1 parent 3f6ef83 commit 9205fad

File tree

12 files changed

+34
-24
lines changed

12 files changed

+34
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func AddToPending(ctx context.Context, clientID, allocationID string, pendingWri
207207
defer lock.Unlock()
208208

209209
pending := new(Pending)
210-
err = db.Model(&Pending{}).Where("id=?", key).First(pending).Error
210+
err = db.Model(&Pending{}).Where("id=?", key).Take(pending).Error
211211
switch {
212212
case err == nil:
213213
pending.PendingWrite += pendingWrite

code/go/0chain.net/blobbercore/challenge/timing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func GetChallengeTiming(challengeID string) (*ChallengeTiming, error) {
183183

184184
err := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
185185
tx := datastore.GetStore().GetTransaction(ctx)
186-
return tx.Model(&ChallengeTiming{}).Where("challenge_id = ?", challengeID).First(&ch).Error
186+
return tx.Model(&ChallengeTiming{}).Where("challenge_id = ?", challengeID).Take(&ch).Error
187187
})
188188
return ch, err
189189
}

code/go/0chain.net/blobbercore/datastore/mocket.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package datastore
22

33
import (
44
"context"
5+
"database/sql"
56

67
. "github.com/0chain/blobber/code/go/0chain.net/core/logging"
78
mocket "github.com/selvatico/go-mocket"
@@ -71,7 +72,7 @@ func (store *Mocket) Close() {
7172
}
7273
}
7374

74-
func (store *Mocket) CreateTransaction(ctx context.Context) context.Context {
75+
func (store *Mocket) CreateTransaction(ctx context.Context,opts ...*sql.TxOptions) context.Context {
7576
db := store.db.Begin()
7677
return context.WithValue(ctx, ContextKeyTransaction, EnhanceDB(db))
7778
}

code/go/0chain.net/blobbercore/datastore/postgres.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package datastore
22

33
import (
44
"context"
5+
"database/sql"
56
"fmt"
67
"time"
78

@@ -84,7 +85,7 @@ func (store *postgresStore) Close() {
8485
}
8586
}
8687

87-
func (store *postgresStore) CreateTransaction(ctx context.Context) context.Context {
88+
func (store *postgresStore) CreateTransaction(ctx context.Context, opts ...*sql.TxOptions) context.Context {
8889
db := store.db.WithContext(ctx).Begin()
8990
return context.WithValue(ctx, ContextKeyTransaction, EnhanceDB(db))
9091
}

code/go/0chain.net/blobbercore/datastore/sqlmock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package datastore
22

33
import (
44
"context"
5+
"database/sql"
56

67
. "github.com/0chain/blobber/code/go/0chain.net/core/logging"
78
"github.com/DATA-DOG/go-sqlmock"
@@ -66,7 +67,7 @@ func (store *Sqlmock) Close() {
6667
}
6768
}
6869

69-
func (store *Sqlmock) CreateTransaction(ctx context.Context) context.Context {
70+
func (store *Sqlmock) CreateTransaction(ctx context.Context, opts ...*sql.TxOptions) context.Context {
7071
db := store.db.Begin()
7172
return context.WithValue(ctx, ContextKeyTransaction, db)
7273
}

code/go/0chain.net/blobbercore/datastore/store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package datastore
22

33
import (
44
"context"
5+
"database/sql"
56

67
"gorm.io/gorm"
78
)
@@ -41,7 +42,7 @@ type Store interface {
4142
// GetDB get raw gorm db
4243
GetDB() *gorm.DB
4344
// CreateTransaction create transaction, and save it in context
44-
CreateTransaction(ctx context.Context) context.Context
45+
CreateTransaction(ctx context.Context, opts ...*sql.TxOptions) context.Context
4546
// GetTransaction get transaction from context
4647
GetTransaction(ctx context.Context) *EnhancedDB
4748
WithNewTransaction(f func(ctx context.Context) error) error

code/go/0chain.net/blobbercore/handler/handler.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ package handler
1818

1919
import (
2020
"context"
21+
"database/sql"
2122
"encoding/hex"
2223
"encoding/json"
2324
"errors"
2425
"fmt"
25-
"github.com/0chain/gosdk/core/zcncrypto"
2626
"net/http"
2727
"os"
2828
"runtime/pprof"
2929
"strconv"
3030
"strings"
3131
"time"
3232

33+
"github.com/0chain/gosdk/core/zcncrypto"
34+
3335
"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
3436

3537
"github.com/go-openapi/runtime/middleware"
@@ -276,7 +278,9 @@ func setupHandlers(r *mux.Router) {
276278

277279
func WithReadOnlyConnection(handler common.JSONResponderF) common.JSONResponderF {
278280
return func(ctx context.Context, r *http.Request) (interface{}, error) {
279-
ctx = GetMetaDataStore().CreateTransaction(ctx)
281+
ctx = GetMetaDataStore().CreateTransaction(ctx, &sql.TxOptions{
282+
ReadOnly: true,
283+
})
280284
tx := GetMetaDataStore().GetTransaction(ctx)
281285
defer func() {
282286
tx.Rollback()

code/go/0chain.net/blobbercore/handler/handler_common.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"context"
5+
"database/sql"
56
"errors"
67
"fmt"
78
"net/http"
@@ -133,7 +134,9 @@ func WithStatusConnectionForWM(handler common.StatusCodeResponderF) common.Statu
133134
}
134135
mutex.Lock()
135136
defer mutex.Unlock()
136-
ctx = GetMetaDataStore().CreateTransaction(ctx)
137+
ctx = GetMetaDataStore().CreateTransaction(ctx, &sql.TxOptions{
138+
Isolation: sql.LevelRepeatableRead,
139+
})
137140
tx := GetMetaDataStore().GetTransaction(ctx)
138141
resp, statusCode, err = handler(ctx, r)
139142

code/go/0chain.net/blobbercore/readmarker/readmarker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (rm *ReadMarkerEntity) VerifyMarker(ctx context.Context, sa *allocation.All
126126
func GetLatestReadMarkerEntity(ctx context.Context, clientID, allocID string) (*ReadMarkerEntity, error) {
127127
db := datastore.GetStore().GetTransaction(ctx)
128128
rm := &ReadMarkerEntity{}
129-
err := db.First(rm, "client_id = ? AND allocation_id = ?", clientID, allocID).Error
129+
err := db.Take(rm, "client_id = ? AND allocation_id = ?", clientID, allocID).Error
130130
if errors.Is(err, gorm.ErrRecordNotFound) {
131131
latestRM, err := GetLatestReadMarkerEntityFromChain(clientID, allocID)
132132
if err != nil {

code/go/0chain.net/blobbercore/reference/ref.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,16 @@ func Mkdir(ctx context.Context, allocationID, destpath string) (*Ref, error) {
207207

208208
// GetReference get FileRef with allcationID and path from postgres
209209
func GetReference(ctx context.Context, allocationID, path string) (*Ref, error) {
210-
ref := &Ref{}
211-
db := datastore.GetStore().GetTransaction(ctx)
212-
err := db.Where(&Ref{AllocationID: allocationID, Path: path}).First(ref).Error
213-
if err != nil {
214-
return nil, err
215-
}
216-
return ref, nil
210+
lookupHash := GetReferenceLookup(allocationID, path)
211+
return GetReferenceByLookupHash(ctx, allocationID, lookupHash)
217212
}
218213

219214
// GetLimitedRefFieldsByPath get FileRef selected fields with allocationID and path from postgres
220215
func GetLimitedRefFieldsByPath(ctx context.Context, allocationID, path string, selectedFields []string) (*Ref, error) {
221216
ref := &Ref{}
222217
t := datastore.GetStore().GetTransaction(ctx)
223218
db := t.Select(selectedFields)
224-
err := db.Where(&Ref{AllocationID: allocationID, Path: path}).First(ref).Error
219+
err := db.Where(&Ref{AllocationID: allocationID, Path: path}).Take(ref).Error
225220
if err != nil {
226221
return nil, err
227222
}
@@ -236,7 +231,7 @@ func GetLimitedRefFieldsByLookupHashWith(ctx context.Context, allocationID, look
236231
err := db.
237232
Select(selectedFields).
238233
Where(&Ref{LookupHash: lookupHash}).
239-
First(ref).Error
234+
Take(ref).Error
240235

241236
if err != nil {
242237
return nil, err
@@ -249,7 +244,7 @@ func GetLimitedRefFieldsByLookupHash(ctx context.Context, allocationID, lookupHa
249244
ref := &Ref{}
250245
t := datastore.GetStore().GetTransaction(ctx)
251246
db := t.Select(selectedFields)
252-
err := db.Where(&Ref{LookupHash: lookupHash}).First(ref).Error
247+
err := db.Where(&Ref{LookupHash: lookupHash}).Take(ref).Error
253248
if err != nil {
254249
return nil, err
255250
}
@@ -259,7 +254,7 @@ func GetLimitedRefFieldsByLookupHash(ctx context.Context, allocationID, lookupHa
259254
func GetReferenceByLookupHash(ctx context.Context, allocationID, pathHash string) (*Ref, error) {
260255
ref := &Ref{}
261256
db := datastore.GetStore().GetTransaction(ctx)
262-
err := db.Where(&Ref{LookupHash: pathHash}).First(ref).Error
257+
err := db.Where(&Ref{LookupHash: pathHash}).Take(ref).Error
263258
if err != nil {
264259
return nil, err
265260
}
@@ -271,7 +266,7 @@ func GetReferenceByLookupHashForDownload(ctx context.Context, allocationID, path
271266
db := datastore.GetStore().GetTransaction(ctx)
272267

273268
err := db.Transaction(func(tx *gorm.DB) error {
274-
err := tx.Clauses(clause.Locking{Strength: "SHARE"}).Where(&Ref{LookupHash: pathHash}).First(ref).Error
269+
err := tx.Clauses(clause.Locking{Strength: "SHARE"}).Where(&Ref{LookupHash: pathHash}).Take(ref).Error
275270
if err != nil {
276271
return err
277272
}
@@ -403,7 +398,7 @@ func GetRefWithDirListFields(ctx context.Context, pathHash string) (*Ref, error)
403398
db := datastore.GetStore().GetTransaction(ctx)
404399
err := db.Select(dirListFields).
405400
Where(&Ref{LookupHash: pathHash}).
406-
First(ref).Error
401+
Take(ref).Error
407402
if err != nil {
408403
return nil, err
409404
}

0 commit comments

Comments
 (0)