Skip to content

Commit 189c9f1

Browse files
committed
Issue 47 - Resolved comments and added ACL cleanup
Signed-off-by: Anukriti Jain <[email protected]>
1 parent cce5bba commit 189c9f1

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

core/storage/couchStorage.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (store *CouchStorage) Init() common.SyncServiceError {
160160
return err
161161
}
162162
if !exists {
163-
client.CreateDB(context.TODO(), store.loginInfo["dbName"])
163+
err := client.CreateDB(context.TODO(), store.loginInfo["dbName"])
164164
if err != nil {
165165
return err
166166
}
@@ -316,9 +316,11 @@ func (store *CouchStorage) RetrieveObjectAndStatus(orgID string, objectType stri
316316
return &result.MetaData, result.Status, nil
317317
}
318318

319-
// DeleteStoredObject deletes the object
319+
// DeleteStoredObject deletes the object and any associated data
320320
func (store *CouchStorage) DeleteStoredObject(orgID string, objectType string, objectID string) common.SyncServiceError {
321321
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
322324
if err := store.deleteObject(id); err != nil {
323325
if err != notFound {
324326
return err
@@ -512,6 +514,9 @@ func (store *CouchStorage) RetrieveUpdatedObjects(orgID string, objectType strin
512514
}
513515

514516
if err := store.findAll(query, &result); err != nil {
517+
if err == notFound {
518+
return nil, nil
519+
}
515520
return nil, &Error{fmt.Sprintf("Failed to fetch the objects. Error: %s.", err)}
516521
}
517522

@@ -534,6 +539,9 @@ func (store *CouchStorage) RetrieveObjects(orgID string, destType string, destID
534539
}}}
535540

536541
if err := store.findAll(query, &result); err != nil {
542+
if err == notFound {
543+
return nil, nil
544+
}
537545
return nil, &Error{fmt.Sprintf("Failed to fetch the objects. Error: %s.", err)}
538546
}
539547

@@ -714,7 +722,7 @@ func (store *CouchStorage) RetrieveDestinations(orgID string, destType string) (
714722
err = store.findAll(query, &result)
715723
}
716724
}
717-
if err != nil {
725+
if err != nil && err != notFound {
718726
return nil, &Error{fmt.Sprintf("Failed to fetch the destinations. Error: %s.", err)}
719727
}
720728

@@ -957,13 +965,13 @@ func (store *CouchStorage) ReadObjectData(orgID string, objectType string, objec
957965
if err == notFound {
958966
return nil, true, 0, notFound
959967
}
960-
return nil, true, 0, err
968+
return nil, true, 0, &Error{fmt.Sprintf("Failed to get data. Error: %s.", err)}
961969
}
962970

963971
data, err := ioutil.ReadAll(attachment.Content)
964972
defer attachment.Content.Close()
965973
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)}
967975
}
968976

969977
lod := int64(len(data))
@@ -1001,11 +1009,21 @@ func (store *CouchStorage) StoreObjectData(orgID string, objectType string, obje
10011009
store.UpdateObjectStatus(orgID, objectType, objectID, common.ReadyToSend)
10021010
}
10031011

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 {
10051019
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)}
10091027
}
10101028

10111029
size, err := store.addAttachment(id, dataReader)
@@ -1014,15 +1032,15 @@ func (store *CouchStorage) StoreObjectData(orgID string, objectType string, obje
10141032
}
10151033

10161034
// 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 {
10191037
return false, &Error{fmt.Sprintf("Failed to store the data. Error: %s.", err)}
10201038
}
10211039

10221040
// Update object size
1023-
updatedResult.MetaData.ObjectSize = size
1041+
updatedResult2.MetaData.ObjectSize = size
10241042

1025-
if err := store.upsertObject(id, updatedResult); err != nil {
1043+
if err := store.upsertObject(id, updatedResult2); err != nil {
10261044
return false, &Error{fmt.Sprintf("Failed to update object's size. Error: %s.", err)}
10271045
}
10281046

@@ -1270,7 +1288,6 @@ func (store *CouchStorage) RemoveInactiveDestinations(lastTimestamp time.Time) {
12701288
}
12711289

12721290
// GetNumberOfDestinations returns the number of currently registered ESS nodes (for CSS)
1273-
// This function needs to be checked
12741291
func (store *CouchStorage) GetNumberOfDestinations() (uint32, common.SyncServiceError) {
12751292

12761293
query := map[string]interface{}{"selector": map[string]interface{}{
@@ -1455,7 +1472,6 @@ func (store *CouchStorage) RetrieveNotificationRecord(orgID string, objectType s
14551472
}
14561473

14571474
// DeleteNotificationRecords deletes notification records to an object
1458-
//very different
14591475
func (store *CouchStorage) DeleteNotificationRecords(orgID string, objectType string, objectID string, destType string, destID string) common.SyncServiceError {
14601476
var err error
14611477
if objectType != "" && objectID != "" {
@@ -1690,6 +1706,10 @@ func (store *CouchStorage) DeleteOrganization(orgID string) common.SyncServiceEr
16901706
return &Error{fmt.Sprintf("Failed to delete objects. Error: %s.", err)}
16911707
}
16921708

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+
16931713
return nil
16941714
}
16951715

core/storage/couchStorageHelpers.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,37 @@ func (store *CouchStorage) deleteAllNotificationObjects(query interface{}) commo
342342
return nil
343343
}
344344

345+
func (store *CouchStorage) deleteAllACLObjects(query interface{}) common.SyncServiceError {
346+
347+
db := store.client.DB(context.TODO(), store.loginInfo["dbName"])
348+
349+
rows, err := db.Find(context.TODO(), query)
350+
if err != nil {
351+
if kivik.StatusCode(err) == http.StatusNotFound {
352+
return notFound
353+
}
354+
return err
355+
}
356+
357+
for rows.Next() {
358+
359+
var tempObject couchACLObject
360+
err = rows.ScanDoc(&tempObject)
361+
if err != nil {
362+
return err
363+
}
364+
365+
if _, err := db.Delete(context.TODO(), tempObject.ID, tempObject.Rev); err != nil {
366+
return err
367+
}
368+
}
369+
370+
if err := rows.Err(); err != nil {
371+
return err
372+
}
373+
return nil
374+
}
375+
345376
func (store *CouchStorage) getAttachment(id string) (*kivik.Attachment, common.SyncServiceError) {
346377

347378
db := store.client.DB(context.TODO(), store.loginInfo["dbName"])

0 commit comments

Comments
 (0)