func (tx *Tx) Check() error {
	if !tx.writable {
		return ErrTxNotWritable
	}
	var errors ErrorList
	freed := make(map[pgid]bool)
	for _, id := range tx.db.freelist.all() {
		if freed[id] {
			errors = append(errors, fmt.Errorf("page %d: already freed", id))
		}
		freed[id] = true
	}
	reachable := make(map[pgid]*page)
	reachable[0] = tx.page(0)  
	reachable[1] = tx.page(1)  
	for i := uint32(0); i <= tx.page(tx.meta.freelist).overflow; i++ {
		reachable[tx.meta.freelist+pgid(i)] = tx.page(tx.meta.freelist)
	}
	tx.checkBucket(&tx.root, reachable, &errors)
	for i := pgid(0); i < tx.meta.pgid; i++ {
		_, isReachable := reachable[i]
		if !isReachable && !freed[i] {
			errors = append(errors, fmt.Errorf("page %d: unreachable unfreed", int(i)))
		} else if isReachable && freed[i] {
			errors = append(errors, fmt.Errorf("page %d: reachable freed", int(i)))
		}
	}
	if len(errors) > 0 {
		return errors
	}
	return nil
}
Check performs several consistency checks on the database for this transaction.
An error is returned if any inconsistency is found or if executed on a read-only transaction.
package bolt
import (
	"errors"
	"fmt"
	"sort"
	"time"
	"unsafe"
)
var (
	ErrTxNotWritable = errors.New("tx not writable")
	ErrTxClosed = errors.New("tx closed")
)
type txid uint64
type Tx struct {
	writable       bool
	managed        bool
	db             *DB
	meta           *meta
	root           Bucket
	pages          map[pgid]*page
	stats          TxStats
	commitHandlers []func()
}
func (tx *Tx) init(db *DB) {
	tx.db = db
	tx.pages = nil
	tx.meta = &meta{}
	db.meta().copy(tx.meta)
	tx.root = newBucket(tx)
	tx.root.bucket = &bucket{}
	*tx.root.bucket = tx.meta.root
	if tx.writable {
		tx.pages = make(map[pgid]*page)
		tx.meta.txid += txid(1)
	}
}
func (tx *Tx) id() txid {
	return tx.meta.txid
}
func (tx *Tx) DB() *DB {
	return tx.db
}
func (tx *Tx) Writable() bool {
	return tx.writable
}
func (tx *Tx) Cursor() *Cursor {
	return tx.root.Cursor()
}
func (tx *Tx) Stats() TxStats {
	return tx.stats
}
func (tx *Tx) Bucket(name []byte) *Bucket {
	return tx.root.Bucket(name)
}
func (tx *Tx) CreateBucket(name []byte) (*Bucket, error) {
	return tx.root.CreateBucket(name)
}
func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {
	return tx.root.CreateBucketIfNotExists(name)
}
func (tx *Tx) DeleteBucket(name []byte) error {
	return tx.root.DeleteBucket(name)
}
func (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error {
	return tx.root.ForEach(func(k, v []byte) error {
		if err := fn(k, tx.root.Bucket(k)); err != nil {
			return err
		}
		return nil
	})
}
func (tx *Tx) OnCommit(fn func()) {
	tx.commitHandlers = append(tx.commitHandlers, fn)
}
func (tx *Tx) Commit() error {
	_assert(!tx.managed, "managed tx commit not allowed")
	if tx.db == nil {
		return ErrTxClosed
	} else if !tx.writable {
		return ErrTxNotWritable
	}
	var startTime = time.Now()
	tx.root.rebalance()
	tx.stats.RebalanceTime += time.Since(startTime)
	startTime = time.Now()
	if err := tx.root.spill(); err != nil {
		tx.close()
		return err
	}
	tx.stats.SpillTime += time.Since(startTime)
	tx.meta.root.root = tx.root.root
	tx.db.freelist.free(tx.id(), tx.db.page(tx.meta.freelist))
	p, err := tx.allocate((tx.db.freelist.size() / tx.db.pageSize) + 1)
	if err != nil {
		tx.close()
		return err
	}
	tx.db.freelist.write(p)
	tx.meta.freelist = p.id
	startTime = time.Now()
	if err := tx.write(); err != nil {
		tx.close()
		return err
	}
	if tx.db.StrictMode {
		if err := tx.Check(); err != nil {
			err := err.(ErrorList)
			panic("check fail: " + err.Error() + ": " + err.join("; "))
		}
	}
	if err := tx.writeMeta(); err != nil {
		tx.close()
		return err
	}
	tx.stats.WriteTime += time.Since(startTime)
	tx.close()
	for _, fn := range tx.commitHandlers {
		fn()
	}
	return nil
}
func (tx *Tx) Rollback() error {
	_assert(!tx.managed, "managed tx rollback not allowed")
	if tx.db == nil {
		return ErrTxClosed
	}
	tx.close()
	return nil
}
func (tx *Tx) close() {
	if tx.writable {
		tx.db.rwlock.Unlock()
		tx.db.statlock.Lock()
		tx.db.stats.TxStats.add(&tx.stats)
		tx.db.statlock.Unlock()
	} else {
		tx.db.removeTx(tx)
	}
	tx.db = nil
}