github.com/renegr87/renegr87@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/statedb/statecouchdb/version_cache.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package statecouchdb 8 9 import ( 10 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version" 11 ) 12 13 type versions map[string]nsVersions 14 type revisions map[string]nsRevisions 15 type nsRevisions map[string]string 16 type nsVersions map[string]*version.Height 17 18 // versionsCache contains maps of versions and revisions. 19 // Used as a local cache during bulk processing of a block. 20 // versions - contains the committed versions and used for state validation of readsets 21 // revisions - contains the committed revisions and used during commit phase for couchdb bulk updates 22 type versionsCache struct { 23 vers versions 24 revs revisions 25 } 26 27 func newVersionCache() *versionsCache { 28 return &versionsCache{make(versions), make(revisions)} 29 } 30 31 func (c *versionsCache) getVersion(ns, key string) (*version.Height, bool) { 32 ver, ok := c.vers[ns][key] 33 if ok { 34 return ver, true 35 } 36 return nil, false 37 } 38 39 // setVerAndRev sets the given version and couch revision into cache for given ns/key 40 // This function is invoked during bulk loading of versions for read-set validation. 41 // The revisions are not required for the validation but they are used during committing 42 // the write-sets to the couch. We load revisions as a bonus along with the versions during 43 // the bulkload in anticipation, because, in a typical workload, it is expected to be a good overlap 44 // between the read-set and the write-set. During the commit, we load missing revisions for 45 // any additional writes in the write-sets corresponding to which there were no reads in the read-sets 46 func (c *versionsCache) setVerAndRev(ns, key string, ver *version.Height, rev string) { 47 _, ok := c.vers[ns] 48 if !ok { 49 c.vers[ns] = make(nsVersions) 50 c.revs[ns] = make(nsRevisions) 51 } 52 c.vers[ns][key] = ver 53 c.revs[ns][key] = rev 54 }