Exemple #1
0
 protected void OnBeforeDelete(object s, BeforeIndexingEventArgs args)
 {
     if (BeforeDelete != null)
     {
         BeforeDelete(s, args);
     }
 }
Exemple #2
0
 public void Delete <T>(List <T> toDelete) where T : BaseModel
 {
     lock (write_lock)
     {
         try
         {
             if (!CanWrite)
             {
                 return;
             }
             var analyzer = PuckCache.AnalyzerForModel[typeof(T)];
             var parser   = new PuckQueryParser <T>(Lucene.Net.Util.LuceneVersion.LUCENE_48, FieldKeys.PuckDefaultField, analyzer);
             SetWriter(false);
             Writer.Flush(true, true);
             var cancelled = new List <BaseModel>();
             foreach (var m in toDelete)
             {
                 var args = new BeforeIndexingEventArgs()
                 {
                     Node = m, Cancel = false
                 };
                 OnBeforeDelete(this, args);
                 if (args.Cancel)
                 {
                     cancelled.Add(m);
                     continue;
                 }
                 string removeQuery = "+" + FieldKeys.ID + ":" + m.Id.ToString() + " +" + FieldKeys.Variant + ":" + m.Variant;
                 var    q           = parser.Parse(removeQuery);
                 Writer.DeleteDocuments(q);
             }
             Writer.Flush(true, true);
             Writer.Commit();
             toDelete
             .Where(x => !cancelled.Contains(x))
             .ToList()
             .ForEach(x => { OnAfterDelete(this, new IndexingEventArgs()
                 {
                     Node = x
                 }); });
         }
         catch (Exception ex)
         {
             throw;
             //logger.Log(ex);
         }
         finally
         {
             CloseWriter();
             SetSearcher();
         }
     }
 }
Exemple #3
0
        public bool Index <T>(List <T> models, bool triggerEvents = true, bool delete = true, bool queueIfBusy = false) where T : BaseModel
        {
            if (models == null || models.Count == 0)
            {
                return(true);
            }
            bool      taken           = false;
            Exception caughtException = null;

            try
            {
                var timeout = queueIfBusy ? TimeSpan.FromMilliseconds(0) : TimeSpan.FromMilliseconds(-1);
                Monitor.TryEnter(write_lock, timeout, ref taken);
                if (!taken && queueIfBusy)
                {
                    PuckCache.PublishQueue.Enqueue(models as List <BaseModel>);
                    return(false);
                }
                var cancelled = new List <BaseModel>();
                var count     = 1;

                if (!CanWrite)
                {
                    return(true);
                }
                SetWriter(false);
                //Writer.Flush(true, true, true);
                Parallel.ForEach(models, (m, state, index) => {
                    PuckCache.IndexingStatus = $"indexing item {count} of {models.Count}";
                    //var type = ApiHelper.GetType(m.Type);
                    //if (type == null)
                    //    type = typeof(BaseModel);
                    var type     = ApiHelper.GetTypeFromName(m.Type, defaultToBaseModel: true);
                    var analyzer = PuckCache.AnalyzerForModel[type];
                    var parser   = new PuckQueryParser <T>(Lucene.Net.Util.LuceneVersion.LUCENE_48, FieldKeys.PuckDefaultField, analyzer);
                    if (triggerEvents)
                    {
                        var args = new BeforeIndexingEventArgs()
                        {
                            Node = m, Cancel = false
                        };
                        OnBeforeIndex(this, args);
                        if (args.Cancel)
                        {
                            cancelled.Add(m);
                            return;
                        }
                    }
                    if (delete)
                    {
                        //delete doc
                        string removeQuery = "+" + FieldKeys.ID + ":" + m.Id.ToString() + " +" + FieldKeys.Variant + ":" + m.Variant.ToLower();
                        var q = parser.Parse(removeQuery);
                        Writer.DeleteDocuments(q);
                    }
                    Document doc = new Document();
                    //get fields to index
                    List <FlattenedObject> props = null;
                    using (MiniProfiler.Current.CustomTiming("get properties", ""))
                    {
                        props = ObjectDumper.Write(m, int.MaxValue);
                    }
                    using (MiniProfiler.Current.CustomTiming("add fields to doc", ""))
                    {
                        GetFieldSettings(props, doc, null);
                    }//add cms properties
                    if (!PuckCache.StoreReferences)
                    {
                        m.References = new List <string>();
                    }
                    string jsonDoc = JsonConvert.SerializeObject(m);
                    //doc in json form for deserialization later
                    doc.Add(new StringField(FieldKeys.PuckValue, jsonDoc, Field.Store.YES));
                    using (MiniProfiler.Current.CustomTiming("add document", ""))
                    {
                        Writer.AddDocument(doc, analyzer);
                    }
                    count++;
                });

                //Writer.Flush(true,true,true);
                using (MiniProfiler.Current.CustomTiming("commit", ""))
                {
                    Writer.Commit();
                }
                CloseWriter();
                SetSearcher();
                if (triggerEvents)
                {
                    models
                    .Where(x => !cancelled.Contains(x))
                    .ToList()
                    .ForEach(x => { OnAfterIndex(this, new IndexingEventArgs()
                        {
                            Node = x
                        }); });
                }
                if (!handleQueueStarted)
                {
                    handleQueueStarted = true;
                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(2000);
                        HandleIndexQueue();
                    });
                }
                //Optimize();
            }
            catch (Exception ex)
            {
                logger.Log(ex);
                caughtException = ex;
            }
            finally
            {
                if (taken)
                {
                    Monitor.Exit(write_lock);
                }
            }
            if (caughtException != null)
            {
                throw caughtException;
            }
            return(true);
        }
Exemple #4
0
 private static void DelegateBeforeDelete(object n, BeforeIndexingEventArgs e)
 {
     DelegateBeforeEvent(BeforeDeleteActionList, n, e);
 }
Exemple #5
0
        private static void DelegateBeforeEvent(Dictionary <string, Tuple <Type, Action <object, BeforeIndexingEventArgs>, bool> > list, object n, BeforeIndexingEventArgs e)
        {
            var type = e.Node.GetType();
            //refactor:can probably use is operator to implement event propagation
            var types = ApiHelper.BaseTypes(type);

            types.Add(type);
            list.Where(x => x.Value.Item1 == type || (x.Value.Item3 && types.Contains(x.Value.Item1)))
            .ToList().ForEach(x =>
            {
                x.Value.Item2(n, e);
            });
        }
        public void Index <T>(List <T> models, bool triggerEvents = true, bool delete = true) where T : BaseModel
        {
            if (models.Count == 0)
            {
                return;
            }
            lock (write_lock)
            {
                var cancelled = new List <BaseModel>();
                var count     = 1;
                try
                {
                    if (!CanWrite)
                    {
                        return;
                    }
                    SetWriter(false);
                    //Writer.Flush(true, true, true);
                    Parallel.ForEach(models, (m, state, index) => {
                        PuckCache.IndexingStatus = $"indexing item {count} of {models.Count}";
                        //var type = ApiHelper.GetType(m.Type);
                        //if (type == null)
                        //    type = typeof(BaseModel);
                        var type     = ApiHelper.GetTypeFromName(m.Type, defaultToBaseModel: true);
                        var analyzer = PuckCache.AnalyzerForModel[type];
                        var parser   = new PuckQueryParser <T>(Lucene.Net.Util.LuceneVersion.LUCENE_48, FieldKeys.PuckDefaultField, analyzer);
                        if (triggerEvents)
                        {
                            var args = new BeforeIndexingEventArgs()
                            {
                                Node = m, Cancel = false
                            };
                            OnBeforeIndex(this, args);
                            if (args.Cancel)
                            {
                                cancelled.Add(m);
                                return;
                            }
                        }
                        if (delete)
                        {
                            //delete doc
                            string removeQuery = "+" + FieldKeys.ID + ":" + m.Id.ToString() + " +" + FieldKeys.Variant + ":" + m.Variant.ToLower();
                            var q = parser.Parse(removeQuery);
                            Writer.DeleteDocuments(q);
                        }
                        Document doc = new Document();
                        //get fields to index
                        List <FlattenedObject> props = null;
                        using (MiniProfiler.Current.CustomTiming("get properties", ""))
                        {
                            props = ObjectDumper.Write(m, int.MaxValue);
                        }
                        using (MiniProfiler.Current.CustomTiming("add fields to doc", ""))
                        {
                            GetFieldSettings(props, doc, null);
                        }//add cms properties
                        if (!PuckCache.StoreReferences)
                        {
                            m.References = new List <string>();
                        }
                        string jsonDoc = JsonConvert.SerializeObject(m);
                        //doc in json form for deserialization later
                        doc.Add(new StringField(FieldKeys.PuckValue, jsonDoc, Field.Store.YES));
                        using (MiniProfiler.Current.CustomTiming("add document", ""))
                        {
                            Writer.AddDocument(doc, analyzer);
                        }
                        count++;
                    });

                    //Writer.Flush(true,true,true);
                    using (MiniProfiler.Current.CustomTiming("commit", ""))
                    {
                        Writer.Commit();
                    }
                    //Optimize();
                }
                catch (Exception ex)
                {
                    throw;
                    //logger.Log(ex);
                }
                finally
                {
                    CloseWriter();
                    SetSearcher();
                    if (triggerEvents)
                    {
                        models
                        .Where(x => !cancelled.Contains(x))
                        .ToList()
                        .ForEach(x => { OnAfterIndex(this, new IndexingEventArgs()
                            {
                                Node = x
                            }); });
                    }
                }
            }
        }