Exemple #1
0
        /// <summary>
        /// Method to add delete queue items for each schema association since we're not tracking
        /// revisions with schemas, we need to first remove the associations like groups/attribute defs and then re-add them
        /// with the up-to-date info.
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="manager"></param>
        /// <param name="transaction"></param>
        internal static void RemoveSchemaAssociations(EntitySchema schema, ExamineManager manager, ExamineTransaction transaction)
        {
            //find all associations previously added to the transaction queue
            transaction.RemoveSchemaAssociations(schema);

            //if there's no id already assigned to the schema, then we can't look it up
            if (schema.Id.IsNullValueOrEmpty())
            {
                return;
            }

            //lookup all associated: AttributeGroup, AttributeDefinition that already exist in the index
            var toRemove = manager.Search(
                manager.CreateSearchCriteria()
                .Must().EntityType <AttributeGroup>()
                .Must().HiveId(schema.Id, FixedIndexedFields.SchemaId).Compile())
                           .Concat(manager.Search(manager.CreateSearchCriteria()
                                                  .Must().EntityType <AttributeDefinition>()
                                                  .Must().HiveId(schema.Id, FixedIndexedFields.SchemaId).Compile()));

            foreach (var r in toRemove)
            {
                //need to copy to closure
                var r1 = r;
                transaction.EnqueueIndexOperation(new LinearHiveIndexOperation
                {
                    Id            = new Lazy <string>(() => r1.Id),
                    OperationType = IndexOperationType.Delete
                });
            }
        }
        /// <summary>
        /// Looks up a TypedEntity or EntitySchema Id in the queue or the index based on the id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="idsinQueue">The ids already in the queue</param>
        /// <returns></returns>
        private string GetItem(string id, IEnumerable <string> idsinQueue)
        {
            var foundInQueue = idsinQueue.SingleOrDefault(x => x == id);

            if (foundInQueue != null)
            {
                return(foundInQueue);
            }

            var typedEntity = ExamineManager.Search(
                ExamineManager.CreateSearchCriteria()
                .Must().EntityType <TypedEntity>()
                .Must().Id(id, FixedIndexedFields.EntityId).Compile());

            if (typedEntity.Any())
            {
                return(typedEntity.Last().Fields[FixedIndexedFields.EntityId]);
            }
            var schema = ExamineManager.Search(
                ExamineManager.CreateSearchCriteria()
                .Must().EntityType <EntitySchema>()
                .Must().Id(id).Compile());

            if (schema.Any())
            {
                return(schema.Last().Id);
            }
            return(null);
        }
        public override InstallStatus GetInstallStatus()
        {
            if (_installStatus != null)
            {
                return(_installStatus);
            }
            if (_existingConfig == null)
            {
                return(new InstallStatus(InstallStatusType.RequiresConfiguration));
            }

            var systemRoot = _examineManager.Search(
                _examineManager.CreateSearchCriteria().Must().HiveId(new HiveId("ExamineInstalled".EncodeAsGuid()))
                .Compile());

            return(!systemRoot.Any()
                ? new InstallStatus(InstallStatusType.Pending)
                : new InstallStatus(InstallStatusType.Completed));
        }
        /// <summary>
        /// Need to do a bit of cleanup now for Revision entries to ensure that we keep the records flagged as IsLatest to a minimum.
        /// To do this we'll lookup all revisions in the index that are marked as IsLatest that have Ids of revisions that we've just committed
        /// that have a UtcModified date of less than the ones we've just committed... thankfully, Lucene has a Range query that we can
        /// use to do this cleanup
        /// </summary>
        /// <param name="revisionsCommitted">The revisions committed during this transaction</param>
        private void CleanupIndexedRevisions(IEnumerable <Tuple <TypedEntity, RevisionData> > revisionsCommitted)
        {
            foreach (var r in revisionsCommitted)
            {
                //TODO: Figure out how Lucene is dealing with DateTime and UTC... currently were putting it back a day
                // but if we can do an Hour that would be better, just need to figure out what it is storing.
                var criteria = ExamineManager.CreateSearchCriteria()
                               .Must().EntityType <TypedEntity>()
                               .Must().HiveId(r.Item1.Id, FixedIndexedFields.EntityId)
                               .Must().Field(FixedRevisionIndexFields.IsLatest, "1".Escape())
                               .Must().Range(FixedIndexedFields.UtcModified, DateTime.MinValue, r.Item1.UtcModified.UtcDateTime.AddDays(-1));

                foreach (var i in ExamineManager.Search(criteria.Compile()))
                {
                    //convert the fields returned
                    var fields = i.Fields.ToDictionary(f => f.Key, f => new ItemField(f.Value));

                    //remove the flag
                    fields[FixedRevisionIndexFields.IsLatest] = new ItemField(0)
                    {
                        DataType = FieldDataType.Int
                    };

                    //now we need to update the index item to not be latest
                    var updateOp = new IndexOperation
                    {
                        Operation = IndexOperationType.Add,
                        Item      = new IndexItem
                        {
                            Fields       = fields,
                            Id           = i.Id,
                            ItemCategory = i.Fields[LuceneIndexer.IndexCategoryFieldName]
                        }
                    };

                    //do the update
                    ExamineManager.PerformIndexing(updateOp);
                }
            }
        }
        /// <summary>
        /// Determines if the status is changing for the entity passed in
        /// </summary>
        /// <param name="rev"></param>
        /// <returns></returns>
        private SearchResult GetLatestEntry <T>(Revision <T> rev)
            where T : class, IVersionableEntity
        {
            //get all latest versions of the typed entity
            var found = ExamineManager.Search(
                ExamineManager.CreateSearchCriteria()
                .Must().EntityType <TypedEntity>()
                .Must().HiveId(rev.Item.Id, FixedIndexedFields.EntityId)
                .Must().Range(FixedRevisionIndexFields.IsLatest, 1, 1)
                .Compile());

            //if nothing found, then its brand new
            if (!found.Any())
            {
                return(null);
            }

            //get the very latest version
            var latest = found.OrderBy(x => x.Fields[FixedIndexedFields.UtcModified]).Last();

            //return the latest entry
            return(latest);
        }