<commit_before>package buckets_test

import (
	"bytes"
	"fmt"
	"testing"
)

// Ensure we can put an item in a bucket.
func TestPut(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	key, value := []byte("A"), []byte("alpha")

	// Put key/value into the `things` bucket.
	if err := things.Put(key, value); err != nil {
		t.Error(err.Error())
	}

	// Read value back in a different read-only transaction.
	got, err := things.Get(key)
	if err != nil && !bytes.Equal(got, value) {
		t.Error(err.Error())
	}
}

// Show we can put an item in a bucket and get it back out.
func ExampleBucket_Put() {
	// Create a new `things` bucket.
	bucket := []byte("things")
	things, _ := bx.New(bucket)

	// Put key/value into the `things` bucket.
	key, value := []byte("A"), []byte("alpha")
	if err := things.Put(key, value); err != nil {
		fmt.Println("could not insert items!")
	}

	// Read value back in a different read-only transaction.
	got, _ := things.Get(key)

	fmt.Printf("The value of %q in `%s` is %q\n", key, bucket, got)

	// Output:
	// The value of "A" in `things` is "alpha"
}

// Ensure that a bucket that gets a non-existent key returns nil.
func TestGetMissing(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	key := []byte("missing")
	if got, _ := things.Get(key); got != nil {
		t.Errorf("not expecting value for key %q: got %q", key, got)
	}
}

// Ensure that we can delete stuff in a bucket.
func TestDelete(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	k, v := []byte("foo"), []byte("bar")
	if err = things.Put(k, v); err != nil {
		t.Error(err.Error())
	}

	if err = things.Delete(k); err != nil {
		t.Error(err.Error())
	}
}

// Ensure we can insert items into a bucket and get them back out.
func TestInsert(t *testing.T) {
	paths, err := bx.New([]byte("paths"))

	// k, v pairs to put in `paths` bucket
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("foo/"), []byte("foo")},
		{[]byte("foo/bar/"), []byte("bar")},
		{[]byte("foo/bar/baz/"), []byte("baz")},
		{[]byte("food/"), []byte("")},
		{[]byte("good/"), []byte("")},
		{[]byte("goo/"), []byte("")},
	}

	err = paths.Insert(items)
	if err != nil {
		t.Error(err.Error())
	}

	gotItems, err := paths.Items()
	if err != nil {
		t.Error(err.Error())
	}

	// expected k/v mapping
	expected := map[string][]byte{
		"foo/":         []byte("foo"),
		"foo/bar/":     []byte("bar"),
		"foo/bar/baz/": []byte("baz"),
		"food/":        []byte(""),
		"good/":        []byte(""),
		"goo/":         []byte(""),
	}

	for _, item := range gotItems {
		want := expected[string(item.Key)]
		if !bytes.Equal(item.Value, want) {
			t.Errorf("got %v, want %v", item.Value, want)
		}
	}
}

// Show we can insert items into a bucket and get them back out.
func ExampleBucket_Insert() {
	paths, _ := bx.New([]byte("paths"))

	// Setup items to insert in `paths` bucket.
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("alpha")},
		{[]byte("B"), []byte("beta")},
		{[]byte("C"), []byte("gamma")},
	}

	// Insert items into `paths` bucket.
	if err := paths.Insert(items); err != nil {
		fmt.Println("could not insert items!")
	}

	// Get items back out in separate read-only transaction.
	results, _ := paths.Items()

	for _, item := range results {
		fmt.Printf("%s -> %s\n", item.Key, item.Value)
	}

	// Output:
	// A -> alpha
	// B -> beta
	// C -> gamma
}

/* Ensure that we can apply functions to each k/v pair.
func TestMap(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	wantKeys := []string{"A", "B", "C"}
	wantValues := []string{"alpha", "beta", "gamma"}

	var keys, values []string
	do := func(k, v []byte) error {
		keys = append(keys, string(k))
		values = append(values, string(v))
		return nil
	}
	if err := things.Map(do); err != nil {
		t.Error(err.Error())
	}

	for i, want := range wantKeys {
		if got := keys[i]; want != got {
			t.Errorf("got %v, want %v", got, want)
		}
	}
	for i, want := range wantValues {
		if got := values[i]; want != got {
			t.Errorf("got %v, want %v", got, want)
		}
	}
}
*/

// Ensure that we can apply a function to the k/v pairs
// of keys with a given prefix.
func TestMapPrefix(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	// k, v pairs to put in `things` bucket
	pairs := []struct {
		k, v string
	}{
		{"A", "1"}, // `A` prefix match
		{"B", "0"},
		{"AA", "2"},  // match
		{"AAA", "3"}, // match
		{"BA", "1"},
		{"BAA", "2"},
		{"AAB", "2"}, // match
	}

	for _, pair := range pairs {
		key, val := []byte(pair.k), []byte(pair.v)
		if err = things.Put(key, val); err != nil {<commit_msg>fix map test<commit_after>package buckets_test

import (
	"bytes"
	"fmt"
	"testing"
)

// Ensure we can put an item in a bucket.
func TestPut(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	key, value := []byte("A"), []byte("alpha")

	// Put key/value into the `things` bucket.
	if err := things.Put(key, value); err != nil {
		t.Error(err.Error())
	}

	// Read value back in a different read-only transaction.
	got, err := things.Get(key)
	if err != nil && !bytes.Equal(got, value) {
		t.Error(err.Error())
	}
}

// Show we can put an item in a bucket and get it back out.
func ExampleBucket_Put() {
	// Create a new `things` bucket.
	bucket := []byte("things")
	things, _ := bx.New(bucket)

	// Put key/value into the `things` bucket.
	key, value := []byte("A"), []byte("alpha")
	if err := things.Put(key, value); err != nil {
		fmt.Println("could not insert items!")
	}

	// Read value back in a different read-only transaction.
	got, _ := things.Get(key)

	fmt.Printf("The value of %q in `%s` is %q\n", key, bucket, got)

	// Output:
	// The value of "A" in `things` is "alpha"
}

// Ensure that a bucket that gets a non-existent key returns nil.
func TestGetMissing(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	key := []byte("missing")
	if got, _ := things.Get(key); got != nil {
		t.Errorf("not expecting value for key %q: got %q", key, got)
	}
}

// Ensure that we can delete stuff in a bucket.
func TestDelete(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	k, v := []byte("foo"), []byte("bar")
	if err = things.Put(k, v); err != nil {
		t.Error(err.Error())
	}

	if err = things.Delete(k); err != nil {
		t.Error(err.Error())
	}
}

// Ensure we can insert items into a bucket and get them back out.
func TestInsert(t *testing.T) {
	paths, err := bx.New([]byte("paths"))

	// k, v pairs to put in `paths` bucket
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("foo/"), []byte("foo")},
		{[]byte("foo/bar/"), []byte("bar")},
		{[]byte("foo/bar/baz/"), []byte("baz")},
		{[]byte("food/"), []byte("")},
		{[]byte("good/"), []byte("")},
		{[]byte("goo/"), []byte("")},
	}

	err = paths.Insert(items)
	if err != nil {
		t.Error(err.Error())
	}

	gotItems, err := paths.Items()
	if err != nil {
		t.Error(err.Error())
	}

	// expected k/v mapping
	expected := map[string][]byte{
		"foo/":         []byte("foo"),
		"foo/bar/":     []byte("bar"),
		"foo/bar/baz/": []byte("baz"),
		"food/":        []byte(""),
		"good/":        []byte(""),
		"goo/":         []byte(""),
	}

	for _, item := range gotItems {
		want := expected[string(item.Key)]
		if !bytes.Equal(item.Value, want) {
			t.Errorf("got %v, want %v", item.Value, want)
		}
	}
}

// Show we can insert items into a bucket and get them back out.
func ExampleBucket_Insert() {
	letters, _ := bx.New([]byte("letters"))

	// Setup items to insert in `paths` bucket.
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("alpha")},
		{[]byte("B"), []byte("beta")},
		{[]byte("C"), []byte("gamma")},
	}

	// Insert items into `letters` bucket.
	if err := letters.Insert(items); err != nil {
		fmt.Println("could not insert items!")
	}

	// Get items back out in separate read-only transaction.
	results, _ := letters.Items()

	for _, item := range results {
		fmt.Printf("%s -> %s\n", item.Key, item.Value)
	}

	// Output:
	// A -> alpha
	// B -> beta
	// C -> gamma
}

// Ensure that we can apply functions to each k/v pair.
func TestMap(t *testing.T) {
	// Delete any existing bucket named "letters".
	bx.Delete([]byte("letters"))

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

	// Setup items to insert.
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("alpha")},
		{[]byte("B"), []byte("beta")},
		{[]byte("C"), []byte("gamma")},
	}

	// Insert items into `letters` bucket.
	if err := letters.Insert(items); err != nil {
		fmt.Println("could not insert items!")
	}

	wantKeys := []string{"A", "B", "C"}
	wantValues := []string{"alpha", "beta", "gamma"}

	var keys, values []string
	do := func(k, v []byte) error {
		keys = append(keys, string(k))
		values = append(values, string(v))
		return nil
	}
	if err := letters.Map(do); err != nil {
		t.Error(err.Error())
	}

	for i, want := range wantKeys {
		if got := keys[i]; want != got {
			t.Errorf("got %v, want %v", got, want)
		}
	}
	for i, want := range wantValues {
		if got := values[i]; want != got {
			t.Errorf("got %v, want %v", got, want)
		}
	}
}

// Ensure that we can apply a function to the k/v pairs
// of keys with a given prefix.
func TestMapPrefix(t *testing.T) {
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	// k, v pairs to put in `things` bucket
	pairs := []struct {
		k, v string
	}{
		{"A", "1"}, // `A` prefix match
		{"B", "0"},
		{"AA", "2"},  // match
		{"AAA", "3"}, // match
		{"BA", "1"},
		{"BAA", "2"},
		{"AAB", "2"}, // match
	}

	for _, pair := range pairs {
		key, val := []byte(pair.k), []byte(pair.v)
		if err = things.Put(key, val); err != nil {
{'fraction_non_alphanumeric': 0.223668402082499, 'fraction_numerical': 0.0014016820184221065, 'mean_word_length': 3.131100082712986, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2523187', 'n_tokens_mistral': 3973, 'n_tokens_neox': 3755, 'n_words': 1098}