Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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