Esempio n. 1
0
        async Task <SourcedState> LoadSourcedStateFromScratch()
        {
            SourcedState state = new SourcedState();

            if (!await file.ExistsAsync())
            {
                return(state);
            }

            var records = await GetAllAsync();

            var dict = new Dictionary <long, RecordContainer>();

            foreach (var e in records)
            {
                dict[e.VersionId] = e;
            }

            if (dict.Count > 0)
            {
                state.AddNewRecords(dict);
            }

            return(state);
        }
Esempio n. 2
0
        protected async Task EnqueueAsync(IEnumerable <string> records)
        {
            try
            {
                using (StateVar)
                {
                    SourcedState state = await StateVar.GetAsync();

                    if (!await file.ExistsAsync())
                    {
                        await file.CreateOrReplaceAsync();
                    }

                    long          currentVersion = state.CurrentVersion;
                    StringBuilder sb             = new StringBuilder();
                    Dictionary <long, RecordContainer> containers = new Dictionary <long, RecordContainer>();
                    foreach (var data in records)
                    {
                        //Create the container in memory
                        RecordContainer container = new RecordContainer(++currentVersion, data.Replace("\r", "").Replace("\n", ""));
                        containers.Add(container.VersionId, container);

                        //Test if need to create pointer
                        if (currentVersion % SourcedState.PointersToRecordsChunkSize == 0)
                        {
                            state.PointersToRecords.Add(currentVersion, await GetFileSize());
                        }

                        //Create string for storage
                        sb.Append(container.VersionId + ",");
                        sb.Append(container.Data);
                        sb.Append("\n");
                    }

                    //Write it to storage, this only stays congruent because this is single threaded
                    await file.AppendTextAsync(sb.ToString());

                    //Add records to the state and update CurrentVersion
                    state.AddNewRecords(containers);
                }
            }
            catch (Exception e)
            {
                throw new RecordWriteException(e);
            }
        }