-
Notifications
You must be signed in to change notification settings - Fork 145
/
trailer.go
182 lines (152 loc) · 4.36 KB
/
trailer.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
/*
Copyright 2022 Huawei Cloud Computing Technologies Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package immutable
import (
"fmt"
"github.com/openGemini/openGemini/lib/numberenc"
"github.com/openGemini/openGemini/lib/util"
)
const (
IndexOfTimeStoreFlag = 0
IndexOfChunkMetaCompressFlag = 1
TimeStoreFlag = 1
)
type Trailer struct {
dataOffset int64
dataSize int64
indexSize int64
metaIndexSize int64
bloomSize int64
idTimeSize int64
TableStat
}
func (t *Trailer) reset() {
t.dataOffset = 0
t.dataSize = 0
t.indexSize = 0
t.metaIndexSize = 0
t.bloomSize = 0
t.idTimeSize = 0
t.idCount = 0
t.minId = 0
t.maxId = 0
t.minTime = 0
t.maxTime = 0
t.metaIndexItemNum = 0
t.bloomM = 0
t.bloomK = 0
t.data = t.data[:0]
t.name = t.name[:0]
}
func (t *Trailer) SetChunkMetaCompressFlag() {
mode := GetChunkMetaCompressMode()
if mode != ChunkMetaCompressNone {
t.SetData(IndexOfChunkMetaCompressFlag, mode)
}
}
func (t *Trailer) SetData(idx int, v byte) {
if cap(t.data) < (idx + 1) {
t.data = append(t.data[:cap(t.data)], make([]byte, idx+1-cap(t.data))...)
}
t.data = t.data[:idx+1]
t.data[idx] = v
}
func (t *Trailer) EqualData(idx int, v byte) bool {
if len(t.data) < idx+1 {
return false
}
return t.data[idx] == v
}
func (t *Trailer) GetData(idx int, def uint8) uint8 {
if len(t.data) < idx+1 {
return def
}
return t.data[idx]
}
func (t *Trailer) marshal(dst []byte) []byte {
dst = numberenc.MarshalInt64Append(dst, t.dataOffset)
dst = numberenc.MarshalInt64Append(dst, t.dataSize)
dst = numberenc.MarshalInt64Append(dst, t.indexSize)
dst = numberenc.MarshalInt64Append(dst, t.metaIndexSize)
dst = numberenc.MarshalInt64Append(dst, t.bloomSize)
dst = numberenc.MarshalInt64Append(dst, t.idTimeSize)
return t.marshalStat(dst)
}
func (t *Trailer) unmarshal(src []byte) ([]byte, error) {
if len(src) < trailerSize {
return nil, fmt.Errorf("tool small data (%v) for unmarshal file Trailer", len(src))
}
t.dataOffset, src = numberenc.UnmarshalInt64(src), src[8:]
t.dataSize, src = numberenc.UnmarshalInt64(src), src[8:]
t.indexSize, src = numberenc.UnmarshalInt64(src), src[8:]
t.metaIndexSize, src = numberenc.UnmarshalInt64(src), src[8:]
t.bloomSize, src = numberenc.UnmarshalInt64(src), src[8:]
t.idTimeSize, src = numberenc.UnmarshalInt64(src), src[8:]
return t.unmarshalStat(src)
}
func (t *Trailer) ContainsId(id uint64) bool {
if id >= t.minId && id <= t.maxId {
return true
}
return false
}
func (t *Trailer) ContainsTime(tm util.TimeRange) bool {
return tm.Overlaps(t.minTime, t.maxTime)
}
func (t *Trailer) MetaIndexItemNum() int64 {
return t.metaIndexItemNum
}
func (t *Trailer) copyTo(tr *Trailer) {
tr.dataOffset = t.dataOffset
tr.dataSize = t.dataSize
tr.indexSize = t.indexSize
tr.metaIndexSize = t.metaIndexSize
tr.bloomSize = t.bloomSize
tr.idCount = t.idCount
tr.minId, tr.maxId = t.minId, t.maxId
tr.minTime, tr.maxTime = t.minTime, t.maxTime
tr.metaIndexItemNum = t.metaIndexItemNum
tr.bloomM, tr.bloomK = t.bloomM, t.bloomK
tr.idTimeSize = t.idTimeSize
if cap(tr.data) < len(t.data) {
tr.data = make([]byte, len(t.data))
} else {
tr.data = tr.data[:len(t.data)]
}
copy(tr.data, t.data)
if cap(tr.name) < len(t.name) {
tr.name = make([]byte, len(t.name))
} else {
tr.name = tr.name[:len(t.name)]
}
copy(tr.name, t.name)
}
func (t *Trailer) metaOffsetSize() (int64, int64) {
return t.dataOffset + t.dataSize, t.indexSize
}
func (t *Trailer) metaIndexOffsetSize() (int64, int64) {
return t.dataOffset + t.dataSize + t.indexSize, t.metaIndexSize
}
func (t *Trailer) idTimeOffsetSize() (int64, int64) {
idTimeOff := t.dataOffset + t.dataSize + t.indexSize + t.metaIndexSize + t.bloomSize
return idTimeOff, t.idTimeSize
}
func (t *Trailer) DataSize() int64 {
return t.dataSize
}
func (t *Trailer) IndexSize() int64 {
return t.indexSize
}
func (t *Trailer) MetaIndexSize() int64 {
return t.metaIndexSize
}