Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make history file optional #16

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions ConsoleSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
import graql.lang.query.GraqlQuery;
import io.grpc.StatusRuntimeException;
import jline.console.ConsoleReader;
import jline.console.history.History;
import jline.console.history.FileHistory;
import jline.console.history.MemoryHistory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
Expand All @@ -48,6 +52,7 @@
* A Grakn Console Session that allows a user to interact with the Grakn Server
*/
public class ConsoleSession implements AutoCloseable {
Logger logger = LoggerFactory.getLogger(ConsoleSession.class);

private static final String COPYRIGHT = "\n" +
"Welcome to Grakn Console. You are now in Grakn Wonderland!\n" +
Expand Down Expand Up @@ -76,7 +81,7 @@ public class ConsoleSession implements AutoCloseable {
private final ConsoleReader consoleReader;
private final Printer<?> printer = Printer.stringPrinter(true);

private final FileHistory historyFile;
private final History history;

private final GraknClient client;
private final GraknClient.Session session;
Expand All @@ -97,10 +102,17 @@ public class ConsoleSession implements AutoCloseable {
this.consoleReader.setPrompt(ANSI_PURPLE + session.keyspace().name() + ANSI_RESET + "> ");
this.printErr = printErr;

File file = new File(HISTORY_FILE);
file.createNewFile();
this.historyFile = new FileHistory(file);
this.consoleReader.setHistory(this.historyFile);
History history;
try {
File file = new File(HISTORY_FILE);
Copy link

@marco-scoppetta marco-scoppetta Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can maybe use File.exists() rather than creating a history = new FlushableMemoryHistory(); in the catch block

Copy link

@marco-scoppetta marco-scoppetta Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can also keep the catch, but I think it's important that we log with a warning the fact that we are falling back to an in-memory history, so maybe just add in the catch:

LOG.warn("An in-memory history will be used due to exception raised while trying to access history file: ", e.getMessage());
history = new FlushableMemoryHistory();

It's not a huge deal in this case, but it's generally important to be transparent with what's going on, i.e. don't secretly handle exceptions without warning if we're changing the expected behaviour

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marco-scoppetta I think we actually want to fallback on any case on not being able to create/open a file. How could I utilize File.exists if creating a new file triggers IOException?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file.createNewFile();
history = new FileHistory(file);
} catch (IOException e) {
logger.warn("An in-memory history will be used due to exception raised while trying to access history file: ", e.getMessage());
history = new MemoryHistory();
}
this.history = history;
this.consoleReader.setHistory(this.history);
}

void load(Path filePath) throws IOException {
Expand Down Expand Up @@ -244,7 +256,6 @@ private void rollback() {
}

/**
*
* @return true if a clean took place, false otherwise
* @throws IOException
*/
Expand Down Expand Up @@ -280,7 +291,9 @@ public final void close() {
session.close();
client.close();
try {
historyFile.flush();
if (history instanceof FileHistory) {
((FileHistory) history).flush();
}
} catch (IOException e) {
// Print stacktrace to any available stream
// nothing more to do here
Expand Down
2 changes: 1 addition & 1 deletion dependencies/graknlabs/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def graknlabs_protocol():
git_repository(
name = "graknlabs_protocol",
remote = "https://github.com/graknlabs/protocol",
commit = "af2daa4b0ddce19b4668919ecccd059eb282292b", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
commit = "246139d83ad8d3b5b3f37e2bf26d3b56fad4d8bc", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
)

def graknlabs_client_java():
Expand Down