Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Насибуллин Оскар, 397, Storeable #533

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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,27 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Table;

import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdCommit extends Command {
public CmdCommit() {
setName("commit");
setArgs(1);
}

@Override
public String run(List<String> args) throws Exception {
checkArgumentsAmount(args);
String resultMessage;
Table currTable = tableProvider.getTable(currentTableName);
if (currTable == null) {
throw new Exception("no table");
}
resultMessage = new Integer(currTable.commit()).toString();
return resultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Table;
import ru.fizteh.fivt.students.oscar_nasibullin.Storeable.TableImpl;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdCreate extends Command {
public CmdCreate() {
setName("create");
//setArgs(); >= 3
}

@Override
public String run(List<String> args) throws Exception {
if (3 > args.size()) {
throw new IllegalArgumentException("Not enough arguments for " + getName());
}
String resultMessage = "";
Table createTable = tableProvider.getTable(args.get(1));

if (createTable != null) {
resultMessage = args.get(1) + " exists";
} else {
List<Class<?>> types = new ArrayList<>();
for (String typeName : args.subList(2, args.size())) {
typeName = typeName.replaceAll("[()]", "");
if (!typeName.equals("")) {
String className = TableImpl.convertClassName(typeName);
try {
types.add(Class.forName(className));
} catch (ClassNotFoundException e) {
throw new Exception("wrong type (no type with name: " + typeName + ")", e);
}
}
}
tableProvider.createTable(args.get(1), types);
resultMessage = "created";
}
return resultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Table;


import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdDrop extends Command {
public CmdDrop() {
setName("drop");
setArgs(2);
}

@Override
public String run(List<String> args) throws Exception {
checkArgumentsAmount(args);
String resultMessage = "";
Table dropTable = tableProvider.getTable(args.get(1));

if (dropTable == null) {
resultMessage = args.get(1) + " not exists";
} else {
tableProvider.removeTable(args.get(1));
resultMessage = "dropped";
}
return resultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;


import ru.fizteh.fivt.storage.structured.Table;
import ru.fizteh.fivt.students.oscar_nasibullin.Storeable.TableImpl;

import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdExit extends Command {
public CmdExit() {
setName("exit");
setArgs(1);
}

@Override
public String run(List<String> args) throws Exception {
checkArgumentsAmount(args);
Table currTable = tableProvider.getTable(currentTableName);
if (tableProvider.getTable(currentTableName) != null) {
Integer unsaved = tableProvider.getTable(currentTableName).getNumberOfUncommittedChanges();
if (unsaved == 0) {
((TableImpl) tableProvider.getTable(currentTableName)).close();
} else {
throw new Exception(unsaved.toString() + " unsaved changes");
}
}
try {
if (tableProvider.getTable(currentTableName) != null) {
((TableImpl) currTable).close();
}
} catch (Exception e) {
if (e.getMessage() != null) {
System.err.println("exit error: " + e.getMessage());
} else {
System.err.println("exit error: something went wrong when I tried to close current table");
e.printStackTrace();
}
System.exit(1);
}
System.exit(0);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Table;
import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdGet extends Command {
public CmdGet() {
setName("get");
setArgs(2);
}

@Override
public String run(List<String> args) throws Exception {
checkArgumentsAmount(args);
String resultMessage = "";
Table currTable = tableProvider.getTable(currentTableName);
if (currTable == null) {
throw new Exception("no table");
}

String value = tableProvider.serialize(currTable, currTable.get(args.get(1)));
if (value != null) {
resultMessage = "found\n" + value;
} else {
resultMessage = "not found";
}
return resultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Table;
import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdList extends Command {
public CmdList() {
setName("list");
setArgs(1);
}

@Override
public String run(List<String> args) throws Exception {
checkArgumentsAmount(args);
String resultMessage = "";
Table currTable = tableProvider.getTable(currentTableName);
if (currTable == null) {
throw new Exception("no table");
}
List<String> keys = currTable.list();
resultMessage = String.join(", ", keys);
return resultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Storeable;
import ru.fizteh.fivt.storage.structured.Table;
import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdPut extends Command {
public CmdPut() {
setName("put");
//setArgs(3); >= 3
}

@Override
public String run(List<String> args) throws Exception {
if (3 > args.size()) {
throw new IllegalArgumentException("Not enough arguments for " + getName());
}
String rezultMessage = "";
Table currTable = tableProvider.getTable(currentTableName);
if (currTable == null) {
throw new Exception("no table");
}
try {
String arguments = String.join(" ", args.subList(2, args.size()));
Storeable newValue = tableProvider.deserialize(currTable, arguments);
Storeable changed = currTable.put(args.get(1), newValue);
if (changed != null) {
rezultMessage = "overwrite\n" + tableProvider.serialize(currTable, changed);
} else {
rezultMessage = "new";
}
} catch (Exception e) {
throw new Exception("wrong type (" + e.getMessage() + ")", e);
}
return rezultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Table;
import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdRemove extends Command {
public CmdRemove() {
setName("remove");
setArgs(2);
}

@Override
public String run(List<String> args) throws Exception {
checkArgumentsAmount(args);
String rezultMessage = "";
Table currTable = tableProvider.getTable(currentTableName);
if (currTable == null) {
throw new Exception("no table");
}

if (currTable.remove(args.get(1)) != null) {
rezultMessage = "removed";
} else {
rezultMessage = "not found";
}
return rezultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.storage.structured.Table;
import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdRollback extends Command {
public CmdRollback() {
setName("rollback");
setArgs(1);
}

@Override
public String run(List<String> args) throws Exception {
checkArgumentsAmount(args);
String resultMessage;
Table currTable = tableProvider.getTable(currentTableName);
if (currTable == null) {
throw new Exception("no table");
}
resultMessage = new Integer(currTable.rollback()).toString();
return resultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.fizteh.fivt.students.oscar_nasibullin.Storeable.Comands;

import ru.fizteh.fivt.students.oscar_nasibullin.Storeable.TableImpl;
import java.util.List;

/**
* Created by Oskar on 23.11.14.
*/
public class CmdShowTables extends Command {

public CmdShowTables() {
setName("show");
}

@Override
public String run(List<String> args) throws Exception {
if (args.size() > 2) {
throw new IllegalArgumentException(
"Illegal arguments for show tables");
}
if (args.size() < 2 || !args.get(1).equals("tables")) {
throw new Exception("show: no such command");
}
String resultMessage = "";
List<String> tableNames = tableProvider.getTableNames();

for (String tableName : tableNames) {
if (!tableName.equals(currentTableName)) {
((TableImpl) tableProvider.getTable(tableName)).open();
}
resultMessage += tableName + " " + tableProvider.getTable(tableName).size() + "\n";
if (!tableName.equals(currentTableName)) {
((TableImpl) tableProvider.getTable(tableName)).close();
}
}
if (resultMessage.equals("")) {
resultMessage = "no tables found";
} else {
resultMessage = resultMessage.substring(0, resultMessage.length() - 1);
}
return resultMessage;
}
}
Loading