本站源代码
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.

107 lines
1.6KB

  1. package nodb
  2. import (
  3. "sync"
  4. "github.com/lunny/nodb/store"
  5. )
  6. type batch struct {
  7. l *Nodb
  8. store.WriteBatch
  9. sync.Locker
  10. logs [][]byte
  11. tx *Tx
  12. }
  13. func (b *batch) Commit() error {
  14. b.l.commitLock.Lock()
  15. defer b.l.commitLock.Unlock()
  16. err := b.WriteBatch.Commit()
  17. if b.l.binlog != nil {
  18. if err == nil {
  19. if b.tx == nil {
  20. b.l.binlog.Log(b.logs...)
  21. } else {
  22. b.tx.logs = append(b.tx.logs, b.logs...)
  23. }
  24. }
  25. b.logs = [][]byte{}
  26. }
  27. return err
  28. }
  29. func (b *batch) Lock() {
  30. b.Locker.Lock()
  31. }
  32. func (b *batch) Unlock() {
  33. if b.l.binlog != nil {
  34. b.logs = [][]byte{}
  35. }
  36. b.WriteBatch.Rollback()
  37. b.Locker.Unlock()
  38. }
  39. func (b *batch) Put(key []byte, value []byte) {
  40. if b.l.binlog != nil {
  41. buf := encodeBinLogPut(key, value)
  42. b.logs = append(b.logs, buf)
  43. }
  44. b.WriteBatch.Put(key, value)
  45. }
  46. func (b *batch) Delete(key []byte) {
  47. if b.l.binlog != nil {
  48. buf := encodeBinLogDelete(key)
  49. b.logs = append(b.logs, buf)
  50. }
  51. b.WriteBatch.Delete(key)
  52. }
  53. type dbBatchLocker struct {
  54. l *sync.Mutex
  55. wrLock *sync.RWMutex
  56. }
  57. func (l *dbBatchLocker) Lock() {
  58. l.wrLock.RLock()
  59. l.l.Lock()
  60. }
  61. func (l *dbBatchLocker) Unlock() {
  62. l.l.Unlock()
  63. l.wrLock.RUnlock()
  64. }
  65. type txBatchLocker struct {
  66. }
  67. func (l *txBatchLocker) Lock() {}
  68. func (l *txBatchLocker) Unlock() {}
  69. type multiBatchLocker struct {
  70. }
  71. func (l *multiBatchLocker) Lock() {}
  72. func (l *multiBatchLocker) Unlock() {}
  73. func (l *Nodb) newBatch(wb store.WriteBatch, locker sync.Locker, tx *Tx) *batch {
  74. b := new(batch)
  75. b.l = l
  76. b.WriteBatch = wb
  77. b.tx = tx
  78. b.Locker = locker
  79. b.logs = [][]byte{}
  80. return b
  81. }
上海开阖软件有限公司 沪ICP备12045867号-1