github.com/nutsdb/nutsdb@v1.0.4/pending.go (about) 1 package nutsdb 2 3 // EntryStatus represents the Entry status in the current Tx 4 type EntryStatus = uint8 5 6 const ( 7 // NotFoundEntry means there is no changes for this entry in current Tx 8 NotFoundEntry EntryStatus = 0 9 // EntryDeleted means this Entry has been deleted in the current Tx 10 EntryDeleted EntryStatus = 1 11 // EntryUpdated means this Entry has been updated in the current Tx 12 EntryUpdated EntryStatus = 2 13 ) 14 15 // BucketStatus represents the current status of bucket in current Tx 16 type BucketStatus = uint8 17 18 const ( 19 // BucketStatusExistAlready means this bucket already exists 20 BucketStatusExistAlready = 1 21 // BucketStatusDeleted means this bucket is already deleted 22 BucketStatusDeleted = 2 23 // BucketStatusNew means this bucket is created in current Tx 24 BucketStatusNew = 3 25 // BucketStatusUpdated means this bucket is updated in current Tx 26 BucketStatusUpdated = 4 27 // BucketStatusUnknown means this bucket doesn't exist 28 BucketStatusUnknown = 5 29 ) 30 31 // pendingBucketList the uncommitted bucket changes in this Tx 32 type pendingBucketList map[Ds]map[BucketName]*Bucket 33 34 // pendingEntriesInBTree means the changes Entries in DataStructureBTree in the Tx 35 type pendingEntriesInBTree map[BucketName]map[string]*Entry 36 37 // pendingEntryList the uncommitted Entry changes in this Tx 38 type pendingEntryList struct { 39 entriesInBTree pendingEntriesInBTree 40 entries map[Ds]map[BucketName][]*Entry 41 size int 42 } 43 44 // newPendingEntriesList create a new pendingEntryList object for a Tx 45 func newPendingEntriesList() *pendingEntryList { 46 pending := &pendingEntryList{ 47 entriesInBTree: map[BucketName]map[string]*Entry{}, 48 entries: map[Ds]map[BucketName][]*Entry{}, 49 size: 0, 50 } 51 return pending 52 } 53 54 // submitEntry submit an entry into pendingEntryList 55 func (pending *pendingEntryList) submitEntry(ds Ds, bucket string, e *Entry) { 56 switch ds { 57 case DataStructureBTree: 58 if _, exist := pending.entriesInBTree[bucket]; !exist { 59 pending.entriesInBTree[bucket] = map[string]*Entry{} 60 } 61 if _, exist := pending.entriesInBTree[bucket][string(e.Key)]; !exist { 62 pending.entriesInBTree[bucket][string(e.Key)] = e 63 pending.size++ 64 } 65 default: 66 if _, exist := pending.entries[ds]; !exist { 67 pending.entries[ds] = map[BucketName][]*Entry{} 68 } 69 entries := pending.entries[ds][bucket] 70 entries = append(entries, e) 71 pending.entries[ds][bucket] = entries 72 pending.size++ 73 } 74 } 75 76 // rangeBucket input a range handler function f and call it with every bucket in pendingBucketList 77 func (p pendingBucketList) rangeBucket(f func(bucket *Bucket) error) error { 78 for _, bucketsInDs := range p { 79 for _, bucket := range bucketsInDs { 80 err := f(bucket) 81 if err != nil { 82 return err 83 } 84 } 85 } 86 return nil 87 } 88 89 // toList collect all the entries in pendingEntryList to a list. 90 func (pending *pendingEntryList) toList() []*Entry { 91 list := make([]*Entry, 0, pending.size) 92 for _, entriesInBucket := range pending.entriesInBTree { 93 for _, entry := range entriesInBucket { 94 list = append(list, entry) 95 } 96 } 97 for _, entriesInDS := range pending.entries { 98 for _, entries := range entriesInDS { 99 for _, entry := range entries { 100 list = append(list, entry) 101 } 102 } 103 } 104 return list 105 }