Exemplo n.º 1
0
// Commit implements method in interface `txmgmt.TxMgr`
func (txmgr *CouchDBTxMgr) Commit() error {
	logger.Debugf("===COUCHDB=== Entering CouchDBTxMgr.Commit()")

	if txmgr.updateSet == nil {
		panic("validateAndPrepare() method should have been called before calling commit()")
	}

	txmgr.commitRWLock.Lock()
	defer txmgr.commitRWLock.Unlock()
	defer func() { txmgr.updateSet = nil }()

	for k, v := range txmgr.updateSet.m {

		if couchdb.IsJSON(string(v.value)) {

			// SaveDoc using couchdb client and use JSON format
			rev, err := txmgr.couchDB.SaveDoc(k, "", v.value, nil)
			if err != nil {
				logger.Errorf("===COUCHDB=== Error during Commit(): %s\n", err.Error())
				return err
			}
			if rev != "" {
				logger.Debugf("===COUCHDB=== Saved document revision number: %s\n", rev)
			}

		} else {

			//Create an attachment structure and load the bytes
			attachment := &couchdb.Attachment{}
			attachment.AttachmentBytes = v.value
			attachment.ContentType = "application/octet-stream"
			attachment.Name = "valueBytes"

			attachments := []couchdb.Attachment{}
			attachments = append(attachments, *attachment)

			// SaveDoc using couchdb client and use attachment
			rev, err := txmgr.couchDB.SaveDoc(k, "", nil, attachments)
			if err != nil {
				logger.Errorf("===COUCHDB=== Error during Commit(): %s\n", err.Error())
				return err
			}
			if rev != "" {
				logger.Debugf("===COUCHDB=== Saved document revision number: %s\n", rev)
			}

		}

	}

	// Record a savepoint
	err := txmgr.recordSavepoint()
	if err != nil {
		logger.Errorf("===COUCHDB=== Error during recordSavepoint: %s\n", err.Error())
		return err
	}

	logger.Debugf("===COUCHDB=== Exiting CouchDBTxMgr.Commit()")
	return nil
}
Exemplo n.º 2
0
// Commit implements method in interface `histmgmt.HistMgr`
// This writes to a separate history database.
// TODO dpending on how invalid transactions are handled may need to filter what history commits.
func (histmgr *CouchDBHistMgr) Commit(block *common.Block) error {
	logger.Debugf("===HISTORYDB=== Entering CouchDBHistMgr.Commit()")

	//Get the blocknumber off of the header
	blockNo := block.Header.Number
	//Set the starting tranNo to 0
	var tranNo uint64

	logger.Debugf("===HISTORYDB=== Updating history for blockNo: %v with [%d] transactions",
		blockNo, len(block.Data.Data))
	for _, envBytes := range block.Data.Data {
		tranNo++
		logger.Debugf("===HISTORYDB=== Updating history for tranNo: %v", tranNo)

		// extract actions from the envelope message
		respPayload, err := putils.GetActionFromEnvelope(envBytes)
		if err != nil {
			return err
		}

		//preparation for extracting RWSet from transaction
		txRWSet := &rwset.TxReadWriteSet{}

		// Get the Result from the Action and then Unmarshal
		// it into a TxReadWriteSet using custom unmarshalling
		if err = txRWSet.Unmarshal(respPayload.Results); err != nil {
			return err
		}

		//Transactions that have data that is not JSON such as binary data,
		// the write value will not write to history database.
		//These types of transactions will have the key written to the history
		// database to support history key scans.  We do not write the binary
		// value to CouchDB since the purpose of the history database value is
		// for query andbinary data can not be queried.
		for _, nsRWSet := range txRWSet.NsRWs {
			ns := nsRWSet.NameSpace

			for _, kvWrite := range nsRWSet.Writes {
				writeKey := kvWrite.Key
				writeValue := kvWrite.Value
				compositeKey := constructCompositeKey(ns, writeKey, blockNo, tranNo)
				var bytesDoc []byte

				logger.Debugf("===HISTORYDB=== ns (namespace or cc id) = %v, writeKey: %v, compositeKey: %v, writeValue = %v",
					ns, writeKey, compositeKey, writeValue)

				if couchdb.IsJSON(string(writeValue)) {
					//logger.Debugf("===HISTORYDB=== yes JSON store writeValue = %v", string(writeValue))
					bytesDoc = writeValue
				} else {
					//For data that is not in JSON format only store the key
					//logger.Debugf("===HISTORYDB=== not JSON only store key")
					bytesDoc = []byte(`{}`)
				}

				// SaveDoc using couchdb client and use JSON format
				rev, err := histmgr.couchDB.SaveDoc(compositeKey, "", bytesDoc, nil)
				if err != nil {
					logger.Errorf("===HISTORYDB=== Error during Commit(): %s\n", err.Error())
					return err
				}
				if rev != "" {
					logger.Debugf("===HISTORYDB=== Saved document revision number: %s\n", rev)
				}

			}
		}

	}
	return nil
}