diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java deleted file mode 100644 index b73da6672..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java +++ /dev/null @@ -1,10 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import java.io.IOException; -import java.util.Map; - -public interface Command { - String getName(); - - void executeCmd(Map dataBase, String[] args) throws IOException; -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java deleted file mode 100644 index e8ab2cb10..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; - -public class Exit implements Command { - - public final String getName() { - return "exit"; - } - - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - Utils.write(((FileMap) filemap).getFileMapState().getDataBase(), - ((FileMap) filemap).getFileMapState().getDataFile()); - System.exit(0); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java deleted file mode 100644 index 9e091b20a..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import java.io.*; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -public class FileMap extends Shell { - - private FileMapState state; - - public final FileMapState getFileMapState() { - return state; - } - - public FileMap(final File currentFile) throws IOException { - state = new FileMapState(currentFile); - Utils.readDataBase(state); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java deleted file mode 100644 index 9bf168e89..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java +++ /dev/null @@ -1,24 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -public class FileMapExecutor extends Executor { - - public FileMapExecutor() { - list(); - } - - public final void list() { - Command put = new Put(); - mapOfCmd.put(put.getName(), put); - Command get = new Get(); - mapOfCmd.put(get.getName(), get); - Command remove = new Remove(); - mapOfCmd.put(remove.getName(), remove); - Command list = new List(); - mapOfCmd.put(list.getName(), list); - Command exit = new Exit(); - mapOfCmd.put(exit.getName(), exit); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java index fdd077282..703c5a6d5 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java @@ -1,27 +1,32 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; + + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.File; import java.io.IOException; public class FileMapMain { - public static void main(final String[] args) throws IOException { + public static void main(String[] args) throws IOException { - String currentProperty = System.getProperty("db.file"); - File base = new File(currentProperty); - if (!base.exists()) { - base.createNewFile(); + String currentProperty = System.getProperty("fizteh.db.dir"); + if (currentProperty == null) { + System.exit(-1); } - + File base = new File(currentProperty); try { + if (!base.exists()) { + base.createNewFile(); + } + base = base.getCanonicalFile().toPath().resolve("db.dat").toFile(); - } catch (Exception e) { - System.err.println(e.getMessage()); - System.exit(1); - } + FileMapState startState = new FileMapState(base); + FileMapUtils.readDataBase(startState); + Shell filemap = new Shell(startState); + + Executor exec = new Executor(); - FileMap filemap = new FileMap(base); - FileMapExecutor exec = new FileMapExecutor(); - try { if (args.length > 0) { filemap.batchState(args, exec); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java index 2018efc35..cf9762f65 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java @@ -6,23 +6,27 @@ public class FileMapState { - public Map dataBase; - public File dataFile; + private Map dataBase; + private File dataFile; + + public FileMapState(File currentFile) { + + dataBase = new HashMap(); + dataFile = currentFile; + } + + public Map getDataBase() { - public final Map getDataBase() { return dataBase; } - public final File getDataFile() { + public File getDataFile() { + return dataFile; } - public FileMapState(final File currentFile) { - dataBase = new HashMap(); - dataFile = currentFile; - } - public void setDataBase(Map map) { + dataBase = map; } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Utils.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapUtils.java similarity index 54% rename from src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Utils.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapUtils.java index 65ca99794..c3edef8c4 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Utils.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapUtils.java @@ -6,21 +6,22 @@ import java.util.List; import java.util.Map; - -public class Utils { +public class FileMapUtils { private static final long MAX_SIZE = 1024 * 1024; - private static final int POS_STEP = 5; - private static final int BUF_SIZE = 4096; - private static String readKey(final DataInputStream dataStream) throws IOException { + private static String readKey(DataInputStream dataStream) throws IOException { ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); byte b = dataStream.readByte(); int length = 0; while (b != 0) { byteOutputStream.write(b); - b = dataStream.readByte(); + try { + b = dataStream.readByte(); + } catch (EOFException e) { + throw new IOException("wrong data format"); + } length++; if (length > MAX_SIZE) { throw new IOException("wrong data format"); @@ -29,12 +30,12 @@ private static String readKey(final DataInputStream dataStream) throws IOExcepti if (length == 0) { throw new IOException("wrong data format"); } - return byteOutputStream.toString(StandardCharsets.UTF_8.toString()); } - private static String readValue(final DataInputStream dis, final long offset1, - final long offset2, final long position, final long len) throws IOException { + private static String readValue(DataInputStream dis, long offset1, + long offset2, long position, long len) throws IOException { + dis.mark((int) len); dis.skip(offset1 - position); byte[] buffer = new byte[(int) (offset2 - offset1)]; @@ -44,13 +45,14 @@ private static String readValue(final DataInputStream dis, final long offset1, return value; } - public static void readDataBase(final FileMapState state) throws IOException { + public static void readDataBase(FileMapState state) throws IOException { + if (state.getDataFile().length() == 0) { return; } InputStream currentStream = new FileInputStream(state.getDataFile()); - BufferedInputStream bufferStream = new BufferedInputStream(currentStream, BUF_SIZE); + BufferedInputStream bufferStream = new BufferedInputStream(currentStream, 4096); DataInputStream dataStream = new DataInputStream(bufferStream); int fileLength = (int) state.getDataFile().length(); @@ -58,16 +60,16 @@ public static void readDataBase(final FileMapState state) throws IOException { try { int position = 0; String key1 = readKey(dataStream); + position += key1.getBytes(StandardCharsets.UTF_8).length; int offset1 = dataStream.readInt(); int firstOffset = offset1; - position += POS_STEP; - + position += 5; while (position != firstOffset) { String key2 = readKey(dataStream); position += key2.getBytes(StandardCharsets.UTF_8).length; int offset2 = dataStream.readInt(); - position += POS_STEP; + position += 5; String value = readValue(dataStream, offset1, offset2, position, fileLength); state.getDataBase().put(key1, value); offset1 = offset2; @@ -78,36 +80,42 @@ public static void readDataBase(final FileMapState state) throws IOException { } finally { closeStream(dataStream); } - } - private static void closeStream(final Closeable stream) throws IOException { + private static void closeStream(Closeable stream) throws IOException { + stream.close(); } - public static void write(final Map dataBase, final File currentFile) throws IOException { - - OutputStream currentStream = new FileOutputStream(currentFile); - BufferedOutputStream bufferStream = new BufferedOutputStream(currentStream, BUF_SIZE); - DataOutputStream dataStream = new DataOutputStream(bufferStream); - long biasing = 0; - - for (String key : dataBase.keySet()) { - biasing += key.getBytes(StandardCharsets.UTF_8).length + POS_STEP; - } - List values = new ArrayList(dataBase.keySet().size()); - for (String key : dataBase.keySet()) { - String value = dataBase.get(key); - values.add(value); - dataStream.write(key.getBytes(StandardCharsets.UTF_8)); - dataStream.writeByte(0); - dataStream.writeInt((int) biasing); - biasing += value.getBytes(StandardCharsets.UTF_8).length; - } + public static void write(Map dataBase, File currentFile) throws IOException { - for (String value : values) { - dataStream.write(value.getBytes()); + try { + FileOutputStream currentStream = new FileOutputStream(currentFile); + BufferedOutputStream bufferStream = new BufferedOutputStream(currentStream, 4096); + DataOutputStream dataStream = new DataOutputStream(bufferStream); + long biasing = 0; + try { + for (String key : dataBase.keySet()) { + biasing += key.getBytes(StandardCharsets.UTF_8).length + 5; + } + List values = new ArrayList(dataBase.keySet().size()); + for (String key : dataBase.keySet()) { + String value = dataBase.get(key); + values.add(value); + dataStream.write(key.getBytes(StandardCharsets.UTF_8)); + dataStream.writeByte(0); + dataStream.writeInt((int) biasing); + biasing += value.getBytes(StandardCharsets.UTF_8).length; + } + + for (String value : values) { + dataStream.write(value.getBytes()); + } + } finally { + closeStream(dataStream); + } + } catch (IOException e) { + throw new IOException("cannot write '" + currentFile.getName() + "'", e); } - closeStream(dataStream); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java deleted file mode 100644 index 26d512632..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java +++ /dev/null @@ -1,24 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; - -public class Get implements Command { - - public final String getName() { - return "get"; - } - - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - String key = args[0]; - String value = ((FileMap) filemap).getFileMapState().getDataBase().get(key); - if (value == null) { - System.out.println("not found"); - } else { - System.out.println("found"); - System.out.println(value); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java deleted file mode 100644 index e8f89af8a..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; -import java.util.Set; - -public class List implements Command { - - public final String getName() { - return "list"; - } - - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - Set keys = ((FileMap) filemap).getFileMapState().getDataBase().keySet(); - System.out.println(String.join(", ", keys)); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java deleted file mode 100644 index 8d4692932..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java +++ /dev/null @@ -1,25 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import java.io.IOException; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -public class Put implements Command { - - public final String getName() { - return "put"; - } - - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - String key = args[0]; - String value = args[1]; - String oldValue = ((FileMap) filemap).getFileMapState().getDataBase().put(key, value); - if (oldValue == null) { - System.out.println("new"); - } else { - System.out.println("overwrite"); - System.out.println(oldValue); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java deleted file mode 100644 index eb4341c9b..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java +++ /dev/null @@ -1,23 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import java.io.IOException; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -public class Remove implements Command { - - public final String getName() { - return "remove"; - } - - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - String key = args[0]; - String value = ((FileMap) filemap).getFileMapState().getDataBase().remove(key); - if (value == null) { - System.out.println("not found"); - } else { - System.out.println("removed"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java deleted file mode 100644 index c577de764..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java +++ /dev/null @@ -1,30 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.IOException; - -public class CmdCreate implements Command { - - @Override - public final String getName() { - - return "create"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - - if (((MultiFileHashMap) shell).getMFHMState().createTable(args[0]) != null) { - System.out.println("created"); - } else { - System.out.println(args[0] + " exists"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java deleted file mode 100644 index 018a0bdfc..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java +++ /dev/null @@ -1,46 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.IOException; -import java.util.Set; - -public class CmdDrop implements Command { - - @Override - public final String getName() { - - return "drop"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - - if (((MultiFileHashMap) shell).getMFHMState().getTable(args[0]) == null) { - System.out.println(args[0] + " not exists"); - return; - } - - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 1) { - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable().getName().equals(args[0])) { - Set keys = ((MultiFileHashMap) shell).getMFHMState(). - getDataBaseFromCurrentTable().keySet(); - for (String key : keys) { - ((MultiFileHashMap) shell).getMFHMState().removeFromCurrentTable(key); - } - ((MultiFileHashMap) shell).getMFHMState().setCurrentTable(); - ((MultiFileHashMap) shell).getMFHMState().changeFlag(); - } - } - - ((MultiFileHashMap) shell).getMFHMState().deleteTable(args[0]); - System.out.println("dropped"); - - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java deleted file mode 100644 index 90fb31ae1..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java +++ /dev/null @@ -1,46 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.File; -import java.io.IOException; -import java.util.Map; -import java.util.Set; - -public class CmdShowTables implements Command { - - @Override - public final String getName() { - - return "show"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 1) { - throw new IOException("Can't find key"); - } - - if (!args[0].equals("tables")) { - throw new IOException("Can't find key"); - } - - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 1) { - File fileForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataFile(); - Map mapForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataBase(); - MultiFileHashMapUtils.write(fileForWrite, mapForWrite); - } - - System.out.println("table_name row_count"); - - Set tables = ((MultiFileHashMap) shell).getMFHMState().getTableSet(); - for (String table : tables) { - System.out.println(String.join(" ", table, String.valueOf(((MultiFileHashMap) shell). - getMFHMState().getTable(table).getDataBase().keySet().size()))); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java deleted file mode 100644 index 9af5a3adb..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java +++ /dev/null @@ -1,47 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.File; -import java.io.IOException; -import java.util.Map; - -public class CmdUse implements Command { - - @Override - public final String getName() { - - return "use"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - - - if (((MultiFileHashMap) shell).getMFHMState().getTable(args[0]) == null) { - System.out.println(args[0] + " not exists"); - return; - } - - if (1 == ((MultiFileHashMap) shell).getMFHMState().getFlag()) { - File fileForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataFile(); - Map mapForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataBase(); - MultiFileHashMapUtils.write(fileForWrite, mapForWrite); - } - - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 0) { - ((MultiFileHashMap) shell).getMFHMState().changeFlag(); - } - - ((MultiFileHashMap) shell).getMFHMState().setCurrentTable(args[0]); - System.out.println("using " + args[0]); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMap.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMap.java deleted file mode 100644 index 1a4703e06..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMap.java +++ /dev/null @@ -1,21 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.File; -import java.io.IOException; - -public class MultiFileHashMap extends Shell { - - private MultiFileHashMapState state; - - public MultiFileHashMap(final File currentFile) throws IOException { - - state = new MultiFileHashMapState(currentFile); - } - - public final MultiFileHashMapState getMFHMState() { - - return state; - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java deleted file mode 100644 index b25743893..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java +++ /dev/null @@ -1,35 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; - -public class MultiFileHashMapExecutor extends Executor { - - public MultiFileHashMapExecutor() { - - list(); - } - - @Override -public final void list() { - - Command create = new CmdCreate(); - mapOfCmd.put(create.getName(), create); - Command drop = new CmdDrop(); - mapOfCmd.put(drop.getName(), drop); - Command use = new CmdUse(); - mapOfCmd.put(use.getName(), use); - Command show = new CmdShowTables(); - mapOfCmd.put(show.getName(), show); - Command exit = new MultiFileHashMapExit(); - mapOfCmd.put(exit.getName(), exit); - Command get = new MultiFileHashMapGet(); - mapOfCmd.put(get.getName(), get); - Command put = new MultiFileHashMapPut(); - mapOfCmd.put(put.getName(), put); - Command remove = new MultiFileHashMapRemove(); - mapOfCmd.put(remove.getName(), remove); - Command list = new MultiFileHashMapList(); - mapOfCmd.put(list.getName(), list); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java deleted file mode 100644 index bc26dee94..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java +++ /dev/null @@ -1,33 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.File; -import java.io.IOException; -import java.util.Map; - -public class MultiFileHashMapExit implements Command { - - @Override - public final String getName() { - return "exit"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - if (args.length != 0) { - System.out.println("incorrect number of arguments"); - return; - } - - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 1) { - File fileForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataFile(); - Map mapForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataBase(); - MultiFileHashMapUtils.write(fileForWrite, mapForWrite); - } - System.exit(0); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java deleted file mode 100644 index 19c4226eb..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java +++ /dev/null @@ -1,35 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.IOException; - -public class MultiFileHashMapGet implements Command { - - @Override - public final String getName() { - - return "get"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { - System.out.println("no table"); - return; - } - String value = ((MultiFileHashMap) shell).getMFHMState().getFromCurrentTable(args[0]); - if (null == value) { - System.out.println("not found"); - } else { - System.out.println("found"); - System.out.println(value); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java deleted file mode 100644 index 983019841..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java +++ /dev/null @@ -1,30 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.IOException; -import java.util.Set; - -public class MultiFileHashMapList implements Command { - - @Override - public final String getName() { - return "list"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 0) { - System.out.println("incorrect number of arguments"); - return; - } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { - System.out.println("no table"); - return; - } - Set keys = ((MultiFileHashMap) shell).getMFHMState().getDataBaseFromCurrentTable().keySet(); - System.out.println(String.join(", ", keys)); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java deleted file mode 100644 index ba73a50e1..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java +++ /dev/null @@ -1,59 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import java.io.File; -import java.io.IOException; - -public class MultiFileHashMapMain { - - public static void main(final String[] args) throws IOException { - - - String currentProperty = System.getProperty("fizteh.db.dir"); - try { - - - if (currentProperty == null) { - throw new IOException("Working directory is not specified"); - } - - if (!new File(currentProperty).exists()) { - throw new IOException("Working directory does not exist"); - } - - if (!new File(currentProperty).isDirectory()) { - throw new IOException("Working directory is not a directory"); - } - } catch (IOException e) { - System.err.println(e.getMessage()); - System.exit(1); - } - - File base = new File(currentProperty); - if (!base.exists()) { - base.createNewFile(); - } - - - try { - base = base.getCanonicalFile(); - } catch (Exception e) { - System.err.println(e.getMessage()); - System.exit(1); - } - MultiFileHashMap mfhm = new MultiFileHashMap(base); - MultiFileHashMapExecutor exec = new MultiFileHashMapExecutor(); - - try { - if (args.length > 0) { - mfhm.batchState(args, exec); - } else { - mfhm.interactiveState(exec); - } - } catch (IOException e) { - System.err.println(e.getMessage()); - System.exit(1); - } - - System.exit(0); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java deleted file mode 100644 index 9efa71c53..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java +++ /dev/null @@ -1,35 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.IOException; - -public class MultiFileHashMapPut implements Command { - - @Override - public final String getName() { - - return "put"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 2) { - System.out.println("incorrect number of arguments"); - return; - } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { - System.out.println("no table"); - return; - } - String value = ((MultiFileHashMap) shell).getMFHMState().putToCurrentTable(args[0], args[1]); - if (null == value) { - System.out.println("new"); - } else { - System.out.println("overwrite"); - System.out.println(value); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java deleted file mode 100644 index 45c3ba9e0..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java +++ /dev/null @@ -1,33 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.IOException; - -public class MultiFileHashMapRemove implements Command { - - @Override - public final String getName() { - return "remove"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { - System.out.println("no table"); - return; - } - String value = ((MultiFileHashMap) shell).getMFHMState().removeFromCurrentTable(args[0]); - if (null == value) { - System.out.println("not found"); - } else { - System.out.println("removed"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java deleted file mode 100644 index 1ac716cbb..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java +++ /dev/null @@ -1,82 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import java.io.File; -import java.io.IOException; -import java.util.Map; -import java.util.Set; - -public class MultiFileHashMapState { - - private MultiFileHashMapTableProvider provider; - private MultiFileHashMapTable currentTable; - private int flag; //flag = 0 - we`re not working with any table, 1 - we are - - public MultiFileHashMapState(final File inFile) throws IOException { - - flag = 0; - currentTable = null; - MultiFileHashMapTableProviderFactory factory = new MultiFileHashMapTableProviderFactory(); - provider = factory.create(inFile.getPath()); - } - - public final int getFlag() { - - return flag; - } - - public final void changeFlag() { - - if (0 == flag) { - flag = 1; - } else { - flag = 0; - } - } - - - public final MultiFileHashMapTable createTable(final String name) throws IOException { - - return provider.createTable(name); - } - - public final MultiFileHashMapTable getTable(final String name) throws IOException { - - return provider.getTable(name); - } - - public final MultiFileHashMapTable getCurrentTable() throws IOException { - - return currentTable; - } - - public final void setCurrentTable(final String name) throws IOException { - - currentTable = provider.getTable(name); - } - - public final void setCurrentTable() throws IOException { - currentTable = null; - } - - public final void deleteTable(final String name) throws IOException { - provider.removeTable(name); - } - - public final String putToCurrentTable(final String key, final String value) { - return currentTable.put(key, value); - } - - public final String getFromCurrentTable(final String key) { - return currentTable.get(key); - } - - public final String removeFromCurrentTable(final String key) { - return currentTable.remove(key); - } - public final Map getDataBaseFromCurrentTable() { - return currentTable.getDataBase(); - } - public final Set getTableSet() { - return provider.getTables(); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java deleted file mode 100644 index 85770352d..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java +++ /dev/null @@ -1,48 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -public class MultiFileHashMapTable { - - private Map dataBase; - private File dataFile; - - public MultiFileHashMapTable(final File currentFile) throws IOException { - - dataBase = new HashMap<>(); - dataFile = currentFile; - MultiFileHashMapUtils.read(currentFile, dataBase); - } - - public final Map getDataBase() { - return dataBase; - } - - public final File getDataFile() { - return dataFile; - } - - public final String getName() { - - return dataFile.getName(); - } - - public final String get(final String key) { - - return dataBase.get(key); - } - - public final String put(final String key, final String value) { - - return dataBase.put(key, value); - } - - public final String remove(final String key) { - - return dataBase.remove(key); - } - -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java deleted file mode 100644 index 2dd627ab6..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java +++ /dev/null @@ -1,60 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import static ru.fizteh.fivt.students.ivan_ivanov.shell.Rm.remove; - -public class MultiFileHashMapTableProvider { - - private Map mapOfTables; - private File currentDir; - - public MultiFileHashMapTableProvider(final File inDir) throws IOException { - - mapOfTables = new HashMap<>(); - currentDir = inDir; - File[] fileMas = currentDir.listFiles(); - if (fileMas.length != 0) { - for (int i = 0; i < fileMas.length; ++i) { - if (fileMas[i].isDirectory()) { - mapOfTables.put(fileMas[i].getName(), new MultiFileHashMapTable(fileMas[i])); - } - } - } - } - - public final MultiFileHashMapTable getTable(final String name) throws IOException { - - if (!mapOfTables.containsKey(name)) { - return null; - } - return new MultiFileHashMapTable(new File(currentDir, name)); - } - - public final MultiFileHashMapTable createTable(final String name) throws IOException { - - File dirOfTable = new File(currentDir, name); - if (!dirOfTable.mkdir()) { - return null; - } - MultiFileHashMapTable table = new MultiFileHashMapTable(dirOfTable); - mapOfTables.put(name, table); - return table; - } - - public final void removeTable(final String name) throws IOException { - - File dirOfTable = new File(currentDir, name); - remove(dirOfTable.getCanonicalFile().toPath()); - mapOfTables.remove(name); - } - - public final Set getTables() { - Set tables = mapOfTables.keySet(); - return tables; - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java deleted file mode 100644 index 5fab52900..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java +++ /dev/null @@ -1,12 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - - -import java.io.File; -import java.io.IOException; - -public class MultiFileHashMapTableProviderFactory { - - public final MultiFileHashMapTableProvider create(final String dir) throws IOException { - return new MultiFileHashMapTableProvider(new File(dir)); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java new file mode 100644 index 000000000..e64ecd1fa --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java @@ -0,0 +1,30 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdCommit extends Command { + + private int numArg = 0; + + @Override + public String getName() { + + return "commit"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (!checkTable(inState)) { + return; + } + + try { + System.out.println(inState.getCurrentTable().commit()); + } catch (RuntimeException e) { + throw new IOException(e.getMessage()); + } + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java new file mode 100644 index 000000000..cab1d711e --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java @@ -0,0 +1,26 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdCreate extends Command { + + private int numArg = 1; + + @Override + public String getName() { + + return "create"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (inState.createTable(args[0]) != null) { + System.out.println("created"); + } else { + System.out.println(args[0] + " exists"); + } + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java new file mode 100644 index 000000000..5916a4c44 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java @@ -0,0 +1,28 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdDrop extends Command { + + private int numArg = 0; + + @Override + public String getName() { + + return "drop"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (inState.getTable(args[0]) == null) { + System.out.println(args[0] + " not exists"); + return; + } else { + inState.deleteTable(args[0]); + System.out.println("dropped"); + } + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java new file mode 100644 index 000000000..0e7c490ab --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java @@ -0,0 +1,26 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdRollback extends Command { + + private int numArg = 0; + + @Override + public String getName() { + + return "rollback"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (!checkTable(inState)) { + return; + } + + System.out.println(inState.getCurrentTable().rollback()); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java new file mode 100644 index 000000000..76671e296 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java @@ -0,0 +1,39 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; +import java.util.Set; + +public class CmdShowTables extends Command { + + @Override + public final String getName() { + return "show"; + } + + @Override + public final void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + + if (args.length != 1) { + throw new IOException("Can't find key"); + } + + if (!args[0].equals("tables")) { + throw new IOException("Can't find key"); + } + + work(inState, args); + } + + @Override + public final void work(MultiFileHashMapState inState, String[] args) throws IOException { + System.out.println("table_name row_count"); + + Set tables = inState.getTableSet(); + for (String table : tables) { + System.out.println(String.join(" ", table, + String.valueOf(inState.getTable(table).getDataBase().keySet().size()))); + } + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java new file mode 100644 index 000000000..2553c5396 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java @@ -0,0 +1,27 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdSize extends Command { + + private int numArg = 0; + + @Override + public String getName() { + + return "size"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + + if (!checkTable(inState)) { + return; + } + + System.out.println(inState.getCurrentTable().size()); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java new file mode 100644 index 000000000..d83d890bd --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java @@ -0,0 +1,46 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +public class CmdUse extends Command { + + private int numArg = 1; + + @Override + public String getName() { + + return "use"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (inState.getCurrentTable() != null) { + if (!inState.getCurrentTable().getName().equals(args[0])) { + int size = inState.getChangesBaseSize(); + if (size != 0) { + System.out.println(size + " unsaved changes"); + return; + } + } else { + return; + } + } + + if (inState.getTable(args[0]) == null) { + System.out.println(args[0] + " not exists"); + return; + } + + Map tmpDataBase = inState.getTable(args[0]).getDataBase(); + File tmpDataFile = inState.getTable(args[0]).getDataFile(); + MultiFileHashMapUtils.read(tmpDataFile, tmpDataBase); + inState.setCurrentTable(args[0], tmpDataBase, tmpDataFile); + + System.out.println("using " + args[0]); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java new file mode 100644 index 000000000..a40ee1e00 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java @@ -0,0 +1,26 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class MultiFileHashMapExit extends Command { + + private int numArg = 0; + + @Override + public String getName() { + + return "exit"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + + if (inState.getCurrentTable() != null) { + inState.getCurrentTable().commit(); + } + System.exit(0); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java new file mode 100644 index 000000000..5658571f6 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java @@ -0,0 +1,31 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class MultiFileHashMapGet extends Command { + + private int numArg = 1; + + @Override + public String getName() { + + return "get"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (!checkTable(inState)) { + return; + } + String value = inState.getFromCurrentTable(args[0]); + if (null == value) { + System.out.println("not found"); + } else { + System.out.println("found"); + System.out.println(value); + } + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java new file mode 100644 index 000000000..43ec5de10 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java @@ -0,0 +1,26 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; +import java.util.List; + +public class MultiFileHashMapList extends Command { + + private int numArg = 0; + + @Override + public final String getName() { + return "list"; + } + + @Override + public final void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (!checkTable(inState)) { + return; + } + List keys = inState.getCurrentTableListOfKeys(); + System.out.println(String.join(", ", keys)); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java new file mode 100644 index 000000000..3da0499cd --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java @@ -0,0 +1,32 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class MultiFileHashMapPut extends Command { + + private int numArg = 2; + + @Override + public String getName() { + + return "put"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (!checkTable(inState)) { + return; + } + String value = inState.putToCurrentTable(args[0], args[1]); + if (null == value) { + System.out.println("new"); + } else { + System.out.println("overwrite"); + System.out.println(value); + } + } +} + diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java new file mode 100644 index 000000000..2264924da --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java @@ -0,0 +1,30 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class MultiFileHashMapRemove extends Command { + + private int numArg = 1; + + @Override + public String getName() { + + return "remove"; + } + + @Override + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + + if (!checkTable(inState)) { + return; + } + String value = inState.removeFromCurrentTable(args[0]); + if (null == value) { + System.out.println("not found"); + } else { + System.out.println("removed"); + } + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapState.java new file mode 100644 index 000000000..a3d15398f --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapState.java @@ -0,0 +1,76 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class MultiFileHashMapState { + + private MultiFileHashMapTableProvider provider; + private MultiFileHashMapTable currentTable; + + public MultiFileHashMapState(File inFile) throws IOException { + + currentTable = null; + MultiFileHashMapTableProviderFactory factory = new MultiFileHashMapTableProviderFactory(); + provider = factory.create(inFile.getPath()); + } + + public int getChangesBaseSize() { + + return currentTable.getChangesBaseSize(); + } + + public MultiFileHashMapTable createTable(String name) throws IOException { + + MultiFileHashMapTable tmp = provider.createTable(name); + return tmp; + } + + public MultiFileHashMapTable getTable(String name) throws IOException { + + return provider.getTable(name); + } + + public MultiFileHashMapTable getCurrentTable() throws IOException { + + return currentTable; + } + + public void setCurrentTable(String name, Map inMap, File inFile) throws IOException { + + currentTable = provider.getTable(name); + currentTable.changeCurrentTable(inMap, inFile); + } + + public void deleteTable(String name) throws IOException { + + provider.removeTable(name); + currentTable = null; + } + + public String putToCurrentTable(String key, String value) { + + return currentTable.put(key, value); + } + + public String getFromCurrentTable(String key) { + + return currentTable.get(key); + } + + public String removeFromCurrentTable(String key) { + + return currentTable.remove(key); + } + + public Set getTableSet() { + return provider.getTables(); + } + + public List getCurrentTableListOfKeys() { + return currentTable.list(); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTable.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTable.java new file mode 100644 index 000000000..54be42cf4 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTable.java @@ -0,0 +1,176 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.storage.strings.Table; + +import java.io.File; +import java.io.IOException; +import java.util.*; + +public class MultiFileHashMapTable implements Table { + + private Map dataBase; + private Map changesBase; + private File dataFile; + private int sizeTable; + + public MultiFileHashMapTable(File currentFile) { + + dataBase = new HashMap(); + changesBase = new HashMap(); + dataFile = currentFile; + } + + public int getChangesBaseSize() { + + return changesBase.size(); + } + + public Map getDataBase() { + + return dataBase; + } + + public File getDataFile() { + + return dataFile; + } + + public void changeCurrentTable(Map inMap, File inFile) { + + dataBase = inMap; + dataFile = inFile; + sizeTable = dataBase.size(); + } + + @Override + public String getName() { + + return dataFile.getName(); + } + + @Override + public String get(String key) { + + if (key == null) { + throw new IllegalArgumentException("Incorrect key to get"); + } + String newKey = key.trim(); + if (newKey.isEmpty()) { + throw new IllegalArgumentException("Incorrect key to get"); + } + String returnValue; + if (changesBase.containsKey(newKey)) { + if (changesBase.get(newKey) == null) { + returnValue = null; + } else { + returnValue = changesBase.get(newKey); + } + } else { + if (dataBase.containsKey(newKey)) { + returnValue = dataBase.get(newKey); + } else { + returnValue = null; + } + } + + return returnValue; + } + + @Override + public String put(String key, String value) { + + if (key == null || value == null) { + throw new IllegalArgumentException("Incorrect key or value to put."); + } + + String newKey = key.trim(); + String newValue = value.trim(); + if (newKey.isEmpty() || newValue.isEmpty()) { + throw new IllegalArgumentException("Incorrect key or value to put"); + } + + if ((!changesBase.containsKey(newKey) && !dataBase.containsKey(newKey)) + || (changesBase.containsKey(newKey) && changesBase.get(newKey) == null)) { + ++sizeTable; + } + String result = get(newKey); + changesBase.put(newKey, newValue); + if (value.equals(dataBase.get(newKey))) { + changesBase.remove(newKey); + } + + return result; + } + + @Override + public String remove(String key) { + + if (key == null) { + throw new IllegalArgumentException("Incorrect key to remove."); + } + + String newKey = key.trim(); + if (newKey.isEmpty()) { + throw new IllegalArgumentException("Incorrect key to remove"); + } + + if (changesBase.get(newKey) != null || (!changesBase.containsKey(newKey) && dataBase.get(newKey) != null)) { + --sizeTable; + } + String result = get(newKey); + changesBase.put(newKey, null); + if (dataBase.get(newKey) == null) { + changesBase.remove(newKey); + } + return result; + } + + @Override + public int size() { + + return sizeTable; + } + + @Override + public int commit() throws RuntimeException { + + int size = changesBase.size(); + try { + if (size != 0) { + Set> set = changesBase.entrySet(); + for (Map.Entry pair : set) { + pair.getKey(); + if (pair.getValue() == null) { + dataBase.remove(pair.getKey()); + } else { + dataBase.put(pair.getKey(), pair.getValue()); + } + } + MultiFileHashMapUtils.write(dataFile, dataBase); + } + } catch (IOException e) { + throw new RuntimeException(e.getMessage()); + } + + changesBase.clear(); + sizeTable = dataBase.size(); + return size; + } + + @Override + public int rollback() { + + int size = changesBase.size(); + changesBase.clear(); + sizeTable = dataBase.size(); + return size; + } + + @Override + public List list() { + List keys = new ArrayList(dataBase.keySet()); + Collections.sort(keys); + return keys; + + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProvider.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProvider.java new file mode 100644 index 000000000..93d6b1d80 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProvider.java @@ -0,0 +1,107 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.storage.strings.Table; +import ru.fizteh.fivt.storage.strings.TableProvider; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Rm; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class MultiFileHashMapTableProvider implements TableProvider { + + private static final String REG_EXP = "[a-zA-Zа-яА-Я0-9]+"; + + private Map mapOfTables; + private File currentDir; + + public MultiFileHashMapTableProvider(File inDir) { + + if (inDir == null) { + throw new IllegalArgumentException("null directory"); + } + if (!inDir.isDirectory()) { + throw new IllegalArgumentException("not a directory"); + } + mapOfTables = new HashMap(); + currentDir = inDir; + File[] fileMas = currentDir.listFiles(); + if (fileMas.length != 0) { + for (File fileMa : fileMas) { + if (fileMa.isDirectory()) { + mapOfTables.put(fileMa.getName(), new MultiFileHashMapTable(fileMa)); + } + } + } + } + + @Override + public MultiFileHashMapTable getTable(String name) { + + if (name == null) { + throw new IllegalArgumentException("null name to get"); + } + if (!name.matches(REG_EXP)) { + throw new IllegalArgumentException("incorrect name to get"); + } + + if (!mapOfTables.containsKey(name)) { + return null; + } + + return mapOfTables.get(name); + } + + @Override + public MultiFileHashMapTable createTable(String name) { + + if (name == null) { + throw new IllegalArgumentException("null name to create"); + } + if (!name.matches(REG_EXP)) { + throw new IllegalArgumentException("incorrect name to create"); + } + + File dirOfTable = new File(currentDir, name); + if (!dirOfTable.mkdir()) { + return null; + } + + MultiFileHashMapTable table = new MultiFileHashMapTable(dirOfTable); + + Table tmp = mapOfTables.get(name); + if (tmp != null) { + return null; + } + mapOfTables.put(name, table); + return table; + + } + + @Override + public void removeTable(String name) { + + if (name == null || !name.matches(REG_EXP)) { + throw new IllegalArgumentException("incorrect table name to remove"); + } + if (!mapOfTables.containsKey(name)) { + throw new IllegalStateException("table doesn't exist"); + } + + File dirOfTable = new File(currentDir, name); + try { + Rm.remove(dirOfTable.getCanonicalFile().toPath()); + } catch (IOException e) { + System.err.println(e); + } + + mapOfTables.remove(name); + } + + public Set getTables() { + Set tables = mapOfTables.keySet(); + return tables; + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProviderFactory.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProviderFactory.java new file mode 100644 index 000000000..15a788bff --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProviderFactory.java @@ -0,0 +1,23 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; + +import ru.fizteh.fivt.storage.strings.TableProviderFactory; + +import java.io.File; + +public class MultiFileHashMapTableProviderFactory implements TableProviderFactory { + + @Override + public MultiFileHashMapTableProvider create(String dir) { + + if (dir == null) { + throw new IllegalArgumentException("null directory"); + } + if (dir.trim().isEmpty()) { + throw new IllegalArgumentException("empty directory"); + } + + File file = new File(dir); + + return new MultiFileHashMapTableProvider(file); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapUtils.java similarity index 59% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapUtils.java index 702042a9c..9998e9adf 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapUtils.java @@ -1,7 +1,7 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.filemap.FileMapState; -import ru.fizteh.fivt.students.ivan_ivanov.filemap.Utils; +import ru.fizteh.fivt.students.ivan_ivanov.filemap.FileMapUtils; import java.io.File; import java.io.IOException; @@ -11,12 +11,15 @@ public class MultiFileHashMapUtils { - private static final int MAX = 16; + private static final int MAX_DIRS = 16; + private static final int MAX_FILES = 16; + private static final String DIR_SUFFIX = ".dir"; + private static final String FILE_SUFFIX = ".dat"; - public static void read(final File currentDir, final Map currentMap) throws IOException { + public static void read(File currentDir, Map currentMap) throws IOException { - for (int directNumber = 0; directNumber < MAX; ++directNumber) { - File subDir = new File(currentDir, directNumber + ".dir"); + for (int directNumber = 0; directNumber < MAX_DIRS; ++directNumber) { + File subDir = new File(currentDir, directNumber + DIR_SUFFIX); if (!subDir.exists()) { continue; } @@ -24,19 +27,20 @@ public static void read(final File currentDir, final Map current throw new IOException(subDir.getName() + "isn't directory"); } - for (int fileNumber = 0; fileNumber < MAX; ++fileNumber) { - File currentFile = new File(subDir, fileNumber + ".dat"); + for (int fileNumber = 0; fileNumber < MAX_FILES; ++fileNumber) { + File currentFile = new File(subDir, fileNumber + FILE_SUFFIX); if (!currentFile.exists()) { continue; } FileMapState state = new FileMapState(currentFile); state.setDataBase(currentMap); - Utils.readDataBase(state); + FileMapUtils.readDataBase(state); } } } - public static void deleteDirectory(final File directory) throws IOException { + public static void deleteDirectory(File directory) throws IOException { + File[] files = directory.listFiles(); if (files != null) { for (File f : files) { @@ -49,23 +53,23 @@ public static void deleteDirectory(final File directory) throws IOException { } } - public static void write(final File currentDir, final Map currentMap) throws IOException { + public static void write(File currentDir, Map currentMap) throws IOException { - Map[][] arrayOfMap = new Map[MAX][MAX]; + Map[][] arrayOfMap = new Map[MAX_DIRS][MAX_FILES]; for (String key : currentMap.keySet()) { int byteOfKey = key.getBytes(StandardCharsets.UTF_8)[0]; - int nDirectory = byteOfKey % MAX; - int nFile = byteOfKey / MAX % MAX; + int nDirectory = Math.abs(byteOfKey) % MAX_DIRS; + int nFile = Math.abs(byteOfKey) / MAX_DIRS % MAX_FILES; if (arrayOfMap[nDirectory][nFile] == null) { arrayOfMap[nDirectory][nFile] = new HashMap(); } arrayOfMap[nDirectory][nFile].put(key, currentMap.get(key)); } - for (int i = 0; i < MAX; i++) { - File dir = new File(currentDir, i + ".dir"); - for (int j = 0; j < MAX; j++) { - File file = new File(dir, j + ".dat"); + for (int i = 0; i < MAX_DIRS; i++) { + File dir = new File(currentDir, i + DIR_SUFFIX); + for (int j = 0; j < 16; j++) { + File file = new File(dir, j + FILE_SUFFIX); if (null == arrayOfMap[i][j]) { if (file.exists()) { file.delete(); @@ -80,7 +84,7 @@ public static void write(final File currentDir, final Map curren if (!file.exists()) { file.createNewFile(); } - Utils.write(arrayOfMap[i][j], file); + FileMapUtils.write(arrayOfMap[i][j], file); } if (dir.exists()) { @@ -89,6 +93,5 @@ public static void write(final File currentDir, final Map curren } } } - } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java new file mode 100644 index 000000000..655a5ff34 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java @@ -0,0 +1,64 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.interpreter; + +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.*; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; + +import java.io.File; +import java.io.IOException; + +public class MultiFileHashMapMain { + + public static void main(String[] args) throws IOException { + + + String currentProperty = System.getProperty("fizteh.db.dir"); + try { + + if (currentProperty == null) { + throw new IOException("Working directory is not specified"); + } + + if (!new File(currentProperty).exists()) { + throw new IOException("Working directory does not exist"); + } + + if (!new File(currentProperty).isDirectory()) { + throw new IOException("Working directory is not a directory"); + } + } catch (IOException e) { + System.err.println(e.getMessage()); + System.exit(1); + } + + Command[] cmds = new Command[]{new CmdCommit(), new CmdCreate(), new CmdDrop(), new CmdRollback(), + new CmdShowTables(), new CmdSize(), new CmdUse(), new MultiFileHashMapExit(), new MultiFileHashMapGet(), + new MultiFileHashMapRemove(), new MultiFileHashMapPut(), new MultiFileHashMapList()}; + + File base = new File(currentProperty); + try { + if (!base.exists()) { + base.createNewFile(); + } + + base = base.getCanonicalFile(); + MultiFileHashMapState startState = new MultiFileHashMapState(base); + Shell mfhm = new Shell(startState); + + Executor exec = new Executor(); + exec.setMapOfCmd(cmds); + + if (args.length > 0) { + mfhm.batchState(args, exec); + } else { + mfhm.interactiveState(exec); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + System.exit(1); + } + + System.exit(0); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderFactoryTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderFactoryTest.java new file mode 100644 index 000000000..6338c553b --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderFactoryTest.java @@ -0,0 +1,44 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.test; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTableProviderFactory; + +import java.io.File; + +public class MultiFileHashMapTableProviderFactoryTest { + + private static File database; + private MultiFileHashMapTableProviderFactory tableProviderFactory = new MultiFileHashMapTableProviderFactory(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + database = new File("newNotExistedTable").getCanonicalFile(); + if (database.isFile()) { + database.delete(); + } + database.mkdir(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + database.delete(); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateNull() throws Exception { + tableProviderFactory.create(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateEmpty() throws Exception { + tableProviderFactory.create(" "); + } + + @Test + public void testCreateNotExisted() throws Exception { + Assert.assertNotNull(tableProviderFactory.create("newNotExistedTable")); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderTest.java new file mode 100644 index 000000000..f85344baa --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderTest.java @@ -0,0 +1,113 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTableProvider; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTableProviderFactory; + +import java.io.File; + +public class MultiFileHashMapTableProviderTest { + private static File database; + private static MultiFileHashMapTableProvider tableProvider; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + database = new File("javatest").getCanonicalFile(); + database.mkdir(); + MultiFileHashMapTableProviderFactory factory = new MultiFileHashMapTableProviderFactory(); + tableProvider = factory.create(database.toString()); + } + + @Before + public void setUp() throws Exception { + tableProvider.createTable("existingTable"); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetTableNull() throws Exception { + + tableProvider.getTable(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetTableEmpty() throws Exception { + + tableProvider.getTable(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetTableIncorrect() throws Exception { + + tableProvider.getTable("$#*_%"); + } + + @Test + public void testGetTableExisted() throws Exception { + + Assert.assertEquals("existingTable", tableProvider.getTable("existingTable").getName()); + } + + @Test + public void testGetTableNotExisted() throws Exception { + + Assert.assertNull(tableProvider.getTable("notExistingTable")); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTableNull() throws Exception { + + tableProvider.createTable(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTableEmpty() throws Exception { + + tableProvider.createTable(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTableIncorrect() throws Exception { + + tableProvider.createTable("$#*_%"); + } + + @Test + public void testCreateTableExisted() throws Exception { + + Assert.assertNull(tableProvider.createTable("existingTable")); + } + + @Test(expected = IllegalStateException.class) + public void testRemoveTableNotExisted() throws Exception { + + tableProvider.removeTable("notExistingTable"); + } + + @Test + public void testRemoveTableExisted() throws Exception { + + tableProvider.removeTable("existingTable"); + } + + @Test + public void testCreateTableNotExisted() throws Exception { + + Assert.assertNotNull(tableProvider.createTable("notExistingTable")); + tableProvider.removeTable("notExistingTable"); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveTableNull() throws Exception { + + tableProvider.removeTable(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveTableEmpty() throws Exception { + + tableProvider.removeTable(""); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableTest.java new file mode 100644 index 000000000..6692813bc --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableTest.java @@ -0,0 +1,150 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.test; + +import org.junit.Before; +import org.junit.Test; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTable; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static junit.framework.Assert.*; + +public class MultiFileHashMapTableTest { + + private static MultiFileHashMapTable table; + + @Before + public void setUp() throws Exception { + File file = new File("testingTable"); + file.mkdir(); + table = new MultiFileHashMapTable(file); + } + + @Test + public void testGetName() throws Exception { + + assertEquals("testingTable", table.getName()); + } + + @Test + public void testGetInEnglish() throws Exception { + + table.put("getEnglishKey", "getEnglishValue"); + assertEquals("getEnglishValue", table.get("getEnglishKey")); + } + + @Test + public void testGetInRussian() throws Exception { + + table.put("ключ", "значение"); + assertEquals("значение", table.get("ключ")); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetNull() throws Exception { + + table.get(null); + } + + @Test + public void testPutNew() throws Exception { + + assertNull(table.put("putNewKey", "putNewValue")); + table.remove("putNewKey"); + } + + @Test(expected = IllegalArgumentException.class) + public void testPutNullKey() throws Exception { + + table.put(null, "valueOfNullKey"); + } + + @Test(expected = IllegalArgumentException.class) + public void testPutEmptyKey() throws Exception { + + table.put("", "valueOfEmptyKey"); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveNull() throws Exception { + + table.remove(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveWhitespace() throws Exception { + table.remove(" "); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetWhitespace() throws Exception { + table.get(" "); + } + + @Test + public void testRemoveExisted() throws Exception { + + table.put("Key", "Value"); + assertEquals("Value", table.remove("Key")); + } + + @Test + public void testRemoveNotExisted() throws Exception { + + assertNull(table.remove("Key")); + } + + @Test + public void testSize() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + assertEquals(3, table.size()); + } + + @Test + public void testPutAndRollback() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + assertEquals(3, table.rollback()); + } + + @Test + public void testRollbackAndCheck() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + table.rollback(); + assertEquals(0, table.size()); + } + + @Test + public void testPutCommit() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + assertEquals(3, table.commit()); + } + + @Test + public void testRemovePutCommit() throws Exception { + table.put("Key", "Value"); + table.commit(); + table.remove("Key"); + table.put("Key", "Value"); + assertEquals(0, table.commit()); + } + + @Test + public void testList() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + table.commit(); + List ans = new ArrayList(Arrays.asList("Key1", "Key2", "Key3")); + assertEquals(ans, table.list()); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java deleted file mode 100644 index 002be3b83..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java +++ /dev/null @@ -1,52 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.nio.file.Path; - -public class Cat implements Command { - - public final String getName() { - - return "cat"; - } - - public static void catenate(final String fileName) { - String buffer = ""; - try (FileReader filescan = new FileReader(fileName); - BufferedReader bufreader = new BufferedReader(filescan)) { - while ((buffer = bufreader.readLine()) != null) { - System.out.println(buffer); - } - } catch (FileNotFoundException e) { - System.out.println("File \"" + fileName + "\" not found"); - return; - } catch (IOException e) { - System.out.println("Can't read file"); - } - } - - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - Path thePath = shell.getState().getPath().resolve(args[0]); - try { - if (1 == args.length) { - if (thePath.toFile().exists()) { - catenate(thePath.toString()); - } - } else { - throw new IOException("not allowed number of arguments"); - } - } catch (IOException e) { - throw e; - } finally { - while (!thePath.toFile().isDirectory()) { - thePath = thePath.getParent(); - } - shell.setState(thePath); - } - } -} - - diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java deleted file mode 100644 index 76f328779..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java +++ /dev/null @@ -1,27 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -public class Cd implements Command { - - public final String getName() { - - return "cd"; - } - - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (1 == args.length) { - Path thePath = shell.getState().getPath().resolve(args[0]); - File theFile = new File(thePath.toString()); - if (!theFile.isDirectory()) { - throw new IOException("Directory doesn't exist"); - } - shell.setState(thePath.normalize()); - } else { - throw new IOException("not allowed number of arguments"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java index 46e978711..ec544c3a0 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java @@ -1,10 +1,39 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapState; + import java.io.IOException; -public interface Command { +public abstract class Command { + + protected int numArg; + + public boolean checkArgs(int needArg, int haveArg) { + if (needArg != haveArg) { + System.out.println("incorrect number of arguments"); + return false; + } + return true; + } + + public boolean checkTable(MultiFileHashMapState inState) throws IOException { + if (inState.getCurrentTable() == null) { + System.out.println("no table"); + return false; + } + return true; + } + + public abstract String getName(); + + public abstract void work(State shell, String[] args) throws IOException; + + public void executeCmd(State shell, String[] args) throws IOException { + if (!checkArgs(numArg, args.length)) { + return; + } + work(shell, args); + } - String getName(); - void executeCmd(Shell shell, String[] args) throws IOException; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java deleted file mode 100644 index 12e4f59ea..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java +++ /dev/null @@ -1,69 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class Cp implements Command { - - public final String getName() { - - return "cp"; - } - - private void copy(final Path source, final Path target) throws IOException { - - if (source.toFile().isFile()) { - Files.copy(source, target.resolve(source.getFileName())); - } else { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - for (File sourceEntry : masOfSource != null ? masOfSource : new File[0]) { - copy(sourceEntry.toPath(), target); - } - } - } - - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (2 == args.length) { - Path source = shell.getState().getPath().resolve(args[0]).normalize(); - Path target = shell.getState().getPath().resolve(args[1]).normalize(); - if (source.toFile().isFile() && target.toFile().isFile()) { - throw new IOException("not allowed to copy file to file"); - } - if (source.equals(target)) { - throw new IOException("not allowed to copy file on itself"); - } - if (!source.toFile().exists()) { - throw new IOException("source file doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isFile()) { - throw new IOException("not allowed to copy directory in file"); - } - if (source.toFile().isDirectory() && !target.toFile().exists()) { - throw new IOException("target directory doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isDirectory() && target.startsWith(source)) { - throw new IOException("not allowed to copy parent directory in kid's directory"); - } - - if (source.toFile().isFile() && !target.toFile().exists()) { - Files.copy(source, target); - } else if (source.toFile().isFile() && target.toFile().isDirectory()) { - Files.copy(source, target.resolve(source.getFileName())); - } else if (source.toFile().isDirectory() && target.toFile().isDirectory()) { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - if (masOfSource != null) { - for (File sourceEntry : masOfSource) { - copy(sourceEntry.toPath(), target); - } - } - } - } else { - throw new IOException("not allowed number of arguments"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java index f82645080..951064246 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java @@ -4,11 +4,18 @@ import java.util.HashMap; import java.util.Map; -public abstract class Executor { +public class Executor { public Map mapOfCmd = new HashMap(); - final String[] argsCheck(final String inCommand) { + public void setMapOfCmd(Command[] cmds) { + for (Command cmd : cmds) { + mapOfCmd.put(cmd.getName(), cmd); + } + } + + + public String[] argsCheck(String inCommand) { int space = inCommand.indexOf(" "); if (-1 == space) { @@ -18,7 +25,7 @@ final String[] argsCheck(final String inCommand) { return substr.trim().split("\\ "); } - public final String cmdCheck(final String cmd) { + public String cmdCheck(String cmd) { String tmp; int space = cmd.indexOf(" "); @@ -29,13 +36,12 @@ public final String cmdCheck(final String cmd) { return tmp; } - public abstract void list(); - final void execute(final Shell shell, final String cmd) throws IOException { + public void execute(State state, String cmd) throws IOException { if (!mapOfCmd.containsKey(cmdCheck(cmd))) { throw new IOException("Can't find key"); } - mapOfCmd.get(cmdCheck(cmd)).executeCmd(shell, argsCheck(cmd)); + mapOfCmd.get(cmdCheck(cmd)).executeCmd(state, argsCheck(cmd)); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java deleted file mode 100644 index 5825a3875..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java +++ /dev/null @@ -1,17 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.IOException; - -public class Exit implements Command { - - @Override - public final String getName() { - - return "exit"; - } - - @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - System.exit(0); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java deleted file mode 100644 index d66826e57..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java +++ /dev/null @@ -1,29 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; - -public class Ls implements Command { - - public final String getName() { - - return "ls"; - } - - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (0 == args.length) { - File currentDirectory = new File(shell.getState().getPath().toString()); - File[] listOfFiles = currentDirectory.listFiles(); - PrintStream print = new PrintStream(System.out); - if (listOfFiles != null) { - for (File listOfFile : listOfFiles) { - print.println(listOfFile.getName()); - } - } - } else { - throw new IOException("not allowed number of arguments"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java deleted file mode 100644 index 2b657f595..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java +++ /dev/null @@ -1,22 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; - -public class Mkdir implements Command { - - public final String getName() { - - return "mkdir"; - } - - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (1 == args.length) { - File theFile = new File(shell.getState().getPath().resolve(args[0]).toString()); - theFile.mkdir(); - } else { - throw new IOException("Not correct name of directory"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java deleted file mode 100644 index 3cd84da40..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java +++ /dev/null @@ -1,77 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class Mv implements Command { - - public final String getName() { - - return "mv"; - } - - private void move(final Path source, final Path target) throws IOException { - - if (source.toFile().isFile()) { - Files.copy(source, target); - } else { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - for (File sourceEntry : masOfSource != null ? masOfSource : new File[0]) { - move(sourceEntry.toPath(), target); - } - } - source.toFile().delete(); - } - - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (2 == args.length) { - Path source = shell.getState().getPath().resolve(args[0]).normalize(); - Path target = shell.getState().getPath().resolve(args[1]).normalize(); - if (source.toFile().isFile() && target.toFile().isFile()) { - throw new IOException("not allowed to move file to file"); - } - if (source.equals(target)) { - throw new IOException("not allowed to move file on itself"); - } - if (!source.toFile().exists()) { - throw new IOException("source file doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isFile()) { - throw new IOException("not allowed to move directory in file"); - } - if (source.toFile().isDirectory() && !target.toFile().exists()) { - throw new IOException("target directory doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isDirectory() && target.startsWith(source)) { - throw new IOException("not allowed to move parent directory in kid's directory"); - } - - if (source.toFile().isFile() && !target.toFile().exists()) { - Files.copy(source, target); - source.toFile().delete(); - } else if (source.toFile().isFile() && target.toFile().isDirectory()) { - Files.copy(source, target.resolve(source.getFileName())); - source.toFile().delete(); - } else if (source.toFile().isDirectory() && target.toFile().isDirectory()) { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - if (masOfSource != null) { - for (File sourceEntry : masOfSource) { - move(sourceEntry.toPath(), target); - } - } - } - } else { - throw new IOException("not allowed number of arguments"); - } - Path path = shell.getState().getPath(); - while (!path.toFile().isDirectory()) { - path = path.getParent(); - } - shell.setState(path); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java deleted file mode 100644 index cf91c4686..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.IOException; - -public class Pwd implements Command { - - public final String getName() { - - return "pwd"; - } - - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length > 0) { - throw new IOException("too many args"); - } - System.out.println(shell.getState().getPath().toString()); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java index 0238b43d9..7f260f138 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java @@ -4,14 +4,14 @@ import java.io.IOException; import java.nio.file.Path; -public class Rm implements Command { +public class Rm extends Command { - public final String getName() { + public String getName() { return "rm"; } - public static void remove(final Path thePath) throws IOException { + public static void remove(Path thePath) throws IOException { File theFile = thePath.toFile(); if (theFile.isFile()) { @@ -32,9 +32,9 @@ public static void remove(final Path thePath) throws IOException { } } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { - Path thePath = shell.getState().getPath().resolve(args[0]); + Path thePath = inState.getPath().resolve(args[0]); try { if (1 == args.length) { if (thePath.toFile().exists()) { @@ -49,7 +49,9 @@ public final void executeCmd(final Shell shell, final String[] args) throws IOEx while (!thePath.toFile().isDirectory()) { thePath = thePath.getParent(); } - shell.setState(thePath); + inState.setPath(thePath); } } + public void work(ShellState inState, String[] args) throws IOException{ + } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java index 706ffb358..13ff061ac 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java @@ -1,33 +1,18 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; import java.io.IOException; -import java.io.File; import java.util.Scanner; -import java.nio.file.Path; -public class Shell { +public class Shell { - private ShellState state; + private State state; - public Shell(final File currentDirectory) { - state = new ShellState(); - state.setPath((currentDirectory.getAbsoluteFile().toPath())); - } - - public Shell() { - } - + public Shell(State inState) { - final ShellState getState() { - return state; + state = inState; } - final void setState(final Path inState) { - - state.setPath(inState); - } - - public final void batchState(final String[] args, final Executor exec) throws IOException { + public void batchState(String[] args, Executor exec) throws IOException { StringBuilder tmp = new StringBuilder(); for (String arg : args) { @@ -38,35 +23,36 @@ public final void batchState(final String[] args, final Executor exec) throws IO String cmd = ""; - for (int i = 0; i < command.length; ++i) { + for (int i = 0; i < command.length - 1; ++i) { + cmd = command[i].trim(); + if (cmd.equals("exit")) { + break; + } try { - exec.execute(this, cmd); + exec.execute(state, cmd); } catch (Exception e) { System.err.println(e.getMessage()); + System.exit(1); } } - exec.execute(this, "exit"); } - public final void interactiveState(final Executor exec) throws IOException { - - try (Scanner scanner = new Scanner(System.in)) { + public void interactiveState(Executor exec) throws IOException { - String[] cmd; - while (true) { - System.out.print("$ "); - cmd = scanner.nextLine().trim().split("\\s*;\\s*"); + Scanner scanner = new Scanner(System.in); + String[] cmd; + while (true) { + System.out.print("$ "); + cmd = scanner.nextLine().trim().split("\\s*;\\s*"); - try { - for (String aCmd : cmd) { - exec.execute(this, aCmd); - } - } catch (Exception e) { - System.err.println(e.getMessage()); + try { + for (String aCmd : cmd) { + exec.execute(state, aCmd); } - + } catch (Exception e) { + System.err.println(e.getMessage()); } } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java deleted file mode 100644 index 5f44c191f..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java +++ /dev/null @@ -1,31 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -public class ShellExecutor extends Executor { - - public ShellExecutor() { - - list(); - } - - public final void list() { - - Command pwd = new Pwd(); - mapOfCmd.put(pwd.getName(), pwd); - Command cd = new Cd(); - mapOfCmd.put(cd.getName(), cd); - Command mkdir = new Mkdir(); - mapOfCmd.put(mkdir.getName(), mkdir); - Command cp = new Cp(); - mapOfCmd.put(cp.getName(), cp); - Command mv = new Mv(); - mapOfCmd.put(mv.getName(), mv); - Command ls = new Ls(); - mapOfCmd.put(ls.getName(), ls); - Command rm = new Rm(); - mapOfCmd.put(rm.getName(), rm); - Command exit = new Exit(); - mapOfCmd.put(exit.getName(), exit); - Command cat = new Cat(); - mapOfCmd.put(cat.getName(), cat); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java index d70bd2043..f5c8a3607 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java @@ -3,16 +3,14 @@ import java.io.File; import java.io.IOException; -final class ShellMain { +public class ShellMain { - private ShellMain() { - } - - public static void main(final String[] args) throws IOException { + public static void main(String[] args) throws IOException { File currentDirectory = new File(""); - Shell shell = new Shell(currentDirectory); - ShellExecutor exec = new ShellExecutor(); + ShellState startState = new ShellState(currentDirectory); + Shell shell = new Shell(startState); + Executor exec = new Executor(); if (args.length != 0) { shell.batchState(args, exec); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java index 3f50ce8d5..ac8e0b849 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java @@ -1,17 +1,23 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; +import java.io.File; import java.nio.file.Path; public class ShellState { private Path path; - final Path getPath() { + public ShellState(File inFile) { + + path = inFile.getAbsoluteFile().toPath(); + } + + public Path getPath() { return path; } - final void setPath(final Path inPath) { + public void setPath(Path inPath) { path = inPath; }