github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitpage/table_test.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bitpage
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  	"github.com/zuoyebang/bitalosdb/internal/utils"
    25  )
    26  
    27  var testPath = filepath.Join(os.TempDir(), "testdata")
    28  
    29  func testNewTable() *table {
    30  	opts := &tableOptions{
    31  		openType:     tableWriteMmap,
    32  		initMmapSize: 10 << 20,
    33  	}
    34  	tbl, err := openTable(testPath, opts)
    35  	if err != nil {
    36  		panic(any(err))
    37  	}
    38  
    39  	return tbl
    40  }
    41  
    42  func testCloseTable(t *table) error {
    43  	if err := t.close(); err != nil {
    44  		return err
    45  	}
    46  	os.Remove(testPath)
    47  	return nil
    48  }
    49  
    50  func TestTable_ExpandSize(t *testing.T) {
    51  	defer os.Remove(testPath)
    52  	os.Remove(testPath)
    53  	tbl := testNewTable()
    54  	sz, _ := tbl.calcExpandSize(1)
    55  	require.Equal(t, 32768, sz)
    56  	sz, _ = tbl.calcExpandSize(32770)
    57  	require.Equal(t, 65536, sz)
    58  	sz, _ = tbl.calcExpandSize(65540)
    59  	require.Equal(t, 131072, sz)
    60  	sz, _ = tbl.calcExpandSize((1 << 30) + 1)
    61  	require.Equal(t, 1207959552, sz)
    62  	sz, _ = tbl.calcExpandSize(1<<30 + 128<<20 + 1)
    63  	require.Equal(t, 1342177280, sz)
    64  }
    65  
    66  func TestTable_WriteRead(t *testing.T) {
    67  	defer os.Remove(testPath)
    68  	os.Remove(testPath)
    69  	tbl := testNewTable()
    70  
    71  	vals := make([][]byte, 0, 100)
    72  	offs := make([]uint32, 0, 100)
    73  	for i := 1; i <= 100; i++ {
    74  		vals = append(vals, utils.FuncRandBytes(i))
    75  	}
    76  	for _, val := range vals {
    77  		size := len(val)
    78  		off, e1 := tbl.alloc(uint32(size))
    79  		offs = append(offs, off)
    80  		require.NoError(t, e1)
    81  		wn, e2 := tbl.writeAt(val, off)
    82  		require.NoError(t, e2)
    83  		require.Equal(t, size, wn)
    84  	}
    85  
    86  	for i, offset := range offs {
    87  		size := i + 1
    88  		v := tbl.getBytes(offset, uint32(size))
    89  		require.Equal(t, vals[i], v)
    90  	}
    91  
    92  	tblOffset := tbl.offset.Load()
    93  
    94  	require.NoError(t, tbl.close())
    95  
    96  	tbl1 := testNewTable()
    97  	require.Equal(t, tblOffset, tbl1.offset.Load())
    98  	fmt.Println(tbl1.offset.Load())
    99  	for i, offset := range offs {
   100  		size := i + 1
   101  		v := tbl1.getBytes(offset, uint32(size))
   102  		require.Equal(t, vals[i], v)
   103  	}
   104  	require.NoError(t, tbl1.close())
   105  	_ = os.Remove(testPath)
   106  }
   107  
   108  func TestTable_Expand(t *testing.T) {
   109  	defer os.Remove(testPath)
   110  	os.Remove(testPath)
   111  	tbl := testNewTable()
   112  	val := utils.FuncRandBytes(1024)
   113  	for i := 1; i <= 40*1024; i++ {
   114  		off, e1 := tbl.alloc(1024)
   115  		require.NoError(t, e1)
   116  		wn, e2 := tbl.writeAt(val, off)
   117  		require.NoError(t, e2)
   118  		require.Equal(t, 1024, wn)
   119  		require.Equal(t, val, tbl.getBytes(off, 1024))
   120  	}
   121  	offset := tableDataOffset
   122  	for i := 1; i <= 40*1024; i++ {
   123  		v := tbl.getBytes(uint32(offset), 1024)
   124  		require.Equal(t, val, v)
   125  		offset += 1024
   126  	}
   127  	tblOffset := tbl.offset.Load()
   128  	require.Equal(t, int64(tbl.filesz), tbl.fileStatSize())
   129  	require.Equal(t, 67108864, tbl.filesz)
   130  	require.Equal(t, uint32(41943044), tbl.offset.Load())
   131  	require.Equal(t, 67108864, tbl.datasz)
   132  	require.NoError(t, tbl.close())
   133  
   134  	tbl1 := testNewTable()
   135  	require.Equal(t, tblOffset, tbl1.offset.Load())
   136  	offset = tableDataOffset
   137  	for i := 1; i <= 40*1024; i++ {
   138  		v := tbl1.getBytes(uint32(offset), 1024)
   139  		require.Equal(t, val, v)
   140  		offset += 1024
   141  	}
   142  	require.Equal(t, int64(tbl1.filesz), tbl1.fileStatSize())
   143  	require.Equal(t, 67108864, tbl1.filesz)
   144  	require.Equal(t, uint32(41943044), tbl1.offset.Load())
   145  	require.Equal(t, 67108864, tbl1.datasz)
   146  	require.NoError(t, tbl1.close())
   147  	_ = os.Remove(testPath)
   148  }