<commit_before>package raft

import (
	"fmt"

	pb "github.com/coreos/etcd/raft/raftpb"
)

const (
	defaultCompactThreshold = 10000
)

type raftLog struct {
	ents             []pb.Entry
	unstable         int64
	committed        int64
	applied          int64
	offset           int64
	snapshot         pb.Snapshot
	unstableSnapshot pb.Snapshot

	// want a compact after the number of entries exceeds the threshold
	// TODO(xiangli) size might be a better criteria
	compactThreshold int64
}

func newLog() *raftLog {
	return &raftLog{
		ents:             make([]pb.Entry, 1),
		unstable:         1,
		committed:        0,
		applied:          0,
		compactThreshold: defaultCompactThreshold,
	}
}

func (l *raftLog) isEmpty() bool {
	return l.offset == 0 && len(l.ents) == 1
}

func (l *raftLog) String() string {
	return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
}

func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...pb.Entry) bool {
	if l.matchTerm(index, logTerm) {
		from := index + 1
		ci := l.findConflict(from, ents)
		switch {
		case ci == -1:
		case ci <= l.committed:
			panic("conflict with committed entry")
		default:
			l.append(ci-1, ents[ci-from:]...)
		}
		if l.committed < committed {
			l.committed = min(committed, l.lastIndex())
		}
		return true
	}
	return false
}

func (l *raftLog) append(after int64, ents ...pb.Entry) int64 {
	l.ents = append(l.slice(l.offset, after+1), ents...)
	l.unstable = min(l.unstable, after+1)
	return l.lastIndex()
}

func (l *raftLog) findConflict(from int64, ents []pb.Entry) int64 {
	for i, ne := range ents {
		if oe := l.at(from + int64(i)); oe == nil || oe.Term != ne.Term {
			return from + int64(i)
		}
	}
	return -1
}

func (l *raftLog) unstableEnts() []pb.Entry {
	ents := l.entries(l.unstable)
	if ents == nil {
		return nil
	}
	cpy := make([]pb.Entry, len(ents))
	copy(cpy, ents)
	return cpy
}

func (l *raftLog) resetUnstable() {
	l.unstable = l.lastIndex() + 1
}

// nextEnts returns all the available entries for execution.
// all the returned entries will be marked as applied.
func (l *raftLog) nextEnts() (ents []pb.Entry) {
	if l.committed > l.applied {
		ents := l.slice(l.applied+1, l.committed+1)
		if ents == nil {
			return nil
		}
		cpy := make([]pb.Entry, len(ents))
		copy(cpy, ents)
		return cpy
	}
	return nil
}

func (l *raftLog) resetNextEnts() {
	if l.committed > l.applied {
		l.applied = l.committed
	}
}

func (l *raftLog) lastIndex() int64 {
	return int64(len(l.ents)) - 1 + l.offset
}

func (l *raftLog) term(i int64) int64 {<commit_msg>raft: do not need to copy committed entries<commit_after>package raft

import (
	"fmt"

	pb "github.com/coreos/etcd/raft/raftpb"
)

const (
	defaultCompactThreshold = 10000
)

type raftLog struct {
	ents             []pb.Entry
	unstable         int64
	committed        int64
	applied          int64
	offset           int64
	snapshot         pb.Snapshot
	unstableSnapshot pb.Snapshot

	// want a compact after the number of entries exceeds the threshold
	// TODO(xiangli) size might be a better criteria
	compactThreshold int64
}

func newLog() *raftLog {
	return &raftLog{
		ents:             make([]pb.Entry, 1),
		unstable:         1,
		committed:        0,
		applied:          0,
		compactThreshold: defaultCompactThreshold,
	}
}

func (l *raftLog) isEmpty() bool {
	return l.offset == 0 && len(l.ents) == 1
}

func (l *raftLog) String() string {
	return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
}

func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...pb.Entry) bool {
	if l.matchTerm(index, logTerm) {
		from := index + 1
		ci := l.findConflict(from, ents)
		switch {
		case ci == -1:
		case ci <= l.committed:
			panic("conflict with committed entry")
		default:
			l.append(ci-1, ents[ci-from:]...)
		}
		if l.committed < committed {
			l.committed = min(committed, l.lastIndex())
		}
		return true
	}
	return false
}

func (l *raftLog) append(after int64, ents ...pb.Entry) int64 {
	l.ents = append(l.slice(l.offset, after+1), ents...)
	l.unstable = min(l.unstable, after+1)
	return l.lastIndex()
}

func (l *raftLog) findConflict(from int64, ents []pb.Entry) int64 {
	for i, ne := range ents {
		if oe := l.at(from + int64(i)); oe == nil || oe.Term != ne.Term {
			return from + int64(i)
		}
	}
	return -1
}

func (l *raftLog) unstableEnts() []pb.Entry {
	ents := l.entries(l.unstable)
	if ents == nil {
		return nil
	}
	cpy := make([]pb.Entry, len(ents))
	copy(cpy, ents)
	return cpy
}

func (l *raftLog) resetUnstable() {
	l.unstable = l.lastIndex() + 1
}

// nextEnts returns all the available entries for execution.
// all the returned entries will be marked as applied.
func (l *raftLog) nextEnts() (ents []pb.Entry) {
	if l.committed > l.applied {
		return l.slice(l.applied+1, l.committed+1)
	}
	return nil
}

func (l *raftLog) resetNextEnts() {
	if l.committed > l.applied {
		l.applied = l.committed
	}
}

func (l *raftLog) lastIndex() int64 {
	return int64(len(l.ents)) - 1 + l.offset
}

func (l *raftLog) term(i int64) int64 {
{'fraction_non_alphanumeric': 0.1436356558972363, 'fraction_numerical': 0.020630595562475672, 'mean_word_length': 3.0818109610802225, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '167183', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1892, 'n_words': 525}