/
branch.go
136 lines (113 loc) · 3.7 KB
/
branch.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package keeper
import (
"encoding/binary"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gitopia/gitopia/v3/x/gitopia/types"
)
// GetBranchCount get the total number of branch
func (k Keeper) GetBranchCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.BranchCountKey)
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetBranchCount set the total number of branch
func (k Keeper) SetBranchCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.BranchCountKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// AppendBranch appends a branch in the store with a new id and update the count
func (k Keeper) AppendBranch(
ctx sdk.Context,
branch types.Branch,
) uint64 {
// Create the branch
count := k.GetBranchCount(ctx)
// Set the ID of the appended value
branch.Id = count
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetBranchKeyForRepositoryId(branch.RepositoryId)),
)
appendedValue := k.cdc.MustMarshal(&branch)
store.Set([]byte(branch.Name), appendedValue)
// Update branch count
k.SetBranchCount(ctx, count+1)
return count
}
// SetBranch set a specific branch in the store for repository-id
func (k Keeper) SetRepositoryBranch(ctx sdk.Context, branch types.Branch) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetBranchKeyForRepositoryId(branch.RepositoryId)),
)
b := k.cdc.MustMarshal(&branch)
store.Set([]byte(branch.Name), b)
}
// GetRepositoryBranch returns a branch from its name
func (k Keeper) GetRepositoryBranch(ctx sdk.Context, repositoryId uint64, branch string) (val types.Branch, found bool) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetBranchKeyForRepositoryId(repositoryId)),
)
b := store.Get([]byte(branch))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveBranch removes a branch from the store
func (k Keeper) RemoveRepositoryBranch(ctx sdk.Context, repositoryId uint64, branchName string) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetBranchKeyForRepositoryId(repositoryId)),
)
store.Delete([]byte(branchName))
}
// GetAllBranch returns all branch
func (k Keeper) GetAllBranch(ctx sdk.Context) (list []types.Branch) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BranchKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Branch
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAllRepositoryBranch returns all branch for repository-id
func (k Keeper) GetAllRepositoryBranch(ctx sdk.Context, repositoryId uint64) (list []types.Branch) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetBranchKeyForRepositoryId(repositoryId)),
)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Branch
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetBranchIDBytes returns the byte representation of the ID
func GetBranchIDBytes(id uint64) []byte {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, id)
return bz
}
// GetBranchIDFromBytes returns ID in uint64 format from a byte array
func GetBranchIDFromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
}