github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/syndtr/goleveldb/leveldb/batch_test.go (about)

     1  // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
     2  // All rights reserved.
     3  //
     4  // Use of this source code is governed by a BSD-style license that can be
     5  // found in the LICENSE file.
     6  
     7  package leveldb
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  
    13  	"github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/comparer"
    14  	"github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/memdb"
    15  )
    16  
    17  type tbRec struct {
    18  	kt         keyType
    19  	key, value []byte
    20  }
    21  
    22  type testBatch struct {
    23  	rec []*tbRec
    24  }
    25  
    26  func (p *testBatch) Put(key, value []byte) {
    27  	p.rec = append(p.rec, &tbRec{keyTypeVal, key, value})
    28  }
    29  
    30  func (p *testBatch) Delete(key []byte) {
    31  	p.rec = append(p.rec, &tbRec{keyTypeDel, key, nil})
    32  }
    33  
    34  func compareBatch(t *testing.T, b1, b2 *Batch) {
    35  	if b1.seq != b2.seq {
    36  		t.Errorf("invalid seq number want %d, got %d", b1.seq, b2.seq)
    37  	}
    38  	if b1.Len() != b2.Len() {
    39  		t.Fatalf("invalid record length want %d, got %d", b1.Len(), b2.Len())
    40  	}
    41  	p1, p2 := new(testBatch), new(testBatch)
    42  	err := b1.Replay(p1)
    43  	if err != nil {
    44  		t.Fatal("error when replaying batch 1: ", err)
    45  	}
    46  	err = b2.Replay(p2)
    47  	if err != nil {
    48  		t.Fatal("error when replaying batch 2: ", err)
    49  	}
    50  	for i := range p1.rec {
    51  		r1, r2 := p1.rec[i], p2.rec[i]
    52  		if r1.kt != r2.kt {
    53  			t.Errorf("invalid type on record '%d' want %d, got %d", i, r1.kt, r2.kt)
    54  		}
    55  		if !bytes.Equal(r1.key, r2.key) {
    56  			t.Errorf("invalid key on record '%d' want %s, got %s", i, string(r1.key), string(r2.key))
    57  		}
    58  		if r1.kt == keyTypeVal {
    59  			if !bytes.Equal(r1.value, r2.value) {
    60  				t.Errorf("invalid value on record '%d' want %s, got %s", i, string(r1.value), string(r2.value))
    61  			}
    62  		}
    63  	}
    64  }
    65  
    66  func TestBatch_EncodeDecode(t *testing.T) {
    67  	b1 := new(Batch)
    68  	b1.seq = 10009
    69  	b1.Put([]byte("key1"), []byte("value1"))
    70  	b1.Put([]byte("key2"), []byte("value2"))
    71  	b1.Delete([]byte("key1"))
    72  	b1.Put([]byte("k"), []byte(""))
    73  	b1.Put([]byte("zzzzzzzzzzz"), []byte("zzzzzzzzzzzzzzzzzzzzzzzz"))
    74  	b1.Delete([]byte("key10000"))
    75  	b1.Delete([]byte("k"))
    76  	buf := b1.encode()
    77  	b2 := new(Batch)
    78  	err := b2.decode(0, buf)
    79  	if err != nil {
    80  		t.Error("error when decoding batch: ", err)
    81  	}
    82  	compareBatch(t, b1, b2)
    83  }
    84  
    85  func TestBatch_Append(t *testing.T) {
    86  	b1 := new(Batch)
    87  	b1.seq = 10009
    88  	b1.Put([]byte("key1"), []byte("value1"))
    89  	b1.Put([]byte("key2"), []byte("value2"))
    90  	b1.Delete([]byte("key1"))
    91  	b1.Put([]byte("foo"), []byte("foovalue"))
    92  	b1.Put([]byte("bar"), []byte("barvalue"))
    93  	b2a := new(Batch)
    94  	b2a.seq = 10009
    95  	b2a.Put([]byte("key1"), []byte("value1"))
    96  	b2a.Put([]byte("key2"), []byte("value2"))
    97  	b2a.Delete([]byte("key1"))
    98  	b2b := new(Batch)
    99  	b2b.Put([]byte("foo"), []byte("foovalue"))
   100  	b2b.Put([]byte("bar"), []byte("barvalue"))
   101  	b2a.append(b2b)
   102  	compareBatch(t, b1, b2a)
   103  	if b1.size() != b2a.size() {
   104  		t.Fatalf("invalid batch size want %d, got %d", b1.size(), b2a.size())
   105  	}
   106  }
   107  
   108  func TestBatch_Size(t *testing.T) {
   109  	b := new(Batch)
   110  	for i := 0; i < 2; i++ {
   111  		b.Put([]byte("key1"), []byte("value1"))
   112  		b.Put([]byte("key2"), []byte("value2"))
   113  		b.Delete([]byte("key1"))
   114  		b.Put([]byte("foo"), []byte("foovalue"))
   115  		b.Put([]byte("bar"), []byte("barvalue"))
   116  		mem := memdb.New(&iComparer{comparer.DefaultComparer}, 0)
   117  		b.memReplay(mem)
   118  		if b.size() != mem.Size() {
   119  			t.Errorf("invalid batch size calculation, want=%d got=%d", mem.Size(), b.size())
   120  		}
   121  		b.Reset()
   122  	}
   123  }