tx.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. package bolt
  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. if err := fn(k, tx.root.Bucket(k)); err != nil {
  111. return err
  112. }
  113. return nil
  114. })
  115. }
  116. // OnCommit adds a handler function to be executed after the transaction successfully commits.
  117. func (tx *Tx) OnCommit(fn func()) {
  118. tx.commitHandlers = append(tx.commitHandlers, fn)
  119. }
  120. // Commit writes all changes to disk and updates the meta page.
  121. // Returns an error if a disk write error occurs, or if Commit is
  122. // called on a read-only transaction.
  123. func (tx *Tx) Commit() error {
  124. _assert(!tx.managed, "managed tx commit not allowed")
  125. if tx.db == nil {
  126. return ErrTxClosed
  127. } else if !tx.writable {
  128. return ErrTxNotWritable
  129. }
  130. // TODO(benbjohnson): Use vectorized I/O to write out dirty pages.
  131. // Rebalance nodes which have had deletions.
  132. var startTime = time.Now()
  133. tx.root.rebalance()
  134. if tx.stats.Rebalance > 0 {
  135. tx.stats.RebalanceTime += time.Since(startTime)
  136. }
  137. // spill data onto dirty pages.
  138. startTime = time.Now()
  139. if err := tx.root.spill(); err != nil {
  140. tx.rollback()
  141. return err
  142. }
  143. tx.stats.SpillTime += time.Since(startTime)
  144. // Free the old root bucket.
  145. tx.meta.root.root = tx.root.root
  146. opgid := tx.meta.pgid
  147. // Free the freelist and allocate new pages for it. This will overestimate
  148. // the size of the freelist but not underestimate the size (which would be bad).
  149. tx.db.freelist.free(tx.meta.txid, tx.db.page(tx.meta.freelist))
  150. p, err := tx.allocate((tx.db.freelist.size() / tx.db.pageSize) + 1)
  151. if err != nil {
  152. tx.rollback()
  153. return err
  154. }
  155. if err := tx.db.freelist.write(p); err != nil {
  156. tx.rollback()
  157. return err
  158. }
  159. tx.meta.freelist = p.id
  160. // If the high water mark has moved up then attempt to grow the database.
  161. if tx.meta.pgid > opgid {
  162. if err := tx.db.grow(int(tx.meta.pgid+1) * tx.db.pageSize); err != nil {
  163. tx.rollback()
  164. return err
  165. }
  166. }
  167. // Write dirty pages to disk.
  168. startTime = time.Now()
  169. if err := tx.write(); err != nil {
  170. tx.rollback()
  171. return err
  172. }
  173. // If strict mode is enabled then perform a consistency check.
  174. // Only the first consistency error is reported in the panic.
  175. if tx.db.StrictMode {
  176. ch := tx.Check()
  177. var errs []string
  178. for {
  179. err, ok := <-ch
  180. if !ok {
  181. break
  182. }
  183. errs = append(errs, err.Error())
  184. }
  185. if len(errs) > 0 {
  186. panic("check fail: " + strings.Join(errs, "\n"))
  187. }
  188. }
  189. // Write meta to disk.
  190. if err := tx.writeMeta(); err != nil {
  191. tx.rollback()
  192. return err
  193. }
  194. tx.stats.WriteTime += time.Since(startTime)
  195. // Finalize the transaction.
  196. tx.close()
  197. // Execute commit handlers now that the locks have been removed.
  198. for _, fn := range tx.commitHandlers {
  199. fn()
  200. }
  201. return nil
  202. }
  203. // Rollback closes the transaction and ignores all previous updates. Read-only
  204. // transactions must be rolled back and not committed.
  205. func (tx *Tx) Rollback() error {
  206. _assert(!tx.managed, "managed tx rollback not allowed")
  207. if tx.db == nil {
  208. return ErrTxClosed
  209. }
  210. tx.rollback()
  211. return nil
  212. }
  213. func (tx *Tx) rollback() {
  214. if tx.db == nil {
  215. return
  216. }
  217. if tx.writable {
  218. tx.db.freelist.rollback(tx.meta.txid)
  219. tx.db.freelist.reload(tx.db.page(tx.db.meta().freelist))
  220. }
  221. tx.close()
  222. }
  223. func (tx *Tx) close() {
  224. if tx.db == nil {
  225. return
  226. }
  227. if tx.writable {
  228. // Grab freelist stats.
  229. var freelistFreeN = tx.db.freelist.free_count()
  230. var freelistPendingN = tx.db.freelist.pending_count()
  231. var freelistAlloc = tx.db.freelist.size()
  232. // Remove transaction ref & writer lock.
  233. tx.db.rwtx = nil
  234. tx.db.rwlock.Unlock()
  235. // Merge statistics.
  236. tx.db.statlock.Lock()
  237. tx.db.stats.FreePageN = freelistFreeN
  238. tx.db.stats.PendingPageN = freelistPendingN
  239. tx.db.stats.FreeAlloc = (freelistFreeN + freelistPendingN) * tx.db.pageSize
  240. tx.db.stats.FreelistInuse = freelistAlloc
  241. tx.db.stats.TxStats.add(&tx.stats)
  242. tx.db.statlock.Unlock()
  243. } else {
  244. tx.db.removeTx(tx)
  245. }
  246. // Clear all references.
  247. tx.db = nil
  248. tx.meta = nil
  249. tx.root = Bucket{tx: tx}
  250. tx.pages = nil
  251. }
  252. // Copy writes the entire database to a writer.
  253. // This function exists for backwards compatibility. Use WriteTo() instead.
  254. func (tx *Tx) Copy(w io.Writer) error {
  255. _, err := tx.WriteTo(w)
  256. return err
  257. }
  258. // WriteTo writes the entire database to a writer.
  259. // If err == nil then exactly tx.Size() bytes will be written into the writer.
  260. func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {
  261. // Attempt to open reader with WriteFlag
  262. f, err := os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0)
  263. if err != nil {
  264. return 0, err
  265. }
  266. defer func() { _ = f.Close() }()
  267. // Generate a meta page. We use the same page data for both meta pages.
  268. buf := make([]byte, tx.db.pageSize)
  269. page := (*page)(unsafe.Pointer(&buf[0]))
  270. page.flags = metaPageFlag
  271. *page.meta() = *tx.meta
  272. // Write meta 0.
  273. page.id = 0
  274. page.meta().checksum = page.meta().sum64()
  275. nn, err := w.Write(buf)
  276. n += int64(nn)
  277. if err != nil {
  278. return n, fmt.Errorf("meta 0 copy: %s", err)
  279. }
  280. // Write meta 1 with a lower transaction id.
  281. page.id = 1
  282. page.meta().txid -= 1
  283. page.meta().checksum = page.meta().sum64()
  284. nn, err = w.Write(buf)
  285. n += int64(nn)
  286. if err != nil {
  287. return n, fmt.Errorf("meta 1 copy: %s", err)
  288. }
  289. // Move past the meta pages in the file.
  290. if _, err := f.Seek(int64(tx.db.pageSize*2), os.SEEK_SET); err != nil {
  291. return n, fmt.Errorf("seek: %s", err)
  292. }
  293. // Copy data pages.
  294. wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2))
  295. n += wn
  296. if err != nil {
  297. return n, err
  298. }
  299. return n, f.Close()
  300. }
  301. // CopyFile copies the entire database to file at the given path.
  302. // A reader transaction is maintained during the copy so it is safe to continue
  303. // using the database while a copy is in progress.
  304. func (tx *Tx) CopyFile(path string, mode os.FileMode) error {
  305. f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
  306. if err != nil {
  307. return err
  308. }
  309. err = tx.Copy(f)
  310. if err != nil {
  311. _ = f.Close()
  312. return err
  313. }
  314. return f.Close()
  315. }
  316. // Check performs several consistency checks on the database for this transaction.
  317. // An error is returned if any inconsistency is found.
  318. //
  319. // It can be safely run concurrently on a writable transaction. However, this
  320. // incurs a high cost for large databases and databases with a lot of subbuckets
  321. // because of caching. This overhead can be removed if running on a read-only
  322. // transaction, however, it is not safe to execute other writer transactions at
  323. // the same time.
  324. func (tx *Tx) Check() <-chan error {
  325. ch := make(chan error)
  326. go tx.check(ch)
  327. return ch
  328. }
  329. func (tx *Tx) check(ch chan error) {
  330. // Check if any pages are double freed.
  331. freed := make(map[pgid]bool)
  332. for _, id := range tx.db.freelist.all() {
  333. if freed[id] {
  334. ch <- fmt.Errorf("page %d: already freed", id)
  335. }
  336. freed[id] = true
  337. }
  338. // Track every reachable page.
  339. reachable := make(map[pgid]*page)
  340. reachable[0] = tx.page(0) // meta0
  341. reachable[1] = tx.page(1) // meta1
  342. for i := uint32(0); i <= tx.page(tx.meta.freelist).overflow; i++ {
  343. reachable[tx.meta.freelist+pgid(i)] = tx.page(tx.meta.freelist)
  344. }
  345. // Recursively check buckets.
  346. tx.checkBucket(&tx.root, reachable, freed, ch)
  347. // Ensure all pages below high water mark are either reachable or freed.
  348. for i := pgid(0); i < tx.meta.pgid; i++ {
  349. _, isReachable := reachable[i]
  350. if !isReachable && !freed[i] {
  351. ch <- fmt.Errorf("page %d: unreachable unfreed", int(i))
  352. }
  353. }
  354. // Close the channel to signal completion.
  355. close(ch)
  356. }
  357. func (tx *Tx) checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error) {
  358. // Ignore inline buckets.
  359. if b.root == 0 {
  360. return
  361. }
  362. // Check every page used by this bucket.
  363. b.tx.forEachPage(b.root, 0, func(p *page, _ int) {
  364. if p.id > tx.meta.pgid {
  365. ch <- fmt.Errorf("page %d: out of bounds: %d", int(p.id), int(b.tx.meta.pgid))
  366. }
  367. // Ensure each page is only referenced once.
  368. for i := pgid(0); i <= pgid(p.overflow); i++ {
  369. var id = p.id + i
  370. if _, ok := reachable[id]; ok {
  371. ch <- fmt.Errorf("page %d: multiple references", int(id))
  372. }
  373. reachable[id] = p
  374. }
  375. // We should only encounter un-freed leaf and branch pages.
  376. if freed[p.id] {
  377. ch <- fmt.Errorf("page %d: reachable freed", int(p.id))
  378. } else if (p.flags&branchPageFlag) == 0 && (p.flags&leafPageFlag) == 0 {
  379. ch <- fmt.Errorf("page %d: invalid type: %s", int(p.id), p.typ())
  380. }
  381. })
  382. // Check each bucket within this bucket.
  383. _ = b.ForEach(func(k, v []byte) error {
  384. if child := b.Bucket(k); child != nil {
  385. tx.checkBucket(child, reachable, freed, ch)
  386. }
  387. return nil
  388. })
  389. }
  390. // allocate returns a contiguous block of memory starting at a given page.
  391. func (tx *Tx) allocate(count int) (*page, error) {
  392. p, err := tx.db.allocate(count)
  393. if err != nil {
  394. return nil, err
  395. }
  396. // Save to our page cache.
  397. tx.pages[p.id] = p
  398. // Update statistics.
  399. tx.stats.PageCount++
  400. tx.stats.PageAlloc += count * tx.db.pageSize
  401. return p, nil
  402. }
  403. // write writes any dirty pages to disk.
  404. func (tx *Tx) write() error {
  405. // Sort pages by id.
  406. pages := make(pages, 0, len(tx.pages))
  407. for _, p := range tx.pages {
  408. pages = append(pages, p)
  409. }
  410. // Clear out page cache early.
  411. tx.pages = make(map[pgid]*page)
  412. sort.Sort(pages)
  413. // Write pages to disk in order.
  414. for _, p := range pages {
  415. size := (int(p.overflow) + 1) * tx.db.pageSize
  416. offset := int64(p.id) * int64(tx.db.pageSize)
  417. // Write out page in "max allocation" sized chunks.
  418. ptr := (*[maxAllocSize]byte)(unsafe.Pointer(p))
  419. for {
  420. // Limit our write to our max allocation size.
  421. sz := size
  422. if sz > maxAllocSize-1 {
  423. sz = maxAllocSize - 1
  424. }
  425. // Write chunk to disk.
  426. buf := ptr[:sz]
  427. if _, err := tx.db.ops.writeAt(buf, offset); err != nil {
  428. return err
  429. }
  430. // Update statistics.
  431. tx.stats.Write++
  432. // Exit inner for loop if we've written all the chunks.
  433. size -= sz
  434. if size == 0 {
  435. break
  436. }
  437. // Otherwise move offset forward and move pointer to next chunk.
  438. offset += int64(sz)
  439. ptr = (*[maxAllocSize]byte)(unsafe.Pointer(&ptr[sz]))
  440. }
  441. }
  442. // Ignore file sync if flag is set on DB.
  443. if !tx.db.NoSync || IgnoreNoSync {
  444. if err := fdatasync(tx.db); err != nil {
  445. return err
  446. }
  447. }
  448. // Put small pages back to page pool.
  449. for _, p := range pages {
  450. // Ignore page sizes over 1 page.
  451. // These are allocated using make() instead of the page pool.
  452. if int(p.overflow) != 0 {
  453. continue
  454. }
  455. buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:tx.db.pageSize]
  456. // See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1
  457. for i := range buf {
  458. buf[i] = 0
  459. }
  460. tx.db.pagePool.Put(buf)
  461. }
  462. return nil
  463. }
  464. // writeMeta writes the meta to the disk.
  465. func (tx *Tx) writeMeta() error {
  466. // Create a temporary buffer for the meta page.
  467. buf := make([]byte, tx.db.pageSize)
  468. p := tx.db.pageInBuffer(buf, 0)
  469. tx.meta.write(p)
  470. // Write the meta page to file.
  471. if _, err := tx.db.ops.writeAt(buf, int64(p.id)*int64(tx.db.pageSize)); err != nil {
  472. return err
  473. }
  474. if !tx.db.NoSync || IgnoreNoSync {
  475. if err := fdatasync(tx.db); err != nil {
  476. return err
  477. }
  478. }
  479. // Update statistics.
  480. tx.stats.Write++
  481. return nil
  482. }
  483. // page returns a reference to the page with a given id.
  484. // If page has been written to then a temporary buffered page is returned.
  485. func (tx *Tx) page(id pgid) *page {
  486. // Check the dirty pages first.
  487. if tx.pages != nil {
  488. if p, ok := tx.pages[id]; ok {
  489. return p
  490. }
  491. }
  492. // Otherwise return directly from the mmap.
  493. return tx.db.page(id)
  494. }
  495. // forEachPage iterates over every page within a given page and executes a function.
  496. func (tx *Tx) forEachPage(pgid pgid, depth int, fn func(*page, int)) {
  497. p := tx.page(pgid)
  498. // Execute function.
  499. fn(p, depth)
  500. // Recursively loop over children.
  501. if (p.flags & branchPageFlag) != 0 {
  502. for i := 0; i < int(p.count); i++ {
  503. elem := p.branchPageElement(uint16(i))
  504. tx.forEachPage(elem.pgid, depth+1, fn)
  505. }
  506. }
  507. }
  508. // Page returns page information for a given page number.
  509. // This is only safe for concurrent use when used by a writable transaction.
  510. func (tx *Tx) Page(id int) (*PageInfo, error) {
  511. if tx.db == nil {
  512. return nil, ErrTxClosed
  513. } else if pgid(id) >= tx.meta.pgid {
  514. return nil, nil
  515. }
  516. // Build the page info.
  517. p := tx.db.page(pgid(id))
  518. info := &PageInfo{
  519. ID: id,
  520. Count: int(p.count),
  521. OverflowCount: int(p.overflow),
  522. }
  523. // Determine the type (or if it's free).
  524. if tx.db.freelist.freed(pgid(id)) {
  525. info.Type = "free"
  526. } else {
  527. info.Type = p.typ()
  528. }
  529. return info, nil
  530. }
  531. // TxStats represents statistics about the actions performed by the transaction.
  532. type TxStats struct {
  533. // Page statistics.
  534. PageCount int // number of page allocations
  535. PageAlloc int // total bytes allocated
  536. // Cursor statistics.
  537. CursorCount int // number of cursors created
  538. // Node statistics
  539. NodeCount int // number of node allocations
  540. NodeDeref int // number of node dereferences
  541. // Rebalance statistics.
  542. Rebalance int // number of node rebalances
  543. RebalanceTime time.Duration // total time spent rebalancing
  544. // Split/Spill statistics.
  545. Split int // number of nodes split
  546. Spill int // number of nodes spilled
  547. SpillTime time.Duration // total time spent spilling
  548. // Write statistics.
  549. Write int // number of writes performed
  550. WriteTime time.Duration // total time spent writing to disk
  551. }
  552. func (s *TxStats) add(other *TxStats) {
  553. s.PageCount += other.PageCount
  554. s.PageAlloc += other.PageAlloc
  555. s.CursorCount += other.CursorCount
  556. s.NodeCount += other.NodeCount
  557. s.NodeDeref += other.NodeDeref
  558. s.Rebalance += other.Rebalance
  559. s.RebalanceTime += other.RebalanceTime
  560. s.Split += other.Split
  561. s.Spill += other.Spill
  562. s.SpillTime += other.SpillTime
  563. s.Write += other.Write
  564. s.WriteTime += other.WriteTime
  565. }
  566. // Sub calculates and returns the difference between two sets of transaction stats.
  567. // This is useful when obtaining stats at two different points and time and
  568. // you need the performance counters that occurred within that time span.
  569. func (s *TxStats) Sub(other *TxStats) TxStats {
  570. var diff TxStats
  571. diff.PageCount = s.PageCount - other.PageCount
  572. diff.PageAlloc = s.PageAlloc - other.PageAlloc
  573. diff.CursorCount = s.CursorCount - other.CursorCount
  574. diff.NodeCount = s.NodeCount - other.NodeCount
  575. diff.NodeDeref = s.NodeDeref - other.NodeDeref
  576. diff.Rebalance = s.Rebalance - other.Rebalance
  577. diff.RebalanceTime = s.RebalanceTime - other.RebalanceTime
  578. diff.Split = s.Split - other.Split
  579. diff.Spill = s.Spill - other.Spill
  580. diff.SpillTime = s.SpillTime - other.SpillTime
  581. diff.Write = s.Write - other.Write
  582. diff.WriteTime = s.WriteTime - other.WriteTime
  583. return diff
  584. }