|
1 | 1 | /*
|
2 |
| -Copyright IBM Corp. 2016 All Rights Reserved. |
| 2 | +Copyright IBM Corp. 2016, 2017 All Rights Reserved. |
3 | 3 |
|
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | 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
|
261 | 261 | logger.Debugf("Applying key=%#v, versionedValue=%s", compositeKey, versionedValueDump)
|
262 | 262 | }
|
263 | 263 |
|
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 { |
271 | 266 |
|
272 |
| - if couchdb.IsJSON(string(vv.Value)) { |
| 267 | + vdb.db.DeleteDoc(string(compositeKey), "") |
273 | 268 |
|
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 | + } |
283 | 284 |
|
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 |
285 | 286 |
|
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" |
291 | 292 |
|
292 |
| - attachments := []couchdb.Attachment{} |
293 |
| - attachments = append(attachments, *attachment) |
| 293 | + attachments := []couchdb.Attachment{} |
| 294 | + attachments = append(attachments, *attachment) |
294 | 295 |
|
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 | + } |
303 | 305 | }
|
304 | 306 | }
|
305 | 307 | }
|
|
0 commit comments