Beispiel #1
0
func BenchmarkGet(b *testing.B) {
	db, _ := cdb.Open("./test/test.cdb")
	b.ResetTimer()

	rand.Seed(time.Now().UnixNano())
	for i := 0; i < b.N; i++ {
		record := expectedRecords[rand.Intn(len(expectedRecords))]
		db.Get(record[0])
	}
}
Beispiel #2
0
func BenchmarkIterator(b *testing.B) {
	db, _ := cdb.Open("./test/test.cdb")
	iter := db.Iter()
	b.ResetTimer()

	rand.Seed(time.Now().UnixNano())
	for i := 0; i < b.N; i++ {
		for iter.Next() {
		}
	}
}
Beispiel #3
0
func ExampleCDB() {
	db, err := cdb.Open("./test/test.cdb")
	if err != nil {
		log.Fatal(err)
	}

	// Fetch a value.
	v, err := db.Get([]byte("foo"))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(v))
	// Output: bar
}
Beispiel #4
0
func TestGet(t *testing.T) {
	db, err := cdb.Open("./test/test.cdb")
	require.NoError(t, err)
	require.NotNil(t, db)

	records := append(append(expectedRecords, expectedRecords...), expectedRecords...)
	shuffle(records)

	for _, record := range records {
		msg := "while fetching " + string(record[0])

		value, err := db.Get(record[0])
		require.NoError(t, err, msg)
		assert.Equal(t, string(record[1]), string(value), msg)
	}
}
Beispiel #5
0
func ExampleIterator() {
	db, err := cdb.Open("./test/test.cdb")
	if err != nil {
		log.Fatal(err)
	}

	// Create an iterator for the database.
	iter := db.Iter()
	for iter.Next() {
		// Do something with iter.Key()/iter.Value()
	}

	if err := iter.Err(); err != nil {
		log.Fatal(err)
	}
}
Beispiel #6
0
func TestIterator(t *testing.T) {
	db, err := cdb.Open("./test/test.cdb")
	require.NoError(t, err)
	require.NotNil(t, db)

	n := 0
	iter := db.Iter()
	for iter.Next() {
		assert.Equal(t, string(expectedRecords[n][0]), string(iter.Key()))
		assert.Equal(t, string(expectedRecords[n][1]), string(iter.Value()))
		require.NoError(t, iter.Err())
		n++
	}

	assert.Equal(t, len(expectedRecords)-1, n)

	require.NoError(t, iter.Err())
}