github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/query/metadata/metareader_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 metadata 18 19 import ( 20 "os" 21 "sync" 22 "testing" 23 24 localstorage "github.com/siglens/siglens/pkg/blob/local" 25 "github.com/siglens/siglens/pkg/segment/structs" 26 "github.com/siglens/siglens/pkg/segment/writer" 27 log "github.com/sirupsen/logrus" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func Test_readWriteMicroIndices(t *testing.T) { 32 globalMetadata = &allSegmentMetadata{ 33 allSegmentMicroIndex: make([]*SegmentMicroIndex, 0), 34 segmentMetadataReverseIndex: make(map[string]*SegmentMicroIndex), 35 tableSortedMetadata: make(map[string][]*SegmentMicroIndex), 36 updateLock: &sync.RWMutex{}, 37 } 38 segDir := "data/" 39 _ = os.MkdirAll(segDir, 0755) 40 segKey := segDir + "test" 41 blockSummariesFile := structs.GetBsuFnameFromSegKey(segKey) 42 numBlocks := 10 43 _, blockSummaries, _, _, allBmh, allColsSizes := writer.WriteMockColSegFile(segKey, numBlocks, 30) 44 writer.WriteMockBlockSummary(blockSummariesFile, blockSummaries, allBmh) 45 46 bMicro := &SegmentMicroIndex{ 47 SegMeta: structs.SegMeta{ 48 SegmentKey: segKey, 49 ColumnNames: allColsSizes, 50 VirtualTableName: "test", 51 SegbaseDir: segKey, // its actually one dir up but for mocks/tests its fine 52 }, 53 } 54 bMicro.SegbaseDir = segKey // for mocks its fine 55 56 _ = localstorage.InitLocalStorage() 57 _, blockSum, _, err := bMicro.readBlockSummaries([]byte{}) 58 assert.Nil(t, err) 59 log.Infof("num block summaries: %d", len(blockSum)) 60 assert.Len(t, blockSum, numBlocks) 61 os.RemoveAll(blockSummariesFile) 62 os.RemoveAll(segDir) 63 } 64 65 func Test_readEmptyColumnMicroIndices(t *testing.T) { 66 67 globalMetadata = &allSegmentMetadata{ 68 allSegmentMicroIndex: make([]*SegmentMicroIndex, 0), 69 segmentMetadataReverseIndex: make(map[string]*SegmentMicroIndex), 70 tableSortedMetadata: make(map[string][]*SegmentMicroIndex), 71 updateLock: &sync.RWMutex{}, 72 } 73 _ = localstorage.InitLocalStorage() 74 75 cnames := make(map[string]*structs.ColSizeInfo) 76 cnames["clickid"] = &structs.ColSizeInfo{CmiSize: 0, CsgSize: 0} 77 bMicro := &SegmentMicroIndex{ 78 SegMeta: structs.SegMeta{ 79 SegmentKey: "test-key", 80 ColumnNames: cnames, 81 VirtualTableName: "test", 82 }, 83 } 84 85 err := bMicro.loadMicroIndices(map[uint16]map[string]bool{}, true, map[string]bool{}, false) 86 if err != nil { 87 log.Errorf("failed to read cmi, err=%v", err) 88 } 89 assert.Nil(t, err) 90 }