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

Andrew Timokhin, JUnit #576

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ru;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Неправильное имя пакет. Имя пакета должно совпадать с путём до папки.


import java.io.IOException;
import java.util.*;

public class Functional {

void put(Map<String, Object>[] map, int i, String key, String value) {
if (i == -1) {
System.out.println("NotDefBD#");
return;
}

if (map[i].containsKey(key)) {
System.out.println("overwrite");
System.out.println(map[i].get(key));
} else {
System.out.println("new");
}
map[i].put(key, value);
}

void list(Map<String, Object>[] map) {
if (map == null || map.length == 0) {
System.out.println("DataBase empty!");
return;
}
for (int i = 0; i < 16; i++) {
if (map[i] != null) {
Set<String> time = map[i].keySet();
for (String local : time) {
System.out.println(time);
}
}
}
return;
}

void get(Map<String, Object>[] map, int i, String key) {
if (i == -1) {
System.out.println("NotDefBD#");
return;
}
if (map[i] != null) {
if (map[i].containsKey(key)) {
System.out.println("found");
System.out.println(map[i].get(key));
} else {
System.out.println("not found");
}
}
}

void remove(Map<String, Object>[] map, int i, String key) {
if (i == -1) {
System.out.println("NotDefBD#");
return;
}
if (map[i] != null) {
if (map[i].containsKey(key)) {
map[i].remove(key);
System.out.println("deleted");
} else {
System.out.println("not found");
}
}
}

void showtables(TableDriver[] td) throws IOException, NullPointerException {
if (td != null) {
for (int i = 0; i < td.length; i++) {
if (td[i] != null) {

System.out.println("in table we have " + td[i].getName()
+ " " + td[i].size());
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.fizteh.fivt.students.AndrewTimokhin.MultiDataBase;

import java.io.*;
import java.util.*;

public class MainClass {

public static void main(String[] args) {
@SuppressWarnings("unchecked")
Map<String, Object>[] map = (Map<String, Object>[]) new Map[16];
for (int i = 0; i < 16; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Завести константу под 16, дать осмысленное название

map[i] = new HashMap<String, Object>();
}

Reader rd = new Reader(System.getProperty("fizteh.db.dir").toString());
map = rd.read();
Write wr = new Write(System.getProperty("fizteh.db.dir").toString());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Избежать копирования. Закешировать строку, которая достаётся через getProperty().

toString() лишний?

Functional fn = new Functional(map);
ModeWork mw = new ModeWork(map, System.getProperty("fizteh.db.dir")
.toString());
mw.usermode(fn);

try {
wr.write(map);

} catch (IOException e) {
System.out.println(e.toString());
}

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package ru.fizteh.fivt.students.AndrewTimokhin.MultiDataBase;

import java.util.*;

class ModeWork {
String dir;
TableWork tw = new TableWork();

public ModeWork(Map<String, Object>[] time, String direct) {
dir = direct;
}

int stepMode(String[] array, Functional f, int index) {

switch (array[0 + index]) {
case "put":
f.put(f.map, tw.setDir, array[1 + index], array[2 + index]);
return 2;
case "get":
f.get(f.map, tw.setDir, array[1 + index]);
return 1;
case "remove":
f.remove(f.map, tw.setDir, array[1 + index]);
return 1;
case "create":
tw.create(f.map, dir, array[1 + index]);
tw.createforUser(f.map, Integer.parseInt(array[1 + index]));
return 1;
case "drop":
tw.drop(dir, array[1 + index]);
tw.dropForUser(f.map, Integer.parseInt(array[1 + index]));
return 1;
case "use":
if (tw.userUse(dir, array[1 + index]) == 0) {
System.out.println("not exist");
} else {
System.out.println("using " + array[1 + index]);
}
tw.setDir = Integer.parseInt(array[1 + index]
.replaceAll(".dat", ""));
return 1;

case "showtables":
f.showtables();
return 1;
case "exit":
return -1;
default:
System.out.println("Unknow operation. Fail.");
return -1;
}
}

void usermode(Functional func) {

String str = new String();
String[] array;
Scanner rd = new Scanner(System.in);
while (true) {
System.out.print("$ ");
str = rd.nextLine().toString();
array = str.trim().split(" ");

if (stepMode(array, func, 0) == -1) {
break;
}
}
rd.close();

}

/*
* void interactive( Map<String, Object>[] time , String[] mass) { int
* offset = 0; Functional func = new Functional(time); int i = 0; while
* (true) { if (i < mass.length) { offset = stepMode(time,mass, func, i); i
* += offset; if (offset == -1) { break; } i++; } else { break; } }
*
* }
*/

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ru.fizteh.fivt.students.AndrewTimokhin.MultiDataBase;

import java.util.*;
import java.io.*;

public class Reader {
private TableWork tw = new TableWork();
private String filepath;

private Map<String, Object>[] map;

public Reader(String path) {
filepath = path;

map = (Map<String, Object>[]) new Map[16];

}

public Map<String, Object>[] read() {
for (Integer i = 0; i < 16; i++) {
if (tw.useForReader(filepath, i.toString()) == 1) {
map[i] = new HashMap<String, Object>();
StringBuilder keyBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
File localbase = new File(filepath + "/" + i.toString());
String[] list = localbase.list();
if (list != null) {
for (String currentDat : list) {
int length = 0;
try (DataInputStream rd = new DataInputStream(
new FileInputStream(filepath + "/"
+ i.toString() + "/" + currentDat))) {
while (true) {
try {
length = rd.readInt();
for (int k = 0; k < length; k++) {
keyBuilder.append(rd.readChar());

}
length = rd.readInt();
for (int k = 0; k < length; k++) {
valueBuilder.append(rd.readChar());

}
map[i].put(keyBuilder.toString(),
valueBuilder.toString());
keyBuilder.replace(0, keyBuilder.length(),
"");
valueBuilder.replace(0,
valueBuilder.length(), "");
if (i == 15) {
tw.setDir = -1;
}
} catch (EOFException e) {
break;
}

}

} catch (FileNotFoundException e) {
try {
File newdb = new File(filepath);
newdb.createNewFile();
} catch (IOException err) {
System.err.print(err.toString());
}
} catch (IOException e) {
System.err.print(e.toString());
}
}
}
}
}

return map;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package ru.fizteh.fivt.students.AndrewTimokhin.MultiDataBase;

import java.io.*;
import java.util.*;

public class TableWork {
public int setDir = -1; // by default

public int getset() {
return setDir;
}

public void createforUser(Map<String, Object>[] agr, int numdir) {
if (agr[numdir] != null) {

return;
}
agr[numdir] = new HashMap<String, Object>();
return;
}

public int create(Map<String, Object>[] agr, String homeDir,
String tableToAdd) {

File local = new File(homeDir);
String[] list;
list = local.list();
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (list[i].equals(tableToAdd)) {
System.out.println(tableToAdd + " exists");
return 1; // if exist
}

}
}
File newtable = new File(homeDir + "/" + tableToAdd);
newtable.mkdirs();
if (agr[Integer.parseInt(tableToAdd)] == null) {
agr[Integer.parseInt(tableToAdd)] = new HashMap<String, Object>();
}
System.out.println("created");
return 0; // if added
}

public void dropForUser(Map<String, Object>[] agr, int numdir) {
if (agr[numdir] == null) {
return;
}
agr[numdir] = null;
return;

}

public int drop(String homeDir, String tableToDel) {
int haveDir = 0;
File local = new File(homeDir);
String[] list;
list = local.list();
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (list[i].equals(tableToDel)) {
haveDir = 1; // if exist
}
}
}
if (haveDir == 0) {
System.out.println("not exists");
return 0;
}

File delTable = new File(homeDir + "/" + tableToDel);
for (File file : new File(homeDir + "/" + tableToDel).listFiles()) {
if (file.isFile()) {
file.delete();
}
}
delTable.delete();
System.out.println(tableToDel + " dropped");
return 1; // if was deleted
}

public int useForReader(String homeDir, String numDir) {
File local = new File(homeDir);
String[] list;
list = local.list();
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (list[i].equals(numDir)) {
return 1; // if exist
}
}
}
return 0; // if n
}

public int userUse(String homeDir, String table) {
File local = new File(homeDir);
String[] list;
list = local.list();
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (list[i].equals(table)) {
return 1; // if exist
}
}
}
return 0; // if added
}
}

Loading