1package bolt 2 3import ( 4 "fmt" 5 "io" 6 "os" 7 "sort" 8 "strings" 9 "time" 10 "unsafe" 11) 12 13// txid represents the internal transaction identifier. 14type txid uint64 15 16// Tx represents a read-only or read/write transaction on the database. 17// Read-only transactions can be used for retrieving values for keys and creating cursors. 18// Read/write transactions can create and remove buckets and create and remove keys. 19// 20// IMPORTANT: You must commit or rollback transactions when you are done with 21// them. Pages can not be reclaimed by the writer until no more transactions 22// are using them. A long running read transaction can cause the database to 23// quickly grow. 24type Tx struct { 25 writable bool 26 managed bool 27 db *DB 28 meta *meta 29 root Bucket 30 pages map[pgid]*page 31 stats TxStats 32 commitHandlers []func() 33 34 // WriteFlag specifies the flag for write-related methods like WriteTo(). 35 // Tx opens the database file with the specified flag to copy the data. 36 // 37 // By default, the flag is unset, which works well for mostly in-memory 38 // workloads. For databases that are much larger than available RAM, 39 // set the flag to syscall.O_DIRECT to avoid trashing the page cache. 40 WriteFlag int 41} 42 43// init initializes the transaction. 44func (tx *Tx) init(db *DB) { 45 tx.db = db 46 tx.pages = nil 47 48 // Copy the meta page since it can be changed by the writer. 49 tx.meta = &meta{} 50 db.meta().copy(tx.meta) 51 52 // Copy over the root bucket. 53 tx.root = newBucket(tx) 54 tx.root.bucket = &bucket{} 55 *tx.root.bucket = tx.meta.root 56 57 // Increment the transaction id and add a page cache for writable transactions. 58 if tx.writable { 59 tx.pages = make(map[pgid]*page) 60 tx.meta.txid += txid(1) 61 } 62} 63 64// ID returns the transaction id. 65func (tx *Tx) ID() int { 66 return int(tx.meta.txid) 67} 68 69// DB returns a reference to the database that created the transaction. 70func (tx *Tx) DB() *DB { 71 return tx.db 72} 73 74// Size returns current database size in bytes as seen by this transaction. 75func (tx *Tx) Size() int64 { 76 return int64(tx.meta.pgid) * int64(tx.db.pageSize) 77} 78 79// Writable returns whether the transaction can perform write operations. 80func (tx *Tx) Writable() bool { 81 return tx.writable 82} 83 84// Cursor creates a cursor associated with the root bucket. 85// All items in the cursor will return a nil value because all root bucket keys point to buckets. 86// The cursor is only valid as long as the transaction is open. 87// Do not use a cursor after the transaction is closed. 88func (tx *Tx) Cursor() *Cursor { 89 return tx.root.Cursor() 90} 91 92// Stats retrieves a copy of the current transaction statistics. 93func (tx *Tx) Stats() TxStats { 94 return tx.stats 95} 96 97// Bucket retrieves a bucket by name. 98// Returns nil if the bucket does not exist. 99// The bucket instance is only valid for the lifetime of the transaction. 100func (tx *Tx) Bucket(name []byte) *Bucket { 101 return tx.root.Bucket(name) 102} 103 104// CreateBucket creates a new bucket. 105// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long. 106// The bucket instance is only valid for the lifetime of the transaction. 107func (tx *Tx) CreateBucket(name []byte) (*Bucket, error) { 108 return tx.root.CreateBucket(name) 109} 110 111// CreateBucketIfNotExists creates a new bucket if it doesn't already exist. 112// Returns an error if the bucket name is blank, or if the bucket name is too long. 113// The bucket instance is only valid for the lifetime of the transaction. 114func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) { 115 return tx.root.CreateBucketIfNotExists(name) 116} 117 118// DeleteBucket deletes a bucket. 119// Returns an error if the bucket cannot be found or if the key represents a non-bucket value. 120func (tx *Tx) DeleteBucket(name []byte) error { 121 return tx.root.DeleteBucket(name) 122} 123 124// ForEach executes a function for each bucket in the root. 125// If the provided function returns an error then the iteration is stopped and 126// the error is returned to the caller. 127func (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error { 128 return tx.root.ForEach(func(k, v []byte) error { 129 if err := fn(k, tx.root.Bucket(k)); err != nil { 130 return err 131 } 132 return nil 133 }) 134} 135 136// OnCommit adds a handler function to be executed after the transaction successfully commits. 137func (tx *Tx) OnCommit(fn func()) { 138 tx.commitHandlers = append(tx.commitHandlers, fn) 139} 140 141// Commit writes all changes to disk and updates the meta page. 142// Returns an error if a disk write error occurs, or if Commit is 143// called on a read-only transaction. 144func (tx *Tx) Commit() error { 145 _assert(!tx.managed, "managed tx commit not allowed") 146 if tx.db == nil { 147 return ErrTxClosed 148 } else if !tx.writable { 149 return ErrTxNotWritable 150 } 151 152 // TODO(benbjohnson): Use vectorized I/O to write out dirty pages. 153 154 // Rebalance nodes which have had deletions. 155 var startTime = time.Now() 156 tx.root.rebalance() 157 if tx.stats.Rebalance > 0 { 158 tx.stats.RebalanceTime += time.Since(startTime) 159 } 160 161 // spill data onto dirty pages. 162 startTime = time.Now() 163 if err := tx.root.spill(); err != nil { 164 tx.rollback() 165 return err 166 } 167 tx.stats.SpillTime += time.Since(startTime) 168 169 // Free the old root bucket. 170 tx.meta.root.root = tx.root.root 171 172 opgid := tx.meta.pgid 173 174 // Free the freelist and allocate new pages for it. This will overestimate 175 // the size of the freelist but not underestimate the size (which would be bad). 176 tx.db.freelist.free(tx.meta.txid, tx.db.page(tx.meta.freelist)) 177 p, err := tx.allocate((tx.db.freelist.size() / tx.db.pageSize) + 1) 178 if err != nil { 179 tx.rollback() 180 return err 181 } 182 if err := tx.db.freelist.write(p); err != nil { 183 tx.rollback() 184 return err 185 } 186 tx.meta.freelist = p.id 187 188 // If the high water mark has moved up then attempt to grow the database. 189 if tx.meta.pgid > opgid { 190 if err := tx.db.grow(int(tx.meta.pgid+1) * tx.db.pageSize); err != nil { 191 tx.rollback() 192 return err 193 } 194 } 195 196 // Write dirty pages to disk. 197 startTime = time.Now() 198 if err := tx.write(); err != nil { 199 tx.rollback() 200 return err 201 } 202 203 // If strict mode is enabled then perform a consistency check. 204 // Only the first consistency error is reported in the panic. 205 if tx.db.StrictMode { 206 ch := tx.Check() 207 var errs []string 208 for { 209 err, ok := <-ch 210 if !ok { 211 break 212 } 213 errs = append(errs, err.Error()) 214 } 215 if len(errs) > 0 { 216 panic("check fail: " + strings.Join(errs, "\n")) 217 } 218 } 219 220 // Write meta to disk. 221 if err := tx.writeMeta(); err != nil { 222 tx.rollback() 223 return err 224 } 225 tx.stats.WriteTime += time.Since(startTime) 226 227 // Finalize the transaction. 228 tx.close() 229 230 // Execute commit handlers now that the locks have been removed. 231 for _, fn := range tx.commitHandlers { 232 fn() 233 } 234 235 return nil 236} 237 238// Rollback closes the transaction and ignores all previous updates. Read-only 239// transactions must be rolled back and not committed. 240func (tx *Tx) Rollback() error { 241 _assert(!tx.managed, "managed tx rollback not allowed") 242 if tx.db == nil { 243 return ErrTxClosed 244 } 245 tx.rollback() 246 return nil 247} 248 249func (tx *Tx) rollback() { 250 if tx.db == nil { 251 return 252 } 253 if tx.writable { 254 tx.db.freelist.rollback(tx.meta.txid) 255 tx.db.freelist.reload(tx.db.page(tx.db.meta().freelist)) 256 } 257 tx.close() 258} 259 260func (tx *Tx) close() { 261 if tx.db == nil { 262 return 263 } 264 if tx.writable { 265 // Grab freelist stats. 266 var freelistFreeN = tx.db.freelist.free_count() 267 var freelistPendingN = tx.db.freelist.pending_count() 268 var freelistAlloc = tx.db.freelist.size() 269 270 // Remove transaction ref & writer lock. 271 tx.db.rwtx = nil 272 tx.db.rwlock.Unlock() 273 274 // Merge statistics. 275 tx.db.statlock.Lock() 276 tx.db.stats.FreePageN = freelistFreeN 277 tx.db.stats.PendingPageN = freelistPendingN 278 tx.db.stats.FreeAlloc = (freelistFreeN + freelistPendingN) * tx.db.pageSize 279 tx.db.stats.FreelistInuse = freelistAlloc 280 tx.db.stats.TxStats.add(&tx.stats) 281 tx.db.statlock.Unlock() 282 } else { 283 tx.db.removeTx(tx) 284 } 285 286 // Clear all references. 287 tx.db = nil 288 tx.meta = nil 289 tx.root = Bucket{tx: tx} 290 tx.pages = nil 291} 292 293// Copy writes the entire database to a writer. 294// This function exists for backwards compatibility. Use WriteTo() instead. 295func (tx *Tx) Copy(w io.Writer) error { 296 _, err := tx.WriteTo(w) 297 return err 298} 299 300// WriteTo writes the entire database to a writer. 301// If err == nil then exactly tx.Size() bytes will be written into the writer. 302func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) { 303 // Attempt to open reader with WriteFlag 304 f, err := os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0) 305 if err != nil { 306 return 0, err 307 } 308 defer func() { _ = f.Close() }() 309 310 // Generate a meta page. We use the same page data for both meta pages. 311 buf := make([]byte, tx.db.pageSize) 312 page := (*page)(unsafe.Pointer(&buf[0])) 313 page.flags = metaPageFlag 314 *page.meta() = *tx.meta 315 316 // Write meta 0. 317 page.id = 0 318 page.meta().checksum = page.meta().sum64() 319 nn, err := w.Write(buf) 320 n += int64(nn) 321 if err != nil { 322 return n, fmt.Errorf("meta 0 copy: %s", err) 323 } 324 325 // Write meta 1 with a lower transaction id. 326 page.id = 1 327 page.meta().txid -= 1 328 page.meta().checksum = page.meta().sum64() 329 nn, err = w.Write(buf) 330 n += int64(nn) 331 if err != nil { 332 return n, fmt.Errorf("meta 1 copy: %s", err) 333 } 334 335 // Move past the meta pages in the file. 336 if _, err := f.Seek(int64(tx.db.pageSize*2), os.SEEK_SET); err != nil { 337 return n, fmt.Errorf("seek: %s", err) 338 } 339 340 // Copy data pages. 341 wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2)) 342 n += wn 343 if err != nil { 344 return n, err 345 } 346 347 return n, f.Close() 348} 349 350// CopyFile copies the entire database to file at the given path. 351// A reader transaction is maintained during the copy so it is safe to continue 352// using the database while a copy is in progress. 353func (tx *Tx) CopyFile(path string, mode os.FileMode) error { 354 f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) 355 if err != nil { 356 return err 357 } 358 359 err = tx.Copy(f) 360 if err != nil { 361 _ = f.Close() 362 return err 363 } 364 return f.Close() 365} 366 367// Check performs several consistency checks on the database for this transaction. 368// An error is returned if any inconsistency is found. 369// 370// It can be safely run concurrently on a writable transaction. However, this 371// incurs a high cost for large databases and databases with a lot of subbuckets 372// because of caching. This overhead can be removed if running on a read-only 373// transaction, however, it is not safe to execute other writer transactions at 374// the same time. 375func (tx *Tx) Check() <-chan error { 376 ch := make(chan error) 377 go tx.check(ch) 378 return ch 379} 380 381func (tx *Tx) check(ch chan error) { 382 // Check if any pages are double freed. 383 freed := make(map[pgid]bool) 384 all := make([]pgid, tx.db.freelist.count()) 385 tx.db.freelist.copyall(all) 386 for _, id := range all { 387 if freed[id] { 388 ch <- fmt.Errorf("page %d: already freed", id) 389 } 390 freed[id] = true 391 } 392 393 // Track every reachable page. 394 reachable := make(map[pgid]*page) 395 reachable[0] = tx.page(0) // meta0 396 reachable[1] = tx.page(1) // meta1 397 for i := uint32(0); i <= tx.page(tx.meta.freelist).overflow; i++ { 398 reachable[tx.meta.freelist+pgid(i)] = tx.page(tx.meta.freelist) 399 } 400 401 // Recursively check buckets. 402 tx.checkBucket(&tx.root, reachable, freed, ch) 403 404 // Ensure all pages below high water mark are either reachable or freed. 405 for i := pgid(0); i < tx.meta.pgid; i++ { 406 _, isReachable := reachable[i] 407 if !isReachable && !freed[i] { 408 ch <- fmt.Errorf("page %d: unreachable unfreed", int(i)) 409 } 410 } 411 412 // Close the channel to signal completion. 413 close(ch) 414} 415 416func (tx *Tx) checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error) { 417 // Ignore inline buckets. 418 if b.root == 0 { 419 return 420 } 421 422 // Check every page used by this bucket. 423 b.tx.forEachPage(b.root, 0, func(p *page, _ int) { 424 if p.id > tx.meta.pgid { 425 ch <- fmt.Errorf("page %d: out of bounds: %d", int(p.id), int(b.tx.meta.pgid)) 426 } 427 428 // Ensure each page is only referenced once. 429 for i := pgid(0); i <= pgid(p.overflow); i++ { 430 var id = p.id + i 431 if _, ok := reachable[id]; ok { 432 ch <- fmt.Errorf("page %d: multiple references", int(id)) 433 } 434 reachable[id] = p 435 } 436 437 // We should only encounter un-freed leaf and branch pages. 438 if freed[p.id] { 439 ch <- fmt.Errorf("page %d: reachable freed", int(p.id)) 440 } else if (p.flags&branchPageFlag) == 0 && (p.flags&leafPageFlag) == 0 { 441 ch <- fmt.Errorf("page %d: invalid type: %s", int(p.id), p.typ()) 442 } 443 }) 444 445 // Check each bucket within this bucket. 446 _ = b.ForEach(func(k, v []byte) error { 447 if child := b.Bucket(k); child != nil { 448 tx.checkBucket(child, reachable, freed, ch) 449 } 450 return nil 451 }) 452} 453 454// allocate returns a contiguous block of memory starting at a given page. 455func (tx *Tx) allocate(count int) (*page, error) { 456 p, err := tx.db.allocate(count) 457 if err != nil { 458 return nil, err 459 } 460 461 // Save to our page cache. 462 tx.pages[p.id] = p 463 464 // Update statistics. 465 tx.stats.PageCount++ 466 tx.stats.PageAlloc += count * tx.db.pageSize 467 468 return p, nil 469} 470 471// write writes any dirty pages to disk. 472func (tx *Tx) write() error { 473 // Sort pages by id. 474 pages := make(pages, 0, len(tx.pages)) 475 for _, p := range tx.pages { 476 pages = append(pages, p) 477 } 478 // Clear out page cache early. 479 tx.pages = make(map[pgid]*page) 480 sort.Sort(pages) 481 482 // Write pages to disk in order. 483 for _, p := range pages { 484 size := (int(p.overflow) + 1) * tx.db.pageSize 485 offset := int64(p.id) * int64(tx.db.pageSize) 486 487 // Write out page in "max allocation" sized chunks. 488 ptr := (*[maxAllocSize]byte)(unsafe.Pointer(p)) 489 for { 490 // Limit our write to our max allocation size. 491 sz := size 492 if sz > maxAllocSize-1 { 493 sz = maxAllocSize - 1 494 } 495 496 // Write chunk to disk. 497 buf := ptr[:sz] 498 if _, err := tx.db.ops.writeAt(buf, offset); err != nil { 499 return err 500 } 501 502 // Update statistics. 503 tx.stats.Write++ 504 505 // Exit inner for loop if we've written all the chunks. 506 size -= sz 507 if size == 0 { 508 break 509 } 510 511 // Otherwise move offset forward and move pointer to next chunk. 512 offset += int64(sz) 513 ptr = (*[maxAllocSize]byte)(unsafe.Pointer(&ptr[sz])) 514 } 515 } 516 517 // Ignore file sync if flag is set on DB. 518 if !tx.db.NoSync || IgnoreNoSync { 519 if err := fdatasync(tx.db); err != nil { 520 return err 521 } 522 } 523 524 // Put small pages back to page pool. 525 for _, p := range pages { 526 // Ignore page sizes over 1 page. 527 // These are allocated using make() instead of the page pool. 528 if int(p.overflow) != 0 { 529 continue 530 } 531 532 buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:tx.db.pageSize] 533 534 // See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1 535 for i := range buf { 536 buf[i] = 0 537 } 538 tx.db.pagePool.Put(buf) 539 } 540 541 return nil 542} 543 544// writeMeta writes the meta to the disk. 545func (tx *Tx) writeMeta() error { 546 // Create a temporary buffer for the meta page. 547 buf := make([]byte, tx.db.pageSize) 548 p := tx.db.pageInBuffer(buf, 0) 549 tx.meta.write(p) 550 551 // Write the meta page to file. 552 if _, err := tx.db.ops.writeAt(buf, int64(p.id)*int64(tx.db.pageSize)); err != nil { 553 return err 554 } 555 if !tx.db.NoSync || IgnoreNoSync { 556 if err := fdatasync(tx.db); err != nil { 557 return err 558 } 559 } 560 561 // Update statistics. 562 tx.stats.Write++ 563 564 return nil 565} 566 567// page returns a reference to the page with a given id. 568// If page has been written to then a temporary buffered page is returned. 569func (tx *Tx) page(id pgid) *page { 570 // Check the dirty pages first. 571 if tx.pages != nil { 572 if p, ok := tx.pages[id]; ok { 573 return p 574 } 575 } 576 577 // Otherwise return directly from the mmap. 578 return tx.db.page(id) 579} 580 581// forEachPage iterates over every page within a given page and executes a function. 582func (tx *Tx) forEachPage(pgid pgid, depth int, fn func(*page, int)) { 583 p := tx.page(pgid) 584 585 // Execute function. 586 fn(p, depth) 587 588 // Recursively loop over children. 589 if (p.flags & branchPageFlag) != 0 { 590 for i := 0; i < int(p.count); i++ { 591 elem := p.branchPageElement(uint16(i)) 592 tx.forEachPage(elem.pgid, depth+1, fn) 593 } 594 } 595} 596 597// Page returns page information for a given page number. 598// This is only safe for concurrent use when used by a writable transaction. 599func (tx *Tx) Page(id int) (*PageInfo, error) { 600 if tx.db == nil { 601 return nil, ErrTxClosed 602 } else if pgid(id) >= tx.meta.pgid { 603 return nil, nil 604 } 605 606 // Build the page info. 607 p := tx.db.page(pgid(id)) 608 info := &PageInfo{ 609 ID: id, 610 Count: int(p.count), 611 OverflowCount: int(p.overflow), 612 } 613 614 // Determine the type (or if it's free). 615 if tx.db.freelist.freed(pgid(id)) { 616 info.Type = "free" 617 } else { 618 info.Type = p.typ() 619 } 620 621 return info, nil 622} 623 624// TxStats represents statistics about the actions performed by the transaction. 625type TxStats struct { 626 // Page statistics. 627 PageCount int // number of page allocations 628 PageAlloc int // total bytes allocated 629 630 // Cursor statistics. 631 CursorCount int // number of cursors created 632 633 // Node statistics 634 NodeCount int // number of node allocations 635 NodeDeref int // number of node dereferences 636 637 // Rebalance statistics. 638 Rebalance int // number of node rebalances 639 RebalanceTime time.Duration // total time spent rebalancing 640 641 // Split/Spill statistics. 642 Split int // number of nodes split 643 Spill int // number of nodes spilled 644 SpillTime time.Duration // total time spent spilling 645 646 // Write statistics. 647 Write int // number of writes performed 648 WriteTime time.Duration // total time spent writing to disk 649} 650 651func (s *TxStats) add(other *TxStats) { 652 s.PageCount += other.PageCount 653 s.PageAlloc += other.PageAlloc 654 s.CursorCount += other.CursorCount 655 s.NodeCount += other.NodeCount 656 s.NodeDeref += other.NodeDeref 657 s.Rebalance += other.Rebalance 658 s.RebalanceTime += other.RebalanceTime 659 s.Split += other.Split 660 s.Spill += other.Spill 661 s.SpillTime += other.SpillTime 662 s.Write += other.Write 663 s.WriteTime += other.WriteTime 664} 665 666// Sub calculates and returns the difference between two sets of transaction stats. 667// This is useful when obtaining stats at two different points and time and 668// you need the performance counters that occurred within that time span. 669func (s *TxStats) Sub(other *TxStats) TxStats { 670 var diff TxStats 671 diff.PageCount = s.PageCount - other.PageCount 672 diff.PageAlloc = s.PageAlloc - other.PageAlloc 673 diff.CursorCount = s.CursorCount - other.CursorCount 674 diff.NodeCount = s.NodeCount - other.NodeCount 675 diff.NodeDeref = s.NodeDeref - other.NodeDeref 676 diff.Rebalance = s.Rebalance - other.Rebalance 677 diff.RebalanceTime = s.RebalanceTime - other.RebalanceTime 678 diff.Split = s.Split - other.Split 679 diff.Spill = s.Spill - other.Spill 680 diff.SpillTime = s.SpillTime - other.SpillTime 681 diff.Write = s.Write - other.Write 682 diff.WriteTime = s.WriteTime - other.WriteTime 683 return diff 684} 685