public Task Reset()
        {
            this.previousEntryId = default(LogEntryId);
            this.builder.Clear();

            return(Task.FromResult(0));
        }
Beispiel #2
0
        public override bool Equals(object obj)
        {
            var logEntry = obj as LogEntry;

            if (logEntry == null)
            {
                return(false);
            }
            return(LogEntryId.Equals(logEntry.LogEntryId));
        }
        public Task Apply(LogEntry <string> entry)
        {
            if (entry.Id.Index != this.previousEntryId.Index + 1)
            {
                throw new InvalidOperationException(
                          $"Tried to apply Entry({entry.Id}) which is not subsequent to previous entry ({this.previousEntryId})");
            }

            this.previousEntryId = entry.Id;
            this.builder.Append(entry.Operation);
            return(Task.FromResult(0));
        }
Beispiel #4
0
        public bool Contains(LogEntryId entryId)
        {
            if (null == entryId)
            {
                throw new ArgumentNullException(nameof(entryId));
            }

            if (entryId > this.LastLogEntryId || entryId < this.FirstLogEntryId)
            {
                return(false);
            }

            return(this.Get(entryId.Index).Id == entryId);
        }
Beispiel #5
0
        public async Task AppendOrOverwrite(IEnumerable <LogEntry <TOperation> > entries)
        {
            if (null == entries)
            {
                throw new ArgumentNullException(nameof(entries));
            }

            var sortedEntries = entries.OrderBy(x => x.Id.Index).ToArray();

            if (sortedEntries.Length == 0)
            {
                return;
            }

            if (this.LastLogEntryId.Index + 1 < sortedEntries.First().Id.Index)
            {
                throw new ArgumentOutOfRangeException("entries are missing");
            }

            if (this.LastLogEntryId >= sortedEntries.First().Id)
            {
                // rewind the stream
                Rewind(sortedEntries.First().Id.Index);
            }

            if (stream.Length == 0)
            {
                // stream is empty, so this is the first entry in the stream
                this.FirstLogEntryId = sortedEntries.First().Id;
            }

            foreach (var entry in sortedEntries)
            {
                var position = stream.Position;
                serializer.Serialize(entry, stream);
                await stream.FlushAsync();

                this.bTreeIndex.Insert(entry.Id.Index, position);
                this.AddToCache(entry);
            }

            if (stream.Length > stream.Position)
            {
                // we have reduced the length of the stream by deleting entries, so reclaim the space.
                stream.SetLength(stream.Position);
            }

            this.LastLogEntryId = sortedEntries.Last().Id;
        }
        public virtual bool Contains(LogEntryId entryId)
        {
            // The log starts at index 1, index 0 is implicitly included.
            if (entryId.Index == 0)
            {
                return true;
            }

            if (this.Entries.Count < entryId.Index)
            {
                return false;
            }

            if (this.Entries[(int)entryId.Index - 1].Id != entryId)
            {
                return false;
            }

            return true;
        }
Beispiel #7
0
        /// <summary>
        /// does an existing entry have a different term?
        /// </summary>
        /// <param name="entryId"></param>
        /// <returns></returns>
        public bool ConflictsWith(LogEntryId entryId)
        {
            if (null == entryId)
            {
                throw new ArgumentNullException(nameof(entryId));
            }

            // entry doesn't exist yet, so it doesn't conflict
            if (entryId.Index > this.LastLogEntryId.Index || entryId.Index < this.FirstLogEntryId.Index)
            {
                return(false);
            }

            var entry = Get(entryId.Index);

            if (entry.Id.Term != entryId.Term)
            {
                return(true);
            }
            return(false);
        }
 public RequestVoteRequest(long term, string candidate, LogEntryId lastLogEntryId)
 {
     this.Term           = term;
     this.Candidate      = candidate;
     this.LastLogEntryId = lastLogEntryId;
 }
 public RequestVoteRequest(long term, string candidate, LogEntryId lastLogEntryId)
 {
     this.Term = term;
     this.Candidate = candidate;
     this.LastLogEntryId = lastLogEntryId;
 }
 public bool Contains(LogEntryId entryId)
 {
     return(this.connection.Query <int>("select count(*) from [log] where [index] = @Index and [term] = @Term", entryId).First() > 0);
 }
Beispiel #11
0
 public override int GetHashCode()
 {
     return(LogEntryId.GetHashCode());
 }