Skip to content
This repository was archived by the owner on Sep 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7fea5b6
Готовчиц Максим, 396, Storeable
MaximGotovchits Dec 3, 2014
cf2d795
Готовчиц Максим, 396, Storeable
MaximGotovchits Dec 3, 2014
f915545
Готовчиц Максим, 396, Storable
MaximGotovchits Dec 3, 2014
f8567f3
Готовчиц Макисм, 396, Storable
MaximGotovchits Dec 3, 2014
421371d
Готовчиц Максим, Storeable, 396
MaximGotovchits Dec 10, 2014
78f2eff
Готовчиц Максим, Storeable, 396
MaximGotovchits Dec 10, 2014
7795b38
Готовчиц Максим, Storeable, 396
MaximGotovchits Dec 10, 2014
e613dc2
Готовчиц Максим, 396, Storable
MaximGotovchits Dec 22, 2014
bd030d1
Готовчиц Максим, 396, Storable
MaximGotovchits Dec 22, 2014
509e893
Edite
MaximGotovchits Dec 22, 2014
24bcb5e
Готовчиц Максим, 396, Parallel
MaximGotovchits Dec 24, 2014
2736957
Parallel
MaximGotovchits Feb 5, 2015
674eeec
New structure
MaximGotovchits Feb 13, 2015
4f2444c
Merge branch 'fix-web-tasks' of https://github.com/dkomanov/fizteh-ja…
MaximGotovchits Feb 13, 2015
df9e427
Максим Готовчиц, Parallel, 396
MaximGotovchits Mar 18, 2015
768c00f
Готовчиц Максим, Parallel, 396
MaximGotovchits Mar 20, 2015
0e7eae8
Готовчиц Максим, Parallel, 396
MaximGotovchits Mar 20, 2015
3138e18
Готовчиц Максим, 396, Parallel
MaximGotovchits Mar 20, 2015
10616cf
Готовчиц Максим, 396, Parallel
MaximGotovchits Mar 21, 2015
16a1b22
Parallel
MaximGotovchits Mar 22, 2015
14ca53e
Parallel
MaximGotovchits Mar 22, 2015
3985e62
Parallel
MaximGotovchits Mar 22, 2015
a846f09
Parallel
MaximGotovchits Mar 23, 2015
9f8fa63
Parallel
MaximGotovchits Mar 23, 2015
0cf94d8
Parallel
MaximGotovchits Mar 23, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 java.io.File;
import java.util.HashSet;
import java.util.Set;

public class Main { // Using JSON format.
public static void main(final String[] args) throws Exception {
boolean fromCmdLine;
if (args.length == 0) {
fromCmdLine = false;
launchInterpreter(fromCmdLine, null);
} else {
fromCmdLine = true;
String cmd = String.join(" ", args).replaceAll("\\s+", " ");
launchInterpreter(fromCmdLine, cmd);
new Exit().execute(null);
}
}

private static void launchInterpreter(boolean fromCmdLine, String cmd) {
Set<Command> commandSet = new HashSet<>();
makeDirs();
fillCommandSet(commandSet);
Interpreter interpreter = new Interpreter(commandSet);
interpreter.startUp(cmd, fromCmdLine);
}

private static void fillCommandSet(Set<Command> commandSet) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

заменить сигнатуру на
private static Set<Command> commandSet getCommandSet() {
сет нужно создавать внутри этой функции

commandSet.add(new Commit());
commandSet.add(new Create());
commandSet.add(new Drop());
commandSet.add(new Exit());
commandSet.add(new Get());
commandSet.add(new List());
commandSet.add(new Put());
commandSet.add(new Remove());
commandSet.add(new Rollback());
commandSet.add(new ShowTables());
commandSet.add(new Use());
}

private static void makeDirs() {
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");
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTable;
import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTableProvider;

public class CommandTools {
static boolean tableIsChosen = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Убрать эту переменную. Она однозначно вычисляется по currentTableObject != null

public static final String DATA_BASE_NAME = System.getProperty("fizteh.db.dir");
private static ObjectTable currentTableObject;
static ObjectTableProvider currentTableProvider = new ObjectTableProvider(System.getProperty("fizteh.db.dir"));
static void informToChooseTable() {
System.err.println("table is not chosen");
}

public static ObjectTable getUsingTable() {
return currentTableObject;
}

static boolean amountOfArgumentsIs(int argsAmount, String[] cmd) {
return argsAmount == cmd.length;
}

static boolean amountOfArgumentsIsMoreThan(int argsAmount, String[] cmd) {
return argsAmount < cmd.length;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command;

public abstract class CommandWithCheckedNumArgs extends Command {
@Override
public boolean execute(String[] cmd, int args) {
if (CommandTools.amountOfArgumentsIs(args, cmd)) {
executeWithCompleteArgs(cmd);
return true;
} else {
return false;
}
}
abstract void executeWithCompleteArgs(String[] cmd);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

public class Commit extends CommandWithCheckedNumArgs {
@Override
void executeWithCompleteArgs(String[] cmd) {
CommandTools.getUsingTable().commit();
}

@Override
public String getCmdName() {
return "commit";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTable;
import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTableProvider;
import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command;

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, int args) {
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);
}
return true;
}
return false;
}

public List<Class<?>> getTypeList(String line) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Я просил написать тесты на этот метод

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Есть
typetest

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,20 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTableProvider;

public class Drop extends CommandWithCheckedNumArgs {
@Override
void executeWithCompleteArgs(String[] cmd) {
try {
new ObjectTableProvider().removeTable(cmd[1]);
System.out.println("dropped");
} catch (Exception e) {
System.err.println(e);
}
}

@Override
public String getCmdName() {
return "drop";
}
}
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 Exit extends Command {
@Override
public boolean execute(String[] cmd, int args) {
if (CommandTools.getUsingTable() != null && exitAndUseAvailable()) {
CommandTools.currentTableProvider.fillTable();
} else {
return false;
}
System.exit(0);
return true;
}

public boolean exitAndUseAvailable() {
if (CommandTools.getUsingTable() != null) {
int uncommitedChanges = CommandTools.currentTableProvider.getChangesNumber();
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,30 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectStoreable;

public class Get extends CommandWithCheckedNumArgs {
@Override
void executeWithCompleteArgs(String[] cmd) {
try {
if (CommandTools.tableIsChosen) {
ObjectStoreable temp = (ObjectStoreable) CommandTools.getUsingTable()
.get(cmd[1]);
if (temp == null) {
System.err.println("not found");
} else {
System.out.println("found");
System.out.println(temp.getSerialisedValue());
}
} else {
CommandTools.informToChooseTable();
}
} catch (Exception e) {
System.out.println(e);
}
}

@Override
public String getCmdName() {
return "get";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectStoreable;

public class List extends CommandWithCheckedNumArgs {
@Override
void executeWithCompleteArgs(String[] cmd) {
try {
if (CommandTools.tableIsChosen) {
ObjectStoreable temp = (ObjectStoreable) CommandTools.getUsingTable()
.get(cmd[1]);
if (temp == null) {
System.err.println("not found");
} else {
System.out.println("found");
System.out.println(temp.getSerialisedValue());
}
} else {
CommandTools.informToChooseTable();
}
} catch (Exception e) {
System.out.println(e);
}
}

@Override
public String getCmdName() {
return "list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectStoreable;
import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectTableProvider;
import ru.fizteh.fivt.students.MaximGotovchits.Parallel.interpreter.Command;

import java.util.Arrays;

public class Put extends Command {
public boolean execute(String[] cmd, int args) {
if (CommandTools.amountOfArgumentsIsMoreThan(2, cmd)) {
if (CommandTools.tableIsChosen) {
String putParameter;
String key = cmd[1];
putParameter = String.join(" " , Arrays.copyOfRange(cmd, 2, cmd.length));
try {
ObjectStoreable value = (ObjectStoreable) new ObjectTableProvider().
deserialize(CommandTools.getUsingTable(), putParameter);
ObjectStoreable temp = (ObjectStoreable) CommandTools.getUsingTable()
.put(key, value);
if (temp == null) {
System.out.println("new");
} else {
System.out.println("overwrite");
}
} catch (Exception e) {
System.err.println(e);
}
} else {
CommandTools.informToChooseTable();
}
return true;
}
return false;
}

@Override
public String getCmdName() {
return "put";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

import ru.fizteh.fivt.students.MaximGotovchits.Parallel.objects.ObjectStoreable;

public class Remove extends CommandWithCheckedNumArgs {
@Override
void executeWithCompleteArgs(String[] cmd) {
if (CommandTools.tableIsChosen) {
try {
ObjectStoreable temp = (ObjectStoreable) CommandTools.getUsingTable().remove(cmd[1]);
if (temp == null) {
System.out.println("not found");
} else {
System.out.println("removed");
}
} catch (Exception e) {
System.err.println(e);
}
} else {
CommandTools.informToChooseTable();
}
}

@Override
public String getCmdName() {
return "remove";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.fizteh.fivt.students.MaximGotovchits.Parallel.base.commands;

public class Rollback extends CommandWithCheckedNumArgs {
@Override
void executeWithCompleteArgs(String[] cmd) {
CommandTools.getUsingTable().rollback();
}
@Override
public String getCmdName() {
return "rollback";
}
}
Loading