@@ -160,7 +160,7 @@ func (store *CouchStorage) Init() common.SyncServiceError {
160
160
return err
161
161
}
162
162
if ! exists {
163
- client .CreateDB (context .TODO (), store .loginInfo ["dbName" ])
163
+ err := client .CreateDB (context .TODO (), store .loginInfo ["dbName" ])
164
164
if err != nil {
165
165
return err
166
166
}
@@ -316,9 +316,11 @@ func (store *CouchStorage) RetrieveObjectAndStatus(orgID string, objectType stri
316
316
return & result .MetaData , result .Status , nil
317
317
}
318
318
319
- // DeleteStoredObject deletes the object
319
+ // DeleteStoredObject deletes the object and any associated data
320
320
func (store * CouchStorage ) DeleteStoredObject (orgID string , objectType string , objectID string ) common.SyncServiceError {
321
321
id := createObjectCollectionID (orgID , objectType , objectID )
322
+ // In CouchDB, data is stored as attachment to object
323
+ // so data is deleted automatically when object is deleted
322
324
if err := store .deleteObject (id ); err != nil {
323
325
if err != notFound {
324
326
return err
@@ -512,6 +514,9 @@ func (store *CouchStorage) RetrieveUpdatedObjects(orgID string, objectType strin
512
514
}
513
515
514
516
if err := store .findAll (query , & result ); err != nil {
517
+ if err == notFound {
518
+ return nil , nil
519
+ }
515
520
return nil , & Error {fmt .Sprintf ("Failed to fetch the objects. Error: %s." , err )}
516
521
}
517
522
@@ -534,6 +539,9 @@ func (store *CouchStorage) RetrieveObjects(orgID string, destType string, destID
534
539
}}}
535
540
536
541
if err := store .findAll (query , & result ); err != nil {
542
+ if err == notFound {
543
+ return nil , nil
544
+ }
537
545
return nil , & Error {fmt .Sprintf ("Failed to fetch the objects. Error: %s." , err )}
538
546
}
539
547
@@ -714,7 +722,7 @@ func (store *CouchStorage) RetrieveDestinations(orgID string, destType string) (
714
722
err = store .findAll (query , & result )
715
723
}
716
724
}
717
- if err != nil {
725
+ if err != nil && err != notFound {
718
726
return nil , & Error {fmt .Sprintf ("Failed to fetch the destinations. Error: %s." , err )}
719
727
}
720
728
@@ -957,13 +965,13 @@ func (store *CouchStorage) ReadObjectData(orgID string, objectType string, objec
957
965
if err == notFound {
958
966
return nil , true , 0 , notFound
959
967
}
960
- return nil , true , 0 , err
968
+ return nil , true , 0 , & Error { fmt . Sprintf ( "Failed to get data. Error: %s." , err )}
961
969
}
962
970
963
971
data , err := ioutil .ReadAll (attachment .Content )
964
972
defer attachment .Content .Close ()
965
973
if err != nil {
966
- return nil , true , 0 , err
974
+ return nil , true , 0 , & Error { fmt . Sprintf ( "Failed to read the data. Error: %s." , err )}
967
975
}
968
976
969
977
lod := int64 (len (data ))
@@ -1001,11 +1009,21 @@ func (store *CouchStorage) StoreObjectData(orgID string, objectType string, obje
1001
1009
store .UpdateObjectStatus (orgID , objectType , objectID , common .ReadyToSend )
1002
1010
}
1003
1011
1004
- if result .Status == common .NotReadyToSend || result .Status == common .ReadyToSend {
1012
+ // need to get result object again because status has been updated
1013
+ updatedResult1 := & couchObject {}
1014
+ if err := store .getOne (id , updatedResult1 ); err != nil {
1015
+ return false , & Error {fmt .Sprintf ("Failed to store the data. Error: %s." , err )}
1016
+ }
1017
+
1018
+ if updatedResult1 .Status == common .ReadyToSend {
1005
1019
newID := store .getInstanceID ()
1006
- result .MetaData .DataID = newID
1007
- result .MetaData .InstanceID = newID
1008
- result .LastUpdate = time .Now ()
1020
+ updatedResult1 .MetaData .DataID = newID
1021
+ updatedResult1 .MetaData .InstanceID = newID
1022
+ updatedResult1 .LastUpdate = time .Now ()
1023
+ }
1024
+
1025
+ if err := store .upsertObject (id , updatedResult1 ); err != nil {
1026
+ return false , & Error {fmt .Sprintf ("Failed to update object. Error: %s." , err )}
1009
1027
}
1010
1028
1011
1029
size , err := store .addAttachment (id , dataReader )
@@ -1014,15 +1032,15 @@ func (store *CouchStorage) StoreObjectData(orgID string, objectType string, obje
1014
1032
}
1015
1033
1016
1034
// need to get result object again because attachment has been added
1017
- updatedResult := & couchObject {}
1018
- if err := store .getOne (id , updatedResult ); err != nil {
1035
+ updatedResult2 := & couchObject {}
1036
+ if err := store .getOne (id , updatedResult2 ); err != nil {
1019
1037
return false , & Error {fmt .Sprintf ("Failed to store the data. Error: %s." , err )}
1020
1038
}
1021
1039
1022
1040
// Update object size
1023
- updatedResult .MetaData .ObjectSize = size
1041
+ updatedResult2 .MetaData .ObjectSize = size
1024
1042
1025
- if err := store .upsertObject (id , updatedResult ); err != nil {
1043
+ if err := store .upsertObject (id , updatedResult2 ); err != nil {
1026
1044
return false , & Error {fmt .Sprintf ("Failed to update object's size. Error: %s." , err )}
1027
1045
}
1028
1046
@@ -1270,7 +1288,6 @@ func (store *CouchStorage) RemoveInactiveDestinations(lastTimestamp time.Time) {
1270
1288
}
1271
1289
1272
1290
// GetNumberOfDestinations returns the number of currently registered ESS nodes (for CSS)
1273
- // This function needs to be checked
1274
1291
func (store * CouchStorage ) GetNumberOfDestinations () (uint32 , common.SyncServiceError ) {
1275
1292
1276
1293
query := map [string ]interface {}{"selector" : map [string ]interface {}{
@@ -1455,7 +1472,6 @@ func (store *CouchStorage) RetrieveNotificationRecord(orgID string, objectType s
1455
1472
}
1456
1473
1457
1474
// DeleteNotificationRecords deletes notification records to an object
1458
- //very different
1459
1475
func (store * CouchStorage ) DeleteNotificationRecords (orgID string , objectType string , objectID string , destType string , destID string ) common.SyncServiceError {
1460
1476
var err error
1461
1477
if objectType != "" && objectID != "" {
@@ -1690,6 +1706,10 @@ func (store *CouchStorage) DeleteOrganization(orgID string) common.SyncServiceEr
1690
1706
return & Error {fmt .Sprintf ("Failed to delete objects. Error: %s." , err )}
1691
1707
}
1692
1708
1709
+ if err := store .deleteAllACLObjects (map [string ]interface {}{"selector" : map [string ]interface {}{"org-id" : orgID }}); err != nil && err != notFound {
1710
+ return & Error {fmt .Sprintf ("Failed to delete ACLs. Error: %s." , err )}
1711
+ }
1712
+
1693
1713
return nil
1694
1714
}
1695
1715
0 commit comments