forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
204 lines (154 loc) · 4.83 KB
/
metadata.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package nutsdb
// DataStructure represents the data structure we have already supported
type DataStructure = uint16
// DataFlag means the data operations have done by users.
type DataFlag = uint16
// DataStatus means the status of data
type DataStatus = uint16
const (
// DataStructureSet represents the data structure set flag
DataStructureSet DataStructure = 0
// DataStructureSortedSet represents the data structure sorted set flag
DataStructureSortedSet DataStructure = 1
// DataStructureBTree represents the data structure b tree flag
DataStructureBTree DataStructure = 2
// DataStructureList represents the data structure list flag
DataStructureList DataStructure = 3
)
const (
// DataDeleteFlag represents the data delete flag
DataDeleteFlag DataFlag = 0
// DataSetFlag represents the data set flag
DataSetFlag DataFlag = 1
// DataLPushFlag represents the data LPush flag
DataLPushFlag DataFlag = 2
// DataRPushFlag represents the data RPush flag
DataRPushFlag DataFlag = 3
// DataLRemFlag represents the data LRem flag
DataLRemFlag DataFlag = 4
// DataLPopFlag represents the data LPop flag
DataLPopFlag DataFlag = 5
// DataRPopFlag represents the data RPop flag
DataRPopFlag DataFlag = 6
// DataLTrimFlag represents the data LTrim flag
DataLTrimFlag DataFlag = 8
// DataZAddFlag represents the data ZAdd flag
DataZAddFlag DataFlag = 9
// DataZRemFlag represents the data ZRem flag
DataZRemFlag DataFlag = 10
// DataZRemRangeByRankFlag represents the data ZRemRangeByRank flag
DataZRemRangeByRankFlag DataFlag = 11
// DataZPopMaxFlag represents the data ZPopMax flag
DataZPopMaxFlag DataFlag = 12
// DataZPopMinFlag represents the data aZPopMin flag
DataZPopMinFlag DataFlag = 13
// DataSetBucketDeleteFlag represents the delete Set bucket flag
DataSetBucketDeleteFlag DataFlag = 14
// DataSortedSetBucketDeleteFlag represents the delete Sorted Set bucket flag
DataSortedSetBucketDeleteFlag DataFlag = 15
// DataBTreeBucketDeleteFlag represents the delete BTree bucket flag
DataBTreeBucketDeleteFlag DataFlag = 16
// DataListBucketDeleteFlag represents the delete List bucket flag
DataListBucketDeleteFlag DataFlag = 17
// DataLRemByIndex represents the data LRemByIndex flag
DataLRemByIndex DataFlag = 18
// DataExpireListFlag represents that set ttl for the list
DataExpireListFlag DataFlag = 19
)
const (
// UnCommitted represents the tx unCommitted status
UnCommitted uint16 = 0
// Committed represents the tx committed status
Committed uint16 = 1
)
// Persistent represents the data persistent flag
const Persistent uint32 = 0
type MetaData struct {
KeySize uint32
ValueSize uint32
Timestamp uint64
TTL uint32
Flag DataFlag // delete / set
BucketSize uint32
TxID uint64
Status DataStatus // committed / uncommitted
Ds DataStructure // data structure
Crc uint32
BucketId BucketId
}
func (meta *MetaData) Size() int64 {
// CRC
size := 4
size += UvarintSize(uint64(meta.KeySize))
size += UvarintSize(uint64(meta.ValueSize))
size += UvarintSize(meta.Timestamp)
size += UvarintSize(uint64(meta.TTL))
size += UvarintSize(uint64(meta.Flag))
size += UvarintSize(meta.TxID)
size += UvarintSize(uint64(meta.Status))
size += UvarintSize(uint64(meta.Ds))
size += UvarintSize(meta.BucketId)
return int64(size)
}
func (meta *MetaData) PayloadSize() int64 {
return int64(meta.BucketSize) + int64(meta.KeySize) + int64(meta.ValueSize)
}
func NewMetaData() *MetaData {
return new(MetaData)
}
func (meta *MetaData) WithKeySize(keySize uint32) *MetaData {
meta.KeySize = keySize
return meta
}
func (meta *MetaData) WithValueSize(valueSize uint32) *MetaData {
meta.ValueSize = valueSize
return meta
}
func (meta *MetaData) WithTimeStamp(timestamp uint64) *MetaData {
meta.Timestamp = timestamp
return meta
}
func (meta *MetaData) WithTTL(ttl uint32) *MetaData {
meta.TTL = ttl
return meta
}
func (meta *MetaData) WithFlag(flag uint16) *MetaData {
meta.Flag = flag
return meta
}
func (meta *MetaData) WithBucketSize(bucketSize uint32) *MetaData {
meta.BucketSize = bucketSize
return meta
}
func (meta *MetaData) WithTxID(txID uint64) *MetaData {
meta.TxID = txID
return meta
}
func (meta *MetaData) WithStatus(status uint16) *MetaData {
meta.Status = status
return meta
}
func (meta *MetaData) WithDs(ds uint16) *MetaData {
meta.Ds = ds
return meta
}
func (meta *MetaData) WithCrc(crc uint32) *MetaData {
meta.Crc = crc
return meta
}
func (meta *MetaData) WithBucketId(bucketID uint64) *MetaData {
meta.BucketId = bucketID
return meta
}
func (meta *MetaData) IsBPlusTree() bool {
return meta.Ds == DataStructureBTree
}
func (meta *MetaData) IsSet() bool {
return meta.Ds == DataStructureSet
}
func (meta *MetaData) IsSortSet() bool {
return meta.Ds == DataStructureSortedSet
}
func (meta *MetaData) IsList() bool {
return meta.Ds == DataStructureList
}