mirror of
https://github.com/pikami/cosmium.git
synced 2026-11-12 21:42:15 +00:00
Honor If-Match when deleting documents (#21)
Compare the document ETag and delete it in one datastore operation so a stale If-Match returns 412 and leaves the newer document in place. Co-authored-by: Cursor Agent <[email protected]>
This commit is contained in:
1 parent
af271092c3
commit
2fddaf7498
6 files changed
+185
-14
No files matched your search
@@ -45,6 +45,14 @@ func (r *JsonDataStore) GetDocument(databaseId string, collectionId string, docu
|
||||
}
|
||||
|
||||
func (r *JsonDataStore) DeleteDocument(databaseId string, collectionId string, documentId string) datastore.DataStoreStatus {
|
||||
return r.deleteDocument(databaseId, collectionId, documentId, "")
|
||||
}
|
||||
|
||||
func (r *JsonDataStore) DeleteDocumentIfMatch(databaseId string, collectionId string, documentId string, ifMatch string) datastore.DataStoreStatus {
|
||||
return r.deleteDocument(databaseId, collectionId, documentId, ifMatch)
|
||||
}
|
||||
|
||||
func (r *JsonDataStore) deleteDocument(databaseId string, collectionId string, documentId string, ifMatch string) datastore.DataStoreStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
@@ -56,10 +64,18 @@ func (r *JsonDataStore) DeleteDocument(databaseId string, collectionId string, d
|
||||
return datastore.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||
document, ok := r.storeState.Documents[databaseId][collectionId][documentId]
|
||||
if !ok {
|
||||
return datastore.StatusNotFound
|
||||
}
|
||||
|
||||
if ifMatch != "" {
|
||||
etag, _ := document["_etag"].(string)
|
||||
if etag != ifMatch {
|
||||
return datastore.PreconditionFailed
|
||||
}
|
||||
}
|
||||
|
||||
delete(r.storeState.Documents[databaseId][collectionId], documentId)
|
||||
|
||||
return datastore.StatusOk
|
||||
|
||||
Reference in new issue
Block a user