github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/nbs/table_set_test.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package nbs
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  var testChunks = [][]byte{[]byte("hello2"), []byte("goodbye2"), []byte("badbye2")}
    14  
    15  func TestTableSetPrependEmpty(t *testing.T) {
    16  	ts := newFakeTableSet().Prepend(newMemTable(testMemTableSize), &Stats{})
    17  	assert.Empty(t, ts.ToSpecs())
    18  }
    19  
    20  func TestTableSetPrepend(t *testing.T) {
    21  	assert := assert.New(t)
    22  	ts := newFakeTableSet()
    23  	assert.Empty(ts.ToSpecs())
    24  	mt := newMemTable(testMemTableSize)
    25  	mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
    26  	ts = ts.Prepend(mt, &Stats{})
    27  
    28  	firstSpecs := ts.ToSpecs()
    29  	assert.Len(firstSpecs, 1)
    30  
    31  	mt = newMemTable(testMemTableSize)
    32  	mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
    33  	mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
    34  	ts = ts.Prepend(mt, &Stats{})
    35  
    36  	secondSpecs := ts.ToSpecs()
    37  	assert.Len(secondSpecs, 2)
    38  	assert.Equal(firstSpecs, secondSpecs[1:])
    39  }
    40  
    41  func TestTableSetToSpecsExcludesEmptyTable(t *testing.T) {
    42  	assert := assert.New(t)
    43  	ts := newFakeTableSet()
    44  	assert.Empty(ts.ToSpecs())
    45  	mt := newMemTable(testMemTableSize)
    46  	mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
    47  	ts = ts.Prepend(mt, &Stats{})
    48  
    49  	mt = newMemTable(testMemTableSize)
    50  	ts = ts.Prepend(mt, &Stats{})
    51  
    52  	mt = newMemTable(testMemTableSize)
    53  	mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
    54  	mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
    55  	ts = ts.Prepend(mt, &Stats{})
    56  
    57  	specs := ts.ToSpecs()
    58  	assert.Len(specs, 2)
    59  }
    60  
    61  func TestTableSetFlattenExcludesEmptyTable(t *testing.T) {
    62  	assert := assert.New(t)
    63  	ts := newFakeTableSet()
    64  	assert.Empty(ts.ToSpecs())
    65  	mt := newMemTable(testMemTableSize)
    66  	mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
    67  	ts = ts.Prepend(mt, &Stats{})
    68  
    69  	mt = newMemTable(testMemTableSize)
    70  	ts = ts.Prepend(mt, &Stats{})
    71  
    72  	mt = newMemTable(testMemTableSize)
    73  	mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
    74  	mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
    75  	ts = ts.Prepend(mt, &Stats{})
    76  
    77  	ts = ts.Flatten()
    78  	assert.EqualValues(ts.Size(), 2)
    79  }
    80  
    81  func TestTableSetExtract(t *testing.T) {
    82  	assert := assert.New(t)
    83  	ts := newFakeTableSet()
    84  	assert.Empty(ts.ToSpecs())
    85  
    86  	// Put in one table
    87  	mt := newMemTable(testMemTableSize)
    88  	mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
    89  	ts = ts.Prepend(mt, &Stats{})
    90  
    91  	// Put in a second
    92  	mt = newMemTable(testMemTableSize)
    93  	mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
    94  	mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
    95  	ts = ts.Prepend(mt, &Stats{})
    96  
    97  	chunkChan := make(chan extractRecord)
    98  	go func() { defer close(chunkChan); ts.extract(chunkChan) }()
    99  	i := 0
   100  	for rec := range chunkChan {
   101  		a := computeAddr(testChunks[i])
   102  		assert.NotNil(rec.data, "Nothing for", a)
   103  		assert.Equal(testChunks[i], rec.data, "Item %d: %s != %s", i, string(testChunks[i]), string(rec.data))
   104  		assert.Equal(a, rec.a)
   105  		i++
   106  	}
   107  }
   108  
   109  func TestTableSetRebase(t *testing.T) {
   110  	assert := assert.New(t)
   111  	persister := newFakeTablePersister()
   112  
   113  	insert := func(ts tableSet, chunks ...[]byte) tableSet {
   114  		for _, c := range chunks {
   115  			mt := newMemTable(testMemTableSize)
   116  			mt.addChunk(computeAddr(c), c)
   117  			ts = ts.Prepend(mt, &Stats{})
   118  		}
   119  		return ts
   120  	}
   121  	fullTS := newTableSet(persister)
   122  	assert.Empty(fullTS.ToSpecs())
   123  	fullTS = insert(fullTS, testChunks...)
   124  	fullTS = fullTS.Flatten()
   125  
   126  	ts := newTableSet(persister)
   127  	ts = insert(ts, testChunks[0])
   128  	assert.Equal(1, ts.Size())
   129  	ts = ts.Flatten()
   130  	ts = insert(ts, []byte("novel"))
   131  
   132  	ts = ts.Rebase(fullTS.ToSpecs(), nil)
   133  	assert.Equal(4, ts.Size())
   134  }
   135  
   136  func TestTableSetPhysicalLen(t *testing.T) {
   137  	assert := assert.New(t)
   138  	ts := newFakeTableSet()
   139  	assert.Empty(ts.ToSpecs())
   140  	mt := newMemTable(testMemTableSize)
   141  	mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
   142  	ts = ts.Prepend(mt, &Stats{})
   143  
   144  	mt = newMemTable(testMemTableSize)
   145  	mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
   146  	mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
   147  	ts = ts.Prepend(mt, &Stats{})
   148  
   149  	assert.True(ts.physicalLen() > indexSize(ts.count()))
   150  }