/
bloomfilter.go
114 lines (97 loc) · 3.01 KB
/
bloomfilter.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
/*
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 logstore
import (
"encoding/binary"
"hash/crc32"
"sync"
"github.com/openGemini/openGemini/lib/index"
"github.com/openGemini/openGemini/lib/record"
"github.com/openGemini/openGemini/lib/util"
"github.com/openGemini/openGemini/lib/util/lifted/influx/influxql"
)
const (
GramTokenizerVersion = 3
CurrentLogTokenizerVersion = 5
)
var (
Table = crc32.MakeTable(crc32.Castagnoli)
)
func FlushVerticalFilter(filterVerBuffer, filterLogBuffer []byte) []byte {
bufCursor := 0
constant := GetConstant(CurrentLogTokenizerVersion)
for i := 0; i < int(constant.VerticalPieceCntPerFilter); i++ {
for j := 0; j < int(constant.FilterCntPerVerticalGorup); j++ {
offset := (j * int(constant.FilterDataDiskSize)) + (i * util.Int64SizeBytes)
val := binary.LittleEndian.Uint64(filterLogBuffer[offset:])
filterVerBuffer = binary.LittleEndian.AppendUint64(filterVerBuffer, val)
}
crc := crc32.Checksum(filterVerBuffer[bufCursor:bufCursor+int(constant.VerticalPieceMemSize)], Table)
filterVerBuffer = binary.LittleEndian.AppendUint32(filterVerBuffer, crc)
bufCursor += int(constant.VerticalPieceDiskSize)
}
return filterVerBuffer
}
// GenSchemaIdxs get bloom filter cols index in the schema
func GenSchemaIdxs(schema record.Schemas, skipIndexRelation *influxql.IndexRelation, fullTextIdx bool) []int {
var res []int
if fullTextIdx {
for i := range schema {
if schema[i].IsString() {
res = append(res, i)
}
}
return res
}
for i := range skipIndexRelation.IndexNames {
if skipIndexRelation.IndexNames[i] != index.BloomFilterIndex {
continue
}
res = make([]int, 0, len(skipIndexRelation.IndexList[i].IList))
for idx := range skipIndexRelation.IndexList[i].IList {
for j := range schema {
if skipIndexRelation.IndexList[i].IList[idx] == schema[j].Name {
res = append(res, j)
}
}
}
}
return res
}
func IsFullTextIdx(indexRelation *influxql.IndexRelation) bool {
if indexRelation == nil {
return false
}
for i := range indexRelation.IndexNames {
if indexRelation.IndexNames[i] == index.BloomFilterFullTextIndex {
return true
}
}
return false
}
var IndexBufferPool sync.Pool
type multiCosBuf [][]byte
func GetIndexBuf(cols int) *multiCosBuf {
buf := IndexBufferPool.Get()
if buf == nil {
multiBuf := make(multiCosBuf, cols)
return &multiBuf
}
return buf.(*multiCosBuf)
}
func PutIndexBuf(buf *multiCosBuf) {
for i := range *buf {
(*buf)[i] = (*buf)[i][:0]
}
IndexBufferPool.Put(buf)
}