You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

726 lines
19 KiB

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