type MemoryStorage struct { // Protects access to all fields. Most methods of MemoryStorage are // run on the raft goroutine, but Append() is run on an application // goroutine. sync.Mutex
// shortcut if there is no new entry. if last < first { // 要回放的都已经在storage中了 returnnil } // truncate compacted entries if first > entries[0].Index { // 要回放的数据有部分已经存在storage中了 做截断 entries = entries[first-entries[0].Index:] }
offset := entries[0].Index - ms.ents[0].Index switch { caseuint64(len(ms.ents)) > offset: // NB: full slice expression protects ms.ents at index >= offset from // rewrites, as they may still be referenced from outside MemoryStorage. ms.ents = append(ms.ents[:offset:offset], entries...) caseuint64(len(ms.ents)) == offset: ms.ents = append(ms.ents, entries...) default: getLogger().Panicf("missing log entry [last: %d, append at: %d]", ms.lastIndex(), entries[0].Index) } returnnil }