Пример #1
0
        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);
        }
Пример #2
0
        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);
                }
            }
        }
Пример #3
0
        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);
            }
        }
Пример #4
0
 public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item)
 {
     ExecuteInCancellableTask(item,
                              TaskTriggers.Hive.Relations.PreRelationAdded,
                              TaskTriggers.Hive.Relations.PostRelationAdded,
                              () => ChildSessions.AddRelation(item, IdRoot));
 }
Пример #5
0
        /// <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);
        }
Пример #6
0
        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));
        }
Пример #10
0
        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);
        }
Пример #12
0
        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);
        }
Пример #13
0
        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);
        }
Пример #14
0
 public void AddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item)
 {
     GetRelationsTable().Add(item);
 }
 public HiveRelationPostActionEventArgs(IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation, AbstractScopedCache scopedCache)
     : base(scopedCache)
 {
     Relation = relation;
 }
Пример #16
0
 public bool EqualsIgnoringProviderId(IReadonlyRelation <IRelatableEntity, IRelatableEntity> other)
 {
     return(base.EqualsIgnoringProviderId(other));
 }
Пример #17
0
 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);
 }
Пример #19
0
 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);
 }
Пример #20
0
 /// <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;
 }
Пример #21
0
 protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item)
 {
     Helper.PerformAddRelation(item, ExamineTransaction);
 }
 public HiveRelationPostActionEventArgs(IReadonlyRelation<IRelatableEntity, IRelatableEntity> relation, AbstractScopedCache scopedCache)
     : base(scopedCache)
 {
     Relation = relation;
 }
Пример #23
0
 protected abstract void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item);
Пример #24
0
 protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item)
 {
     throw new NotImplementedException();
 }
 protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item)
 {
     return;
 }
Пример #26
0
 protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> item)
 {
     Helper.AddRelation(item, this.RepositoryScopedCache);
 }
Пример #27
0
 /// <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;
 }
Пример #28
0
 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);
 }
Пример #30
0
 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);
 }
Пример #31
0
        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);
            }
        }
Пример #32
0
 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);
 }