public async Task <StoreResponse> Store(string bucketName, SiaqodbDocument document) { var db = client.GetDatabase(DbName); var collection = db.GetCollection <BsonDocument>(bucketName); var newDoc = Mapper.ToBsonDoc(document); newDoc["_rev"] = this.GenerateNewVersion(); try { var result = await collection.ReplaceOneAsync(filter : new BsonDocument { { "_id", document.Key }, { "_rev", BsonTypeMapper.MapToBsonValue(document.Version) } }, options : new UpdateOptions { IsUpsert = true }, replacement : newDoc); var cnorResponse = new StoreResponse(); cnorResponse.Version = newDoc["_rev"].AsString; cnorResponse.Key = document.Key; return(cnorResponse); } catch (MongoWriteException ex) { if (ex.Message.Contains("duplicate key"))//conflict { throw new ConflictException("There is a document with the same key and another version already stored"); } throw ex; } }
public async Task <BatchResponse> Store(string bucketName, BatchSet value) { var db = client.GetDatabase(DbName); var collection = db.GetCollection <BsonDocument>(bucketName); var response = new BatchResponse(); response.BatchItemResponses = new List <BatchItemResponse>(); SyncLogItem syncLogItem = new SyncLogItem(); syncLogItem.KeyVersion = new Dictionary <string, string>(); foreach (var document in value.ChangedDocuments) { var newDoc = Mapper.ToBsonDoc(document); newDoc["_rev"] = this.GenerateNewVersion(); try { var exists = await collection.Find(filter : new BsonDocument { { "_id", document.Key } }).FirstOrDefaultAsync(); if (exists == null && !string.IsNullOrEmpty(document.Version)) //somebody else deleted the doc-> conflict { BatchItemResponse respWithError = BuildResponseWithError(document.Key, document.Version, "conflict"); response.ItemsWithErrors++; response.BatchItemResponses.Add(respWithError); continue; } var result = await collection.ReplaceOneAsync(filter : new BsonDocument { { "_id", document.Key }, { "_rev", BsonTypeMapper.MapToBsonValue(document.Version) } }, options : new UpdateOptions { IsUpsert = true }, replacement : newDoc); BatchItemResponse itemResp = new BatchItemResponse { Key = document.Key, Version = newDoc["_rev"].AsString }; response.BatchItemResponses.Add(itemResp); syncLogItem.KeyVersion.Add(itemResp.Key, itemResp.Version); } catch (MongoWriteException ex) { string error = ex.Message; if (ex.Message.Contains("duplicate key"))//conflict { error = "conflict"; } var itemResp = BuildResponseWithError(document.Key, document.Version, error); response.ItemsWithErrors++; response.BatchItemResponses.Add(itemResp); } } foreach (var document in value.DeletedDocuments) { BatchItemResponse itemResp = new BatchItemResponse() { Key = document.Key, Version = document.Version }; var del = await collection.DeleteOneAsync(filter : new BsonDocument { { "_id", document.Key }, { "_rev", BsonTypeMapper.MapToBsonValue(document.Version) } }); if (del.DeletedCount == 0) { var docFromDB = collection.Find(filter: new BsonDocument { { "_id", document.Key } }).FirstOrDefaultAsync(); if (docFromDB != null) { itemResp.Error = "conflict"; itemResp.ErrorDesc = "conflict"; response.ItemsWithErrors++; } } response.BatchItemResponses.Add(itemResp); } //store uploadedAnchor if (syncLogItem.KeyVersion.Count > 0) { syncLogItem.TimeInserted = DateTime.UtcNow; BsonDocument syncLogDoc = new BsonDocument(); syncLogDoc["KeyVersion"] = new BsonDocument(syncLogItem.KeyVersion); syncLogDoc["TimeInserted"] = syncLogItem.TimeInserted; var collectionLog = db.GetCollection <BsonDocument>(SyncLogBucket); await collectionLog.InsertOneAsync(syncLogDoc); response.UploadAnchor = syncLogDoc["_id"].ToString(); } return(response); }