github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/sstable/properties_test.go (about)

     1  // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package sstable
     6  
     7  import (
     8  	"math"
     9  	"math/rand"
    10  	"os"
    11  	"path/filepath"
    12  	"reflect"
    13  	"strings"
    14  	"testing"
    15  	"testing/quick"
    16  	"time"
    17  
    18  	"github.com/kr/pretty"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestPropertiesLoad(t *testing.T) {
    23  	expected := Properties{
    24  		ColumnFamilyID:         math.MaxInt32,
    25  		ComparerName:           "leveldb.BytewiseComparator",
    26  		CompressionName:        "Snappy",
    27  		CompressionOptions:     "window_bits=-14; level=32767; strategy=0; max_dict_bytes=0; zstd_max_train_bytes=0; enabled=0; ",
    28  		DataSize:               13913,
    29  		ExternalFormatVersion:  2,
    30  		IndexSize:              325,
    31  		MergerName:             "nullptr",
    32  		NumDataBlocks:          14,
    33  		NumEntries:             1727,
    34  		NumDeletions:           17,
    35  		NumRangeDeletions:      17,
    36  		PrefixExtractorName:    "nullptr",
    37  		PropertyCollectorNames: "[KeyCountPropertyCollector]",
    38  		RawKeySize:             23938,
    39  		RawValueSize:           1912,
    40  		UserProperties: map[string]string{
    41  			"test.key-count": "1727",
    42  		},
    43  		WholeKeyFiltering: false,
    44  	}
    45  
    46  	{
    47  		// Check that we can read properties from a table.
    48  		f, err := os.Open(filepath.FromSlash("testdata/h.sst"))
    49  		require.NoError(t, err)
    50  
    51  		r, err := NewReader(f, ReaderOptions{})
    52  		require.NoError(t, err)
    53  		defer r.Close()
    54  
    55  		r.Properties.Loaded = nil
    56  		if diff := pretty.Diff(expected, r.Properties); diff != nil {
    57  			t.Fatalf("%s", strings.Join(diff, "\n"))
    58  		}
    59  	}
    60  }
    61  
    62  func TestPropertiesSave(t *testing.T) {
    63  	expected := &Properties{
    64  		ColumnFamilyID:           1,
    65  		ColumnFamilyName:         "column family name",
    66  		ComparerName:             "comparator name",
    67  		CompressionName:          "compression name",
    68  		CompressionOptions:       "compression option",
    69  		CreationTime:             2,
    70  		DataSize:                 3,
    71  		ExternalFormatVersion:    4,
    72  		FilterPolicyName:         "filter policy name",
    73  		FilterSize:               5,
    74  		FixedKeyLen:              6,
    75  		FormatVersion:            7,
    76  		GlobalSeqNum:             8,
    77  		IndexKeyIsUserKey:        9,
    78  		IndexPartitions:          10,
    79  		IndexSize:                11,
    80  		IndexType:                12,
    81  		IndexValueIsDeltaEncoded: 13,
    82  		MergerName:               "merge operator name",
    83  		NumDataBlocks:            14,
    84  		NumDeletions:             15,
    85  		NumEntries:               16,
    86  		NumMergeOperands:         17,
    87  		NumRangeDeletions:        18,
    88  		NumRangeKeyDels:          19,
    89  		NumRangeKeySets:          20,
    90  		NumRangeKeyUnsets:        21,
    91  		OldestKeyTime:            22,
    92  		PrefixExtractorName:      "prefix extractor name",
    93  		PrefixFiltering:          true,
    94  		PropertyCollectorNames:   "prefix collector names",
    95  		RawKeySize:               23,
    96  		RawValueSize:             24,
    97  		TopLevelIndexSize:        25,
    98  		WholeKeyFiltering:        true,
    99  		UserProperties: map[string]string{
   100  			"user-prop-a": "1",
   101  			"user-prop-b": "2",
   102  		},
   103  	}
   104  
   105  	check1 := func(expected *Properties) {
   106  		// Check that we can save properties and read them back.
   107  		var w rawBlockWriter
   108  		w.restartInterval = propertiesBlockRestartInterval
   109  		expected.save(&w)
   110  		var props Properties
   111  		require.NoError(t, props.load(w.finish(), 0))
   112  		props.Loaded = nil
   113  		if diff := pretty.Diff(*expected, props); diff != nil {
   114  			t.Fatalf("%s", strings.Join(diff, "\n"))
   115  		}
   116  	}
   117  
   118  	check1(expected)
   119  
   120  	rng := rand.New(rand.NewSource(time.Now().UnixNano()))
   121  	for i := 0; i < 1000; i++ {
   122  		v, _ := quick.Value(reflect.TypeOf(Properties{}), rng)
   123  		props := v.Interface().(Properties)
   124  		if props.IndexPartitions == 0 {
   125  			props.TopLevelIndexSize = 0
   126  		}
   127  		check1(&props)
   128  	}
   129  }