Skip to content

Commit

Permalink
No history in terminal after auto-truncation of the history file, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Aug 3, 2017
1 parent 29131f0 commit 811d8f4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class DefaultHistory implements History {

private LineReader reader;

private int loaded = 0;
private int lastLoaded = 0;
private int nbEntriesInFile = 0;
private int offset = 0;
private int index = 0;

Expand Down Expand Up @@ -87,7 +88,8 @@ public void load() throws IOException {
String line = unescape(l.substring(idx + 1));
internalAdd(time, line);
});
loaded = items.size();
lastLoaded = items.size();
nbEntriesInFile = lastLoaded;
maybeResize();
}
}
Expand Down Expand Up @@ -118,17 +120,18 @@ public void save() throws IOException {
// Append new items to the history file
try (BufferedWriter writer = Files.newBufferedWriter(path.toAbsolutePath(),
StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
for (Entry entry : items.subList(loaded, items.size())) {
for (Entry entry : items.subList(lastLoaded, items.size())) {
writer.append(format(entry));
}
}
nbEntriesInFile += items.size() - lastLoaded;
// If we are over 25% max size, trim history file
int max = getInt(reader, LineReader.HISTORY_FILE_SIZE, DEFAULT_HISTORY_FILE_SIZE);
if (last() > max + max / 4) {
if (nbEntriesInFile > max + max / 4) {
trimHistory(path, max);
}
}
loaded = items.size();
lastLoaded = items.size();
}

protected void trimHistory(Path path, int max) throws IOException {
Expand All @@ -155,15 +158,18 @@ protected void trimHistory(Path path, int max) throws IOException {
Files.move(temp, path, StandardCopyOption.REPLACE_EXISTING);
// Keep items in memory
internalClear();
offset = allItems.get(0).index();
items.addAll(allItems);
loaded = items.size();
lastLoaded = items.size();
nbEntriesInFile = items.size();
maybeResize();
}

private void internalClear() {
offset = 0;
index = 0;
loaded = 0;
lastLoaded = 0;
nbEntriesInFile = 0;
items.clear();
}

Expand Down Expand Up @@ -275,7 +281,7 @@ protected void internalAdd(Instant time, String line) {
private void maybeResize() {
while (size() > getInt(reader, LineReader.HISTORY_SIZE, DEFAULT_HISTORY_SIZE)) {
items.removeFirst();
loaded--;
lastLoaded--;
offset++;
}
index = size();
Expand Down Expand Up @@ -436,7 +442,7 @@ private static String escape(String s) {
return sb.toString();
}

private static String unescape(String s) {
static String unescape(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -87,6 +91,39 @@ public void testOffset() {
assertEquals("f", history.get(5));
}

@Test
public void testTrimIterate() throws IOException {
Path histFile = Files.createTempFile(null, null);

reader.setVariable(LineReader.HISTORY_FILE, histFile);
reader.setVariable(LineReader.HISTORY_SIZE, 4);
reader.setVariable(LineReader.HISTORY_FILE_SIZE, 4);

assertEquals(0, history.size());
assertEquals(0, history.index());

history.add("a");
history.add("b");
history.add("c");
history.add("d");
history.add("e");
history.add("f");
history.add("g");

assertEquals(4, history.size());
assertEquals(7, history.index());
assertHistoryContains(3, "d", "e", "f", "g");

assertEquals("e", history.get(4));
assertEquals(3, history.iterator().next().index());

try (BufferedReader reader = Files.newBufferedReader(histFile)) {
// We should have 5 lines: c, d, e, f, g
// The history file was trimmed while adding f, but we later added g without trimming
assertEquals(5, reader.lines().count());
}
}

@Test
public void testTrim() {
List<History.Entry> entries = new ArrayList<>();
Expand Down

0 comments on commit 811d8f4

Please sign in to comment.