batch_tx.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if t.pending >= t.backend.batchLimit {
  60. t.commit(false)
  61. t.pending = 0
  62. }
  63. }
  64. // before calling unsafeRange, the caller MUST hold the lock on tx.
  65. func (t *batchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vs [][]byte) {
  66. bucket := t.tx.Bucket(bucketName)
  67. if bucket == nil {
  68. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  69. }
  70. if len(endKey) == 0 {
  71. if v := bucket.Get(key); v == nil {
  72. return keys, vs
  73. } else {
  74. return append(keys, key), append(vs, v)
  75. }
  76. }
  77. c := bucket.Cursor()
  78. for ck, cv := c.Seek(key); ck != nil && bytes.Compare(ck, endKey) < 0; ck, cv = c.Next() {
  79. vs = append(vs, cv)
  80. keys = append(keys, ck)
  81. if limit > 0 && limit == int64(len(keys)) {
  82. break
  83. }
  84. }
  85. return keys, vs
  86. }
  87. // before calling unsafeDelete, the caller MUST hold the lock on tx.
  88. func (t *batchTx) UnsafeDelete(bucketName []byte, key []byte) {
  89. bucket := t.tx.Bucket(bucketName)
  90. if bucket == nil {
  91. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  92. }
  93. err := bucket.Delete(key)
  94. if err != nil {
  95. log.Fatalf("storage: cannot delete key from bucket (%v)", err)
  96. }
  97. t.pending++
  98. if t.pending >= t.backend.batchLimit {
  99. t.commit(false)
  100. t.pending = 0
  101. }
  102. }
  103. // Commit commits a previous tx and begins a new writable one.
  104. func (t *batchTx) Commit() {
  105. t.Lock()
  106. defer t.Unlock()
  107. t.commit(false)
  108. }
  109. // CommitAndStop commits the previous tx and do not create a new one.
  110. func (t *batchTx) CommitAndStop() {
  111. t.Lock()
  112. defer t.Unlock()
  113. t.commit(true)
  114. }
  115. func (t *batchTx) commit(stop bool) {
  116. var err error
  117. // commit the last tx
  118. if t.tx != nil {
  119. err = t.tx.Commit()
  120. if err != nil {
  121. log.Fatalf("storage: cannot commit tx (%s)", err)
  122. }
  123. }
  124. if stop {
  125. return
  126. }
  127. // begin a new tx
  128. t.tx, err = t.backend.db.Begin(true)
  129. if err != nil {
  130. log.Fatalf("storage: cannot begin tx (%s)", err)
  131. }
  132. atomic.StoreInt64(&t.backend.size, t.tx.Size())
  133. }