func TestMapPrefix(t *testing.T) {
	bx := NewTestDB()
	defer bx.Close()

	// Create a new things bucket.
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	// Setup items to insert.
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("1")},   // `A` prefix match
		{[]byte("AA"), []byte("2")},  // match
		{[]byte("AAA"), []byte("3")}, // match
		{[]byte("AAB"), []byte("2")}, // match
		{[]byte("B"), []byte("O")},
		{[]byte("BA"), []byte("0")},
		{[]byte("BAA"), []byte("0")},
	}

	// Insert 'em.
	if err := things.Insert(items); err != nil {
		t.Error(err.Error())
	}

	// Now collect each item whose key starts with "A".
	prefix := []byte("A")

	// Expected items for keys with prefix "A".
	expected := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("1")},
		{[]byte("AA"), []byte("2")},
		{[]byte("AAA"), []byte("3")},
		{[]byte("AAB"), []byte("2")},
	}

	// Setup slice of items to collect results.
	type item struct {
		Key, Value []byte
	}
	results := []item{}

	// Anon func to map over matched keys.
	do := func(k, v []byte) error {
		results = append(results, item{k, v})
		return nil
	}

	if err := things.MapPrefix(do, prefix); err != nil {
		t.Error(err.Error())
	}

	for i, want := range expected {
		got := results[i]
		if !bytes.Equal(got.Key, want.Key) {
			t.Errorf("got %v, want %v", got.Key, want.Key)
		}
		if !bytes.Equal(got.Value, want.Value) {
			t.Errorf("got %v, want %v", got.Value, want.Value)
		}
	}
}
// Ensure that we can apply a function to the k/v pairs
// of keys with a given prefix.
Ensure that we can apply a function to the k/v pairs
of keys with a given prefix.
// Ensure that we can apply a function to the k / v pairs // of keys with a given prefix .
func TestMapPrefix(t *testing.T) {
	bx := NewTestDB()
	defer bx.Close()
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("1")},   
		{[]byte("AA"), []byte("2")},  
		{[]byte("AAA"), []byte("3")}, 
		{[]byte("AAB"), []byte("2")}, 
		{[]byte("B"), []byte("O")},
		{[]byte("BA"), []byte("0")},
		{[]byte("BAA"), []byte("0")},
	}
	if err := things.Insert(items); err != nil {
		t.Error(err.Error())
	}
	prefix := []byte("A")
	expected := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("1")},
		{[]byte("AA"), []byte("2")},
		{[]byte("AAA"), []byte("3")},
		{[]byte("AAB"), []byte("2")},
	}
	type item struct {
		Key, Value []byte
	}
	results := []item{}
	do := func(k, v []byte) error {
		results = append(results, item{k, v})
		return nil
	}
	if err := things.MapPrefix(do, prefix); err != nil {
		t.Error(err.Error())
	}
	for i, want := range expected {
		got := results[i]
		if !bytes.Equal(got.Key, want.Key) {
			t.Errorf("got %v, want %v", got.Key, want.Key)
		}
		if !bytes.Equal(got.Value, want.Value) {
			t.Errorf("got %v, want %v", got.Value, want.Value)
		}
	}
}
func TestMapPrefix ( t * testing . T ) { bx := NewTestDB ( ) defer bx . Close ( ) things , err := bx . New ( [ ] byte ( " " ) ) if err != nil { t . Error ( err . Error ( ) ) } items := [ ] struct { Key , Value [ ] byte } { { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , } if err := things . Insert ( items ) ; err != nil { t . Error ( err . Error ( ) ) } prefix := [ ] byte ( " " ) expected := [ ] struct { Key , Value [ ] byte } { { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , } type item struct { Key , Value [ ] byte } results := [ ] item { } do := func ( k , v [ ] byte ) error { results = append ( results , item { k , v } ) return nil } if err := things . MapPrefix ( do , prefix ) ; err != nil { t . Error ( err . Error ( ) ) } for i , want := range expected { got := results [ i ] if ! bytes . Equal ( got . Key , want . Key ) { t . Errorf ( " " , got . Key , want . Key ) } if ! bytes . Equal ( got . Value , want . Value ) { t . Errorf ( " " , got . Value , want . Value ) } } }
Ensure that we can apply a function to the k/v pairs
of keys with a given prefix.
Ensure that we can apply a function to the k / v pairs of keys with a given prefix .
// Create a new things bucket. // Setup items to insert. // `A` prefix match // match // match // match // Insert 'em. // Now collect each item whose key starts with "A". // Expected items for keys with prefix "A". // Setup slice of items to collect results. // Anon func to map over matched keys.