github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/reader/segread/segstatsreader_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 segread
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  	"testing"
    24  
    25  	"github.com/axiomhq/hyperloglog"
    26  	"github.com/siglens/siglens/pkg/segment/structs"
    27  	"github.com/siglens/siglens/pkg/segment/utils"
    28  	"github.com/siglens/siglens/pkg/segment/writer"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func Test_sstReadWrite(t *testing.T) {
    33  
    34  	fname := "segkey-1.sst"
    35  
    36  	_ = os.MkdirAll(path.Dir(fname), 0755)
    37  
    38  	myHll := hyperloglog.New16()
    39  
    40  	for i := 0; i < 3200; i++ {
    41  		myHll.Insert([]byte(fmt.Sprintf("mystr:%v", i)))
    42  	}
    43  
    44  	myNums := structs.NumericStats{
    45  		Min: utils.NumTypeEnclosure{Ntype: utils.SS_DT_SIGNED_NUM,
    46  			IntgrVal: 456},
    47  		Max: utils.NumTypeEnclosure{Ntype: utils.SS_DT_FLOAT,
    48  			FloatVal: 23.4567},
    49  		Sum: utils.NumTypeEnclosure{Ntype: utils.SS_DT_SIGNED_NUM,
    50  			IntgrVal: 789},
    51  	}
    52  
    53  	inSst := structs.SegStats{
    54  		IsNumeric: true,
    55  		Count:     2345,
    56  		Hll:       myHll,
    57  		NumStats:  &myNums,
    58  	}
    59  
    60  	allSst := make(map[string]*structs.SegStats)
    61  
    62  	allSst["col-a"] = &inSst
    63  	allSst["col-b"] = &inSst
    64  
    65  	ss := writer.SegStore{SegmentKey: "segkey-1",
    66  		AllSst: allSst}
    67  
    68  	err := ss.FlushSegStats()
    69  	assert.Nil(t, err)
    70  
    71  	allSstMap, err := ReadSegStats("segkey-1", 123)
    72  	assert.Nil(t, err)
    73  
    74  	outSst, pres := allSstMap["col-b"]
    75  
    76  	assert.True(t, pres)
    77  
    78  	assert.Equal(t, inSst.IsNumeric, outSst.IsNumeric)
    79  
    80  	assert.Equal(t, inSst.Count, outSst.Count)
    81  
    82  	assert.Equal(t, inSst.NumStats, outSst.NumStats)
    83  
    84  	assert.Equal(t, inSst.Hll.Estimate(), outSst.Hll.Estimate())
    85  
    86  	_ = os.RemoveAll(fname)
    87  }