batch_tx.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package backend
  2. import (
  3. "bytes"
  4. "log"
  5. "sync"
  6. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
  7. )
  8. type BatchTx interface {
  9. Lock()
  10. Unlock()
  11. UnsafeCreateBucket(name []byte)
  12. UnsafePut(bucketName []byte, key []byte, value []byte)
  13. UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte)
  14. UnsafeDelete(bucketName []byte, key []byte)
  15. Commit()
  16. }
  17. type batchTx struct {
  18. sync.Mutex
  19. tx *bolt.Tx
  20. backend *backend
  21. pending int
  22. }
  23. func (t *batchTx) UnsafeCreateBucket(name []byte) {
  24. _, err := t.tx.CreateBucket(name)
  25. if err != nil && err != bolt.ErrBucketExists {
  26. log.Fatalf("storage: cannot create bucket %s (%v)", string(name), err)
  27. }
  28. }
  29. // before calling unsafePut, the caller MUST hold the lock on tnx.
  30. func (t *batchTx) UnsafePut(bucketName []byte, key []byte, value []byte) {
  31. bucket := t.tx.Bucket(bucketName)
  32. if bucket == nil {
  33. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  34. }
  35. if err := bucket.Put(key, value); err != nil {
  36. log.Fatalf("storage: cannot put key into bucket (%v)", err)
  37. }
  38. t.pending++
  39. if t.pending > t.backend.batchLimit {
  40. t.commit()
  41. t.pending = 0
  42. }
  43. }
  44. // before calling unsafeRange, the caller MUST hold the lock on tnx.
  45. func (t *batchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vs [][]byte) {
  46. bucket := t.tx.Bucket(bucketName)
  47. if bucket == nil {
  48. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  49. }
  50. if len(endKey) == 0 {
  51. if v := bucket.Get(key); v == nil {
  52. return keys, vs
  53. } else {
  54. return append(keys, key), append(vs, v)
  55. }
  56. }
  57. c := bucket.Cursor()
  58. for ck, cv := c.Seek(key); ck != nil && bytes.Compare(ck, endKey) < 0; ck, cv = c.Next() {
  59. vs = append(vs, cv)
  60. keys = append(keys, ck)
  61. }
  62. return keys, vs
  63. }
  64. // before calling unsafeDelete, the caller MUST hold the lock on tnx.
  65. func (t *batchTx) UnsafeDelete(bucketName []byte, key []byte) {
  66. bucket := t.tx.Bucket(bucketName)
  67. if bucket == nil {
  68. log.Fatalf("storage: bucket %s does not exist", string(bucketName))
  69. }
  70. err := bucket.Delete(key)
  71. if err != nil {
  72. log.Fatalf("storage: cannot delete key from bucket (%v)", err)
  73. }
  74. t.pending++
  75. if t.pending > t.backend.batchLimit {
  76. t.commit()
  77. t.pending = 0
  78. }
  79. }
  80. // commitAndBegin commits a previous tx and begins a new writable one.
  81. func (t *batchTx) Commit() {
  82. t.Lock()
  83. defer t.Unlock()
  84. t.commit()
  85. }
  86. func (t *batchTx) commit() {
  87. var err error
  88. // commit the last tx
  89. if t.tx != nil {
  90. err = t.tx.Commit()
  91. if err != nil {
  92. log.Fatalf("storage: cannot commit tx (%s)", err)
  93. }
  94. }
  95. // begin a new tx
  96. t.tx, err = t.backend.db.Begin(true)
  97. if err != nil {
  98. log.Fatalf("storage: cannot begin tx (%s)", err)
  99. }
  100. }