public Task AggregateRange(string tag, List <ArticleSummary> articles)
        {
            Task AggregateRange()
            {
                if (!State.ContainsKey(tag))
                {
                    State.Add(tag, new List <ArticleSummary>());
                }

                foreach (var article in articles)
                {
                    var index = State[tag].BinarySearch(article,
                                                        Comparer <ArticleSummary> .Create((summary, articleSummary) =>
                                                                                          DateTime.Compare(articleSummary.CreationDate, summary.CreationDate)));
                    if (index >= 0)
                    {
                        continue;
                    }
                    State[tag].Insert(~index, article);
                }
                State[tag].RemoveRange(100, int.MaxValue);
                return(Task.CompletedTask);
            }

            CommitChanges();
            return(SerialExecutor.AddNext(AggregateRange));
        }
        public Task SetRange(Immutable <IEnumerable <KeyValuePair <TKey, TValue> > > keyvalues)
        {
            Task Set()
            {
                foreach (var keyval in keyvalues.Value)
                {
                    State.Add(keyval.Key, keyval.Value);
                }
                return(Task.CompletedTask);
            }

            CommitChanges();
            return(SerialExecutor.AddNext(Set));
        }
        private Task Flush(object _)
        {
            var elapsed = DateTime.Now.Subtract(LastCommit).TotalSeconds;

            if (!Dirty || !(elapsed > 10) || IsSynchingSchedulded)
            {
                return(Task.CompletedTask);
            }

            LastCommit           = DateTime.UtcNow;
            Dirty                = false;
            IsSynchingSchedulded = true;
            return(SerialExecutor.AddNext(async() => {
                await WriteStateAsync();
                IsSynchingSchedulded = false;
            }));
        }
        public Task Set(Immutable <TKey> key, Immutable <TValue> value)
        {
            Task Set()
            {
                if (State.ContainsKey(key.Value))
                {
                    State[key.Value] = value.Value;
                }
                else
                {
                    State.Add(key.Value, value.Value);
                }
                return(Task.CompletedTask);
            }

            CommitChanges();
            return(SerialExecutor.AddNext(Set));
        }
Beispiel #5
0
        public Task AggregateRange(List <ArticleSummary> articles)
        {
            Task AggregateRange()
            {
                var index = State.BinarySearch(articles.First(),
                                               Comparer <ArticleSummary> .Create((summary, articleSummary) =>
                                                                                 DateTime.Compare(articleSummary.CreationDate, summary.CreationDate)));

                if (index < 0)
                {
                    State.InsertRange(~index, articles);
                }
                State.RemoveRange(100, int.MaxValue);
                return(Task.CompletedTask);
            }

            CommitChanges();
            return(SerialExecutor.AddNext(AggregateRange));
        }
Beispiel #6
0
        public Task CommitQuery(Immutable <QueryDefinition> queryDefinition)
        {
            /*
             *
             * newquery -n test -t "KeyValuePair<Guid, ArticleState>" -s "Articles.Where(pair => pair.Value.Title != null)" -a "Selected.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value).ToList()"
             * newquery -n test1k -t "KeyValuePair<Guid, ArticleState>" -s "Articles.Where(pair => pair.Value.Title != null)" -a "Selected.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value).Take(1000).ToList()"
             *
             * newquery -n test -r User -t "int" -s "Elements.Count()" -a "Selected.Sum()"
             * newquery -n test1k -r Article -t "IEnumerable<ArticleState>" -s "Elements.Where(article => article.Title != null)" -a "Selected.SelectMany(d => d).Take(1000).ToList()"
             *
             * newquery -n testActivities -r Article -t "IEnumerable<UserActivityState>" -s "Elements.Select(list => list.Where(state => state.User.Name.Contains("Iachin")))" -a "Selected.SelectMany(d => d).ToList()"
             *
             * query commit -n ActivitiesCount -r Activity -t int -s "Elements.Sum(e => e.Count())" -a "Selected.Sum()"
             * query commit -n CommentCount -r Activity -t int -s "Elements.Sum(e => e.Count(a => a.Type == UserActivityType.Commented))" -a "Selected.Sum()"
             *
             */

            switch (queryDefinition.Value.TargetRessource)
            {
            case "Article":
                queryDefinition.Value.TargetRessource = "ArticleState";
                break;

            case "User":
                queryDefinition.Value.TargetRessource = "UserState";
                break;

            case "Activity":
                queryDefinition.Value.TargetRessource = "List<UserActivityState>";
                break;

            default:
                throw new Exception("Unknown ressource type");
            }

            return(SerialExecutor.AddNext(() => {
                QueryEngine.CompileAndRegister(queryDefinition.Value);
                State.Add(queryDefinition.Value.Name, queryDefinition.Value);
                return WriteStateAsync();
            }));
        }
        public Task Aggregate(ArticleSummary article)
        {
            Task Aggregate()
            {
                Indexing = true;
                var doc = new Document {
                    new TextField("abstract", article.Abstract, Field.Store.YES),
                    new TextField("title", article.Title, Field.Store.YES),
                    new StringField("tag", article.Tags.First(), Field.Store.YES),
                    new TextField("author", article.Author.Name, Field.Store.YES),
                    new TextField("id", article.Id.ToString(), Field.Store.YES)
                };

                Writer.AddDocument(doc);
                Writer.Commit();
                Indexing = false;
                return(Task.CompletedTask);
            }

            CommitChanges();
            return(SerialExecutor.AddNext(Aggregate));
        }