public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { Action actionToExecute = () => { PerformAddRelation(item); if (!ContextCacheAvailable()) { return; } Transaction.CacheFlushActions.Add(() => { HiveContext.GenerationScopedCache.RemoveWhereKeyMatches <HiveRelationCacheKey>(x => x.EntityId.Value == item.SourceId.Value || x.EntityId.Value == item.DestinationId.Value); }); }; FrameworkContext.TaskManager.ExecuteInCancellableTask( this, item, TaskTriggers.Hive.Relations.PreRelationAdded, TaskTriggers.Hive.Relations.PostRelationAdded, actionToExecute, x => new HiveRelationPreActionEventArgs(x, RepositoryScopedCache), x => new HiveRelationPostActionEventArgs(x, RepositoryScopedCache), FrameworkContext); }
protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation) { // Write relations to disc // TODO: Update to perform in the same manner as the NH provider i.e. in the base class by hooking AddOrUpdate if (relation.Type.RelationName != FixedRelationTypes.DefaultRelationType.RelationName) { EnsureRelationsFolderExists(); var sourceMd5 = relation.SourceId.Value.ToString().ToMd5(); var destMd5 = relation.DestinationId.Value.ToString().ToMd5(); var relationPath = Path.Combine(Settings.RelationsStoragePath, sourceMd5 + "-" + destMd5 + ".xml"); var relationFileInfo = new FileInfo(relationPath); var relationContent = RelationSerializer.ToXml(relation); var relationContentBytes = Encoding.UTF8.GetBytes(relationContent.ToString()); if (relationFileInfo.Exists) { relationFileInfo.Delete(); } using ( var writer = relationFileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { writer.Write(relationContentBytes, 0, relationContentBytes.Length); } } }
public void WhenSourceIsAccessed_LazyRelation_CallsRepository_WithBothSourceAndDestinationId() { // Arrange var context = new FakeFrameworkContext(); var providerGroup = GroupedProviderMockHelper.GenerateProviderGroup(1, 0, 50, context); var idRoot = new Uri("oh-yeah://this-is-my-root/"); var groupUnitFactory = new GroupUnitFactory(providerGroup.Writers, idRoot, FakeHiveCmsManager.CreateFakeRepositoryContext(context), context); HiveId int1 = HiveId.ConvertIntToGuid(1); HiveId int2 = HiveId.ConvertIntToGuid(2); // Act & Assert using (var uow = groupUnitFactory.Create()) { var lazyRelation = new LazyRelation <TypedEntity>(uow.Repositories, FixedRelationTypes.DefaultRelationType, int1, int2, 0); IReadonlyRelation <IRelatableEntity, IRelatableEntity> blah = lazyRelation; IRelationById blah2 = lazyRelation; Assert.False(lazyRelation.IsLoaded); var source = lazyRelation.Source; Assert.True(lazyRelation.IsLoaded); var dest = lazyRelation.Destination; Assert.NotNull(source); Assert.NotNull(dest); } }
public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { ExecuteInCancellableTask(item, TaskTriggers.Hive.Relations.PreRelationAdded, TaskTriggers.Hive.Relations.PostRelationAdded, () => ChildSessions.AddRelation(item, IdRoot)); }
/// <summary> /// Adds the relation. /// </summary> /// <param name="item">The item.</param> /// <param name="transaction"></param> public void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item, ExamineTransaction transaction) { Mandate.ParameterNotNull(item, "item"); var r = _frameworkContext.TypeMappers.MapToIntent <NestedHiveIndexOperation>(item); transaction.EnqueueIndexOperation(r); }
private static void CreateAndAddRelationTags(IReadonlyRelation <IRelatableEntity, IRelatableEntity> incomingRelation, NodeRelation dbRelation) { dbRelation.NodeRelationTags.Clear(); var newRelationMetadata = incomingRelation.MetaData.Select(x => new NodeRelationTag() { Name = x.Key, Value = x.Value, NodeRelation = dbRelation }); newRelationMetadata.ForEach(x => dbRelation.NodeRelationTags.Add(x)); }
public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { FrameworkContext.TaskManager.ExecuteInCancellableTask( this, item, TaskTriggers.Hive.Relations.PreRelationAdded, TaskTriggers.Hive.Relations.PostRelationAdded, () => PerformAddRelation(item), x => new HiveRelationPreActionEventArgs(x, RepositoryScopedCache), x => new HiveRelationPostActionEventArgs(x, RepositoryScopedCache), FrameworkContext); }
private static bool CompareOppositeOfRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> left, IReadonlyRelation <IRelatableEntity, IRelatableEntity> right) { return (((right.SourceId == left.DestinationId && !right.SourceId.IsNullValueOrEmpty()) || right.Source != null && right.Source == left.Destination) && ((right.DestinationId == left.SourceId && !right.DestinationId.IsNullValueOrEmpty()) || right.Destination != null && right.Destination == left.Source) && right.Type == left.Type); }
public static XDocument ToXml(IReadonlyRelation <IRelatableEntity, IRelatableEntity> model) { var relation = new XElement("relation", new XAttribute("type", model.Type.RelationName), new XAttribute("sourceId", model.SourceId.ToString(HiveIdFormatStyle.AsUri)), new XAttribute("destinationId", model.DestinationId.ToString(HiveIdFormatStyle.AsUri)), new XAttribute("ordinal", model.Ordinal)); foreach (var metaDatum in model.MetaData) { relation.Add(new XElement("metaDatum", new XAttribute("key", metaDatum.Key), new XAttribute("value", metaDatum.Value))); } return(new XDocument(relation)); }
public static XDocument ToXml(IReadonlyRelation<IRelatableEntity, IRelatableEntity> model) { var relation = new XElement("relation", new XAttribute("type", model.Type.RelationName), new XAttribute("sourceId", model.SourceId.ToString(HiveIdFormatStyle.AsUri)), new XAttribute("destinationId", model.DestinationId.ToString(HiveIdFormatStyle.AsUri)), new XAttribute("ordinal", model.Ordinal)); foreach(var metaDatum in model.MetaData) { relation.Add(new XElement("metaDatum", new XAttribute("key", metaDatum.Key), new XAttribute("value", metaDatum.Value))); } return new XDocument(relation); }
public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { Transaction.EnsureBegun(); Action actionToExecute = () => { PerformAddRelation(item); NotifyRelationCache(item); }; FrameworkContext.TaskManager.ExecuteInCancellableTask( this, item, TaskTriggers.Hive.Relations.PreRelationAdded, TaskTriggers.Hive.Relations.PostRelationAdded, actionToExecute, x => new HiveRelationPreActionEventArgs(x, RepositoryScopedCache), x => new HiveRelationPostActionEventArgs(x, RepositoryScopedCache), FrameworkContext); }
public bool EqualsIgnoringProviderId(IReadonlyRelation <IRelatableEntity, IRelatableEntity> other) { if (ReferenceEquals(other, null)) { return(false); } if (ReferenceEquals(other, this)) { return(true); } var objCast = other as Relation; if (ReferenceEquals(objCast, null)) { return(false); } var equals = true; if (Type != other.Type) { equals = false; } if (!SourceId.EqualsIgnoringProviderId(other.SourceId)) { equals = false; } if (!DestinationId.EqualsIgnoringProviderId(other.DestinationId)) { equals = false; } if (!MetaData.OrderBy(x => x.Key).SequenceEqual(objCast.MetaData.OrderBy(x => x.Key))) { equals = false; } return(equals); }
public override LazyDictionary <string, ItemField> GetValue(IReadonlyRelation <IRelatableEntity, IRelatableEntity> source) { var d = new LazyDictionary <string, ItemField> { { FixedRelationIndexFields.SourceId, new Lazy <ItemField>(() => new ItemField(source.SourceId.Value.ToString())) }, { FixedRelationIndexFields.DestinationId, new Lazy <ItemField>(() => new ItemField(source.DestinationId.Value.ToString())) }, { FixedRelationIndexFields.RelationType, new ItemField(source.Type.RelationName) }, //{FixedRelationIndexFields.RelationSourceType, new ItemField(source.Source.GetType().AssemblyQualifiedName) }, { FixedIndexedFields.Ordinal, new ItemField(source.Ordinal.ToString()) { DataType = FieldDataType.Int } } }; //store the metadata in the same document, but as keyed prefixed items like: // M.MyKey , MyValue foreach (var m in source.MetaData) { d.Add(FixedRelationIndexFields.MetadatumPrefix + m.Key, new ItemField(m.Value)); } return(d); }
public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { GetRelationsTable().Add(item); }
public HiveRelationPostActionEventArgs(IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation, AbstractScopedCache scopedCache) : base(scopedCache) { Relation = relation; }
public bool EqualsIgnoringProviderId(IReadonlyRelation <IRelatableEntity, IRelatableEntity> other) { return(base.EqualsIgnoringProviderId(other)); }
protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { _helper.CacheHelper.AddRelation(item); }
internal static void AddRelation(this IEnumerable <ICoreRelationsRepository> session, IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation, Uri idRoot) { foreach (var relationsRepository in session) { relationsRepository.AddRelation(relation); } GroupSessionHelper.MakeIdsAbsolute(relation, idRoot); }
internal static IReadonlyRelation <IRelatableEntity, IRelatableEntity> SetProviderAliasOnId(ProviderMetadata providerMetadata, IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation) { if (relation.Source != null) { SetProviderAliasOnId(providerMetadata, relation.Source); } if (relation.Destination != null) { SetProviderAliasOnId(providerMetadata, relation.Destination); } return(relation); }
/// <summary> /// Initializes a new instance of the <see cref="RelationProxy"/> class. /// </summary> /// <param name="newRelation">The new relation.</param> /// <param name="status">The status.</param> public RelationProxy(IReadonlyRelation <IRelatableEntity, IRelatableEntity> newRelation, RelationProxyStatus status) { Item = newRelation; Status = status; }
protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { Helper.PerformAddRelation(item, ExamineTransaction); }
public HiveRelationPostActionEventArgs(IReadonlyRelation<IRelatableEntity, IRelatableEntity> relation, AbstractScopedCache scopedCache) : base(scopedCache) { Relation = relation; }
protected abstract void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item);
protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { throw new NotImplementedException(); }
protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { return; }
protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { Helper.AddRelation(item, this.RepositoryScopedCache); }
/// <summary> /// Initializes a new instance of the <see cref="RelationProxy"/> class. /// </summary> /// <param name="newRelation">The new relation.</param> /// <param name="status">The status.</param> public RelationProxy(IReadonlyRelation<IRelatableEntity, IRelatableEntity> newRelation, RelationProxyStatus status) { Item = newRelation; Status = status; }
private void ExecuteInCancellableTask(IReadonlyRelation <IRelatableEntity, IRelatableEntity> entity, string preActionTaskTrigger, string postActionTaskTrigger, Action execution) { FrameworkContext.TaskManager.ExecuteInCancellableTask(this, entity, preActionTaskTrigger, postActionTaskTrigger, execution, x => new HiveRelationPreActionEventArgs(x, UnitScopedCache), x => new HiveRelationPostActionEventArgs(x, UnitScopedCache), FrameworkContext); }
public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item) { ChildSessions.AddRelation(item, IdRoot); }
public static IReadonlyRelation <IRelatableEntity, IRelatableEntity> MakeIdsAbsolute(IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation, Uri root) { if (relation == null) { return(null); } MakeIdsAbsolute(relation.Destination, root); MakeIdsAbsolute(relation.Source, root); return(relation); }
public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item, AbstractScopedCache repositoryScopedCache) { var sessionIdAsString = GetSessionId().ToString("n"); using (DisposableTimer.TraceDuration <NhSessionHelper>("In AddRelation for session " + sessionIdAsString, "End AddRelation for session " + sessionIdAsString)) { // Get the source and destination items from the Nh session var sourceNode = NhSession.Get <Node>((Guid)item.SourceId.Value); var destNode = NhSession.Get <Node>((Guid)item.DestinationId.Value); // Check the Nh session is already aware of the items if (sourceNode == null || destNode == null) { string extraMessage = string.Empty; if (sourceNode == null) { extraMessage = "Source {0} cannot be found.\n".InvariantFormat(item.SourceId.Value); } if (destNode == null) { extraMessage += "Destination {0} cannot be found.".InvariantFormat(item.DestinationId.Value); } throw new InvalidOperationException( "Before adding a relation between source {0} and destination {1}, you must call AddOrUpdate with those items or they must already exist in the datastore.\n{2}" .InvariantFormat(item.SourceId, item.DestinationId, extraMessage)); } // Try to load an existing relation of the same type between the two var relationType = GetOrCreateNodeRelationType(item.Type.RelationName, repositoryScopedCache); // Grab the existing relation (if exists) using the compound key of start node / end node / relation type var cacheKey = GenerateCacheKeyForRelation(item, relationType); NodeRelation relationToReturn = repositoryScopedCache.GetOrCreateTyped( cacheKey, () => { return(NhSession .QueryOver <NodeRelation>() .Where(x => x.StartNode == sourceNode && x.EndNode == destNode && x.NodeRelationType == relationType) .Cacheable() .SingleOrDefault()); }); // Avoid a duplicate by checking if one already exists if (relationToReturn != null) { // Make sure existing relation has ordinal relationToReturn.Ordinal = item.Ordinal; } else { // Create a new relation relationToReturn = new NodeRelation { StartNode = sourceNode, EndNode = destNode, NodeRelationType = relationType, Ordinal = item.Ordinal }; relationToReturn = NhSession.Merge(relationToReturn) as NodeRelation; } // Ensure metadata correct on existing or new entity CreateAndAddRelationTags(item, relationToReturn); } }
internal static IReadonlyRelation <IRelatableEntity, IRelatableEntity> SetRelationProxyLazyLoadDelegate(this IReadonlyProviderRelationsRepository providerSession, IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation) { if (relation.Source != null) { providerSession.SetRelationProxyLazyLoadDelegate(relation.Source); } if (relation.Destination != null) { providerSession.SetRelationProxyLazyLoadDelegate(relation.Destination); } return(relation); }