1
0
Fork 0

Add function to test if log is "empty"

master
Philip O'Toole 9 months ago
parent c315c0ecf6
commit 433f5016e2

@ -71,6 +71,22 @@ func (l *Log) LastCommandIndex(fi, li uint64) (uint64, error) {
return 0, nil
}
// HasCommand returns whether the Raft log contains any Command log entries.
func (l *Log) HasCommand() (bool, error) {
fi, li, err := l.Indexes()
if err != nil {
return false, err
}
if fi == 0 || li == 0 {
return false, nil
}
i, err := l.LastCommandIndex(fi, li)
if err != nil {
return false, err
}
return i != 0, nil
}
// SetAppliedIndex sets the AppliedIndex value.
func (l *Log) SetAppliedIndex(index uint64) error {
return l.SetUint64([]byte(rqliteAppliedIndex), index)

@ -40,6 +40,14 @@ func Test_LogNewEmpty(t *testing.T) {
if lci != 0 {
t.Fatalf("got wrong value for last command index of not empty log: %d", lci)
}
f, err := l.HasCommand()
if err != nil {
t.Fatalf("failed to get has command: %s", err)
}
if f {
t.Fatalf("got wrong value for has command of empty log: %v", f)
}
}
func Test_LogNewExistNotEmpty(t *testing.T) {
@ -91,6 +99,14 @@ func Test_LogNewExistNotEmpty(t *testing.T) {
t.Fatalf("got wrong value for last command index of not empty log: %d", lci)
}
f, err := l.HasCommand()
if err != nil {
t.Fatalf("failed to get has command: %s", err)
}
if !f {
t.Fatalf("got wrong value for has command of non-empty log: %v", f)
}
if err := l.Close(); err != nil {
t.Fatalf("failed to close log: %s", err)
}
@ -311,6 +327,14 @@ func Test_LogDeleteAll(t *testing.T) {
if li != 0 {
t.Fatalf("got wrong value for last index of empty log: %d", li)
}
f, err := l.HasCommand()
if err != nil {
t.Fatalf("failed to get has command: %s", err)
}
if f {
t.Fatalf("got wrong value for has command of empty log: %v", f)
}
}
func Test_LogLastCommandIndexNotExist(t *testing.T) {

Loading…
Cancel
Save