示例#1
0
func TestBitCask(t *testing.T) {
	// clear dirty
	os.RemoveAll("testBitcask")
	b, err := Open("testBitcask", nil)
	logger.Info(err)
	assert.Nil(t, err)
	assert.NotNil(t, b)

	testKey := []byte("Foo")
	value := []byte("Bar")
	b.Put(testKey, value)
	v, err := b.Get(testKey)
	assert.Nil(t, err)
	logger.Info("value:", string(v))
	assert.Equal(t, v, value)

	testKey = []byte("xiaoMing")
	value = []byte("abc")
	b.Put(testKey, value)
	v, err = b.Get(testKey)
	logger.Info("value:", string(v))
	assert.Equal(t, v, value)

	// hintFile:
	value = []byte("ddddd")
	b.Put(testKey, value)
	v, err = b.Get(testKey)
	logger.Info("value:", string(v))
	assert.Equal(t, v, value)

	b.Close()
}
示例#2
0
func TestSplit2(t *testing.T) {
	storagePath := "split2Bitcask"
	os.RemoveAll(storagePath)
	opts := &Options{
		MaxFileSize: 2,
	}
	bc, err := Open(storagePath, opts)
	assert.Nil(t, err)
	testKey := []byte("Foo")

	value := []byte("Bar")
	bc.Put(testKey, value)
	v, err := bc.Get(testKey)
	assert.Nil(t, err)
	assert.Equal(t, v, value)
	logger.Info("==============================")
	time.Sleep(time.Second * 2)

	// cause split file
	value = []byte("Apple")
	bc.Put(testKey, value)
	v, err = bc.Get(testKey)
	assert.Nil(t, err)
	assert.Equal(t, v, value)
	bc.Close()
}
示例#3
0
// rebuild file test
func TestSplit1(t *testing.T) {
	storagePath := "split1Bitcask"
	os.RemoveAll(storagePath)
	bc, err := Open(storagePath, nil)
	assert.Nil(t, err)
	testKey := []byte("Foo")

	value := []byte("Bar")
	bc.Put(testKey, value)
	v, err := bc.Get(testKey)
	assert.Nil(t, err)
	assert.Equal(t, v, value)

	bc.Close()

	storagePath = "split1Bitcask"
	bc, err = Open(storagePath, nil)
	assert.Nil(t, err)
	testKey = []byte("Foo")

	value = []byte("Bar")
	bc.Put(testKey, value)
	v, err = bc.Get(testKey)
	assert.Nil(t, err)
	assert.Equal(t, v, value)
	bc.Close()
}
示例#4
0
func TestEncodeDecodeEntry(t *testing.T) {
	/**
		crc32	:	tStamp	:	ksz	:	valueSz	:	key	:	value
		4 		:	4 		: 	4 	: 		4	:	xxxx	: xxxx
	**/
	//func encodeEntry(tStamp, keySize, valueSize uint32, key, value []byte) []byte {
	// EncodeEntry
	tStamp := uint32(time.Now().Unix())
	key := []byte("Foo")
	value := []byte("Bar")
	ksz := uint32(len(key))
	valuesz := uint32(len(value))
	buf := make([]byte, HeaderSize+ksz+valuesz)
	binary.LittleEndian.PutUint32(buf[4:8], tStamp)
	binary.LittleEndian.PutUint32(buf[8:12], ksz)
	binary.LittleEndian.PutUint32(buf[12:16], valuesz)
	copy(buf[16:(16+ksz)], key)
	copy(buf[(16+ksz):(16+ksz+valuesz)], value)
	c32 := crc32.ChecksumIEEE(buf[4:])
	binary.LittleEndian.PutUint32(buf[0:4], uint32(c32))
	// Test decode

	ksz = binary.LittleEndian.Uint32(buf[8:12])
	valuesz = binary.LittleEndian.Uint32(buf[12:16])
	tStamp = binary.LittleEndian.Uint32(buf[4:8])
	c32 = binary.LittleEndian.Uint32(buf[:4])
	assert.Equal(t, binary.LittleEndian.Uint32(buf[0:4]), c32)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[4:8]), tStamp)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[8:12]), ksz)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[12:16]), valuesz)
	assert.Equal(t, buf[HeaderSize:(HeaderSize+ksz)], key)
	assert.Equal(t, buf[(HeaderSize+ksz):(HeaderSize+ksz+valuesz)], value)

	// EncodeEntry , ksz = 0, valueSz = 0
	ksz = uint32(0)
	valuesz = uint32(0)
	buf = make([]byte, HeaderSize+ksz+valuesz, HeaderSize+ksz+valuesz)
	binary.LittleEndian.PutUint32(buf[4:8], tStamp)
	binary.LittleEndian.PutUint32(buf[8:12], ksz)
	binary.LittleEndian.PutUint32(buf[12:16], valuesz)
	c32 = crc32.ChecksumIEEE(buf[4:])
	binary.LittleEndian.PutUint32(buf[0:4], c32)
	// decodeEntry, ksz =0, valueSz = 0
	assert.Equal(t, binary.LittleEndian.Uint32(buf[0:4]), c32)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[4:8]), tStamp)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[8:12]), ksz)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[12:16]), valuesz)
}
示例#5
0
func TestEncodeDecodeHint(t *testing.T) {
	/**
			tStamp	:	ksz	:	valueSz	:	valuePos	:	key
			4       :   4   :   4       :       8       :   xxxxx
	**/
	// encodeHint
	tStamp := uint32(time.Now().Unix())
	key := []byte("Foo")
	value := []byte("Bar")
	ksz := uint32(len(key))
	valuesz := uint32(len(value))
	valuePos := uint64(8)
	buf := make([]byte, HintHeaderSize+ksz, HintHeaderSize+ksz)
	binary.LittleEndian.PutUint32(buf[0:4], tStamp)
	binary.LittleEndian.PutUint32(buf[4:8], ksz)
	binary.LittleEndian.PutUint32(buf[8:12], valuesz)
	binary.LittleEndian.PutUint64(buf[12:20], valuePos)
	copy(buf[HintHeaderSize:], key)
	// decodeHint
	assert.Equal(t, binary.LittleEndian.Uint32(buf[:4]), tStamp)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[4:8]), ksz)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[8:12]), valuesz)
	assert.Equal(t, binary.LittleEndian.Uint64(buf[12:20]), valuePos)
	fmt.Println(string(buf[HintHeaderSize : HintHeaderSize+ksz]))
	assert.Equal(t, buf[HintHeaderSize:], key)

	ksz = 0
	valuesz = 0
	valuePos = 0
	buf = make([]byte, HintHeaderSize+ksz, HintHeaderSize+ksz)
	binary.LittleEndian.PutUint32(buf[0:4], tStamp)
	binary.LittleEndian.PutUint32(buf[4:8], ksz)
	binary.LittleEndian.PutUint32(buf[8:12], valuesz)
	binary.LittleEndian.PutUint64(buf[12:20], valuePos)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[:4]), tStamp)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[4:8]), ksz)
	assert.Equal(t, binary.LittleEndian.Uint32(buf[8:12]), valuesz)
	assert.Equal(t, binary.LittleEndian.Uint64(buf[12:20]), valuePos)
}