Пример #1
0
        private static List <CommitEntry> ParseCommits(TextReader textReader)
        {
            List <CommitEntry> entries = new List <CommitEntry>();
            string             line;

            while ((line = textReader.ReadLine()) != null)
            {
                if (!line.StartsWith("--"))
                {
                    continue;
                }

                var entry = new CommitEntry();
                entry.CommitHash = line.Substring(2, 7);
                entry.CommitTime = new DateTime(
                    int.Parse(line.Substring(11, 4)),  // year
                    int.Parse(line.Substring(16, 2)),  // month
                    int.Parse(line.Substring(19, 2))); // day
                entry.AuthorName = line.Substring(23);

                ParseCommitDetails(textReader, entry);

                entries.Add(entry);
            }
            return(entries);
        }
Пример #2
0
        public void Handle(CommitEntry @event)
        {
            if (@event.EntryTerm > Data.CurrentTerm)
            {
                throw new InvalidOperationException("Cannot commit a log entry against a term greater than the current term.");
            }

            Data.CommitIndex = Math.Max(Data.CommitIndex, @event.EntryIdx);

            Data.Log.SetLogEntry(@event.EntryIdx, @event.EntryTerm);
        }
Пример #3
0
        private LogCreator LogFile(params Entry[] entries)
        {
            return((logVersion, positions) =>
            {
                try
                {
                    AtomicLong lastTxId = new AtomicLong();
                    _logVersionRepository.CurrentLogVersion = logVersion;
                    LifeSupport logFileLife = new LifeSupport();
                    logFileLife.start();
                    logFileLife.add(_logFiles);
                    LogFile logFile = _logFiles.LogFile;
                    try
                    {
                        FlushablePositionAwareChannel writeChannel = logFile.Writer;
                        LogPositionMarker positionMarker = new LogPositionMarker();
                        LogEntryWriter writer = new LogEntryWriter(writeChannel);
                        foreach (Entry entry in entries)
                        {
                            LogPosition currentPosition = writeChannel.getCurrentPosition(positionMarker).newPosition();
                            positions.put(entry, currentPosition);
                            if (entry is StartEntry)
                            {
                                writer.writeStartEntry(0, 0, 0, 0, new sbyte[0]);
                            }
                            else if (entry is CommitEntry)
                            {
                                CommitEntry commitEntry = ( CommitEntry )entry;
                                writer.writeCommitEntry(commitEntry.TxId, 0);
                                lastTxId.set(commitEntry.TxId);
                            }
                            else if (entry is CheckPointEntry)
                            {
                                CheckPointEntry checkPointEntry = ( CheckPointEntry )entry;
                                Entry target = checkPointEntry.WithPositionOfEntry;
                                LogPosition logPosition = target != null?positions.get(target) : currentPosition;

                                Debug.Assert(logPosition != null, "No registered log position for " + target);
                                writer.writeCheckPointEntry(logPosition);
                            }
                            else if (entry is PositionEntry)
                            {
                                // Don't write anything, this entry is just for registering a position so that
                                // another CheckPointEntry can refer to it
                            }
                            else
                            {
                                throw new System.ArgumentException("Unknown entry " + entry);
                            }
                        }
                    }
                    finally
                    {
                        logFileLife.shutdown();
                    }
                }
                catch (IOException e)
                {
                    throw new UncheckedIOException(e);
                }
            });
        }