-
Notifications
You must be signed in to change notification settings - Fork 69
Parallel1.1 #602
base: master
Are you sure you want to change the base?
Parallel1.1 #602
Changes from 20 commits
7fea5b6
cf2d795
f915545
f8567f3
421371d
78f2eff
7795b38
e613dc2
bd030d1
509e893
24bcb5e
2736957
674eeec
4f2444c
df9e427
768c00f
0e7eae8
3138e18
10616cf
16a1b22
14ca53e
3985e62
a846f09
9f8fa63
0cf94d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands.*; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Interpreter; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTable; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| public class Main { // Using JSON format. | ||
| public static void main(final String[] args) throws Exception { | ||
| new MakeDirs().execute(null); | ||
| Set<Command> commandSet = new HashSet<>(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. унести в launchInterpreter
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Унес |
||
| ObjectTable currentTableObject = null; | ||
| boolean fromCmdLine; | ||
| if (args.length == 0) { | ||
| fromCmdLine = false; | ||
| fillCommandSet(commandSet, currentTableObject, fromCmdLine); | ||
| Interpreter interpreter = new Interpreter(commandSet); | ||
| interpreter.startUp(null, false); | ||
| } else { | ||
| fromCmdLine = true; | ||
| fillCommandSet(commandSet, currentTableObject, fromCmdLine); | ||
| Interpreter interpreter = new Interpreter(commandSet); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вынести в отдельную функцию строки 23-24 (они же 18-19)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вынес |
||
| String cmd = String.join(" ", args); | ||
| cmd = cmd.replaceAll("\\s+", " "); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Объединить 25-26 в одну строку
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Исправлено вместе с предыдущим |
||
| System.out.println(cmd); | ||
| interpreter.startUp(cmd, fromCmdLine); | ||
| new Exit().execute(null); | ||
| } | ||
| } | ||
|
|
||
| private static void fillCommandSet(Set<Command> commandSet, ObjectTable currentTableObject, boolean fromCmdLine) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Второй и третий аргументы лишние
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Убрал |
||
| commandSet.add(new Commit()); | ||
| commandSet.add(new Create()); | ||
| commandSet.add(new Drop()); | ||
| commandSet.add(new Exit()); | ||
| commandSet.add(new FillTable()); | ||
| commandSet.add(new Get()); | ||
| commandSet.add(new List()); | ||
| commandSet.add(new MakeDirs()); | ||
| commandSet.add(new Put()); | ||
| commandSet.add(new Remove()); | ||
| commandSet.add(new Rollback()); | ||
| commandSet.add(new ShowTables()); | ||
| commandSet.add(new Use()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTable; | ||
|
|
||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class CommandTools { | ||
| protected static final String DATA_BASE_NAME = System.getProperty("fizteh.db.dir"); | ||
| static boolean tableIsChosen = false; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Убрать эту переменную. Она однозначно вычисляется по |
||
| static final int DIR_NUM = 16; | ||
| static final int FILE_NUM = 16; | ||
| static final String DIR_EXT = ".dir"; | ||
| static final String FILE_EXT = ".dat"; | ||
| static String usingTableName; | ||
| public static final Charset UTF = StandardCharsets.UTF_8; | ||
| static ObjectTable currentTable; | ||
| public static void informToChooseTable() { | ||
| System.err.println("table is not chosen"); | ||
| } | ||
|
|
||
| public static boolean amountOfArgumentsIs(int argsAmount, String[] cmd) { | ||
| return argsAmount == cmd.length; | ||
| } | ||
|
|
||
| public static boolean amountOfArgumentsIsMoreThan(int argsAmount, String[] cmd) { | ||
| return argsAmount < cmd.length; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
|
|
||
| public class Commit extends Command { | ||
|
|
||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| if (CommandTools.amountOfArgumentsIs(1, cmd)) { | ||
| CommandTools.currentTable.commit(); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCmdName() { | ||
| return "commit"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTable; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTableProvider; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
|
|
||
| public class Create extends Command { | ||
| private static final String SPLIT_BY_RIGHT_BRACKET = "\\s*\\)\\s*"; | ||
| private static final String SPLIT_BY_LEFT_BRACKET = "\\s*\\(\\s*"; | ||
| private static final String SPLIT_BY_SPACE = "\\s+"; | ||
|
|
||
| @Override | ||
| public String getCmdName() { | ||
| return "create"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| if (CommandTools.amountOfArgumentsIsMoreThan(2, cmd)) { | ||
| String createParameter; // (...) - type list. | ||
| String tableName = cmd[1]; | ||
| createParameter = String.join(" ", Arrays.copyOfRange(cmd, 2, cmd.length)); | ||
| List<Class<?>> typeList; | ||
| try { | ||
| typeList = getTypeList(createParameter); | ||
| if (typeList != null) { | ||
| if (new ObjectTableProvider().createTable(tableName, typeList) != null) { | ||
| System.out.println("created"); | ||
| } else { | ||
| System.out.println(tableName + " exists"); | ||
| } | ||
| } else { | ||
| return false; | ||
| } | ||
| } catch (IOException e) { | ||
| System.err.println(e); | ||
| } catch (Exception e) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это ради какого типа исключений оставлено?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нет таких исключений, IOException достаточно. Убрал |
||
| System.err.println(e); | ||
| } | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public List<Class<?>> getTypeList(String line) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я просил написать тесты на этот метод
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ObjectTable temp = new ObjectTable(); | ||
| List<Class<?>> typeList = new LinkedList<>(); | ||
| line = line.replaceAll(SPLIT_BY_RIGHT_BRACKET, ""); | ||
| line = line.replaceAll(SPLIT_BY_LEFT_BRACKET, ""); | ||
| String[] tmp = line.split(SPLIT_BY_SPACE); | ||
| for (String str : tmp) { | ||
| Class<?> type = temp.getType(str); | ||
| if (type != null) { | ||
| typeList.add(type); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| return typeList; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTableProvider; | ||
|
|
||
| public class Drop extends Command { | ||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| if (CommandTools.amountOfArgumentsIs(2, cmd)) { | ||
| try { | ||
| new ObjectTableProvider().removeTable(cmd[1]); | ||
| System.out.println("dropped"); | ||
| } catch (Exception e) { | ||
| System.err.println(e); | ||
| } | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCmdName() { | ||
| return "drop"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
|
|
||
| public class Exit extends Command { | ||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| if (CommandTools.currentTable != null && exitAndUseAvailable()) { | ||
| new FillTable().execute(null); | ||
| } else { | ||
| return false; | ||
| } | ||
| System.exit(0); | ||
| return true; | ||
| } | ||
|
|
||
| public boolean exitAndUseAvailable() { | ||
| if (CommandTools.currentTable != null) { | ||
| int uncommitedChanges = CommandTools.currentTable.storage.get().size() | ||
| - CommandTools.currentTable.commitStorage.size(); | ||
| if (uncommitedChanges == 0 || !CommandTools.tableIsChosen) { | ||
| return true; | ||
| } | ||
| System.out.println(uncommitedChanges + " uncommited changes"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCmdName() { | ||
| return "exit"; | ||
| } | ||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectStoreable; | ||
| import java.io.DataOutputStream; | ||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.Map; | ||
|
|
||
| public class FillTable extends Command { | ||
| private final Charset coding = StandardCharsets.UTF_8; | ||
|
|
||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| for (Map.Entry<String, ObjectStoreable> entry : CommandTools.currentTable.commitStorage.entrySet()) { | ||
| int hashCode = entry.getKey().hashCode(); | ||
| Integer nDirectory = hashCode % CommandTools.DIR_NUM; | ||
|
||
| Integer nFile = hashCode / CommandTools.DIR_NUM % CommandTools.FILE_NUM; | ||
| Path fileName = Paths.get(CommandTools.DATA_BASE_NAME, CommandTools.currentTable.getName(), nDirectory | ||
| + CommandTools.DIR_EXT); | ||
| File file = new File(fileName.toString()); | ||
| if (!file.exists()) { | ||
| file.mkdir(); | ||
| } | ||
| fileName = Paths.get(fileName.toString(), nFile + CommandTools.FILE_EXT); | ||
| file = new File(fileName.toString()); | ||
| try { | ||
| if (!file.exists()) { | ||
| file.createNewFile(); | ||
| } | ||
| byte[] bytesKey = (" " + entry.getKey() + " ").getBytes(coding); | ||
| DataOutputStream stream = new DataOutputStream(new FileOutputStream(fileName.toString(), true)); | ||
| stream.write(bytesKey.length); | ||
| stream.write(bytesKey); | ||
| byte[] bytesVal = ((" " + entry.getValue().serialisedValue + " ").getBytes(coding)); | ||
| stream.write(bytesVal.length - 1); | ||
| stream.write(bytesVal); | ||
| stream.close(); | ||
| } catch (IOException e) { | ||
| System.err.println(e); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCmdName() { | ||
| return "fill table"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectStoreable; | ||
|
|
||
| public class Get extends Command { | ||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| if (CommandTools.amountOfArgumentsIs(2, cmd)) { | ||
| try { | ||
| if (CommandTools.tableIsChosen) { | ||
| ObjectStoreable temp = (ObjectStoreable) CommandTools.currentTable.get(cmd[1]); | ||
| if (temp == null) { | ||
| System.err.println("not found"); | ||
| } else { | ||
| System.out.println("found"); | ||
| System.out.println(temp.serialisedValue); | ||
| } | ||
| } else { | ||
| CommandTools.informToChooseTable(); | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(e); | ||
| } | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| @Override | ||
| public String getCmdName() { | ||
| return "get"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
|
|
||
| public class List extends Command { | ||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| if (CommandTools.amountOfArgumentsIs(1, cmd)) { | ||
| if (CommandTools.tableIsChosen) { | ||
| java.util.List<String> list = CommandTools.currentTable.list(); | ||
| int size = 0; | ||
| for (Object iter : list) { | ||
| if (size < CommandTools.currentTable.storage.get().size() - 1) { | ||
| System.out.print(iter + ", "); | ||
| } else { | ||
| System.out.print(iter); | ||
| } | ||
| ++size; | ||
| } | ||
| if (size != 0) { | ||
| System.out.println(); | ||
| } | ||
| } else { | ||
| CommandTools.informToChooseTable(); | ||
| } | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCmdName() { | ||
| return "list"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands; | ||
|
|
||
| import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| public class MakeDirs extends Command { | ||
|
||
| @Override | ||
| public boolean execute(String[] cmd) { | ||
| File file = new File(CommandTools.DATA_BASE_NAME); | ||
| if (!file.exists()) { | ||
| file.mkdirs(); | ||
| } else { | ||
| if (!file.isDirectory()) { | ||
| System.err.println(CommandTools.DATA_BASE_NAME + " is not a directory"); | ||
| System.exit(1); | ||
| } else { | ||
| for (File sub : file.listFiles()) { | ||
| if (!sub.isDirectory() && !sub.isHidden()) { | ||
| System.err.println(CommandTools.DATA_BASE_NAME + File.separator | ||
|
||
| + sub.getName() + " is not a directory"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCmdName() { | ||
| return "make"; | ||
| } | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это надо перенести в launchInterpreter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Перенес makeDirs() в launchInterpreter