-
Notifications
You must be signed in to change notification settings - Fork 336
/
bucket_manager.go
137 lines (119 loc) · 3.15 KB
/
bucket_manager.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
137
package nutsdb
import (
"errors"
"os"
)
var ErrBucketNotExist = errors.New("bucket not exist")
const BucketStoreFileName = "bucket.Meta"
type Ds = uint16
type BucketId = uint64
type BucketName = string
type IDMarkerInBucket map[BucketName]map[Ds]BucketId
type InfoMapperInBucket map[BucketId]*Bucket
type BucketManager struct {
fd *os.File
// BucketInfoMapper BucketID => Bucket itself
BucketInfoMapper InfoMapperInBucket
BucketIDMarker IDMarkerInBucket
// IDGenerator helps generates an ID for every single bucket
Gen *IDGenerator
}
func NewBucketManager(dir string) (*BucketManager, error) {
bm := &BucketManager{
BucketInfoMapper: map[BucketId]*Bucket{},
BucketIDMarker: map[BucketName]map[Ds]BucketId{},
}
bucketFilePath := dir + "/" + BucketStoreFileName
_, err := os.Stat(bucketFilePath)
mode := os.O_RDWR
if err != nil {
mode |= os.O_CREATE
}
fd, err := os.OpenFile(bucketFilePath, mode, os.ModePerm)
if err != nil {
return nil, err
}
bm.fd = fd
bm.Gen = &IDGenerator{currentMaxId: 0}
return bm, nil
}
type bucketSubmitRequest struct {
ds Ds
name BucketName
bucket *Bucket
}
func (bm *BucketManager) SubmitPendingBucketChange(reqs []*bucketSubmitRequest) error {
bytes := make([]byte, 0)
for _, req := range reqs {
bs := req.bucket.Encode()
bytes = append(bytes, bs...)
// update the marker info
if _, exist := bm.BucketIDMarker[req.name]; !exist {
bm.BucketIDMarker[req.name] = map[Ds]BucketId{}
}
// recover maxid otherwise new bucket start from 1 again
bm.Gen.CompareAndSetMaxId(req.bucket.Id)
switch req.bucket.Meta.Op {
case BucketInsertOperation:
bm.BucketInfoMapper[req.bucket.Id] = req.bucket
bm.BucketIDMarker[req.name][req.bucket.Ds] = req.bucket.Id
case BucketDeleteOperation:
if len(bm.BucketIDMarker[req.name]) == 1 {
delete(bm.BucketIDMarker, req.name)
} else {
delete(bm.BucketIDMarker[req.name], req.bucket.Ds)
}
delete(bm.BucketInfoMapper, req.bucket.Id)
}
}
_, err := bm.fd.Write(bytes)
return err
}
type IDGenerator struct {
currentMaxId uint64
}
func (g *IDGenerator) GenId() uint64 {
g.currentMaxId++
return g.currentMaxId
}
func (g *IDGenerator) CompareAndSetMaxId(id uint64) {
if id > g.currentMaxId {
g.currentMaxId = id
}
}
func (bm *BucketManager) ExistBucket(ds Ds, name BucketName) bool {
bucket, err := bm.GetBucket(ds, name)
if bucket != nil && err == nil {
return true
}
return false
}
func (bm *BucketManager) GetBucket(ds Ds, name BucketName) (b *Bucket, err error) {
ds2IdMapper := bm.BucketIDMarker[name]
if ds2IdMapper == nil {
return nil, ErrBucketNotExist
}
if id, exist := ds2IdMapper[ds]; exist {
if bucket, ok := bm.BucketInfoMapper[id]; ok {
return bucket, nil
} else {
return nil, ErrBucketNotExist
}
} else {
return nil, ErrBucketNotExist
}
}
func (bm *BucketManager) GetBucketById(id BucketId) (*Bucket, error) {
if bucket, exist := bm.BucketInfoMapper[id]; exist {
return bucket, nil
} else {
return nil, ErrBucketNotExist
}
}
func (bm *BucketManager) GetBucketID(ds Ds, name BucketName) (BucketId, error) {
if bucket, err := bm.GetBucket(ds, name); err != nil {
return 0, err
} else {
return bucket.Id, nil
}
}