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
19 commits
Select commit Hold shift + click to select a range
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<>();
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.

Повтор кода со строками 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.

Там нужно определить значение fromCmdLine, поэтому строку 18 нельзя вынести вверх, а строку 19 можно написать только после 18, поэтому ее тоже нельзя вверх выносить

String cmd = String.join(" ", args);
cmd = cmd.replaceAll("\\s+", " ");
System.out.println(cmd);
interpreter.startUp(cmd, fromCmdLine);
new Exit().execute(null);
}
}

private static void fillCommandSet(Set<Command> commandSet, ObjectTable currentTableObject, boolean fromCmdLine) {
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;
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
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.

Есть

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) {
System.err.println(e);
}
return true;
}
return false;
}

public List<Class<?>> getTypeList(String line) {
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();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Это выгрузка базы? Тогда:

  • команда должна называться "save table" или как-то так
  • весь код сохранения базы должен переехать внутрь пакета с базой, поскольку он нужен всем пользователям базы
  • константы FILE_NUM и DIR_NUM тоже должны уехать в пакет базы

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

Choose a reason for hiding this comment

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

Этот код нужен при использовании базы сторонним приложением в обход интерпретатора? Если да, то часть кода должна уехать в класс базы

} 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";
}
}
Loading