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 20 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,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<>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Это надо перенести в launchInterpreter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Перенес makeDirs() в launchInterpreter

Copy link
Collaborator

Choose a reason for hiding this comment

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

унести в launchInterpreter

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Вынести в отдельную функцию строки 23-24 (они же 18-19)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Вынес

String cmd = String.join(" ", args);
cmd = cmd.replaceAll("\\s+", " ");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Объединить 25-26 в одну строку

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
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.

Убрал

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;
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

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) {
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.

Нет таких исключений, IOException достаточно. Убрал

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,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;
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.

Этого класса больше нет

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

В задании есть команда make?

См. также: #585 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Нет, убрал класс

@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
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.

Этого класса больше нет

+ sub.getName() + " is not a directory");
}
}
}
}
return true;
}

@Override
public String getCmdName() {
return "make";
}
}
Loading