package bbolt

import (
	
	
	
	
	
	
	
	
	
	

	berrors 
	
)

// Tx represents a read-only or read/write transaction on the database.
// Read-only transactions can be used for retrieving values for keys and creating cursors.
// Read/write transactions can create and remove buckets and create and remove keys.
//
// IMPORTANT: You must commit or rollback transactions when you are done with
// them. Pages can not be reclaimed by the writer until no more transactions
// are using them. A long running read transaction can cause the database to
// quickly grow.
type Tx struct {
	writable       bool
	managed        bool
	db             *DB
	meta           *common.Meta
	root           Bucket
	pages          map[common.Pgid]*common.Page
	stats          TxStats
	commitHandlers []func()

	// WriteFlag specifies the flag for write-related methods like WriteTo().
	// Tx opens the database file with the specified flag to copy the data.
	//
	// By default, the flag is unset, which works well for mostly in-memory
	// workloads. For databases that are much larger than available RAM,
	// set the flag to syscall.O_DIRECT to avoid trashing the page cache.
	WriteFlag int
}

// init initializes the transaction.
func ( *Tx) ( *DB) {
	.db = 
	.pages = nil

	// Copy the meta page since it can be changed by the writer.
	.meta = &common.Meta{}
	.meta().Copy(.meta)

	// Copy over the root bucket.
	.root = newBucket()
	.root.InBucket = &common.InBucket{}
	*.root.InBucket = *(.meta.RootBucket())

	// Increment the transaction id and add a page cache for writable transactions.
	if .writable {
		.pages = make(map[common.Pgid]*common.Page)
		.meta.IncTxid()
	}
}

// ID returns the transaction id.
func ( *Tx) () int {
	if  == nil || .meta == nil {
		return -1
	}
	return int(.meta.Txid())
}

// DB returns a reference to the database that created the transaction.
func ( *Tx) () *DB {
	return .db
}

// Size returns current database size in bytes as seen by this transaction.
func ( *Tx) () int64 {
	return int64(.meta.Pgid()) * int64(.db.pageSize)
}

// Writable returns whether the transaction can perform write operations.
func ( *Tx) () bool {
	return .writable
}

// Cursor creates a cursor associated with the root bucket.
// All items in the cursor will return a nil value because all root bucket keys point to buckets.
// The cursor is only valid as long as the transaction is open.
// Do not use a cursor after the transaction is closed.
func ( *Tx) () *Cursor {
	return .root.Cursor()
}

// Stats retrieves a copy of the current transaction statistics.
func ( *Tx) () TxStats {
	return .stats
}

// Inspect returns the structure of the database.
func ( *Tx) () BucketStructure {
	return .root.Inspect()
}

// Bucket retrieves a bucket by name.
// Returns nil if the bucket does not exist.
// The bucket instance is only valid for the lifetime of the transaction.
func ( *Tx) ( []byte) *Bucket {
	return .root.Bucket()
}

// CreateBucket creates a new bucket.
// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func ( *Tx) ( []byte) (*Bucket, error) {
	return .root.CreateBucket()
}

// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.
// Returns an error if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func ( *Tx) ( []byte) (*Bucket, error) {
	return .root.CreateBucketIfNotExists()
}

// DeleteBucket deletes a bucket.
// Returns an error if the bucket cannot be found or if the key represents a non-bucket value.
func ( *Tx) ( []byte) error {
	return .root.DeleteBucket()
}

// MoveBucket moves a sub-bucket from the source bucket to the destination bucket.
// Returns an error if
//  1. the sub-bucket cannot be found in the source bucket;
//  2. or the key already exists in the destination bucket;
//  3. the key represents a non-bucket value.
//
// If src is nil, it means moving a top level bucket into the target bucket.
// If dst is nil, it means converting the child bucket into a top level bucket.
func ( *Tx) ( []byte,  *Bucket,  *Bucket) error {
	if  == nil {
		 = &.root
	}
	if  == nil {
		 = &.root
	}
	return .MoveBucket(, )
}

// ForEach executes a function for each bucket in the root.
// If the provided function returns an error then the iteration is stopped and
// the error is returned to the caller.
func ( *Tx) ( func( []byte,  *Bucket) error) error {
	return .root.ForEach(func(,  []byte) error {
		return (, .root.Bucket())
	})
}

// OnCommit adds a handler function to be executed after the transaction successfully commits.
func ( *Tx) ( func()) {
	.commitHandlers = append(.commitHandlers, )
}

// Commit writes all changes to disk, updates the meta page and closes the transaction.
// Returns an error if a disk write error occurs, or if Commit is
// called on a read-only transaction.
func ( *Tx) () ( error) {
	 := .ID()
	 := .db.Logger()
	if  != discardLogger {
		.Debugf("Committing transaction %d", )
		defer func() {
			if  != nil {
				.Errorf("Committing transaction failed: %v", )
			} else {
				.Debugf("Committing transaction %d successfully", )
			}
		}()
	}

	common.Assert(!.managed, "managed tx commit not allowed")
	if .db == nil {
		return berrors.ErrTxClosed
	} else if !.writable {
		return berrors.ErrTxNotWritable
	}

	// TODO(benbjohnson): Use vectorized I/O to write out dirty pages.

	// Rebalance nodes which have had deletions.
	var  = time.Now()
	.root.rebalance()
	if .stats.GetRebalance() > 0 {
		.stats.IncRebalanceTime(time.Since())
	}

	 := .meta.Pgid()

	// spill data onto dirty pages.
	 = time.Now()
	if  = .root.spill();  != nil {
		.Errorf("spilling data onto dirty pages failed: %v", )
		.rollback()
		return 
	}
	.stats.IncSpillTime(time.Since())

	// Free the old root bucket.
	.meta.RootBucket().SetRootPage(.root.RootPage())

	// Free the old freelist because commit writes out a fresh freelist.
	if .meta.Freelist() != common.PgidNoFreelist {
		.db.freelist.Free(.meta.Txid(), .db.page(.meta.Freelist()))
	}

	if !.db.NoFreelistSync {
		 = .commitFreelist()
		if  != nil {
			.Errorf("committing freelist failed: %v", )
			return 
		}
	} else {
		.meta.SetFreelist(common.PgidNoFreelist)
	}

	// If the high water mark has moved up then attempt to grow the database.
	if .meta.Pgid() >  {
		_ = errors.New("")
		// gofail: var lackOfDiskSpace string
		// tx.rollback()
		// return errors.New(lackOfDiskSpace)
		if  = .db.grow(int(.meta.Pgid()+1) * .db.pageSize);  != nil {
			.Errorf("growing db size failed, pgid: %d, pagesize: %d, error: %v", .meta.Pgid(), .db.pageSize, )
			.rollback()
			return 
		}
	}

	// Write dirty pages to disk.
	 = time.Now()
	if  = .write();  != nil {
		.Errorf("writing data failed: %v", )
		.rollback()
		return 
	}

	// If strict mode is enabled then perform a consistency check.
	if .db.StrictMode {
		 := .Check()
		var  []string
		for {
			,  := <-
			if ! {
				break
			}
			 = append(, .Error())
		}
		if len() > 0 {
			panic("check fail: " + strings.Join(, "\n"))
		}
	}

	// Write meta to disk.
	if  = .writeMeta();  != nil {
		.Errorf("writeMeta failed: %v", )
		.rollback()
		return 
	}
	.stats.IncWriteTime(time.Since())

	// Finalize the transaction.
	.close()

	// Execute commit handlers now that the locks have been removed.
	for ,  := range .commitHandlers {
		()
	}

	return nil
}

func ( *Tx) () error {
	// Allocate new pages for the new free list. This will overestimate
	// the size of the freelist but not underestimate the size (which would be bad).
	,  := .allocate((.db.freelist.EstimatedWritePageSize() / .db.pageSize) + 1)
	if  != nil {
		.rollback()
		return 
	}

	.db.freelist.Write()
	.meta.SetFreelist(.Id())

	return nil
}

// Rollback closes the transaction and ignores all previous updates. Read-only
// transactions must be rolled back and not committed.
func ( *Tx) () error {
	common.Assert(!.managed, "managed tx rollback not allowed")
	if .db == nil {
		return berrors.ErrTxClosed
	}
	.nonPhysicalRollback()
	return nil
}

// nonPhysicalRollback is called when user calls Rollback directly, in this case we do not need to reload the free pages from disk.
func ( *Tx) () {
	if .db == nil {
		return
	}
	if .writable {
		.db.freelist.Rollback(.meta.Txid())
	}
	.close()
}

// rollback needs to reload the free pages from disk in case some system error happens like fsync error.
func ( *Tx) () {
	if .db == nil {
		return
	}
	if .writable {
		.db.freelist.Rollback(.meta.Txid())
		// When mmap fails, the `data`, `dataref` and `datasz` may be reset to
		// zero values, and there is no way to reload free page IDs in this case.
		if .db.data != nil {
			if !.db.hasSyncedFreelist() {
				// Reconstruct free page list by scanning the DB to get the whole free page list.
				// Note: scanning the whole db is heavy if your db size is large in NoSyncFreeList mode.
				.db.freelist.NoSyncReload(.db.freepages())
			} else {
				// Read free page list from freelist page.
				.db.freelist.Reload(.db.page(.db.meta().Freelist()))
			}
		}
	}
	.close()
}

func ( *Tx) () {
	if .db == nil {
		return
	}
	if .writable {
		// Grab freelist stats.
		var  = .db.freelist.FreeCount()
		var  = .db.freelist.PendingCount()
		var  = .db.freelist.EstimatedWritePageSize()

		// Remove transaction ref & writer lock.
		.db.rwtx = nil
		.db.rwlock.Unlock()

		// Merge statistics.
		if .db.stats != nil {
			.db.statlock.Lock()
			.db.stats.FreePageN = 
			.db.stats.PendingPageN = 
			.db.stats.FreeAlloc = ( + ) * .db.pageSize
			.db.stats.FreelistInuse = 
			.db.stats.TxStats.add(&.stats)
			.db.statlock.Unlock()
		}
	} else {
		.db.removeTx()
	}

	// Clear all references.
	.db = nil
	.meta = nil
	.root = Bucket{tx: }
	.pages = nil
}

// Copy writes the entire database to a writer.
// This function exists for backwards compatibility.
//
// Deprecated: Use WriteTo() instead.
func ( *Tx) ( io.Writer) error {
	,  := .WriteTo()
	return 
}

// WriteTo writes the entire database to a writer.
// If err == nil then exactly tx.Size() bytes will be written into the writer.
func ( *Tx) ( io.Writer) ( int64,  error) {
	var  *os.File
	// There is a risk that between the time a read-only transaction
	// is created and the time the file is actually opened, the
	// underlying db file at tx.db.path may have been replaced
	// (e.g. via rename). In that case, opening the file again would
	// unexpectedly point to a different file, rather than the one
	// the transaction was based on.
	//
	// To overcome this, we reuse the already opened file handle when
	// WriteFlag not set. When the WriteFlag is set, we reopen the file
	// but verify that it still refers to the same underlying file
	// (by device and inode). If it does not, we fall back to
	// reusing the existing already opened file handle.
	if .WriteFlag != 0 {
		// Attempt to open reader with WriteFlag
		,  = .db.openFile(.db.path, os.O_RDONLY|.WriteFlag, 0)
		if  != nil {
			return 0, 
		}

		if ,  := sameFile(.db.file, ); ! {
			 := .db.Logger()
			if  := .Close();  != nil {
				.Errorf("failed to close the file (%s): %v", .db.path, )
			}
			.Warningf("The underlying file has changed, so reuse the already opened file (%s): %v", .db.path, )
			 = .db.file
		} else {
			defer func() {
				if  := .Close();  == nil {
					 = 
				}
			}()
		}
	} else {
		 = .db.file
	}

	// Generate a meta page. We use the same page data for both meta pages.
	 := make([]byte, .db.pageSize)
	 := (*common.Page)(unsafe.Pointer(&[0]))
	.SetFlags(common.MetaPageFlag)
	*.Meta() = *.meta

	// Write meta 0.
	.SetId(0)
	.Meta().SetChecksum(.Meta().Sum64())
	,  := .Write()
	 += int64()
	if  != nil {
		return , fmt.Errorf("meta 0 copy: %s", )
	}

	// Write meta 1 with a lower transaction id.
	.SetId(1)
	.Meta().DecTxid()
	.Meta().SetChecksum(.Meta().Sum64())
	,  = .Write()
	 += int64()
	if  != nil {
		return , fmt.Errorf("meta 1 copy: %s", )
	}

	// Copy data pages using a SectionReader to avoid affecting f's offset.
	 := int64(.db.pageSize * 2)
	 := .Size() - 
	 := io.NewSectionReader(, , )

	// Copy data pages.
	,  := io.CopyN(, , )
	 += 
	if  != nil {
		return , 
	}

	return , nil
}

func sameFile(,  *os.File) (bool, error) {
	,  := .Stat()
	if  != nil {
		return false, fmt.Errorf("failed to get fileInfo of the first file (%s): %w", .Name(), )
	}
	,  := .Stat()
	if  != nil {
		return false, fmt.Errorf("failed to get fileInfo of the second file (%s): %w", .Name(), )
	}

	return os.SameFile(, ), nil
}

// CopyFile copies the entire database to file at the given path.
// A reader transaction is maintained during the copy so it is safe to continue
// using the database while a copy is in progress.
func ( *Tx) ( string,  os.FileMode) error {
	,  := .db.openFile(, os.O_RDWR|os.O_CREATE|os.O_TRUNC, )
	if  != nil {
		return 
	}

	_,  = .WriteTo()
	if  != nil {
		_ = .Close()
		return 
	}
	return .Close()
}

// allocate returns a contiguous block of memory starting at a given page.
func ( *Tx) ( int) (*common.Page, error) {
	 := .db.Logger()
	,  := .db.allocate(.meta.Txid(), )
	if  != nil {
		.Errorf("allocating failed, txid: %d, count: %d, error: %v", .meta.Txid(), , )
		return nil, 
	}

	// Save to our page cache.
	.pages[.Id()] = 

	// Update statistics.
	.stats.IncPageCount(int64())
	.stats.IncPageAlloc(int64( * .db.pageSize))

	return , nil
}

// write writes any dirty pages to disk.
func ( *Tx) () error {
	// Sort pages by id.
	 := .db.Logger()
	 := make(common.Pages, 0, len(.pages))
	for ,  := range .pages {
		 = append(, )
	}
	// Clear out page cache early.
	.pages = make(map[common.Pgid]*common.Page)
	sort.Sort()

	// Write pages to disk in order.
	for ,  := range  {
		 := (uint64(.Overflow()) + 1) * uint64(.db.pageSize)
		 := int64(.Id()) * int64(.db.pageSize)
		var  uintptr

		// Write out page in "max allocation" sized chunks.
		for {
			 := 
			if  > common.MaxAllocSize-1 {
				 = common.MaxAllocSize - 1
			}
			 := common.UnsafeByteSlice(unsafe.Pointer(), , 0, int())

			if ,  := .db.ops.writeAt(, );  != nil {
				.Errorf("writeAt failed, offset: %d: %w", , )
				return 
			}

			// Update statistics.
			.stats.IncWrite(1)

			// Exit inner for loop if we've written all the chunks.
			 -= 
			if  == 0 {
				break
			}

			// Otherwise move offset forward and move pointer to next chunk.
			 += int64()
			 += uintptr()
		}
	}

	// Ignore file sync if flag is set on DB.
	if !.db.NoSync || common.IgnoreNoSync {
		// gofail: var beforeSyncDataPages struct{}
		if  := fdatasync(.db);  != nil {
			.Errorf("[GOOS: %s, GOARCH: %s] fdatasync failed: %w", runtime.GOOS, runtime.GOARCH, )
			return 
		}
	}

	// Put small pages back to page pool.
	for ,  := range  {
		// Ignore page sizes over 1 page.
		// These are allocated using make() instead of the page pool.
		if int(.Overflow()) != 0 {
			continue
		}

		 := common.UnsafeByteSlice(unsafe.Pointer(), 0, 0, .db.pageSize)

		// See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1
		for  := range  {
			[] = 0
		}
		.db.pagePool.Put() //nolint:staticcheck
	}

	return nil
}

// writeMeta writes the meta to the disk.
func ( *Tx) () error {
	// gofail: var beforeWriteMetaError string
	// return errors.New(beforeWriteMetaError)

	// Create a temporary buffer for the meta page.
	 := .db.Logger()
	 := make([]byte, .db.pageSize)
	 := .db.pageInBuffer(, 0)
	.meta.Write()

	// Write the meta page to file.
	.db.metalock.Lock()
	if ,  := .db.ops.writeAt(, int64(.Id())*int64(.db.pageSize));  != nil {
		.db.metalock.Unlock()
		.Errorf("writeAt failed, pgid: %d, pageSize: %d, error: %v", .Id(), .db.pageSize, )
		return 
	}
	.db.metalock.Unlock()
	if !.db.NoSync || common.IgnoreNoSync {
		// gofail: var beforeSyncMetaPage struct{}
		if  := fdatasync(.db);  != nil {
			.Errorf("[GOOS: %s, GOARCH: %s] fdatasync failed: %w", runtime.GOOS, runtime.GOARCH, )
			return 
		}
	}

	// Update statistics.
	.stats.IncWrite(1)

	return nil
}

// page returns a reference to the page with a given id.
// If page has been written to then a temporary buffered page is returned.
func ( *Tx) ( common.Pgid) *common.Page {
	// Check the dirty pages first.
	if .pages != nil {
		if ,  := .pages[];  {
			.FastCheck()
			return 
		}
	}

	// Otherwise return directly from the mmap.
	 := .db.page()
	.FastCheck()
	return 
}

// forEachPage iterates over every page within a given page and executes a function.
func ( *Tx) ( common.Pgid,  func(*common.Page, int, []common.Pgid)) {
	 := make([]common.Pgid, 10)
	[0] = 
	.forEachPageInternal([:1], )
}

func ( *Tx) ( []common.Pgid,  func(*common.Page, int, []common.Pgid)) {
	 := .page([len()-1])

	// Execute function.
	(, len()-1, )

	// Recursively loop over children.
	if .IsBranchPage() {
		for  := 0;  < int(.Count()); ++ {
			 := .BranchPageElement(uint16())
			.(append(, .Pgid()), )
		}
	}
}

// Page returns page information for a given page number.
// This is only safe for concurrent use when used by a writable transaction.
func ( *Tx) ( int) (*common.PageInfo, error) {
	if .db == nil {
		return nil, berrors.ErrTxClosed
	} else if common.Pgid() >= .meta.Pgid() {
		return nil, nil
	}

	if .db.freelist == nil {
		return nil, berrors.ErrFreePagesNotLoaded
	}

	// Build the page info.
	 := .db.page(common.Pgid())
	 := &common.PageInfo{
		ID:            ,
		Count:         int(.Count()),
		OverflowCount: int(.Overflow()),
	}

	// Determine the type (or if it's free).
	if .db.freelist.Freed(common.Pgid()) {
		.Type = "free"
	} else {
		.Type = .Typ()
	}

	return , nil
}

// TxStats represents statistics about the actions performed by the transaction.
type TxStats struct {
	// Page statistics.
	//
	// DEPRECATED: Use GetPageCount() or IncPageCount()
	PageCount int64 // number of page allocations
	// DEPRECATED: Use GetPageAlloc() or IncPageAlloc()
	PageAlloc int64 // total bytes allocated

	// Cursor statistics.
	//
	// DEPRECATED: Use GetCursorCount() or IncCursorCount()
	CursorCount int64 // number of cursors created

	// Node statistics
	//
	// DEPRECATED: Use GetNodeCount() or IncNodeCount()
	NodeCount int64 // number of node allocations
	// DEPRECATED: Use GetNodeDeref() or IncNodeDeref()
	NodeDeref int64 // number of node dereferences

	// Rebalance statistics.
	//
	// DEPRECATED: Use GetRebalance() or IncRebalance()
	Rebalance int64 // number of node rebalances
	// DEPRECATED: Use GetRebalanceTime() or IncRebalanceTime()
	RebalanceTime time.Duration // total time spent rebalancing

	// Split/Spill statistics.
	//
	// DEPRECATED: Use GetSplit() or IncSplit()
	Split int64 // number of nodes split
	// DEPRECATED: Use GetSpill() or IncSpill()
	Spill int64 // number of nodes spilled
	// DEPRECATED: Use GetSpillTime() or IncSpillTime()
	SpillTime time.Duration // total time spent spilling

	// Write statistics.
	//
	// DEPRECATED: Use GetWrite() or IncWrite()
	Write int64 // number of writes performed
	// DEPRECATED: Use GetWriteTime() or IncWriteTime()
	WriteTime time.Duration // total time spent writing to disk
}

func ( *TxStats) ( *TxStats) {
	.IncPageCount(.GetPageCount())
	.IncPageAlloc(.GetPageAlloc())
	.IncCursorCount(.GetCursorCount())
	.IncNodeCount(.GetNodeCount())
	.IncNodeDeref(.GetNodeDeref())
	.IncRebalance(.GetRebalance())
	.IncRebalanceTime(.GetRebalanceTime())
	.IncSplit(.GetSplit())
	.IncSpill(.GetSpill())
	.IncSpillTime(.GetSpillTime())
	.IncWrite(.GetWrite())
	.IncWriteTime(.GetWriteTime())
}

// Sub calculates and returns the difference between two sets of transaction stats.
// This is useful when obtaining stats at two different points and time and
// you need the performance counters that occurred within that time span.
func ( *TxStats) ( *TxStats) TxStats {
	var  TxStats
	.PageCount = .GetPageCount() - .GetPageCount()
	.PageAlloc = .GetPageAlloc() - .GetPageAlloc()
	.CursorCount = .GetCursorCount() - .GetCursorCount()
	.NodeCount = .GetNodeCount() - .GetNodeCount()
	.NodeDeref = .GetNodeDeref() - .GetNodeDeref()
	.Rebalance = .GetRebalance() - .GetRebalance()
	.RebalanceTime = .GetRebalanceTime() - .GetRebalanceTime()
	.Split = .GetSplit() - .GetSplit()
	.Spill = .GetSpill() - .GetSpill()
	.SpillTime = .GetSpillTime() - .GetSpillTime()
	.Write = .GetWrite() - .GetWrite()
	.WriteTime = .GetWriteTime() - .GetWriteTime()
	return 
}

// GetPageCount returns PageCount atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.PageCount)
}

// IncPageCount increases PageCount atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.PageCount, )
}

// GetPageAlloc returns PageAlloc atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.PageAlloc)
}

// IncPageAlloc increases PageAlloc atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.PageAlloc, )
}

// GetCursorCount returns CursorCount atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.CursorCount)
}

// IncCursorCount increases CursorCount atomically and return the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.CursorCount, )
}

// GetNodeCount returns NodeCount atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.NodeCount)
}

// IncNodeCount increases NodeCount atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.NodeCount, )
}

// GetNodeDeref returns NodeDeref atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.NodeDeref)
}

// IncNodeDeref increases NodeDeref atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.NodeDeref, )
}

// GetRebalance returns Rebalance atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.Rebalance)
}

// IncRebalance increases Rebalance atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.Rebalance, )
}

// GetRebalanceTime returns RebalanceTime atomically.
func ( *TxStats) () time.Duration {
	return atomicLoadDuration(&.RebalanceTime)
}

// IncRebalanceTime increases RebalanceTime atomically and returns the new value.
func ( *TxStats) ( time.Duration) time.Duration {
	return atomicAddDuration(&.RebalanceTime, )
}

// GetSplit returns Split atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.Split)
}

// IncSplit increases Split atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.Split, )
}

// GetSpill returns Spill atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.Spill)
}

// IncSpill increases Spill atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.Spill, )
}

// GetSpillTime returns SpillTime atomically.
func ( *TxStats) () time.Duration {
	return atomicLoadDuration(&.SpillTime)
}

// IncSpillTime increases SpillTime atomically and returns the new value.
func ( *TxStats) ( time.Duration) time.Duration {
	return atomicAddDuration(&.SpillTime, )
}

// GetWrite returns Write atomically.
func ( *TxStats) () int64 {
	return atomic.LoadInt64(&.Write)
}

// IncWrite increases Write atomically and returns the new value.
func ( *TxStats) ( int64) int64 {
	return atomic.AddInt64(&.Write, )
}

// GetWriteTime returns WriteTime atomically.
func ( *TxStats) () time.Duration {
	return atomicLoadDuration(&.WriteTime)
}

// IncWriteTime increases WriteTime atomically and returns the new value.
func ( *TxStats) ( time.Duration) time.Duration {
	return atomicAddDuration(&.WriteTime, )
}

func atomicAddDuration( *time.Duration,  time.Duration) time.Duration {
	return time.Duration(atomic.AddInt64((*int64)(unsafe.Pointer()), int64()))
}

func atomicLoadDuration( *time.Duration) time.Duration {
	return time.Duration(atomic.LoadInt64((*int64)(unsafe.Pointer())))
}