batch_tx.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package backend
  15. import (
  16. "bytes"
  17. "log"
  18. "sync"
  19. "sync/atomic"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
  21. )
  22. type BatchTx interface {
  23. Lock()
  24. Unlock()
  25. UnsafeCreateBucket(name []byte)
  26. UnsafePut(bucketName []byte, key []byte, value []byte)
  27. UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte)
  28. UnsafeDelete(bucketName []byte, key []byte)
  29. Commit()
  30. CommitAndStop()
  31. }
  32. type batchTx struct {
  33. sync.Mutex
  34. tx *bolt.Tx
  35. backend *backend
  36. pending int
  37. }
  38. func newBatchTx(backend *backend) *batchTx {
  39. tx := &batchTx{backend: backend}
  40. tx.Commit()
  41. return tx
  42. }
  43. func (t *batchTx) UnsafeCreateBucket(name []byte) {
  44. _, err := t.tx.CreateBucket(name)
  45. if err != nil && err != bolt.ErrBucketExists {
  46. log.Fatalf("storage: cannot create bucket %s (%v)", string(name), err)
  47. }
  48. }
  49. // before calling unsafePut, the caller MUST hold the lock on tx.
  50. func (t *batchTx) UnsafePut(bucketName []byte, key []byte, value []byte) {
  51. bucket := t.tx.Bucket(bucketName)
  52. if bucket == nil {
  53. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  54. }
  55. if err := bucket.Put(key, value); err != nil {
  56. log.Fatalf("storage: cannot put key into bucket (%v)", err)
  57. }
  58. t.pending++
  59. }
  60. // before calling unsafeRange, the caller MUST hold the lock on tx.
  61. func (t *batchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vs [][]byte) {
  62. bucket := t.tx.Bucket(bucketName)
  63. if bucket == nil {
  64. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  65. }
  66. if len(endKey) == 0 {
  67. if v := bucket.Get(key); v == nil {
  68. return keys, vs
  69. } else {
  70. return append(keys, key), append(vs, v)
  71. }
  72. }
  73. c := bucket.Cursor()
  74. for ck, cv := c.Seek(key); ck != nil && bytes.Compare(ck, endKey) < 0; ck, cv = c.Next() {
  75. vs = append(vs, cv)
  76. keys = append(keys, ck)
  77. if limit > 0 && limit == int64(len(keys)) {
  78. break
  79. }
  80. }
  81. return keys, vs
  82. }
  83. // before calling unsafeDelete, the caller MUST hold the lock on tx.
  84. func (t *batchTx) UnsafeDelete(bucketName []byte, key []byte) {
  85. bucket := t.tx.Bucket(bucketName)
  86. if bucket == nil {
  87. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  88. }
  89. err := bucket.Delete(key)
  90. if err != nil {
  91. log.Fatalf("storage: cannot delete key from bucket (%v)", err)
  92. }
  93. t.pending++
  94. }
  95. // Commit commits a previous tx and begins a new writable one.
  96. func (t *batchTx) Commit() {
  97. t.Lock()
  98. defer t.Unlock()
  99. t.commit(false)
  100. }
  101. // CommitAndStop commits the previous tx and do not create a new one.
  102. func (t *batchTx) CommitAndStop() {
  103. t.Lock()
  104. defer t.Unlock()
  105. t.commit(true)
  106. }
  107. func (t *batchTx) Unlock() {
  108. if t.pending >= t.backend.batchLimit {
  109. t.commit(false)
  110. t.pending = 0
  111. }
  112. t.Mutex.Unlock()
  113. }
  114. func (t *batchTx) commit(stop bool) {
  115. var err error
  116. // commit the last tx
  117. if t.tx != nil {
  118. if t.pending == 0 && !stop {
  119. return
  120. }
  121. err = t.tx.Commit()
  122. t.pending = 0
  123. if err != nil {
  124. log.Fatalf("storage: cannot commit tx (%s)", err)
  125. }
  126. }
  127. if stop {
  128. return
  129. }
  130. // begin a new tx
  131. t.tx, err = t.backend.db.Begin(true)
  132. if err != nil {
  133. log.Fatalf("storage: cannot begin tx (%s)", err)
  134. }
  135. atomic.StoreInt64(&t.backend.size, t.tx.Size())
  136. }