Exemple #1
0
        public async Task all()
        {
            var evs  = new List <int>();
            var seqs = new List <uint>();
            var ess  = EventStreamService <Ev, Sta> .StartNew(
                GetStream(),
                new[] { new Project() },
                null,
                new[]
            {
                Tuple.Create <EventStream <Ev> .Listener, uint>(
                    (e, s) =>
                {
                    evs.Add(e.Value);
                    seqs.Add(s);
                },
                    0u)
            },
                null,
                CancellationToken.None);

            await ess.Ready;

            CollectionAssert.AreEqual(new[] { 11, 25, 30 }, evs);
            CollectionAssert.AreEqual(new[] { 1u, 2u, 3u }, seqs);
            Assert.AreEqual(11 + 25 + 30, ess.LocalState.Sum);
        }
Exemple #2
0
        public async Task skip_for_projection()
        {
            var stream = GetStream();
            var imm    = new Testing.InMemoryCache();
            var ess1   = EventStreamService <Ev, Sta> .StartNew(
                stream,
                new[] { new Project() },
                imm,
                null,
                CancellationToken.None);

            await ess1.Ready;

            await ess1.TrySaveAsync();

            await ess1.AppendEventsAsync(new[] { new Ev {
                                                     Value = 41
                                                 } });

            var evs  = new List <int>();
            var seqs = new List <uint>();
            var ess  = EventStreamService <Ev, Sta> .StartNew(
                stream,
                new[] { new Project() },
                null,
                new[]
            {
                Tuple.Create <EventStream <Ev> .Listener, uint>(
                    (e, s) =>
                {
                    evs.Add(e.Value);
                    seqs.Add(s);
                },
                    2u)
            },
                null,
                CancellationToken.None);

            await ess.Ready;

            CollectionAssert.AreEqual(new[] { 25, 30, 41 }, evs);
            CollectionAssert.AreEqual(new[] { 2u, 3u, 4u }, seqs);
            Assert.AreEqual(11 + 25 + 30 + 41, ess.LocalState.Sum);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var config = new StorageConfiguration(ConnectionString);

            // The stream service owns a background process that we need
            // to be able to kill.
            var cts = new CancellationTokenSource();
            var svc = EventStreamService <IEvent, State> .StartNew(
                // This is where the events are stored
                storage : config,
                // These are the projections used to turn the event stream
                // into an up-to-date state (our example only has one projection)
                projections : new[] { new Projection() },
                // This is where we would write the projection snapshots, if
                // we had implemented them.
                projectionCache : null,
                // This is used by the service to emit messages about what is happening
                log : new Log(),
                // This cancellation token stops the background process.
                cancel : cts.Token);

            while (true)
            {
                var line = Console.ReadLine();
                if (line == null || line == "QUIT")
                {
                    break;
                }

                if (!svc.IsReady)
                {
                    // The service starts catching up with the stream. This may
                    // take a short while if the stream becomes very long.
                    Console.WriteLine("Service not ready, please wait.");
                    continue;
                }

                var delete = line.StartsWith("--");
                if (delete)
                {
                    line = line.Substring(2);
                }

                var words = line.Split(' ');
                foreach (var word in words)
                {
                    // Append either an update or a deletion event for each word in the
                    // provided sentence, then display the new count for that word.
                    var newCount = svc.AppendEventsAsync(s =>
                    {
                        // Look at the current count (and whether the word exists)
                        var exists = s.Bindings.TryGetValue(word, out int currentCount);

                        // Deleting the entry: only generate a deletion event
                        // if there was an entry present.
                        if (delete)
                        {
                            return(exists
                                ? svc.With(0, new ValueDeleted(word))
                                : svc.With(0));
                        }

                        // Incrementing the entry
                        return(svc.With(currentCount + 1, new ValueUpdated(word, currentCount + 1)));
                    }, CancellationToken.None).Result.More;

                    Console.WriteLine("{0} -> {1}", word, newCount);
                }
            }

            cts.Cancel();
        }
Exemple #4
0
        static async Task Main(string[] args)
        {
            var config = new StorageConfiguration(ConnectionString);

            // The stream service owns a background process that we need
            // to be able to kill.
            var cts = new CancellationTokenSource();
            var svc = EventStreamService <IEvent, State> .StartNew(
                // This is where the events are stored
                storage : config,
                // These are the projections used to turn the event stream
                // into an up-to-date state (our example only has one projection)
                projections : new[] { new Projection() },
                // This is where we would write the projection snapshots, if
                // we had implemented them.
                projectionCache : new MappedCacheProvider(LocalCache),
                // This is used by the service to emit messages about what is happening
                log : new Log(),
                // This cancellation token stops the background process.
                cancel : cts.Token);

            svc.RefreshPeriod = 10 * 60;

            // Once the stream is fully loaded, save it to cache.
            _ = svc.Ready.ContinueWith(_ => svc.TrySaveAsync(cts.Token));

            while (true)
            {
                if (svc.IsReady)
                {
                    Console.WriteLine("{0} words in index", svc.LocalState.Index.Count);
                }

                var line = Console.ReadLine();
                if (line == null || line == "QUIT")
                {
                    break;
                }

                if (!svc.IsReady)
                {
                    // The service starts catching up with the stream. This may
                    // take a short while if the stream becomes very long.
                    Console.WriteLine("Service not ready, please wait.");
                    continue;
                }

                if (line.StartsWith("file "))
                {
                    var    path = line.Substring("file ".Length);
                    string text;

                    try
                    {
                        text = File.ReadAllText(path);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error: {0}", e.Message);
                        continue;
                    }

                    if (text.Length > 65536)
                    {
                        text = text.Substring(0, 65536);
                    }

                    var id =
                        await svc.AppendEventsAsync(
                            state =>
                            svc.With(state.Documents.Count, new DocumentAdded(state.Documents.Count, path, text)),
                            default);

                    Console.WriteLine("Added as document {0}", id.More);

                    continue;
                }

                if (line.StartsWith("folder "))
                {
                    var dir = line.Substring("folder ".Length);

                    foreach (var path in Directory.GetFiles(dir))
                    {
                        var text = File.ReadAllText(path);

                        if (text.Length > 65536)
                        {
                            text = text.Substring(0, 65536);
                        }

                        var id =
                            await svc.AppendEventsAsync(
                                state =>
                                svc.With(state.Documents.Count, new DocumentAdded(state.Documents.Count, path, text)),
                                default);

                        Console.WriteLine("Added document {0} = {1}", id.More, path);
                    }

                    continue;
                }

                if (line.StartsWith("find "))
                {
                    var word = line.Substring("find ".Length).ToLower();

                    var state = await svc.CurrentState(default);