github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/nbs/table_reader_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 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 nbs 16 17 import ( 18 "io/ioutil" 19 "os" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestCompressedChunkIsEmpty(t *testing.T) { 27 if !EmptyCompressedChunk.IsEmpty() { 28 t.Fatal("EmptyCompressedChunkIsEmpty() should equal true.") 29 } 30 if !(CompressedChunk{}).IsEmpty() { 31 t.Fatal("CompressedChunk{}.IsEmpty() should equal true.") 32 } 33 } 34 35 func TestParseTableIndex(t *testing.T) { 36 f, err := os.Open("testdata/0oa7mch34jg1rvghrnhr4shrp2fm4ftd.idx") 37 require.NoError(t, err) 38 defer f.Close() 39 bs, err := ioutil.ReadAll(f) 40 require.NoError(t, err) 41 idx, err := parseTableIndex(bs) 42 require.NoError(t, err) 43 defer idx.Close() 44 assert.Equal(t, uint32(596), idx.ChunkCount()) 45 seen := make(map[addr]bool) 46 for i := uint32(0); i < idx.ChunkCount(); i++ { 47 var onheapaddr addr 48 e := idx.IndexEntry(i, &onheapaddr) 49 if _, ok := seen[onheapaddr]; !ok { 50 seen[onheapaddr] = true 51 lookupe, ok := idx.Lookup(&onheapaddr) 52 assert.True(t, ok) 53 assert.Equal(t, e.Offset(), lookupe.Offset(), "%v does not match %v for address %v", e, lookupe, onheapaddr) 54 assert.Equal(t, e.Length(), lookupe.Length()) 55 } 56 } 57 } 58 59 func TestMMapIndex(t *testing.T) { 60 f, err := os.Open("testdata/0oa7mch34jg1rvghrnhr4shrp2fm4ftd.idx") 61 require.NoError(t, err) 62 defer f.Close() 63 bs, err := ioutil.ReadAll(f) 64 require.NoError(t, err) 65 idx, err := parseTableIndex(bs) 66 require.NoError(t, err) 67 defer idx.Close() 68 mmidx, err := newMmapTableIndex(idx, nil) 69 require.NoError(t, err) 70 defer mmidx.Close() 71 assert.Equal(t, idx.ChunkCount(), mmidx.ChunkCount()) 72 seen := make(map[addr]bool) 73 for i := uint32(0); i < idx.ChunkCount(); i++ { 74 var onheapaddr addr 75 onheapentry := idx.IndexEntry(i, &onheapaddr) 76 var mmaddr addr 77 mmentry := mmidx.IndexEntry(i, &mmaddr) 78 assert.Equal(t, onheapaddr, mmaddr) 79 assert.Equal(t, onheapentry.Offset(), mmentry.Offset()) 80 assert.Equal(t, onheapentry.Length(), mmentry.Length()) 81 if _, ok := seen[onheapaddr]; !ok { 82 seen[onheapaddr] = true 83 mmentry, found := mmidx.Lookup(&onheapaddr) 84 assert.True(t, found) 85 assert.Equal(t, onheapentry.Offset(), mmentry.Offset(), "%v does not match %v for address %v", onheapentry, mmentry, onheapaddr) 86 assert.Equal(t, onheapentry.Length(), mmentry.Length()) 87 } 88 wrongaddr := onheapaddr 89 if wrongaddr[19] != 0 { 90 wrongaddr[19] = 0 91 _, found := mmidx.Lookup(&wrongaddr) 92 assert.False(t, found) 93 } 94 } 95 96 assert.Equal(t, idx.Ordinals(), mmidx.Ordinals()) 97 assert.Equal(t, idx.Prefixes(), mmidx.Prefixes()) 98 assert.Equal(t, idx.TableFileSize(), mmidx.TableFileSize()) 99 assert.Equal(t, idx.TotalUncompressedData(), mmidx.TotalUncompressedData()) 100 }