<commit_before>package lmdb

import (
	mdb "github.com/szferi/gomdb"
)

type ReadTxner interface {
	BucketStat(bucket string) *Stat
	Get(bucket string, key []byte) ([]byte, bool)
	GetNoCopy(bucket string, key []byte) ([]byte, bool)
	Iterate(bucket string) *Iterator
}

type ReadTxn struct {
	// In this package, a DBI is obtained only through Open/Open2, and is never closed until
	// Context.Close(), in which all dbis are closed automatically.
	buckets map[string]mdb.DBI
	txn     *mdb.Txn
	// Cached iterators in the current transaction, will be closed when txn finishes.
	itrs []*Iterator
}

type ReadWriteTxn struct {
	env *mdb.Env
	*ReadTxn
}

//--------------------------------- ReadTxn -------------------------------------------------------

// panic if {bucket} does not exist, internal use
func (txn *ReadTxn) getBucketId(bucket string) mdb.DBI {
	id, b := txn.buckets[bucket]
	if !b {
		panic("bucket does not exist")
	} else {
		return id
	}
}

func (txn *ReadTxn) BucketStat(bucket string) *Stat {
	stat, err := txn.txn.Stat(txn.getBucketId(bucket))
	if err != nil { // Possible errors: EINVAL, MDB_BAD_TXN
		panic(err)
	}
	return (*Stat)(stat)
}

// Return {nil, false} if {key} does not exist, {val, true} if {key} exist<commit_msg>minor update: make an error message clearer<commit_after>package lmdb

import (
	"fmt"
	mdb "github.com/szferi/gomdb"
)

type ReadTxner interface {
	BucketStat(bucket string) *Stat
	Get(bucket string, key []byte) ([]byte, bool)
	GetNoCopy(bucket string, key []byte) ([]byte, bool)
	Iterate(bucket string) *Iterator
}

type ReadTxn struct {
	// In this package, a DBI is obtained only through Open/Open2, and is never closed until
	// Context.Close(), in which all dbis are closed automatically.
	buckets map[string]mdb.DBI
	txn     *mdb.Txn
	// Cached iterators in the current transaction, will be closed when txn finishes.
	itrs []*Iterator
}

type ReadWriteTxn struct {
	env *mdb.Env
	*ReadTxn
}

//--------------------------------- ReadTxn -------------------------------------------------------

// panic if {bucket} does not exist, internal use
func (txn *ReadTxn) getBucketId(bucket string) mdb.DBI {
	id, b := txn.buckets[bucket]
	if !b {
		panic(fmt.Errorf("bucket does not exist: %s", bucket))
	} else {
		return id
	}
}

func (txn *ReadTxn) BucketStat(bucket string) *Stat {
	stat, err := txn.txn.Stat(txn.getBucketId(bucket))
	if err != nil { // Possible errors: EINVAL, MDB_BAD_TXN
		panic(err)
	}
	return (*Stat)(stat)
}

// Return {nil, false} if {key} does not exist, {val, true} if {key} exist
{'fraction_non_alphanumeric': 0.18576456785295267, 'fraction_numerical': 0.0007821666014861165, 'mean_word_length': 2.9353846153846153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3963768', 'n_tokens_mistral': 888, 'n_tokens_neox': 871, 'n_words': 278}