Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. package ho
  2.  
  3. import "github.com/boltdb/bolt"
  4.  
  5. type TCharacterJournalStorage struct {
  6. CS *TCharacterStorage
  7. CharacterKey string
  8. // Pack journal inconsistency log action.
  9. PackJournal_ILA TLimitedAction
  10. }
  11.  
  12. func (this *TCharacterJournalStorage) Create() *TCharacterJournalStorage {
  13. this.PackJournal_ILA.Count = 2
  14. return this
  15. }
  16.  
  17. func (this *TCharacterJournalStorage) GetBucketKey() []byte {
  18. return this.CS.JournalBucketKey
  19. }
  20.  
  21. func (this *TCharacterJournalStorage) Write(item TJournalItem) {
  22. var data = item.Store().ToJson()
  23. var transaction = this.CS.Begin(true); defer transaction.Commit()
  24. var count = this.addRecordCount(transaction)
  25. var key = this.getRecordKey(count - 1)
  26. transaction.Bucket(this.GetBucketKey()).Put(key, data)
  27. this.packIfNecess(transaction)
  28. }
  29.  
  30. func (this *TCharacterJournalStorage) getRecordKey(index int32) []byte {
  31. var subKey = this.getRecordSubKey(index)
  32. var text = this.CharacterKey + subKey
  33. var data = []byte(text)
  34. return data
  35. }
  36.  
  37. func (this *TCharacterJournalStorage) getRecordSubKey(index int32) (result string) {
  38. result = Int32ToStr(index)
  39. result = Add0ToStr(result, CharacterStorageJournalSubKeyLength)
  40. return
  41. }
  42.  
  43. func (this *TCharacterJournalStorage) addRecordCount(tx *bolt.Tx) (result int32) {
  44. result = this.GetRecordCount(tx)
  45. result++
  46. this.setRecordCount(tx, result)
  47. return
  48. }
  49.  
  50. // Public.
  51. func (this *TCharacterJournalStorage) StartWrite(item TJournalItem) {
  52. this.CS.StartJob(func() {
  53. this.Write(item)
  54. })
  55. }
  56.  
  57. func (this *TCharacterJournalStorage) packIfNecess(tx *bolt.Tx) {
  58. var count = this.GetRecordCount(tx)
  59. var bucket = tx.Bucket(this.GetBucketKey())
  60. if CharacterStorageJournalRecordCountLimit*2 < count {
  61. var sourceIndex int32 = count - CharacterStorageJournalRecordCountLimit
  62. var savedCount int32 = 0
  63. for i := int32(0); i < CharacterStorageJournalRecordCountLimit; i++ {
  64. var destinationKey = this.getRecordKey(i)
  65. var sourceKey = this.getRecordKey(sourceIndex)
  66. var data = bucket.Get(sourceKey)
  67. if data != nil {
  68. bucket.Put(destinationKey, data)
  69. bucket.Delete(sourceKey)
  70. savedCount++
  71. } else {
  72. this.PackJournal_ILA.Do(func() {
  73. GlobalLog.Write("Inconsistency: break at " + Int32ToStr(i) + "; characterKey='" + this.CharacterKey + "'")
  74. })
  75. break
  76. }
  77. }
  78. this.setRecordCount(tx, savedCount)
  79. }
  80. }
  81.  
  82. func (this *TCharacterJournalStorage) GetRecordCount(tx *bolt.Tx) (result int32) {
  83. var key = this.getRecordCountKey()
  84. var data = tx.Bucket(this.GetBucketKey()).Get(key)
  85. if data != nil {
  86. TryStrToInt(string(data), &result)
  87. }
  88. return
  89. }
  90.  
  91. func (this *TCharacterJournalStorage) getRecordCountKey() (result []byte) {
  92. var keyText = this.CharacterKey + CharacterStorageJournalLengthSubKey
  93. result = []byte(keyText)
  94. return
  95. }
  96.  
  97. func (this *TCharacterJournalStorage) setRecordCount(tx *bolt.Tx, value int32) {
  98. tx.Bucket(this.GetBucketKey()).Put(
  99. this.getRecordCountKey(),
  100. Int32ToDataStr(value))
  101. }
  102.  
  103. func (this *TCharacterJournalStorage) Read(desiredCount int32) (result [][]byte) {
  104. var transaction = this.CS.Begin(false); defer transaction.Rollback()
  105. var count = this.GetRecordCount(transaction)
  106. var startIndex int32 = 0
  107. if desiredCount < count {
  108. startIndex = count - desiredCount
  109. }
  110. var bucket = transaction.Bucket(this.GetBucketKey())
  111. for i := startIndex; i < count; i++ {
  112. var data = bucket.Get(this.getRecordKey(i))
  113. result = append(result, data)
  114. }
  115. return
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement