From adcb5f66b72945e00576ac4bf90c0a1133ef18e7 Mon Sep 17 00:00:00 2001 From: Max Vorobev Date: Fri, 8 Nov 2019 14:20:23 +0300 Subject: [PATCH 1/3] Make history file optional --- BUILD | 1 + ConsoleSession.java | 20 ++++++++++----- dependencies/graknlabs/dependencies.bzl | 2 +- util/FlushableMemoryHistory.java | 34 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 util/FlushableMemoryHistory.java diff --git a/BUILD b/BUILD index ce0be85d..8100ade1 100644 --- a/BUILD +++ b/BUILD @@ -41,6 +41,7 @@ java_library( "*.java", "exception/*.java", "printer/*.java", + "util/*.java", ]) + [":version"], deps = [ "@graknlabs_client_java//:client-java", diff --git a/ConsoleSession.java b/ConsoleSession.java index 69cb9976..6d98484d 100644 --- a/ConsoleSession.java +++ b/ConsoleSession.java @@ -22,10 +22,12 @@ import grakn.client.exception.GraknClientException; import grakn.console.exception.GraknConsoleException; import grakn.console.printer.Printer; +import grakn.console.util.FlushableMemoryHistory; import graql.lang.Graql; import graql.lang.query.GraqlQuery; import io.grpc.StatusRuntimeException; import jline.console.ConsoleReader; +import jline.console.history.PersistentHistory; import jline.console.history.FileHistory; import java.io.File; @@ -76,7 +78,7 @@ public class ConsoleSession implements AutoCloseable { private final ConsoleReader consoleReader; private final Printer printer = Printer.stringPrinter(true); - private final FileHistory historyFile; + private final PersistentHistory history; private final GraknClient client; private final GraknClient.Session session; @@ -97,10 +99,16 @@ 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); + PersistentHistory history; + try { + File file = new File(HISTORY_FILE); + file.createNewFile(); + history = new FileHistory(file); + } catch (IOException e) { + history = new FlushableMemoryHistory(); + } + this.history = history; + this.consoleReader.setHistory(this.history); } void load(Path filePath) throws IOException { @@ -280,7 +288,7 @@ public final void close() { session.close(); client.close(); try { - historyFile.flush(); + history.flush(); } catch (IOException e) { // Print stacktrace to any available stream // nothing more to do here diff --git a/dependencies/graknlabs/dependencies.bzl b/dependencies/graknlabs/dependencies.bzl index f5e7f94d..36a2c4f5 100644 --- a/dependencies/graknlabs/dependencies.bzl +++ b/dependencies/graknlabs/dependencies.bzl @@ -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(): diff --git a/util/FlushableMemoryHistory.java b/util/FlushableMemoryHistory.java new file mode 100644 index 00000000..2ba72c25 --- /dev/null +++ b/util/FlushableMemoryHistory.java @@ -0,0 +1,34 @@ +/* + * GRAKN.AI - THE KNOWLEDGE GRAPH + * Copyright (C) 2019 Grakn Labs Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package grakn.console.util; + +import java.io.IOException; + +import jline.console.history.MemoryHistory; +import jline.console.history.PersistentHistory; + +/** + * Adapter class to allow calling .flush() regardless of history handler type + */ +public class FlushableMemoryHistory extends MemoryHistory implements PersistentHistory { + public void flush() throws IOException { + } + public void purge() throws IOException { + } +} From f79135b578e65e71430009bbb9ecbbd7332b629e Mon Sep 17 00:00:00 2001 From: Max Vorobev Date: Fri, 8 Nov 2019 15:24:50 +0300 Subject: [PATCH 2/3] Address review comments --- BUILD | 1 - ConsoleSession.java | 19 +++++++++++------- util/FlushableMemoryHistory.java | 34 -------------------------------- 3 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 util/FlushableMemoryHistory.java diff --git a/BUILD b/BUILD index 8100ade1..ce0be85d 100644 --- a/BUILD +++ b/BUILD @@ -41,7 +41,6 @@ java_library( "*.java", "exception/*.java", "printer/*.java", - "util/*.java", ]) + [":version"], deps = [ "@graknlabs_client_java//:client-java", diff --git a/ConsoleSession.java b/ConsoleSession.java index 6d98484d..23e09b71 100644 --- a/ConsoleSession.java +++ b/ConsoleSession.java @@ -22,13 +22,15 @@ import grakn.client.exception.GraknClientException; import grakn.console.exception.GraknConsoleException; import grakn.console.printer.Printer; -import grakn.console.util.FlushableMemoryHistory; import graql.lang.Graql; import graql.lang.query.GraqlQuery; import io.grpc.StatusRuntimeException; import jline.console.ConsoleReader; -import jline.console.history.PersistentHistory; +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; @@ -50,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" + @@ -78,7 +81,7 @@ public class ConsoleSession implements AutoCloseable { private final ConsoleReader consoleReader; private final Printer printer = Printer.stringPrinter(true); - private final PersistentHistory history; + private final History history; private final GraknClient client; private final GraknClient.Session session; @@ -99,13 +102,14 @@ public class ConsoleSession implements AutoCloseable { this.consoleReader.setPrompt(ANSI_PURPLE + session.keyspace().name() + ANSI_RESET + "> "); this.printErr = printErr; - PersistentHistory history; + History history; try { File file = new File(HISTORY_FILE); file.createNewFile(); history = new FileHistory(file); } catch (IOException e) { - history = new FlushableMemoryHistory(); + 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); @@ -252,7 +256,6 @@ private void rollback() { } /** - * * @return true if a clean took place, false otherwise * @throws IOException */ @@ -288,7 +291,9 @@ public final void close() { session.close(); client.close(); try { - history.flush(); + if (history instanceof FileHistory) { + ((FileHistory) history).flush(); + } } catch (IOException e) { // Print stacktrace to any available stream // nothing more to do here diff --git a/util/FlushableMemoryHistory.java b/util/FlushableMemoryHistory.java deleted file mode 100644 index 2ba72c25..00000000 --- a/util/FlushableMemoryHistory.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * GRAKN.AI - THE KNOWLEDGE GRAPH - * Copyright (C) 2019 Grakn Labs Ltd - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package grakn.console.util; - -import java.io.IOException; - -import jline.console.history.MemoryHistory; -import jline.console.history.PersistentHistory; - -/** - * Adapter class to allow calling .flush() regardless of history handler type - */ -public class FlushableMemoryHistory extends MemoryHistory implements PersistentHistory { - public void flush() throws IOException { - } - public void purge() throws IOException { - } -} From fe2138046d3eff482ea9685a04c1b85ecc863242 Mon Sep 17 00:00:00 2001 From: Max Vorobev Date: Fri, 8 Nov 2019 15:35:16 +0300 Subject: [PATCH 3/3] rename logger according to standard --- ConsoleSession.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ConsoleSession.java b/ConsoleSession.java index 23e09b71..984f139a 100644 --- a/ConsoleSession.java +++ b/ConsoleSession.java @@ -52,7 +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 Logger LOG = LoggerFactory.getLogger(ConsoleSession.class); private static final String COPYRIGHT = "\n" + "Welcome to Grakn Console. You are now in Grakn Wonderland!\n" + @@ -108,7 +108,7 @@ public class ConsoleSession implements AutoCloseable { 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()); + LOG.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;