Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ho
- import "github.com/boltdb/bolt"
- type TCharacterJournalStorage struct {
- CS *TCharacterStorage
- CharacterKey string
- // Pack journal inconsistency log action.
- PackJournal_ILA TLimitedAction
- }
- func (this *TCharacterJournalStorage) Create() *TCharacterJournalStorage {
- this.PackJournal_ILA.Count = 2
- return this
- }
- func (this *TCharacterJournalStorage) GetBucketKey() []byte {
- return this.CS.JournalBucketKey
- }
- func (this *TCharacterJournalStorage) Write(item TJournalItem) {
- var data = item.Store().ToJson()
- var transaction = this.CS.Begin(true); defer transaction.Commit()
- var count = this.addRecordCount(transaction)
- var key = this.getRecordKey(count - 1)
- transaction.Bucket(this.GetBucketKey()).Put(key, data)
- this.packIfNecess(transaction)
- }
- func (this *TCharacterJournalStorage) getRecordKey(index int32) []byte {
- var subKey = this.getRecordSubKey(index)
- var text = this.CharacterKey + subKey
- var data = []byte(text)
- return data
- }
- func (this *TCharacterJournalStorage) getRecordSubKey(index int32) (result string) {
- result = Int32ToStr(index)
- result = Add0ToStr(result, CharacterStorageJournalSubKeyLength)
- return
- }
- func (this *TCharacterJournalStorage) addRecordCount(tx *bolt.Tx) (result int32) {
- result = this.GetRecordCount(tx)
- result++
- this.setRecordCount(tx, result)
- return
- }
- // Public.
- func (this *TCharacterJournalStorage) StartWrite(item TJournalItem) {
- this.CS.StartJob(func() {
- this.Write(item)
- })
- }
- func (this *TCharacterJournalStorage) packIfNecess(tx *bolt.Tx) {
- var count = this.GetRecordCount(tx)
- var bucket = tx.Bucket(this.GetBucketKey())
- if CharacterStorageJournalRecordCountLimit*2 < count {
- var sourceIndex int32 = count - CharacterStorageJournalRecordCountLimit
- var savedCount int32 = 0
- for i := int32(0); i < CharacterStorageJournalRecordCountLimit; i++ {
- var destinationKey = this.getRecordKey(i)
- var sourceKey = this.getRecordKey(sourceIndex)
- var data = bucket.Get(sourceKey)
- if data != nil {
- bucket.Put(destinationKey, data)
- bucket.Delete(sourceKey)
- savedCount++
- } else {
- this.PackJournal_ILA.Do(func() {
- GlobalLog.Write("Inconsistency: break at " + Int32ToStr(i) + "; characterKey='" + this.CharacterKey + "'")
- })
- break
- }
- }
- this.setRecordCount(tx, savedCount)
- }
- }
- func (this *TCharacterJournalStorage) GetRecordCount(tx *bolt.Tx) (result int32) {
- var key = this.getRecordCountKey()
- var data = tx.Bucket(this.GetBucketKey()).Get(key)
- if data != nil {
- TryStrToInt(string(data), &result)
- }
- return
- }
- func (this *TCharacterJournalStorage) getRecordCountKey() (result []byte) {
- var keyText = this.CharacterKey + CharacterStorageJournalLengthSubKey
- result = []byte(keyText)
- return
- }
- func (this *TCharacterJournalStorage) setRecordCount(tx *bolt.Tx, value int32) {
- tx.Bucket(this.GetBucketKey()).Put(
- this.getRecordCountKey(),
- Int32ToDataStr(value))
- }
- func (this *TCharacterJournalStorage) Read(desiredCount int32) (result [][]byte) {
- var transaction = this.CS.Begin(false); defer transaction.Rollback()
- var count = this.GetRecordCount(transaction)
- var startIndex int32 = 0
- if desiredCount < count {
- startIndex = count - desiredCount
- }
- var bucket = transaction.Bucket(this.GetBucketKey())
- for i := startIndex; i < count; i++ {
- var data = bucket.Get(this.getRecordKey(i))
- result = append(result, data)
- }
- return
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement