github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/txmgmt/statedb/statecouchdb/version_cache.go (about)

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