public void MongoDbUtility_GetEntityFilter_Can_Get_Id_Filter_From_Uri() { var collection = _provider.GetDatabase().GetCollection <Wellbore>(ObjectNames.Wellbore141); var wellbore141Uri = "eml://witsml14/well(well141)/wellbore(wellbore141)"; var filters141 = MongoDbUtility.GetEntityFilter <Wellbore>(new EtpUri(wellbore141Uri)); Assert.IsNotNull(filters141); var filters141Json = filters141.Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry); Assert.IsNotNull(filters141Json); Assert.AreEqual(2, filters141Json.ElementCount); var filterElements = filters141Json.Elements.ToList(); Assert.AreEqual(ObjectTypes.Uid, filterElements[0].Name); Assert.AreEqual("UidWell", filterElements[1].Name); var wellbore200Uri = "eml://witsml20/wellbore(wellbore200)"; var filters200 = MongoDbUtility.GetEntityFilter <Wellbore>(new EtpUri(wellbore200Uri), ObjectTypes.Uuid); var filters200Json = filters200.Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry); Assert.IsNotNull(filters200Json); Assert.AreEqual(1, filters200Json.ElementCount); Assert.AreEqual(ObjectTypes.Uuid, filters200Json.Elements.ToList()[0].Name); }
/// <summary> /// Partials the delete entity. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="dbCollectionName">Name of the database collection.</param> /// <param name="parser">The parser.</param> /// <param name="uri">The URI.</param> /// <exception cref="WitsmlException"></exception> protected void PartialDeleteEntity <TObject>(string dbCollectionName, WitsmlQueryParser parser, EtpUri uri) { try { Logger.DebugFormat("Partial Deleting {0} MongoDb collection", dbCollectionName); var collection = GetCollection <TObject>(dbCollectionName); var current = GetEntity <TObject>(uri, dbCollectionName); var updates = MongoDbUtility.CreateUpdateFields <TObject>(); var ignores = MongoDbUtility.CreateIgnoreFields <TObject>(GetIgnoredElementNamesForUpdate(parser)); var partialDelete = new MongoDbDelete <TObject>(Container, collection, parser, IdPropertyName, ignores); partialDelete.PartialDelete(current, uri, updates); var transaction = Transaction; if (transaction == null) { return; } transaction.Attach(MongoDbAction.Update, dbCollectionName, IdPropertyName, current.ToBsonDocument(), uri); transaction.Save(); } catch (MongoException ex) { Logger.ErrorFormat("Error partial deleting {0} MongoDb collection: {1}", dbCollectionName, ex); throw new WitsmlException(ErrorCodes.ErrorUpdatingInDataStore, ex); } }
public void MongoDbUtility_BuildPush_Can_Create_UpdateDefinition_For_Specified_Array_Field_With_Value() { var collection = _provider.GetDatabase().GetCollection <Log>(ObjectNames.Log141); AddParents(); DevKit.InitHeader(Log, LogIndexType.measureddepth); DevKit.AddAndAssert <LogList, Log>(Log); var addedLog = DevKit.GetAndAssert(Log); Assert.AreEqual(0, addedLog.LogCurveInfo.Count(l => l.Mnemonic.Value == "curve1")); var updateDefinition = MongoDbUtility.BuildPush <Log>(null, ObjectTypes.LogCurveInfo.ToPascalCase(), DevKit.CreateDoubleLogCurveInfo("curve1", "unit1")); Assert.IsNotNull(updateDefinition); collection.UpdateOne(Builders <Log> .Filter.Eq(ObjectTypes.Uid, Log.Uid), updateDefinition); var updatedLog = DevKit.GetAndAssert(Log); Assert.AreEqual(1, updatedLog.LogCurveInfo.Count(l => l.Mnemonic.Value == "curve1")); var updateDefinitionName = MongoDbUtility.BuildUpdate <Log>(null, ObjectTypes.NameProperty, Log.Name + "Updated"); Assert.AreEqual(0, addedLog.LogCurveInfo.Count(l => l.Mnemonic.Value == "curve2")); var updateDefinitionCurve2 = MongoDbUtility.BuildPush(updateDefinitionName, ObjectTypes.LogCurveInfo.ToPascalCase(), DevKit.CreateDoubleLogCurveInfo("curve2", "unit2")); Assert.IsNotNull(updateDefinitionCurve2); collection.UpdateMany(Builders <Log> .Filter.Eq(ObjectTypes.Uid, Log.Uid), updateDefinitionCurve2); var updatedLogCurve2 = DevKit.GetAndAssert(Log); Assert.AreEqual(1, updatedLogCurve2.LogCurveInfo.Count(l => l.Mnemonic.Value == "curve2")); Assert.AreEqual(Log.Name + "Updated", updatedLogCurve2.Name); }
public void MongoDbUtility_GetDocumentId_Returns_Id_In_BsonDocument_Format() { AddParents(); var bsonDoc = MongoDbUtility.GetDocumentId(Well); Assert.IsNotNull(bsonDoc); Assert.IsTrue(bsonDoc.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uid)).ToList().Count > 0); Assert.AreEqual(Well.Uid, bsonDoc.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uid)).ToList()[0].Value); var bsonDocWellbore = MongoDbUtility.GetDocumentId(Wellbore); Assert.IsNotNull(bsonDocWellbore); Assert.IsTrue(bsonDocWellbore.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uid)).ToList().Count > 0); Assert.AreEqual(Wellbore.Uid, bsonDocWellbore.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uid)).ToList()[0].Value); var bsonDocLog = MongoDbUtility.GetDocumentId(Log); Assert.IsNotNull(bsonDocLog); Assert.IsTrue(bsonDocLog.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uid)).ToList().Count > 0); Assert.AreEqual(Log.Uid, bsonDocLog.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uid)).ToList()[0].Value); var well200 = new Witsml200.Well { Uuid = DevKit.Uid(), Citation = new Witsml200.ComponentSchemas.Citation { Title = DevKit.Name("Well 01") }, TimeZone = DevKit.TimeZone }; var bsonDoc200 = MongoDbUtility.GetDocumentId(well200); Assert.IsTrue(bsonDoc200.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uuid)).ToList().Count > 0); Assert.AreEqual(well200.Uuid, bsonDoc200.Elements.Where(f => f.Name.EqualsIgnoreCase(ObjectTypes.Uuid)).ToList()[0].Value); }
public void MongoDbUtility_BuildUpdate_Can_Create_UpdateDefinition_For_Specified_Field() { var collection = _provider.GetDatabase().GetCollection <Well>(ObjectNames.Well141); AddParents(); var updateWellName = Well.Name + "Updated"; var updateDef = MongoDbUtility.BuildUpdate <Well>(null, ObjectTypes.NameProperty, updateWellName); Assert.IsNotNull(updateDef); collection.UpdateOne(Builders <Well> .Filter.Eq(ObjectTypes.Uid, Well.Uid), updateDef); var updatedWell = DevKit.GetAndAssert(Well); Assert.AreEqual(updateWellName, updatedWell.Name); Assert.IsNull(updatedWell.DateTimeLicense); var updateDefCombine = MongoDbUtility.BuildUpdate(updateDef, "DateTimeLicense", DateTime.UtcNow.ToString("o")); Assert.IsNotNull(updateDefCombine); collection.UpdateOne(Builders <Well> .Filter.Eq(ObjectTypes.Uid, Well.Uid), updateDefCombine); updateDefCombine = MongoDbUtility.BuildUpdate(updateDef, "DateTimeLicenseSpecified", true); Assert.IsNotNull(updateDefCombine); collection.UpdateOne(Builders <Well> .Filter.Eq(ObjectTypes.Uid, Well.Uid), updateDefCombine); updatedWell = DevKit.GetAndAssert(Well); Assert.IsNotNull(updatedWell.DateTimeLicense); Assert.AreEqual(updateWellName, updatedWell.Name); }
/// <summary> /// Deletes a data object by the specified identifier. /// </summary> /// <typeparam name="TObject">The type of data object.</typeparam> /// <param name="uri">The data object URI.</param> /// <param name="dbCollectionName">The name of the database collection.</param> /// <exception cref="WitsmlException"></exception> protected void DeleteEntity <TObject>(EtpUri uri, string dbCollectionName) { try { Logger.DebugFormat("Deleting from {0} MongoDb collection", dbCollectionName); var collection = GetCollection <TObject>(dbCollectionName); var current = GetEntity <TObject>(uri, dbCollectionName); if (current == null) { return; } var transaction = Transaction; if (transaction != null) { //var document = MongoDbUtility.GetDocumentId(current); transaction.Attach(MongoDbAction.Delete, dbCollectionName, IdPropertyName, null, uri); transaction.Save(); } else { var filter = MongoDbUtility.GetEntityFilter <TObject>(uri, IdPropertyName); collection.DeleteOne(filter); } } catch (MongoException ex) { Logger.ErrorFormat("Error deleting from {0} MongoDb collection: {1}", dbCollectionName, ex); throw new WitsmlException(ErrorCodes.ErrorDeletingFromDataStore, ex); } }
public void MongoDbUtility_BuildFilter_Returns_Filter_For_Specified_Field() { var collection = _provider.GetDatabase().GetCollection <Well>(ObjectNames.Well141); var filter = MongoDbUtility.BuildFilter <Well>(ObjectTypes.Uid, ObjectTypes.Uid); Assert.IsNotNull(filter); var filterJson = filter.Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry); Assert.IsNotNull(filterJson); Assert.AreEqual(1, filterJson.ElementCount); Assert.AreEqual(ObjectTypes.Uid, filterJson.Elements.ToList()[0].Name); filter = MongoDbUtility.BuildFilter <Well>(ObjectTypes.Uid, true); Assert.IsNotNull(filter); filterJson = filter.Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry); Assert.IsNotNull(filterJson); Assert.AreEqual(1, filterJson.ElementCount); Assert.AreEqual(ObjectTypes.Uid, filterJson.Elements.ToList()[0].Name); AddParents(); filter = MongoDbUtility.BuildFilter <Well>(ObjectTypes.Uid, Well.Uid); Assert.IsNotNull(filter); var result = collection.Find(filter).ToList(); Assert.IsTrue(result.Count == 1); }
public void MongoDbUtility_LookUpIdField_Returns_IdField_For_Specified_Type() { var well141 = MongoDbUtility.LookUpIdField(typeof(Well)); Assert.AreEqual(ObjectTypes.Uid, well141); var well200 = MongoDbUtility.LookUpIdField(typeof(Witsml200.Well), ObjectTypes.Uuid); Assert.AreEqual(ObjectTypes.Uuid, well200); var well200ChannelIndex = MongoDbUtility.LookUpIdField(typeof(Witsml200.ComponentSchemas.ChannelIndex)); Assert.AreEqual("Mnemonic", well200ChannelIndex); }
public void MongoDbUtility_BuildFilter_Can_Create_Filter_For_Specified_Field() { var collection = _provider.GetDatabase().GetCollection <Well>(ObjectNames.Well141); AddParents(); var filter = MongoDbUtility.BuildFilter <Well>(ObjectTypes.Uid, Well.Uid); Assert.IsNotNull(filter); var result = collection.Find(filter).ToList(); Assert.IsTrue(result.Count == 1); Assert.AreEqual(Well.Name, result[0].Name); }
public void MongoDbUtility_CreateUpdateFields_Returns_Dictionary_Of_common_Objects_For_Update() { var commonPropertiesToUpdate = MongoDbUtility.CreateUpdateFields <Well>(); Assert.AreEqual(1, commonPropertiesToUpdate.Count); Assert.IsNotNull(commonPropertiesToUpdate["CommonData.DateTimeLastChange"]); commonPropertiesToUpdate = MongoDbUtility.CreateUpdateFields <Witsml200.Well>(); Assert.AreEqual(1, commonPropertiesToUpdate.Count); Assert.IsNotNull(commonPropertiesToUpdate["Citation.LastUpdate"]); commonPropertiesToUpdate = MongoDbUtility.CreateUpdateFields <DateTime>(); Assert.AreEqual(0, commonPropertiesToUpdate.Count); }
/// <summary> /// Updates the specified entity. /// </summary> /// <param name="entity">The entity.</param> /// <param name="uri">The URI.</param> /// <param name="updates">The updates.</param> public void Update(T entity, EtpUri uri, Dictionary <string, object> updates) { Logger.DebugFormat($"Updating data object: {uri}"); _entityFilter = MongoDbUtility.GetEntityFilter <T>(uri, IdPropertyName); Entity = entity; Context.Update = Update(updates, uri.ObjectId); BuildUpdate(Parser.Element()); LogUpdateFilter(_entityFilter, Context.Update); Collection.UpdateOne(_entityFilter, Context.Update); WitsmlOperationContext.Current.Warnings.AddRange(Context.Warnings); }
public void MongoDbUtility_GetObjectUris_Returns_List_Of_Uri_By_ObjectType() { var listUri = new List <EtpUri> { new EtpUri("eml://witsml14/well(well141)"), new EtpUri("eml://witsml14/well(well141Another)"), new EtpUri("eml://witsml14/well(well141)/wellbore(wellbore141)"), new EtpUri("eml://witsml20/wellbore(wellbore200)") }; var objUri = MongoDbUtility.GetObjectUris(listUri, ObjectTypes.Well); Assert.AreEqual(2, objUri.Count); var objUriWellbore = MongoDbUtility.GetObjectUris(listUri, ObjectTypes.Wellbore); Assert.AreEqual(2, objUriWellbore.Count); }
public void MongoDbUtility_CreateIgnoreFields_Returns_List_Of_common_Objects_To_Ignore() { var commonPropertiesToIgnore = MongoDbUtility.CreateIgnoreFields <Well>(null); Assert.IsTrue(commonPropertiesToIgnore.Count > 0); commonPropertiesToIgnore = MongoDbUtility.CreateIgnoreFields <Witsml200.Well>(null); Assert.IsTrue(commonPropertiesToIgnore.Count > 0); var ignoredList = new List <string> { "dTimLicense" }; commonPropertiesToIgnore = MongoDbUtility.CreateIgnoreFields <Well>(ignoredList); Assert.IsTrue(commonPropertiesToIgnore.Count > 1); Assert.IsTrue(commonPropertiesToIgnore.Contains("dTimLicense")); }
/// <summary> /// Executes partial delete. /// </summary> /// <param name="entity">The entity.</param> /// <param name="uri">The URI.</param> /// <param name="updates">The updates.</param> public void PartialDelete(T entity, EtpUri uri, Dictionary <string, object> updates) { Logger.DebugFormat($"Partial deleting data object: {uri}"); _entityFilter = MongoDbUtility.GetEntityFilter <T>(uri, _idPropertyName); _entity = entity; Context.Update = Update(updates, uri.ObjectId); BuildPartialDelete(_parser.Element()); LogUpdateFilter(_entityFilter, Context.Update); _collection.UpdateOne(_entityFilter, Context.Update); // Remove recurring elements after all update because of the position being used in field path RemoveArrayElementsByDepth(); WitsmlOperationContext.Current.Warnings.AddRange(Context.Warnings); }
/// <summary> /// Gets the entities having the supplied URIs found in the specified collection. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="uris">The uris.</param> /// <param name="dbCollectionName">Name of the database collection.</param> /// <returns>The query results.</returns> protected List <TObject> GetEntities <TObject>(IEnumerable <EtpUri> uris, string dbCollectionName) { var list = uris.ToList(); Logger.DebugFormat("Querying {0} MongoDb collection by URIs: {1}{2}", dbCollectionName, Environment.NewLine, Logger.IsDebugEnabled ? string.Join(Environment.NewLine, list) : null); if (!list.Any()) { return(GetCollection <TObject>(dbCollectionName) .Find("{}") .ToList()); } var filters = list.Select(x => MongoDbUtility.GetEntityFilter <TObject>(x, IdPropertyName)); return(GetCollection <TObject>(dbCollectionName) .Find(Builders <TObject> .Filter.Or(filters)) .ToList()); }
private void PartialDeleteArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath) { Logger.DebugFormat($"Partial deleting array elements: {parentPath} {propertyInfo?.Name}"); var updateBuilder = Builders <T> .Update; var filterBuilder = Builders <T> .Filter; var idField = MongoDbUtility.LookUpIdField(type); var filterPath = GetPropertyPath(parentPath, idField); var properties = GetPropertyInfo(type); var ids = new List <string>(); var itemsById = GetItemsById((IEnumerable)propertyValue, properties, idField, ids); var updateList = elements .Select(element => { var elementId = GetElementId(element, idField); if (string.IsNullOrEmpty(elementId) || propertyInfo == null) { return(null); } var filters = new List <FilterDefinition <T> >() { _entityFilter }; object current; itemsById.TryGetValue(elementId, out current); if (current == null) { return(null); } var elementFilter = Builders <T> .Filter.EqIgnoreCase(filterPath, elementId); filters.Add(elementFilter); var filter = filterBuilder.And(filters); if (element.Elements().Any()) { var position = ids.IndexOf(elementId); var positionPath = parentPath + "." + position; var update = updateBuilder.Set(GetPropertyPath(positionPath, idField), elementId); var saveUpdate = Context.Update; Context.Update = update; PushPropertyInfo(propertyInfo, current); NavigateElement(element, type, positionPath); PopPropertyInfo(); var model = new UpdateOneModel <T>(filter, Context.Update); Context.Update = saveUpdate; return(model); } else { if (IsRequired(propertyInfo) && itemsById.Count == 1) { throw new WitsmlException(ErrorCodes.MustRetainOneRecurringNode); } var childFilter = MongoDbExtensions.EqualsIgnoreCase(type, idField, elementId); var update = MongoDbExtensions.PullFilter(typeof(T), type, parentPath, childFilter) as UpdateDefinition <T>; if (childFilter != null && update != null) { //var update = updateBuilder.Pull(parentPath, current); AddToPullCollection(parentPath, new UpdateOneModel <T>(filter, update)); } return(null); } }) .Where(x => x != null) .ToList(); if (updateList.Count > 0) { _collection.BulkWrite(updateList); } }
/// <summary> /// Updates the array elements. /// </summary> /// <param name="elements">The elements.</param> /// <param name="propertyInfo">The property information.</param> /// <param name="propertyValue">The property value.</param> /// <param name="type">The type.</param> /// <param name="parentPath">The parent path.</param> protected override void UpdateArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath) { Logger.DebugFormat($"Merge array elements: {parentPath} {propertyInfo?.Name}"); if (MergeDelete && RemoveAll(elements)) { var list = propertyValue as IList; list?.Clear(); return; } var idField = MongoDbUtility.LookUpIdField(type); var properties = GetPropertyInfo(type); var ids = new List <string>(); var itemsById = GetItemsById((IEnumerable)propertyValue, properties, idField, ids); foreach (var element in elements) { var elementId = GetElementId(element, idField); if (string.IsNullOrEmpty(elementId) || propertyInfo == null) { continue; } object current; itemsById.TryGetValue(elementId, out current); if (current == null) { if (MergeDelete) { continue; } ValidateArrayElement(element, properties); ValidateArrayElement(propertyInfo, type, element, parentPath); if (Context.ValidationOnly) { continue; } var item = ParseNestedElement(type, element); if (propertyValue == null) { continue; } var list = propertyValue as IList; list?.Add(item); } else { if (MergeDelete && RemoveItem(element)) { var list = propertyValue as IList; list?.Remove(current); } else { var position = ids.IndexOf(elementId); var positionPath = parentPath + "." + position; ValidateArrayElement(element, properties, false); PushPropertyInfo(propertyInfo, current); NavigateElement(element, type, positionPath); PopPropertyInfo(); } } } }
/// <summary> /// Updates the array elements. /// </summary> /// <param name="elements">The elements.</param> /// <param name="propertyInfo">The property information.</param> /// <param name="propertyValue">The property value.</param> /// <param name="type">The type.</param> /// <param name="parentPath">The parent path.</param> protected override void UpdateArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath) { Logger.DebugFormat($"Updating array elements: {parentPath} {propertyInfo?.Name}"); var updateBuilder = Builders <T> .Update; var filterBuilder = Builders <T> .Filter; var idField = MongoDbUtility.LookUpIdField(type); var filterPath = GetPropertyPath(parentPath, idField); var properties = GetPropertyInfo(type); var ids = new List <string>(); var itemsById = GetItemsById((IEnumerable)propertyValue, properties, idField, ids); var updateList = elements .Select(element => { var elementId = GetElementId(element, idField); if (string.IsNullOrEmpty(elementId) || propertyInfo == null) { return(null); } var filters = new List <FilterDefinition <T> >() { _entityFilter }; object current; itemsById.TryGetValue(elementId, out current); if (current == null) { ValidateArrayElement(element, properties); ValidateArrayElement(propertyInfo, type, element, parentPath); if (Context.ValidationOnly) { return(null); } var item = ParseNestedElement(type, element); var filter = filterBuilder.And(filters); var update = propertyValue == null ? updateBuilder.Set(parentPath, CreateList(propertyInfo.PropertyType, item)) : updateBuilder.Push(parentPath, item); return(new UpdateOneModel <T>(filter, update)); } else { var position = ids.IndexOf(elementId); var positionPath = parentPath + "." + position; ValidateArrayElement(element, properties, false); var elementFilter = Builders <T> .Filter.EqIgnoreCase(filterPath, elementId); filters.Add(elementFilter); var filter = filterBuilder.And(filters); var update = updateBuilder.Set(GetPropertyPath(positionPath, idField), elementId); var saveUpdate = Context.Update; Context.Update = update; PushPropertyInfo(propertyInfo, current); NavigateElement(element, type, positionPath); PopPropertyInfo(); var model = new UpdateOneModel <T>(filter, Context.Update); Context.Update = saveUpdate; return(model); } }) .Where(x => x != null) .ToList(); if (updateList.Count > 0) { Collection.BulkWrite(updateList); } }
/// <summary> /// Gets the entity filter for the specified URI. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="uri">The URI.</param> /// <param name="idPropertyName">Name of the identifier property.</param> /// <returns>The entity filter.</returns> protected virtual FilterDefinition <TObject> GetEntityFilter <TObject>(EtpUri uri, string idPropertyName) { return(MongoDbUtility.GetEntityFilter <TObject>(uri, idPropertyName)); }