-
Notifications
You must be signed in to change notification settings - Fork 41
/
prefixtree.go
83 lines (73 loc) · 3.11 KB
/
prefixtree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package database
import (
"context"
"github.com/forbole/juno/v4/common"
"gorm.io/gorm"
"github.com/bnb-chain/greenfield-storage-provider/store/bsdb"
)
// CreatePrefixTree create prefix tree nodes by input slice
func (db *DB) CreatePrefixTree(ctx context.Context, prefixTree []*bsdb.SlashPrefixTreeNode) (string, []interface{}) {
// because the passed in prefixTree is one object, the array members have same bucketName, we can use the first one
shardTableName := bsdb.GetPrefixesTableName(prefixTree[0].BucketName)
stat := db.Db.Session(&gorm.Session{DryRun: true}).Table(shardTableName).Create(&prefixTree).Statement
return stat.SQL.String(), stat.Vars
}
// DeletePrefixTree delete prefix tree nodes by given conditions
func (db *DB) DeletePrefixTree(ctx context.Context, prefixTree []*bsdb.SlashPrefixTreeNode) (string, []interface{}) {
if len(prefixTree) == 0 {
return "", nil
}
// because the passed in prefixTree is one object, the array members have same bucketName, we can use the first one
shardTableName := bsdb.GetPrefixesTableName(prefixTree[0].BucketName)
tx := db.Db.Session(&gorm.Session{DryRun: true}).Table(shardTableName)
stmt := tx.Where("bucket_name = ? AND full_name = ? AND is_object = ?",
prefixTree[0].BucketName,
prefixTree[0].FullName,
prefixTree[0].IsObject)
for _, object := range prefixTree[1:] {
stmt = stmt.Or("bucket_name = ? AND full_name = ? AND is_object = ?",
object.BucketName,
object.FullName,
object.IsObject)
}
stmt2 := stmt.Unscoped().Delete(&bsdb.SlashPrefixTreeNode{}).Statement
return stmt2.SQL.String(), stmt2.Vars
}
// GetPrefixTree get prefix tree node by full name and bucket name
func (db *DB) GetPrefixTree(ctx context.Context, fullName, bucketName string) (*bsdb.SlashPrefixTreeNode, error) {
var prefixTreeNode *bsdb.SlashPrefixTreeNode
shardTableName := bsdb.GetPrefixesTableName(bucketName)
err := db.Db.WithContext(ctx).Table(shardTableName).
Where("full_name = ? AND bucket_name = ? AND is_object = ?", fullName, bucketName, false).Take(&prefixTreeNode).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, err
}
return prefixTreeNode, nil
}
// GetPrefixTreeObject get prefix tree node object by object id
func (db *DB) GetPrefixTreeObject(ctx context.Context, objectID common.Hash, bucketName string) (*bsdb.SlashPrefixTreeNode, error) {
var prefixTreeNode *bsdb.SlashPrefixTreeNode
shardTableName := bsdb.GetPrefixesTableName(bucketName)
err := db.Db.WithContext(ctx).Table(shardTableName).
Where("object_id = ?", objectID).Take(&prefixTreeNode).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, err
}
return prefixTreeNode, nil
}
// GetPrefixTreeCount get prefix tree nodes count by path and bucket name
func (db *DB) GetPrefixTreeCount(ctx context.Context, pathName, bucketName string) (int64, error) {
var count int64
shardTableName := bsdb.GetPrefixesTableName(bucketName)
err := db.Db.WithContext(ctx).Table(shardTableName).Where("bucket_name = ? AND path_name = ?", bucketName, pathName).Count(&count).Error
if err != nil {
return 0, err
}
return count, nil
}