/// <summary> /// Sets the data provider and data store of the adapter. /// </summary> /// <param name="provider">The data provider of adapter.</param> /// <param name="store">The data store to tie the adapter to, or [null] to automatically select store.</param> public Db4oDataReferencer(Db4oDataProvider provider, Db4oDataStore store) { Initialize(provider, store); }
/// <summary> /// Retrieves all the references to the specified entity. The specified entity can be either the source or reference entity as references work both ways. /// </summary> /// <param name="entityType">The type of the entity to retrieve the corresponding references for.</param> /// <param name="entityID">The ID of the entity to retrieve the corresponding references for.</param> /// <param name="propertyName">The name of the property on the specified entity that the reference corresponds with.</param> /// <param name="referenceType">The type of entity at the other side of the reference to the one specified.</param> /// <param name="activateAll">A value indicating whether to activate the references by loading the corresponding entities and setting them to the SourceEntity and ReferenceEntity properties.</param> /// <returns>A collection of references that match the provided parameters.</returns> public override EntityReferenceCollection GetReferences(Type entityType, Guid entityID, string propertyName, Type referenceType, bool activateAll) { EntityReferenceCollection collection = new EntityReferenceCollection(); using (LogGroup logGroup = LogGroup.StartDebug("Retrieving references.")) { if (entityType == null) { throw new ArgumentNullException("entityType"); } if (referenceType == null) { throw new ArgumentNullException("referenceType"); } LogWriter.Debug("Entity type: " + entityType.ToString()); LogWriter.Debug("Reference type: " + referenceType.ToString()); Db4oDataStore dataStore = (Db4oDataStore)GetDataStore(entityType.Name, referenceType.Name); if (dataStore.DoesExist) { EntityReferenceCollection list = new EntityReferenceCollection(); IQuery query1 = dataStore.ObjectContainer.Query(); query1.Constrain(typeof(EntityReference)); IConstraint constraint1 = query1.Descend("property1Name").Constrain(propertyName).Equal().And( query1.Descend("type1Name").Constrain(EntitiesUtilities.GetShortType(entityType.Name)).Equal().And( query1.Descend("type2Name").Constrain(EntitiesUtilities.GetShortType(referenceType.Name)).Equal())); if (entityID != Guid.Empty) { constraint1.And(query1.Descend("entity1ID").Constrain(entityID).Equal()); } IQuery query2 = dataStore.ObjectContainer.Query(); query2.Constrain(typeof(EntityReference)); IConstraint constraint2 = query2.Descend("property2Name").Constrain(propertyName).Equal().And( query2.Descend("type2Name").Constrain(EntitiesUtilities.GetShortType(entityType.Name)).Equal().And( query2.Descend("type1Name").Constrain(EntitiesUtilities.GetShortType(referenceType.Name)).Equal())); if (entityID != Guid.Empty) { constraint2.And(query2.Descend("entity2ID").Constrain(entityID).Equal()); } IObjectSet os1 = query1.Execute(); while (os1.HasNext()) { EntityReference reference = (EntityReference)os1.Next(); list.Add(reference); } IObjectSet os2 = query2.Execute(); while (os2.HasNext()) { EntityReference reference = (EntityReference)os2.Next(); list.Add(reference); } if (list.Count == 0) { LogWriter.Debug("No references loaded from the data store."); } else { LogWriter.Debug("Count: " + list.Count); foreach (EntityReference r in list) { using (LogGroup logGroup2 = LogGroup.StartDebug("Processing ID reference.")) { EntityReference reference = (EntityReference)r.SwitchFor(entityType, entityID); LogWriter.Debug("Loaded reference - Entity ID 1: " + reference.Entity1ID); LogWriter.Debug("Loaded reference - Entity ID 2: " + reference.Entity2ID); LogWriter.Debug("Loaded reference - Property 1 name: " + reference.Property1Name); LogWriter.Debug("Loaded reference - Property 2 name: " + reference.Property2Name); LogWriter.Debug("Loaded reference - Type name 1: " + reference.Type1Name); LogWriter.Debug("Loaded reference - Type name 2: " + reference.Type2Name); if (reference.Entity1ID != Guid.Empty && reference.Entity2ID != Guid.Empty) { // LogWriter.Debug("Adding to the collection."); collection.Add(reference); } else { LogWriter.Error("Reference not added to the collection. IDs are empty. This shouldn't happen but the system can ignore it and continue. Invalid references like these should probably be deleted."); } } } } } LogWriter.Debug("References #: " + collection.Count.ToString()); if (activateAll) { LogWriter.Debug("Activating references."); foreach (EntityReference reference in collection) { Provider.Activator.ActivateReference(reference); } } LogWriter.Debug("References #: " + collection.Count.ToString()); } return(collection); }
/// <summary> /// Retrieves a single page (at the specified location) of the specified type of entities from the corresponding data store. /// </summary> /// <param name="type">The type of entities to retrieve.</param> /// <param name="location">The coordinates of the page being retrieved.</param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the entities, including only the current page.</returns> public override IEntity[] GetPageOfEntities(Type type, PagingLocation location, string sortExpression) { // Create the collection List <IEntity> entities = new List <IEntity>(); using (LogGroup logGroup = LogGroup.Start("Retrieving a page of entities.", NLog.LogLevel.Debug)) { if (type == null) { throw new ArgumentNullException("type"); } if (location == null) { throw new ArgumentNullException("location"); } // Output the parameters to the log LogWriter.Debug("Type: " + type.ToString()); LogWriter.Debug("Page index: " + location.PageIndex); LogWriter.Debug("Page size: " + location.PageSize); LogWriter.Debug("Sort expression: " + sortExpression); // Get the corresponding data store Db4oDataStore store = (Db4oDataStore)GetDataStore(type); if (store == null) { throw new Exception("Can't find data store."); } if (store.ObjectContainer == null) { throw new Exception("store.ObjectContainer is null"); } // Create the query object IQuery query = store.ObjectContainer.Query(); // Constrain the query to the specified type query.Constrain(type); // Apply the sorting to the query ApplySorting(query, type, sortExpression); // Execute the query and get the object set IObjectSet os = query.Execute(); if (os == null) { throw new Exception("IObjectSet is null"); } int i = 0; // Loop through each index in the object set for (i = 0; i < os.Count; i++) { // ONLY execute during debug because it slows the indexing down if (new ModeDetector().IsDebug) { LogWriter.Debug("At absolute position " + i + ": " + ((IEntity)os[i]).ToString()); } // If it's not in the current page then skip it if (location.IsInPage(i)) { // Add the entity to the collection entities.Add((IEntity)os[i]); } } // Set the absolute total to the paging location // This is the total count including items on ALL pages, not just the current one location.AbsoluteTotal = i; LogWriter.Debug("Absolute count: " + location.AbsoluteTotal.ToString()); LogWriter.Debug("Entities count (single page): " + entities.Count.ToString()); } // Return the entities return(Release(entities.ToArray())); }
/// <summary> /// Checks whether there is a reference between the specified entities (in either order). /// </summary> /// <param name="entityType"></param> /// <param name="entityID"></param> /// <param name="propertyName"></param> /// <param name="referenceType"></param> /// <param name="referenceEntityID"></param> /// <param name="mirrorPropertyName"></param> /// <returns>A value indicating whether a matching entity was found.</returns> public override bool MatchReference(Type entityType, Guid entityID, string propertyName, Type referenceType, Guid referencedEntityID, string mirrorPropertyName) { bool isMatch = false; //using (LogGroup logGroup = LogGroup.StartDebug("Retrieving reference.")) //{ if (entityType == null) { throw new ArgumentNullException("entityType"); } if (referenceType == null) { throw new ArgumentNullException("referenceType"); } bool referenceFound = false; // LogWriter.Debug("Entity ID: " + entityID.ToString()); // LogWriter.Debug("Entity type: " + entityType.ToString()); // LogWriter.Debug("Reference entity ID: " + referencedEntityID.ToString()); // LogWriter.Debug("Reference type: " + referenceType.ToString()); // LogWriter.Debug("Property name: " + propertyName); // LogWriter.Debug("Mirror property name: " + mirrorPropertyName); Db4oDataStore dataStore = (Db4oDataStore)GetDataStore(entityType.Name, referenceType.Name); IQuery query1 = dataStore.ObjectContainer.Query(); query1.Constrain(typeof(EntityReference)); query1.Descend("entity1ID").Constrain(entityID).Equal().And( query1.Descend("property1Name").Constrain(propertyName).Equal().And( query1.Descend("type1Name").Constrain(EntitiesUtilities.GetShortType(entityType.Name)).Equal().And( query1.Descend("entity2ID").Constrain(referencedEntityID).Equal().And( query1.Descend("property2Name").Constrain(mirrorPropertyName).Equal().And( query1.Descend("type2Name").Constrain(EntitiesUtilities.GetShortType(referenceType.Name)).Equal()))))); referenceFound = query1.Execute().HasNext(); // If no reference was found from the first query try the opposite positions if (!referenceFound) { IQuery query2 = dataStore.ObjectContainer.Query(); query2.Constrain(typeof(EntityReference)); query2.Descend("entity2ID").Constrain(entityID).Equal().And( query2.Descend("property2Name").Constrain(propertyName).Equal().And( query2.Descend("type2Name").Constrain(EntitiesUtilities.GetShortType(entityType.Name)).Equal().And( query2.Descend("entity1ID").Constrain(referencedEntityID).Equal().And( query2.Descend("property1Name").Constrain(mirrorPropertyName).Equal().And( query2.Descend("type1Name").Constrain(EntitiesUtilities.GetShortType(referenceType.Name)).Equal()))))); referenceFound = query2.Execute().HasNext(); } isMatch = referenceFound; // LogWriter.Debug("Is match: " + isMatch.ToString()); //} return(isMatch); }
/// <summary> /// Retrieves all the entities of the specified type matching the specified values. /// </summary> /// <param name="type">The type of entity to retrieve.</param> /// <param name="parameters">The parameters to query with.</param> /// <returns></returns> public override IEntity[] GetEntities(Type type, Dictionary <string, object> parameters) { List <IEntity> entities = new List <IEntity>(); using (LogGroup logGroup = LogGroup.Start("Querying the data store based on the provided type and parameters.", NLog.LogLevel.Debug)) { if (type == null) { throw new ArgumentNullException("type"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } Db4oDataStore store = (Db4oDataStore)GetDataStore(type); if (store == null) { throw new Exception("No data store found."); } if (store.DoesExist) { entities = new List <IEntity>( store.ObjectContainer.Query <IEntity>( delegate(IEntity e) { LogWriter.Debug("Checking type " + e.GetType().ToString()); bool matches = true; foreach (string key in parameters.Keys) { LogWriter.Debug("Checking parameter '" + key + "' for value '" + parameters[key].ToString() + "'"); Type parameterType = parameters[key] != null ? parameters[key].GetType() : null; PropertyInfo property = e.GetType().GetProperty(key, parameterType); if (property == null) { throw new InvalidOperationException("The property '" + key + "' was not found on the type " + e.GetType().ToString()); } else { object value = property.GetValue(e, null); LogWriter.Debug("Actual value is: " + (value == null ? "null" : value.ToString())); if (parameters[key] != value && parameters[key] != null && !parameters[key].Equals(value)) { LogWriter.Debug("Parameter match failed for '" + key + "'."); matches = false; } } } LogWriter.Debug("Matches: " + matches.ToString()); return(matches); })); } LogWriter.Debug("Total: " + entities != null ? entities.Count.ToString() : 0.ToString()); } return(Release((IEntity[])(entities != null ? entities.ToArray() : new IEntity[] {}))); }
/// <summary> /// Retrieves all the entities of the specified type with a reference to any of the provided entities. /// </summary> /// <param name="propertyName">The name of the property containing the reference.</param> /// <param name="referencedEntities">An array of entities to check the reference to.</param> /// <returns>An array of the references retrieved.</returns> public override T[] GetEntitiesWithReference <T>(string propertyName, IEntity[] referencedEntities) { List <T> entities = null; using (LogGroup logGroup = LogGroup.StartDebug("Querying the data store based on the provided parameters.")) { LogWriter.Debug("Property name: " + propertyName); LogWriter.Debug("Referenced entities #: " + referencedEntities.Length); if (referencedEntities.Length > 0) { Type referencedEntityType = referencedEntities[0].GetType(); if (referencedEntityType != null) { LogWriter.Debug("Referenced entity type: " + referencedEntityType.ToString()); } else { LogWriter.Debug("Referenced entity type: [null]"); } Type type = typeof(T); EntityReferenceCollection references = new EntityReferenceCollection(); foreach (IEntity referencedEntity in referencedEntities) { // Load the references all in one go, to avoid individual loads references.AddRange(Provider.Referencer.GetReferences(referencedEntity.GetType(), referencedEntity.ID, typeof(T), false)); } Db4oDataStore store = (Db4oDataStore)GetDataStore(type); IObjectContainer container = store.ObjectContainer; entities = new List <T>( container .Query <T>( delegate(T e) { bool matches = true; using (LogGroup logGroup2 = LogGroup.Start("Querying entity.", NLog.LogLevel.Debug)) { LogWriter.Debug("Checking type " + e.GetType().ToString()); LogWriter.Debug("Entity ID: " + e.ID); bool foundReference = references.Includes(e.ID, propertyName); // If referenced entities were provided then entities match if a reference exists if (referencedEntities != null && referencedEntities.Length > 0) { matches = foundReference; } // Otherwise the calling code is trying to get entities where NO reference exists, therefore it matches when no reference is found else { matches = !foundReference; } LogWriter.Debug("Matches: " + matches); } return(matches); })); if (entities != null) { LogWriter.Debug("entities != null"); } else { LogWriter.Debug("entities == null"); } LogWriter.Debug("Total objects: " + entities.Count); } } return(Release(entities.ToArray())); }
/// <summary> /// Retrieves the specified page of entities from the data store. /// </summary> /// <param name="propertyName">The name of the property containing the reference.</param> /// <param name="referencedEntities">An array of entities to check the reference to.</param> /// <param name="location"></param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the objects retrieved.</returns> public override T[] GetPageOfEntitiesWithReference <T>(string propertyName, IEntity[] referencedEntities, PagingLocation location, string sortExpression) { if (referencedEntities.Length == 0) { return new T[] {} } ; Collection <T> page = new Collection <T>(); //using (LogGroup logGroup = LogGroup.Start("Querying the data store based on the provided parameters.", NLog.LogLevel.Debug)) //{ //LogWriter.Debug("Property name: " + propertyName); //LogWriter.Debug("Referenced entity ID: " + referencedEntityID); if (location == null) { throw new ArgumentNullException("location"); } Type type = typeof(T); string sortPropertyName = sortExpression.Replace("Ascending", "").Replace("Descending", ""); Db4oDataStore store = (Db4oDataStore)GetDataStore(type); if (store == null) { throw new ArgumentException("Can't find data store for type '" + type.Name + "'."); } IObjectContainer container = store.ObjectContainer; if (container == null) { throw new Exception("No object container for store '" + store.Name + "'."); } int i = 0; PropertyInfo property = typeof(T).GetType().GetProperty(propertyName); if (referencedEntities.Length > 0) { Type referencedEntityType = referencedEntities.GetType().GetElementType(); Predicate matches = new MatchReferencesPredicate(Provider, typeof(T), propertyName, referencedEntityType, Collection <IEntity> .GetIDs(referencedEntities)); IObjectSet os = store.ObjectContainer.Query(matches, new DynamicComparer( type, sortExpression)); page.AddRange(GetPage(os, location)); } //LogWriter.Debug("Absolute total objects: " + location.AbsoluteTotal); //} return(Release((T[])page.ToArray())); }
/// <summary> /// Sets the data provider and data store of the adapter. /// </summary> /// <param name="provider">The data provider of adapter.</param> /// <param name="store">The data store to tie the adapter to, or [null] to automatically select store.</param> public Db4oDataIndexer(Db4oDataProvider provider, Db4oDataStore store) { Initialize(provider, store); }
/// <summary> /// Sets the data provider and data store of the adapter. /// </summary> /// <param name="provider">The data provider of adapter.</param> /// <param name="store">The data store to tie the adapter to, or [null] to automatically select store.</param> public Db4oDataExporter(Db4oDataProvider provider, Db4oDataStore store) { Initialize(provider, store); }