/
bloom_filter_index.go
251 lines (214 loc) · 8.1 KB
/
bloom_filter_index.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright 2023 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 sparseindex
import (
"encoding/binary"
"fmt"
"hash/crc32"
"os"
"path"
"strings"
"github.com/openGemini/openGemini/engine/hybridqp"
"github.com/openGemini/openGemini/engine/immutable/colstore"
"github.com/openGemini/openGemini/engine/index/bloomfilter"
"github.com/openGemini/openGemini/lib/fileops"
"github.com/openGemini/openGemini/lib/index"
"github.com/openGemini/openGemini/lib/logger"
"github.com/openGemini/openGemini/lib/logstore"
"github.com/openGemini/openGemini/lib/obs"
"github.com/openGemini/openGemini/lib/record"
"github.com/openGemini/openGemini/lib/rpn"
"github.com/openGemini/openGemini/lib/tokenizer"
"go.uber.org/zap"
)
const (
crcSize = 4
FullTextIdxColumnCnt = 1
tmpFileSuffix = ".init"
FullTextIndex = "fullText"
BloomFilterFileSuffix = ".idx"
BloomFilterFilePrefix = "bloomfilter_" // bloomfilter_${columnName}.idx
)
type OBSFilterPath struct {
localPath string
remotePath string
option *obs.ObsOptions
}
func NewOBSFilterPath(localPath, remotePath string, option *obs.ObsOptions) *OBSFilterPath {
return &OBSFilterPath{localPath: localPath, remotePath: remotePath, option: option}
}
func (o *OBSFilterPath) LocalPath() string {
return o.localPath
}
func (o *OBSFilterPath) RemotePath() string {
return o.remotePath
}
func (o *OBSFilterPath) Name() string {
return ""
}
func (o *OBSFilterPath) Option() *obs.ObsOptions {
return o.option
}
var _ = RegistrySKFileReaderCreator(index.BloomFilterIndex, &BloomFilterReaderCreator{})
type BloomFilterReaderCreator struct {
}
func (index *BloomFilterReaderCreator) CreateSKFileReader(rpnExpr *rpn.RPNExpr, schema record.Schemas, option hybridqp.Options, isCache bool) (SKFileReader, error) {
return NewBloomFilterIndexReader(rpnExpr, schema, option, isCache)
}
type BloomFilterIndexReader struct {
isCache bool
version uint32
schema record.Schemas
option hybridqp.Options
bf rpn.SKBaseReader
sk SKCondition
}
func NewBloomFilterIndexReader(rpnExpr *rpn.RPNExpr, schema record.Schemas, option hybridqp.Options, isCache bool) (*BloomFilterIndexReader, error) {
sk, err := NewSKCondition(rpnExpr, schema)
if err != nil {
return nil, err
}
return &BloomFilterIndexReader{schema: schema, option: option, isCache: isCache, version: 4, sk: sk}, nil
}
func (r *BloomFilterIndexReader) MayBeInFragment(fragId uint32) (bool, error) {
return r.sk.IsExist(int64(fragId), r.bf)
}
func (r *BloomFilterIndexReader) ReInit(file interface{}) (err error) {
if f, ok := file.(*OBSFilterPath); ok {
splitMap := make(map[string][]byte)
splitMap[r.schema[0].Name] = tokenizer.CONTENT_SPLIT_TABLE
fileName := BloomFilterFilePrefix + r.schema[0].Name + BloomFilterFileSuffix
if f.localPath != "" {
r.bf, err = bloomfilter.NewFilterReader(f.Option(), r.option.GetCondition(), splitMap, true, true, r.version, f.localPath, f.remotePath, fileName, fileName)
} else {
r.bf, err = bloomfilter.NewVerticalFilterReader(f.RemotePath(), f.Option(), r.option.GetCondition(), r.version, splitMap, fileName)
}
if err != nil {
return err
}
return nil
} else if f1, ok := file.(TsspFile); ok {
index := strings.LastIndex(f1.Path(), "/")
path := f1.Path()[:index]
fileName := f1.Path()[index+1:]
fileNameSlice := strings.Split(fileName, ".")
fileName = fileNameSlice[0] + "." + r.schema[0].Name + colstore.BloomFilterIndexFileSuffix
splitMap := make(map[string][]byte)
splitMap[r.schema[0].Name] = tokenizer.CONTENT_SPLIT_TABLE
r.bf, err = bloomfilter.NewLineFilterReader(path, nil, r.option.GetCondition(), r.version, splitMap, fileName)
if err != nil {
return err
}
return nil
} else {
return fmt.Errorf("not support file type")
}
}
func (r *BloomFilterIndexReader) Close() error {
return nil
}
// GetLocalBloomFilterBlockCnts get one local bloomFilter col's block count,if not exist return 0
func GetLocalBloomFilterBlockCnts(dir, msName, lockPath string, recSchema record.Schemas, skipIndex *SkipIndex,
fullTextIdx bool) int64 {
var localPath string
if fullTextIdx {
localPath = GetFullTextDetachFilePath(dir, msName)
} else {
if skipIndex.bfIdx < 0 || len(skipIndex.schemaIdxes[skipIndex.bfIdx]) == 0 {
logger.GetLogger().Info("bloom filter cols is not exist in the schema", zap.String("measurement name", msName))
return 0
}
fieldName := recSchema[skipIndex.schemaIdxes[skipIndex.bfIdx][0]].Name // get the first bloom filter col
localPath = GetBloomFilterFilePath(dir, msName, fieldName)
}
lock := fileops.FileLockOption(lockPath)
pri := fileops.FilePriorityOption(fileops.IO_PRIORITY_NORMAL)
fd, err := fileops.OpenFile(localPath, os.O_CREATE, 0640, lock, pri)
defer func() {
_ = fd.Close()
}()
if err != nil {
logger.GetLogger().Error("open file fail", zap.String("name", localPath), zap.Error(err))
panic(err)
}
localFileInfo, _ := fd.Stat()
filterDataDiskSize := logstore.GetConstant(logstore.CurrentLogTokenizerVersion).FilterDataDiskSize
return localFileInfo.Size() / filterDataDiskSize
}
func GetBloomFilterFilePath(dir, msName, fieldName string) string {
return path.Join(dir, msName, BloomFilterFilePrefix+fieldName+BloomFilterFileSuffix)
}
type BloomFilterWriter struct {
*skipIndexWriter
}
func NewBloomFilterWriter(dir, msName, dataFilePath, lockPath string) *BloomFilterWriter {
return &BloomFilterWriter{
newSkipIndexWriter(dir, msName, dataFilePath, lockPath),
}
}
func (b *BloomFilterWriter) Open() error {
return nil
}
func (b *BloomFilterWriter) Close() error {
return nil
}
func (b *BloomFilterWriter) getSkipIndexFilePath(fieldName string, detached bool) string {
if detached {
return GetBloomFilterFilePath(b.dir, b.msName, fieldName)
}
return path.Join(b.dir, b.msName, colstore.AppendSKIndexSuffix(b.dataFilePath, fieldName, index.BloomFilterIndex)+tmpFileSuffix)
}
func (b *BloomFilterWriter) CreateAttachSkipIndex(schemaIdx, rowsPerSegment []int, writeRec *record.Record) error {
var err error
var data []byte
var skipIndexFilePath string
for _, i := range schemaIdx {
skipIndexFilePath = b.getSkipIndexFilePath(writeRec.Schema[i].Name, false)
data = b.GenBloomFilterData(&writeRec.ColVals[i], rowsPerSegment, writeRec.Schema[i].Type)
err = writeSkipIndexToDisk(data, b.lockPath, skipIndexFilePath)
if err != nil {
return err
}
}
return nil
}
func (b *BloomFilterWriter) CreateDetachSkipIndex(writeRec *record.Record, schemaIdx, rowsPerSegment []int,
dataBuf [][]byte) ([][]byte, []string) {
skipIndexFilePaths := make([]string, len(schemaIdx))
for k, v := range schemaIdx {
data := b.GenBloomFilterData(&writeRec.ColVals[v], rowsPerSegment, writeRec.Schema[v].Type)
dataBuf[k] = append(dataBuf[k], data...)
skipIndexFilePaths[k] = b.getSkipIndexFilePath(writeRec.Schema[v].Name, true)
}
return dataBuf, skipIndexFilePaths
}
func (b *BloomFilterWriter) GenBloomFilterData(src *record.ColVal, rowsPerSegment []int, refType int) []byte {
tk := tokenizer.NewGramTokenizer(tokenizer.CONTENT_SPLITTER, 0, logstore.GramTokenizerVersion)
segCnt := len(rowsPerSegment)
segBfSize := int(logstore.GetConstant(logstore.CurrentLogTokenizerVersion).FilterDataDiskSize)
res := make([]byte, segCnt*segBfSize)
var segCol []record.ColVal
segCol = src.SplitColBySize(segCol, rowsPerSegment, refType)
start := 0
end := 0
for _, col := range segCol {
end = start + segBfSize
offs, lens := col.GetOffsAndLens()
tk.ProcessTokenizerBatch(col.Val, res[start:end-crcSize], offs, lens)
crc := crc32.Checksum(res[start:end-crcSize], logstore.Table)
binary.LittleEndian.PutUint32(res[end-crcSize:end], crc)
start = end
}
tokenizer.FreeSimpleGramTokenizer(tk)
return res
}