<commit_before>﻿using Aggregates.Contracts;
using Aggregates.Exceptions;
using Aggregates.Extensions;
using EventStore.ClientAPI;
using EventStore.ClientAPI.Exceptions;
using Newtonsoft.Json;
using NServiceBus.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aggregates.Internal
{
    public class EventStream<T> : IEventStream where T : class, IEntity
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(EventStream<T>));
        public String Bucket { get; private set; }
        public String StreamId { get; private set; }
        public Int32 StreamVersion { get { return this._streamVersion + this._uncommitted.Count; } }
        public Int32 CommitVersion { get { return this._streamVersion; } }

        public IEnumerable<IWritableEvent> Events
        {
            get
            {
                return this._committed.Concat(this._uncommitted);
            }
        }

        private readonly IStoreEvents _store;
        private readonly Int32 _streamVersion;
        private Int32 _version;
        private IEnumerable<WritableEvent> _committed;
        private IList<WritableEvent> _uncommitted;
        private IList<WritableEvent> _pendingShots;
        private IList<IEventStream> _children;

        public EventStream(IStoreEvents store, String bucket, String streamId, Int32 streamVersion, IEnumerable<WritableEvent> events)
        {
            this._store = store;
            this.Bucket = bucket;
            this.StreamId = streamId;
            this._streamVersion = streamVersion;
            this._version = streamVersion;
            this._committed = events.ToList();
            this._uncommitted = new List<WritableEvent>();
            this._pendingShots = new List<WritableEvent>();
            this._children = new List<IEventStream>();

            if (events == null || events.Count() == 0) return;
        }

        public void Add(Object @event, IDictionary<String, Object> headers)
        {
            this._uncommitted.Add(new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).FullName,
                    Timestamp = DateTime.UtcNow,
                    Version = ++this._version,
                    Headers = headers
                },
                Event = @event,
                EventId = Guid.NewGuid()
            });
        }

        public void AddSnapshot(Object snapshot, IDictionary<String, Object> headers)
        {
            this._pendingShots.Add(new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).FullName,
                    Timestamp = DateTime.UtcNow,
                    Version = ++this._version,
                    Headers = headers
                },
                Event = snapshot,
                EventId = Guid.NewGuid()
            });
        }

        public void Commit(Guid commitId, IDictionary<String, Object> commitHeaders)
        {
            foreach (var child in this._children)<commit_msg>When adding snapshots, dont increment version..<commit_after>﻿using Aggregates.Contracts;
using Aggregates.Exceptions;
using Aggregates.Extensions;
using EventStore.ClientAPI;
using EventStore.ClientAPI.Exceptions;
using Newtonsoft.Json;
using NServiceBus.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aggregates.Internal
{
    public class EventStream<T> : IEventStream where T : class, IEntity
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(EventStream<T>));
        public String Bucket { get; private set; }
        public String StreamId { get; private set; }
        public Int32 StreamVersion { get { return this._streamVersion + this._uncommitted.Count; } }
        public Int32 CommitVersion { get { return this._streamVersion; } }

        public IEnumerable<IWritableEvent> Events
        {
            get
            {
                return this._committed.Concat(this._uncommitted);
            }
        }

        private readonly IStoreEvents _store;
        private readonly Int32 _streamVersion;
        private Int32 _version;
        private IEnumerable<WritableEvent> _committed;
        private IList<WritableEvent> _uncommitted;
        private IList<WritableEvent> _pendingShots;
        private IList<IEventStream> _children;

        public EventStream(IStoreEvents store, String bucket, String streamId, Int32 streamVersion, IEnumerable<WritableEvent> events)
        {
            this._store = store;
            this.Bucket = bucket;
            this.StreamId = streamId;
            this._streamVersion = streamVersion;
            this._version = streamVersion;
            this._committed = events.ToList();
            this._uncommitted = new List<WritableEvent>();
            this._pendingShots = new List<WritableEvent>();
            this._children = new List<IEventStream>();

            if (events == null || events.Count() == 0) return;
        }

        public void Add(Object @event, IDictionary<String, Object> headers)
        {
            this._uncommitted.Add(new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).FullName,
                    Timestamp = DateTime.UtcNow,
                    Version = ++this._version,
                    Headers = headers
                },
                Event = @event,
                EventId = Guid.NewGuid()
            });
        }

        public void AddSnapshot(Object snapshot, IDictionary<String, Object> headers)
        {
            this._pendingShots.Add(new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).FullName,
                    Timestamp = DateTime.UtcNow,
                    Version = this._version,
                    Headers = headers
                },
                Event = snapshot,
                EventId = Guid.NewGuid()
            });
        }

        public void Commit(Guid commitId, IDictionary<String, Object> commitHeaders)
        {
            foreach (var child in this._children)
{'fraction_non_alphanumeric': 0.0784956304619226, 'fraction_numerical': 0.003433208489388265, 'mean_word_length': 1.9, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3010362', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1542, 'n_words': 388}