Skip to content

Commit 61df373

Browse files
committed
Merge branch 'master' of https://github.com/dkomanov/fizteh-java-2014 into moskupols-04-JUnit-mergeable
2 parents 5d464e3 + 4d7add4 commit 61df373

File tree

145 files changed

+8985
-621
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+8985
-621
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
public interface Command {
4+
void run() throws Exception;
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
import java.io.File;
4+
5+
public class CreateCommand implements Command {
6+
public CreateCommand(MultiDataBase mdb, String name) {
7+
this.tableName = name;
8+
this.multiDataBase = mdb;
9+
}
10+
11+
private String tableName;
12+
private MultiDataBase multiDataBase;
13+
14+
public void run() throws Exception {
15+
if (multiDataBase.tables.containsKey(tableName)) {
16+
System.out.println(tableName + " exists");
17+
} else {
18+
File tableDirectory = new File(multiDataBase.dbDirectory, tableName);
19+
if (!tableDirectory.mkdir()) {
20+
throw new MultiFileHashMapException("Unable to create directory for new table");
21+
}
22+
multiDataBase.tables.put(tableName, new Table(tableDirectory));
23+
System.out.println("created");
24+
}
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
public class DropCommand implements Command {
4+
public DropCommand(MultiDataBase mdb, String name) {
5+
this.tableName = name;
6+
this.multiDataBase = mdb;
7+
}
8+
9+
private String tableName;
10+
private MultiDataBase multiDataBase;
11+
12+
public void run() throws MultiFileHashMapException {
13+
if (!multiDataBase.tables.containsKey(tableName)) {
14+
System.out.println(tableName + " not exists");
15+
} else {
16+
if (tableName.equals(multiDataBase.using)) {
17+
multiDataBase.using = null;
18+
}
19+
multiDataBase.tables.get(tableName).drop();
20+
multiDataBase.tables.remove(tableName);
21+
System.out.println("dropped");
22+
}
23+
}
24+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
import java.util.Scanner;
4+
5+
public class Executor {
6+
public Executor(String[] args) throws Exception {
7+
String dbPath = System.getProperty("fizteh.db.dir");
8+
multiDataBase = new MultiDataBase(dbPath);
9+
if (args.length == 0) {
10+
interactiveMode();
11+
} else {
12+
packageMode(args);
13+
}
14+
}
15+
16+
private MultiDataBase multiDataBase;
17+
18+
private void interactiveMode() throws Exception {
19+
Scanner in = new Scanner(System.in);
20+
System.out.print("$ ");
21+
while (in.hasNextLine()) {
22+
String[] commands = in.nextLine().trim().split(";");
23+
for (String command : commands) {
24+
executeCommand(command);
25+
}
26+
System.out.print("$ ");
27+
}
28+
in.close();
29+
System.out.close();
30+
}
31+
32+
private void packageMode(String[] args) throws Exception {
33+
StringBuilder line = new StringBuilder();
34+
for (String arg : args) {
35+
line.append(arg + ' ');
36+
}
37+
String[] commands = line.toString().trim().split(";");
38+
for (String command : commands) {
39+
executeCommand(command);
40+
}
41+
}
42+
43+
private void executeCommand(String cmd) throws Exception {
44+
String[] command = cmd.trim().split("\\s+");
45+
Command exec;
46+
switch (command[0]) {
47+
case "put":
48+
if (command.length > 3) {
49+
throw new MultiFileHashMapException("put: Too many arguments");
50+
}
51+
if (command.length < 3) {
52+
throw new MultiFileHashMapException("put: Too few arguments");
53+
}
54+
exec = new MultiPutCommand(multiDataBase, command[1], command[2]);
55+
break;
56+
case "get":
57+
if (command.length > 2) {
58+
throw new MultiFileHashMapException("get: Too many arguments");
59+
}
60+
if (command.length < 2) {
61+
throw new MultiFileHashMapException("get: Too few arguments");
62+
}
63+
exec = new MultiGetCommand(multiDataBase, command[1]);
64+
break;
65+
case "remove":
66+
if (command.length > 2) {
67+
throw new MultiFileHashMapException("remove: Too many arguments");
68+
}
69+
if (command.length < 2) {
70+
throw new MultiFileHashMapException("remove: Too few arguments");
71+
}
72+
exec = new MultiRemoveCommand(multiDataBase, command[1]);
73+
break;
74+
case "list":
75+
if (command.length > 1) {
76+
throw new MultiFileHashMapException("list: Too many arguments");
77+
}
78+
exec = new MultiListCommand(multiDataBase);
79+
break;
80+
case "create":
81+
if (command.length > 2) {
82+
throw new MultiFileHashMapException("create: Too many arguments");
83+
}
84+
if (command.length < 2) {
85+
throw new MultiFileHashMapException("create: Too few arguments");
86+
}
87+
exec = new CreateCommand(multiDataBase, command[1]);
88+
break;
89+
case "drop":
90+
if (command.length > 2) {
91+
throw new MultiFileHashMapException("drop: Too many arguments");
92+
}
93+
if (command.length < 2) {
94+
throw new MultiFileHashMapException("drop: Too few arguments");
95+
}
96+
exec = new DropCommand(multiDataBase, command[1]);
97+
break;
98+
case "use":
99+
if (command.length > 2) {
100+
throw new MultiFileHashMapException("use: Too many arguments");
101+
}
102+
if (command.length < 2) {
103+
throw new MultiFileHashMapException("use: Too few agruments");
104+
}
105+
exec = new UseCommand(multiDataBase, command[1]);
106+
break;
107+
case "show":
108+
if (command.length < 2 || !command[1].equals("tables")) {
109+
throw new MultiFileHashMapException("Unknown command");
110+
}
111+
if (command.length > 2) {
112+
throw new MultiFileHashMapException("show tables: Too many arguments");
113+
}
114+
exec = new ShowTablesCommand(multiDataBase);
115+
break;
116+
case "exit":
117+
if (command.length > 1) {
118+
throw new MultiFileHashMapException("exit: Too many arguments");
119+
}
120+
exec = new ExitCommand();
121+
break;
122+
default:
123+
throw new MultiFileHashMapException("Unknown command");
124+
}
125+
exec.run();
126+
}
127+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
public class ExitCommand implements Command {
4+
public ExitCommand() {}
5+
6+
public void run() throws MultiFileHashMapException {
7+
System.exit(0);
8+
}
9+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
import java.io.File;
4+
import java.nio.file.Files;
5+
import java.util.HashMap;
6+
7+
public class MultiDataBase {
8+
public MultiDataBase(String dbDir) throws Exception {
9+
if (dbDir == null) {
10+
throw new MultiFileHashMapException("No database directory name specified");
11+
}
12+
dbDirectory = new File(dbDir);
13+
using = null;
14+
tables = new HashMap<String, Table>();
15+
if (!Files.exists(dbDirectory.toPath()) && !dbDirectory.mkdir()) {
16+
throw new MultiFileHashMapException("Cannot create working directory");
17+
}
18+
if (!dbDirectory.isDirectory()) {
19+
throw new MultiFileHashMapException("Specified path is not a directory");
20+
}
21+
for (String tableName : dbDirectory.list()) {
22+
File tableDirectory = new File(dbDirectory, tableName);
23+
if (tableDirectory.isDirectory()) {
24+
tables.put(tableName, new Table(tableDirectory));
25+
} else {
26+
throw new MultiFileHashMapException(tableName + " from database is not a directory");
27+
}
28+
}
29+
}
30+
31+
public HashMap<String, Table> tables;
32+
public File dbDirectory;
33+
public String using;
34+
35+
public Table getUsing() {
36+
return tables.get(using);
37+
}
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
public class MultiFileHashMapException extends Exception {
4+
public MultiFileHashMapException(String msg) {
5+
System.err.println(msg);
6+
System.exit(1);
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
public class MultiFileHashMapMain {
4+
public static void main(String[] args) throws Exception {
5+
new Executor(args);
6+
}
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
import ru.fizteh.fivt.students.EgorLunichkin.filemap.DataBase;
4+
import ru.fizteh.fivt.students.EgorLunichkin.filemap.GetCommand;
5+
6+
public class MultiGetCommand implements Command {
7+
public MultiGetCommand(MultiDataBase mdb, String key) {
8+
this.key = key;
9+
this.multiDataBase = mdb;
10+
}
11+
12+
private MultiDataBase multiDataBase;
13+
private String key;
14+
15+
public void run() {
16+
if (multiDataBase.using == null) {
17+
System.out.println("no table");
18+
} else {
19+
int hashCode = Math.abs(key.hashCode());
20+
int dir = hashCode % 16;
21+
int file = hashCode / 16 % 16;
22+
DataBase dataBase = multiDataBase.getUsing().dataBases[dir][file];
23+
if (dataBase == null) {
24+
System.out.println("not found");
25+
} else {
26+
new GetCommand(dataBase, key).run();
27+
}
28+
}
29+
}
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ru.fizteh.fivt.students.EgorLunichkin.MultiFileHashMap;
2+
3+
import ru.fizteh.fivt.students.EgorLunichkin.filemap.DataBase;
4+
import ru.fizteh.fivt.students.EgorLunichkin.filemap.ListCommand;
5+
6+
public class MultiListCommand implements Command {
7+
public MultiListCommand(MultiDataBase mdb) {
8+
this.multiDataBase = mdb;
9+
}
10+
11+
private MultiDataBase multiDataBase;
12+
13+
public void run() {
14+
if (multiDataBase.using == null) {
15+
System.out.println("no table");
16+
} else {
17+
StringBuilder listKeys = new StringBuilder();
18+
for (int dir = 0; dir < 16; ++dir) {
19+
for (int file = 0; file < 16; ++file) {
20+
DataBase dataBase = multiDataBase.getUsing().dataBases[dir][file];
21+
if (dataBase != null) {
22+
String curList = new ListCommand(dataBase).list();
23+
if (curList.length() > 0) {
24+
if (listKeys.length() > 0) {
25+
listKeys.append(", ");
26+
}
27+
listKeys.append(curList);
28+
}
29+
}
30+
}
31+
}
32+
System.out.println(listKeys.toString());
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)