github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/writer/rawchecker_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package writer 18 19 import ( 20 "fmt" 21 "os" 22 "time" 23 24 log "github.com/sirupsen/logrus" 25 bbp "github.com/valyala/bytebufferpool" 26 27 "github.com/stretchr/testify/assert" 28 29 "testing" 30 31 "github.com/siglens/siglens/pkg/config" 32 "github.com/siglens/siglens/pkg/segment/pqmr" 33 . "github.com/siglens/siglens/pkg/segment/structs" 34 . "github.com/siglens/siglens/pkg/segment/utils" 35 "github.com/siglens/siglens/pkg/utils" 36 ) 37 38 func Test_ApplySearchToMatchFilterRaw(t *testing.T) { 39 config.InitializeTestingConfig() 40 defer os.RemoveAll(config.GetDataPath()) 41 rangeIndex = map[string]*Numbers{} 42 cases := []struct { 43 input []byte 44 }{ 45 { 46 //case#1 47 []byte(`{ 48 "a":"val1 val2 val3 val4 val5", 49 "c":true, 50 "d":"John", 51 "i":13887551456, 52 "m":-3887551456, 53 "n":-1.323232, 54 "timestamp": 1234 55 }`, 56 )}, 57 } 58 59 for i, test := range cases { 60 cTime := uint64(time.Now().UnixMilli()) 61 sId := fmt.Sprintf("test-a-%d", i) 62 segstore, err := getSegStore(sId, cTime, "test", 0) 63 if err != nil { 64 log.Errorf("AddEntryToInMemBuf, getSegstore err=%v", err) 65 t.Errorf("failed to get segstore! %v", err) 66 } 67 tsKey := config.GetTimeStampKey() 68 _, _, err = segstore.EncodeColumns(test.input, cTime, &tsKey, SIGNAL_EVENTS) 69 assert.Nil(t, err) 70 71 colWips := allSegStores[sId].wipBlock.colWips 72 mf := MatchFilter{ 73 MatchColumn: "*", 74 MatchWords: [][]byte{[]byte("abcdefg"), []byte("val2")}, 75 76 MatchOperator: Or, 77 } 78 79 var found bool 80 for _, colWip := range colWips { 81 result, err := ApplySearchToMatchFilterRawCsg(&mf, colWip.cbuf[:]) 82 assert.Nil(t, err) 83 found = result 84 if found { 85 break 86 } 87 } 88 assert.Equal(t, true, found) 89 90 t.Logf("searching for val2 in all columns worked") 91 92 mf = MatchFilter{ 93 MatchColumn: "a", 94 MatchWords: [][]byte{[]byte("abcdefg"), []byte("val2")}, 95 MatchOperator: Or, 96 } 97 98 result, err := ApplySearchToMatchFilterRawCsg(&mf, colWips[mf.MatchColumn].cbuf[:]) 99 assert.Nil(t, err) 100 assert.Equal(t, true, result) 101 t.Logf("searching for val2 in column-a worked") 102 103 mf = MatchFilter{ 104 MatchColumn: "d", 105 MatchWords: [][]byte{[]byte("abcdefg"), []byte("val2")}, 106 MatchOperator: Or, 107 } 108 109 result, err = ApplySearchToMatchFilterRawCsg(&mf, colWips[mf.MatchColumn].cbuf[:]) 110 assert.Nil(t, err) 111 assert.Equal(t, false, result) 112 t.Logf("searching for val2 in column-d worked (should not be found)") 113 114 mf = MatchFilter{ 115 MatchColumn: "a", 116 MatchWords: [][]byte{[]byte("abcdefg"), []byte("val2")}, 117 MatchOperator: And, 118 } 119 120 result, err = ApplySearchToMatchFilterRawCsg(&mf, colWips[mf.MatchColumn].cbuf[:]) 121 assert.Nil(t, err) 122 assert.Equal(t, false, result) 123 t.Logf("searching for two values in column-a worked (should not be found)") 124 125 mf = MatchFilter{ 126 MatchColumn: "a", 127 MatchWords: [][]byte{[]byte("val1"), []byte("val2"), []byte("val3"), []byte("val4")}, 128 MatchOperator: And, 129 } 130 131 result, err = ApplySearchToMatchFilterRawCsg(&mf, colWips[mf.MatchColumn].cbuf[:]) 132 assert.Nil(t, err) 133 assert.Equal(t, true, result) 134 t.Logf("searching for multiple values in column-a worked (all should be found)") 135 } 136 } 137 138 func Test_applySearchToExpressionFilterSimpleHelper(t *testing.T) { 139 rangeIndex = map[string]*Numbers{} 140 cases := []struct { 141 input []byte 142 }{ 143 { 144 //case#1 145 []byte(`{ 146 "cbool":true, 147 "csigned":-2345, 148 "cunsigned":2345, 149 "cfloat":-2345.35, 150 "cstr":"haystack", 151 "timestamp": 1234 152 }`, 153 )}, 154 } 155 156 for _, test := range cases { 157 allCols := make(map[string]bool) 158 segstats := make(map[string]*SegStats) 159 160 var blockSummary BlockSummary 161 colWips := make(map[string]*ColWip) 162 wipBlock := WipBlock{ 163 columnBlooms: make(map[string]*BloomIndex), 164 columnRangeIndexes: make(map[string]*RangeIndex), 165 colWips: colWips, 166 pqMatches: make(map[string]*pqmr.PQMatchResults), 167 columnsInBlock: make(map[string]bool), 168 blockSummary: blockSummary, 169 tomRollup: make(map[uint64]*RolledRecs), 170 tohRollup: make(map[uint64]*RolledRecs), 171 todRollup: make(map[uint64]*RolledRecs), 172 bb: bbp.Get(), 173 } 174 segstore := &SegStore{ 175 wipBlock: wipBlock, 176 SegmentKey: "test-segkey", 177 AllSeenColumns: allCols, 178 pqTracker: initPQTracker(), 179 AllSst: segstats, 180 numBlocks: 0, 181 } 182 ts := config.GetTimeStampKey() 183 maxIdx, _, err := segstore.EncodeColumns(test.input, 1234, &ts, SIGNAL_EVENTS) 184 t.Logf("encoded len: %v, origlen=%v", maxIdx, len(test.input)) 185 186 assert.Nil(t, err) 187 assert.Greater(t, maxIdx, uint32(0)) 188 189 var holderDte *DtypeEnclosure = &DtypeEnclosure{} 190 var qValDte *DtypeEnclosure 191 192 t.Logf("doing equals search for haystack in cstr") 193 qValDte, _ = CreateDtypeEnclosure("haystack", 0) 194 qValDte.AddStringAsByteSlice() 195 var eOff uint16 = 3 + utils.BytesToUint16LittleEndian(colWips["cstr"].cbuf[1:3]) // 2 bytes stored for string type 196 result, err := ApplySearchToExpressionFilterSimpleCsg(qValDte, Equals, colWips["cstr"].cbuf[:eOff], false, holderDte) 197 assert.Nil(t, err) 198 assert.Equal(t, true, result) 199 qValDte.Reset() 200 201 t.Logf("doing equals search for haystack for col that is not string") 202 qValDte, _ = CreateDtypeEnclosure("haystack", 0) 203 qValDte.AddStringAsByteSlice() 204 result, _ = ApplySearchToExpressionFilterSimpleCsg(qValDte, Equals, colWips["cfloat"].cbuf[:], false, holderDte) 205 assert.Equal(t, false, result) 206 qValDte.Reset() 207 208 //TODO: uncomment when ApplySearchToExpressionFilterSimpleCsg for numbers is implemented 209 t.Logf("doing equals search for float ") 210 t.Logf("cbuf:%s", string(colWips["cfloat"].cbuf[:])) 211 qValDte, _ = CreateDtypeEnclosure(-2345.35, 0) 212 result, _ = ApplySearchToExpressionFilterSimpleCsg(qValDte, Equals, colWips["cfloat"].cbuf[:], false, holderDte) 213 assert.Equal(t, true, result) 214 qValDte.Reset() 215 216 t.Logf("doing equals search for unsigned ") 217 qValDte, _ = CreateDtypeEnclosure(2345, 0) 218 result, _ = ApplySearchToExpressionFilterSimpleCsg(qValDte, Equals, colWips["cunsigned"].cbuf[:], false, holderDte) 219 assert.Equal(t, true, result) 220 qValDte.Reset() 221 } 222 }