Skip to content

Commit c84b829

Browse files
author
Chris Elder
committed
FAB-2098 Add delete to ledger for CouchDB
Motivation for this change: Add DeleteDoc method to the CouchDB layer in order to provide delete by ID capability. This function mirrors the delete capability used by goloveldb. - Add delete to statecouchdb.go if value is set to null - Add unit tests for delete in statecouchdb_test.go Change-Id: I23b8e7e2daf8679dc0fbe8f1a75011530ff8d250 Signed-off-by: Chris Elder <[email protected]>
1 parent 7a09dfb commit c84b829

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb.go

+36-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. 2016, 2017 All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -261,45 +261,47 @@ func (vdb *VersionedDB) ApplyUpdates(batch *statedb.UpdateBatch, height *version
261261
logger.Debugf("Applying key=%#v, versionedValue=%s", compositeKey, versionedValueDump)
262262
}
263263

264-
// TODO add delete logic for couch using this approach from stateleveldb - convert nils to deletes
265-
/* if vv.Value == nil {
266-
levelBatch.Delete(compositeKey)
267-
} else {
268-
levelBatch.Put(compositeKey, encodeValue(vv.Value, vv.Version))
269-
}
270-
*/
264+
//convert nils to deletes
265+
if vv.Value == nil {
271266

272-
if couchdb.IsJSON(string(vv.Value)) {
267+
vdb.db.DeleteDoc(string(compositeKey), "")
273268

274-
// SaveDoc using couchdb client and use JSON format
275-
rev, err := vdb.db.SaveDoc(string(compositeKey), "", addVersionAndChainCodeID(vv.Value, ns, vv.Version), nil)
276-
if err != nil {
277-
logger.Errorf("Error during Commit(): %s\n", err.Error())
278-
return err
279-
}
280-
if rev != "" {
281-
logger.Debugf("Saved document revision number: %s\n", rev)
282-
}
269+
} else {
270+
271+
//Check to see if the value is a valid JSON
272+
//If this is not a valid JSON, then store as an attachment
273+
if couchdb.IsJSON(string(vv.Value)) {
274+
275+
// SaveDoc using couchdb client and use JSON format
276+
rev, err := vdb.db.SaveDoc(string(compositeKey), "", addVersionAndChainCodeID(vv.Value, ns, vv.Version), nil)
277+
if err != nil {
278+
logger.Errorf("Error during Commit(): %s\n", err.Error())
279+
return err
280+
}
281+
if rev != "" {
282+
logger.Debugf("Saved document revision number: %s\n", rev)
283+
}
283284

284-
} else { // if the data is not JSON, save as binary attachment in Couch
285+
} else { // if the data is not JSON, save as binary attachment in Couch
285286

286-
//Create an attachment structure and load the bytes
287-
attachment := &couchdb.Attachment{}
288-
attachment.AttachmentBytes = statedb.EncodeValue(vv.Value, vv.Version)
289-
attachment.ContentType = "application/octet-stream"
290-
attachment.Name = "valueBytes"
287+
//Create an attachment structure and load the bytes
288+
attachment := &couchdb.Attachment{}
289+
attachment.AttachmentBytes = statedb.EncodeValue(vv.Value, vv.Version)
290+
attachment.ContentType = "application/octet-stream"
291+
attachment.Name = "valueBytes"
291292

292-
attachments := []couchdb.Attachment{}
293-
attachments = append(attachments, *attachment)
293+
attachments := []couchdb.Attachment{}
294+
attachments = append(attachments, *attachment)
294295

295-
// SaveDoc using couchdb client and use attachment to persist the binary data
296-
rev, err := vdb.db.SaveDoc(string(compositeKey), "", addVersionAndChainCodeID(nil, ns, vv.Version), attachments)
297-
if err != nil {
298-
logger.Errorf("Error during Commit(): %s\n", err.Error())
299-
return err
300-
}
301-
if rev != "" {
302-
logger.Debugf("Saved document revision number: %s\n", rev)
296+
// SaveDoc using couchdb client and use attachment to persist the binary data
297+
rev, err := vdb.db.SaveDoc(string(compositeKey), "", addVersionAndChainCodeID(nil, ns, vv.Version), attachments)
298+
if err != nil {
299+
logger.Errorf("Error during Commit(): %s\n", err.Error())
300+
return err
301+
}
302+
if rev != "" {
303+
logger.Debugf("Saved document revision number: %s\n", rev)
304+
}
303305
}
304306
}
305307
}

core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. 2016, 2017 All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -64,14 +64,14 @@ func TestMultiDBBasicRW(t *testing.T) {
6464
}
6565
}
6666

67-
/* TODO add delete support in couchdb and then convert key value of nil to a couch delete. This will resolve TestDeletes
6867
func TestDeletes(t *testing.T) {
69-
env := NewTestVDBEnv(t)
70-
env.Cleanup("TestDB")
71-
defer env.Cleanup("TestDB")
72-
commontests.TestDeletes(t, env.DBProvider)
68+
if ledgerconfig.IsCouchDBEnabled() == true {
69+
env := NewTestVDBEnv(t)
70+
env.Cleanup("TestDB")
71+
defer env.Cleanup("TestDB")
72+
commontests.TestDeletes(t, env.DBProvider)
73+
}
7374
}
74-
*/
7575

7676
func TestIterator(t *testing.T) {
7777
if ledgerconfig.IsCouchDBEnabled() == true {

0 commit comments

Comments
 (0)