Skip to content

Commit

Permalink
(fix) optimize 'truncate log' #65
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun committed Mar 26, 2019
1 parent 763ad2b commit 8a959de
Showing 1 changed file with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -226,14 +225,14 @@ public void shutdown() {
private void clearMemoryLogs(LogId id) {
writeLock.lock();
try {
final Iterator<LogEntry> it = this.logsInMemory.iterator();
while (it.hasNext()) {
final LogEntry entry = it.next();
int index = 0;
for (final int size = this.logsInMemory.size(); index < size; index++) {
final LogEntry entry = this.logsInMemory.get(index);
if (entry.getId().compareTo(id) > 0) {
break;
}
it.remove();
}
this.logsInMemory.subList(0, index).clear();
} finally {
writeLock.unlock();
}
Expand Down Expand Up @@ -810,16 +809,16 @@ public void run(Status status) {
}

private boolean truncatePrefix(long firstIndexKept) {
while (!this.logsInMemory.isEmpty()) {
final LogEntry entry = this.logsInMemory.peekFirst();
if (entry.getId().getIndex() < firstIndexKept) {
this.logsInMemory.pollFirst();
} else {
int index = 0;
for (final int size = this.logsInMemory.size(); index < size; index++) {
final LogEntry entry = this.logsInMemory.get(index);
if (entry.getId().getIndex() >= firstIndexKept) {
break;
}
}
this.logsInMemory.subList(0, index).clear();

//TODO maybe it's fine here
// TODO maybe it's fine here
Requires.requireTrue(firstIndexKept >= this.firstLogIndex,
"Try to truncate logs before %d, but the firstLogIndex is %d", firstIndexKept, firstLogIndex);

Expand Down

0 comments on commit 8a959de

Please sign in to comment.