From da7a17c54e2e768893ab06b909e5c96c1150c1ae Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Sun, 2 Nov 2014 01:38:54 +0300 Subject: [PATCH 01/17] MultiDataBase v1.0 --- .../MultiDataBase/Functional.java | 73 ++++++++++++ .../MultiDataBase/MainClass.java | 32 +++++ .../MultiDataBase/ModeWork.java | 82 +++++++++++++ .../AndrewTimokhin/MultiDataBase/Reader.java | 79 +++++++++++++ .../MultiDataBase/TableWork.java | 111 ++++++++++++++++++ .../AndrewTimokhin/MultiDataBase/Write.java | 59 ++++++++++ 6 files changed, 436 insertions(+) create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/MainClass.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/ModeWork.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Reader.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/TableWork.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Write.java diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java new file mode 100644 index 000000000..577df5d48 --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java @@ -0,0 +1,73 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiDataBase; + +import java.util.*; + +class Functional { + public Map[] map; + + public Functional(Map[] map) { + this.map = map; + } + + void put(Map[] 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 get(Map[] 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[] 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); + } else { + System.out.println("not found"); + } + } + } + + void showtables() { + for (int i = 0; i < 16; i++) { + if (map[i] != null) { + if (map[i].size() == 0) { + map[i] = null; + } + } + } + for (int i = 0; i < 16; i++) { + if (map[i] != null) { + + System.out.println("table " + i + " " + map[i].size()); + } + } + + } +} + diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/MainClass.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/MainClass.java new file mode 100644 index 000000000..3e0d27cbf --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/MainClass.java @@ -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[] map = (Map[]) new Map[16]; + for (int i = 0; i < 16; i++) { + map[i] = new HashMap(); + } + + Reader rd = new Reader(System.getProperty("fizteh.db.dir").toString()); + map = rd.read(); + Write wr = new Write(System.getProperty("fizteh.db.dir").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()); + } + + } +} + diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/ModeWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/ModeWork.java new file mode 100644 index 000000000..eb5261a61 --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/ModeWork.java @@ -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[] 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[] 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; } } + * + * } + */ + +} + diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Reader.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Reader.java new file mode 100644 index 000000000..ebdaaab85 --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Reader.java @@ -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[] map; + + public Reader(String path) { + filepath = path; + + map = (Map[]) new Map[16]; + + } + + public Map[] read() { + for (Integer i = 0; i < 16; i++) { + if (tw.useForReader(filepath, i.toString()) == 1) { + map[i] = new HashMap(); + 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; + } + +} + diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/TableWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/TableWork.java new file mode 100644 index 000000000..0abe22833 --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/TableWork.java @@ -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[] agr, int numdir) { + if (agr[numdir] != null) { + + return; + } + agr[numdir] = new HashMap(); + return; + } + + public int create(Map[] 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(); + } + System.out.println("created"); + return 0; // if added + } + + public void dropForUser(Map[] 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 + } +} + diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Write.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Write.java new file mode 100644 index 000000000..1260b4be4 --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Write.java @@ -0,0 +1,59 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiDataBase; + +import java.util.*; +import java.io.*; +import java.util.regex.*; + +public class Write { + private String filepath; + + public Write(String path) { + filepath = path; + } + + public void write(Map[] map) throws IOException { + for (Integer i = 0; i < 16; i++) { + if (map[i] != null) { + + for (Integer j = 0; j < 16; j++) { + String local = j.toString() + ".dat"; + File tmp = new File(filepath + "/" + i.toString() + "/" + + local); + tmp.createNewFile(); + try (DataOutputStream out = new DataOutputStream( + new FileOutputStream(filepath + "/" + i.toString() + + "/" + local))) { + Set st = map[i].keySet(); + for (String time : st) { + + String tm = local.replaceAll(".dat", ""); + if ((time.hashCode() / 16 % 16) == Integer + .parseInt(tm)) { + + out.writeInt(time.length()); + out.writeChars(time); + out.writeInt(((String) map[i].get(time)) + .length()); + out.writeChars((String) map[i].get(time)); + } + + } + + } catch (FileNotFoundException e) { + System.err.print("Not Found " + e.toString()); + } catch (IOException e) { + System.err.print("IOException " + e.toString()); + } + if (tmp.length() == 0) { + tmp.delete(); + } + } + } + if (map[i] == null) { + File delTable = new File(filepath + "/" + i.toString()); + + delTable.delete(); + } + } + } +} From 5e0ca7b354d3bf772e8e62c945539c38c1947171 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Sat, 20 Dec 2014 12:32:22 +0300 Subject: [PATCH 02/17] v2 --- .../MultiFileHashMap v2.0/Functional.java | 58 +++++++ .../MultiFileHashMap v2.0/JUnit.java | 23 +++ .../MultiFileHashMap v2.0/ModeWork.java | 94 +++++++++++ .../MultiFileHashMap v2.0/Reader.java | 95 +++++++++++ .../MultiFileHashMap v2.0/TableDriver.java | 37 +++++ .../MultiFileHashMap v2.0/TableWork.java | 157 ++++++++++++++++++ .../MultiFileHashMap v2.0/Write.java | 72 ++++++++ 7 files changed, 536 insertions(+) create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java create mode 100644 src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java new file mode 100644 index 000000000..252b9f1db --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java @@ -0,0 +1,58 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; + +import java.io.IOException; +import java.util.*; + +public class Functional { + + void put(Map[] 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 get(Map[] 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[] 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); + } else { + System.out.println("not found"); + } + } + } + + void showtables(TableDriver[] td) throws NullPointerException, IOException { + for (TableDriver time : td) { + + System.out.println("table " + time.getName() + " " + time.size()); + } + } +} diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java new file mode 100644 index 000000000..29641658e --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java @@ -0,0 +1,23 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; + +import java.io.*; +import java.util.*; + +public class JUnit { + public static void main(String[] args) throws IOException { + String path = System.getProperty("fizteh.db.dir").toString(); + TableWork tb = new TableWork(0, path); + Functional fn = new Functional(); + + ModeWork mw = new ModeWork(); + + mw.usermode(fn, tb); + Write wr = new Write(); + for (int i = 0; i < tb.t.length; i++) { + + wr.write(tb.t[i], tb); + } + + }; + +} \ No newline at end of file diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java new file mode 100644 index 000000000..edc6c5fbd --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java @@ -0,0 +1,94 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; + +import java.io.IOException; +import java.util.*; + +class ModeWork { + Functional fn; + + ModeWork() { + fn = new Functional(); + } + + int stepMode(String[] array, TableWork f, int index) + throws NullPointerException, IOException { + + switch (array[0 + index]) { + case "put": + if (f.indicator == -1) + System.out.println("UNDEF"); + else + fn.put(f.t[f.indicator].map, array[1 + index].hashCode() % 16, + array[1 + index], array[2 + index]); + return 2; + case "get": + if (f.indicator == -1) + System.out.println("UNDEF"); + else + fn.get(f.t[f.indicator].map, array[1 + index].hashCode() % 16, + array[1 + index]); + return 1; + case "remove": + if (f.indicator == -1) + System.out.println("UNDEF"); + else + fn.remove(f.t[f.indicator].map, + array[1 + index].hashCode() % 16, array[1 + index]); + return 1; + case "create": + f.create(array[1 + index]); + + return 1; + case "drop": + f.drop(array[1 + index]); + + return 1; + case "use": + if (f.userUse(array[1 + index]) == 0) { + + } else { + + } + + return 1; + + case "showtables": + fn.showtables(f.t); + return 1; + case "exit": + return -1; + default: + System.out.println("Unknow operation. Fail."); + return -1; + } + } + + void usermode(Functional func, TableWork t) throws NullPointerException, + IOException { + + 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, t, 0) == -1) { + break; + } + } + rd.close(); + + } + + /* + * void interactive( Map[] 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; } } + * + * } + */ + +} diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java new file mode 100644 index 000000000..5c991effd --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java @@ -0,0 +1,95 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; + +import java.util.*; +import java.io.*; + +public class Reader { + + private Map[] map; + + public Reader() { + + } + + public Map[] read(String filepath) { + + map = (Map[]) new Map[16]; + for (int ind = 0; ind < 16; ind++) { + map[ind] = new HashMap(); + } + + int flag = 0; + File alldir = new File(filepath); + String file[] = alldir.list(); + + for (Integer i = 0; i < 16; i++) { + for (int j = 0; j < file.length; j++) { + if (i.toString().equals(file[j].toString())) { + flag = 1; + break; + } + } + if (flag == 1) + + { + + 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) { + + } + } 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()); + } + } + } + } + flag = 0; + } + + return map; + } + +} diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java new file mode 100644 index 000000000..115cba42e --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java @@ -0,0 +1,37 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; + +import java.util.HashMap; +import java.util.Map; + +public class TableDriver { + private String name; + public Map[] map; + + TableDriver() { + + } + + TableDriver(String name) { + this.name = name; + map = (Map[]) new Map[16]; + for (int i = 0; i < 16; i++) { + map[i] = new HashMap(); + } + + } + + public int size() { + int summ = 0; + if (map != null) { + for (int i = 0; i < 16; i++) { + if (map[i] != null) + summ += map[i].size(); + } + } + return summ; + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java new file mode 100644 index 000000000..59b62cbea --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java @@ -0,0 +1,157 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; + +import java.io.*; +import java.util.*; + +public class TableWork { + public TableDriver t[]; + String homeDir; + int indicator; + int size; + + TableWork(int size, String homeDir) { + Reader rd = new Reader(); + this.size = 0; + File input = new File(homeDir); + String[] st; + st = input.list(); + if (st != null) { + for (String path : st) { + + File tm2 = new File(homeDir + "\\" + path); + + if (tm2.list() != null) { + + if (size == 0) { + t = new TableDriver[1]; + t[0] = new TableDriver(path); + t[0].map = rd.read(homeDir + "\\" + path); + + } else { + TableDriver temp[] = new TableDriver[t.length + 1]; + for (int k = 0; k < t.length; k++) + temp[k] = t[k]; + + temp[t.length] = new TableDriver(path); + temp[t.length].map = rd.read(homeDir + "\\" + path); + + t = temp; + } + size++; + } + + } + } + + this.homeDir = homeDir; + + indicator = -1; + } + + public int setDir = -1; // by default + + public int getset() { + return setDir; + } + + public int create(String tableToAdd) throws NullPointerException { + + if (t != null) { + for (int i = 0; i < t.length; i++) { + if (tableToAdd.equals(t[i].getName())) { + System.out.println("exist!"); + return 1; + } + } + ; + { + + TableDriver temp[] = new TableDriver[t.length + 1]; + for (int k = 0; k < t.length; k++) + temp[k] = t[k]; + + temp[t.length] = new TableDriver(tableToAdd); + t = temp; + System.out.println("created"); + } + } + + if (t == null || size == 0) { + t = new TableDriver[1]; + t[0] = new TableDriver(tableToAdd); + } + System.out.println("created"); + size++; + return 0; // if added + } + + public void remove(File anyfile) { + if (!anyfile.exists()) + return; + if (anyfile.isDirectory()) { + for (File f : anyfile.listFiles()) + remove(f); + anyfile.delete(); + } else { + anyfile.delete(); + } + } + + public void drop(String tableToDel) throws NullPointerException, + IOException { + int haveDir = 0; + for (int i = 0; i < t.length; i++) { + if (t[i] != null && tableToDel.equals(t[i].getName()) == true) { + + if (t.length != 1) { + TableDriver tm = new TableDriver(); + t[i] = t[t.length - 1]; + t[t.length - 1] = null; + + } else + t[i] = null; + size--; + System.out.println(tableToDel + " dropped"); + File in = new File(homeDir); + String st[] = in.list(); + + if (st != null) { + for (String time : st) { + if (time.equals(tableToDel)) { + + in = new File(homeDir + "\\" + tableToDel); + + remove(in); + + } + + } + } + + // if was deleted + break; + } + + } + if (haveDir == 0) { + System.out.println("not exists"); + + } + + } + + public int userUse(String tablename) throws NullPointerException { + if (t != null && t[0] != null) + for (int i = 0; i < t.length; i++) { + if (t[i].getName().equals(tablename)) { + indicator = i; + System.out.println("use"); + return 0; + } + } + System.out.println("not exist"); + return 1; + + } + +} diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java new file mode 100644 index 000000000..ef81ade64 --- /dev/null +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java @@ -0,0 +1,72 @@ +package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; + +import java.util.*; +import java.io.*; +import java.util.regex.*; + +public class Write { + private String filepath; + + public Write() { + + } + + public void write(TableDriver tm, TableWork tb) throws IOException { + if (tm != null) { + filepath = tb.homeDir + "\\" + tm.getName(); + File time2 = new File(filepath); + time2.mkdir(); + + for (Integer i = 0; i < 16; i++) { + if (tm.map[i] != null) { + + for (Integer j = 0; j < 16; j++) { + String local = j.toString() + ".dat"; + + File tmp0 = new File(filepath + "\\" + i.toString()); + tmp0.mkdir(); + + File tmp = new File(filepath + "\\" + i.toString() + + "\\" + local); + tmp.createNewFile(); + try (DataOutputStream out = new DataOutputStream( + new FileOutputStream(filepath + "\\" + + i.toString() + "\\" + local))) { + Set st = tm.map[i].keySet(); + for (String time : st) { + + String tr = local.replaceAll(".dat", ""); + if ((time.hashCode() / 16 % 16) == Integer + .parseInt(tr)) { + + out.writeInt(time.length()); + out.writeChars(time); + out.writeInt(((String) tm.map[i].get(time)) + .length()); + out.writeChars((String) tm.map[i].get(time)); + } + + } + + } catch (FileNotFoundException e) { + System.err.print("Not Found " + e.toString()); + } catch (IOException e) { + System.err.print("IOException " + e.toString()); + } + if (tmp.length() == 0) { + tmp.delete(); + } + } + File tmp0 = new File(filepath + "\\" + i.toString()); + if (tmp0.list().length == 0) + tmp0.delete(); + } + if (tm.map[i] == null) { + File delTable = new File(filepath + "\\" + i.toString()); + + delTable.delete(); + } + } + } + } +} From 2e29807d708042995c1e8428a8ab3f8672fa9fd4 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 02:06:34 +0300 Subject: [PATCH 03/17] Update Functional.java --- .../MultiFileHashMap v2.0/Functional.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java index 252b9f1db..9bb68161b 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; +package ru; import java.io.IOException; import java.util.*; @@ -20,6 +20,22 @@ void put(Map[] map, int i, String key, String value) { map[i].put(key, value); } + void list(Map[] 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 time = map[i].keySet(); + for (String local : time) { + System.out.println(time); + } + } + } + return; + } + void get(Map[] map, int i, String key) { if (i == -1) { System.out.println("NotDefBD#"); @@ -43,16 +59,21 @@ void remove(Map[] map, int i, String key) { 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 NullPointerException, IOException { - for (TableDriver time : td) { + 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("table " + time.getName() + " " + time.size()); - } + System.out.println("in table we have " + td[i].getName() + + " " + td[i].size()); + } } + } From a7f6d16e3a58eee174ef0df01f8e9f1e187156bb Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 02:07:05 +0300 Subject: [PATCH 04/17] Update JUnit.java --- .../MultiFileHashMap v2.0/JUnit.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java index 29641658e..4deaac68b 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java @@ -1,10 +1,11 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; +package ru; import java.io.*; import java.util.*; public class JUnit { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws NullPointerException, + IOException, IllegalAccessException { String path = System.getProperty("fizteh.db.dir").toString(); TableWork tb = new TableWork(0, path); Functional fn = new Functional(); @@ -13,11 +14,12 @@ public static void main(String[] args) throws IOException { mw.usermode(fn, tb); Write wr = new Write(); - for (int i = 0; i < tb.t.length; i++) { + if (tb.t != null) + for (int i = 0; i < tb.t.length; i++) { - wr.write(tb.t[i], tb); - } + wr.write(tb.t[i], tb); + } }; -} \ No newline at end of file +} From 0db17ac8bb82584196676046187f57195f384f12 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 02:07:27 +0300 Subject: [PATCH 05/17] Update ModeWork.java --- .../MultiFileHashMap v2.0/ModeWork.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java index edc6c5fbd..e1cd6b820 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; +package ru; import java.io.IOException; import java.util.*; @@ -18,8 +18,14 @@ int stepMode(String[] array, TableWork f, int index) if (f.indicator == -1) System.out.println("UNDEF"); else - fn.put(f.t[f.indicator].map, array[1 + index].hashCode() % 16, - array[1 + index], array[2 + index]); + for (int i = 2 + index; i < array.length; i++) + + { + fn.put(f.t[f.indicator].map, + array[1 + index].hashCode() % 16, array[1 + index], + array[i]); + + } return 2; case "get": if (f.indicator == -1) @@ -28,6 +34,13 @@ int stepMode(String[] array, TableWork f, int index) fn.get(f.t[f.indicator].map, array[1 + index].hashCode() % 16, array[1 + index]); return 1; + case "list": + if (f.indicator == -1) + System.out.println("UNDEF"); + else { + fn.list(f.t[f.indicator].map); + } + return 1; case "remove": if (f.indicator == -1) System.out.println("UNDEF"); @@ -52,19 +65,20 @@ int stepMode(String[] array, TableWork f, int index) return 1; - case "showtables": - fn.showtables(f.t); + case "show": + if (array[1].equals("tables")) + fn.showtables(f.t); return 1; case "exit": return -1; default: System.out.println("Unknow operation. Fail."); - return -1; + return 1; } } void usermode(Functional func, TableWork t) throws NullPointerException, - IOException { + IOException, IllegalAccessException { String str = new String(); String[] array; @@ -79,7 +93,6 @@ void usermode(Functional func, TableWork t) throws NullPointerException, } } rd.close(); - } /* From 6e955ec6156385ed4fd04cf183b3802ead08ee41 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 02:07:55 +0300 Subject: [PATCH 06/17] Update Reader.java --- .../students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java index 5c991effd..9e598431b 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; +package ru; import java.util.*; import java.io.*; From f73ddbdf2b62b69b529f73ac397ecd898adb31be Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 02:08:29 +0300 Subject: [PATCH 07/17] Update TableDriver.java --- .../AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java index 115cba42e..9eb3f49e6 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; +package ru; import java.util.HashMap; import java.util.Map; @@ -34,4 +34,4 @@ public int size() { public String getName() { return name; } -} \ No newline at end of file +} From 52efcdb1c359e45878b344d75eed8b6c7010704a Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 02:08:55 +0300 Subject: [PATCH 08/17] Update TableWork.java --- .../MultiFileHashMap v2.0/TableWork.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java index 59b62cbea..bbd21bded 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java @@ -1,6 +1,7 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; +package ru; import java.io.*; +import java.security.AccessControlException; import java.util.*; public class TableWork { @@ -72,7 +73,7 @@ public int create(String tableToAdd) throws NullPointerException { temp[t.length] = new TableDriver(tableToAdd); t = temp; - System.out.println("created"); + // System.out.println("created"); } } @@ -118,10 +119,13 @@ public void drop(String tableToDel) throws NullPointerException, if (st != null) { for (String time : st) { if (time.equals(tableToDel)) { + try { + in = new File(homeDir + "\\" + tableToDel); - in = new File(homeDir + "\\" + tableToDel); - - remove(in); + remove(in); + } catch (AccessControlException e) { + System.out.println("Access forbidden!"); + } } From 2a4e6ea0caca636be556da55e3b4737929f1137d Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 02:09:21 +0300 Subject: [PATCH 09/17] Update Write.java --- .../students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java index ef81ade64..dc6709863 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiFileHashMap; +package ru; import java.util.*; import java.io.*; From 2599217713e51a1936bfc361ab268432633e7ca5 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:44:33 +0300 Subject: [PATCH 10/17] Update Functional.java --- .../MultiDataBase/Functional.java | 128 ++++++++++-------- 1 file changed, 68 insertions(+), 60 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java index 577df5d48..319ea961c 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java @@ -1,73 +1,81 @@ -package ru.fizteh.fivt.students.AndrewTimokhin.MultiDataBase; - +package ru; + +import java.io.IOException; import java.util.*; -class Functional { - public Map[] map; +public class Functional { - public Functional(Map[] map) { - this.map = map; - } + void put(Map[] map, int i, String key, String value) { + if (i == -1) { + System.out.println("NotDefBD#"); + return; + } - void put(Map[] 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); + } - 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[] 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 time = map[i].keySet(); + for (String local : time) { + System.out.println(time); + } } + } + return; + } - void get(Map[] 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 get(Map[] 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[] 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); - } else { - System.out.println("not found"); - } - } - } + void remove(Map[] 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() { - for (int i = 0; i < 16; i++) { - if (map[i] != null) { - if (map[i].size() == 0) { - map[i] = null; - } - } - } - for (int i = 0; i < 16; i++) { - if (map[i] != null) { + 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("table " + i + " " + map[i].size()); + System.out.println("in table we have " + td[i].getName() + + " " + td[i].size()); } - } + } + } + } - } } - From d42c8847d081ba1e9f68af25f6a698961092ed81 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:45:45 +0300 Subject: [PATCH 11/17] Update Write.java --- .../MultiFileHashMap v2.0/Write.java | 105 +++++++++--------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java index dc6709863..c06194042 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Write.java @@ -5,68 +5,69 @@ import java.util.regex.*; public class Write { - private String filepath; + private String filepath; - public Write() { + public Write() { - } + } - public void write(TableDriver tm, TableWork tb) throws IOException { - if (tm != null) { - filepath = tb.homeDir + "\\" + tm.getName(); - File time2 = new File(filepath); - time2.mkdir(); + public void write(TableDriver tm, TableWork tb) throws IOException { + if (tm != null) { + filepath = tb.homeDir + "\\" + tm.getName(); + File time2 = new File(filepath); + time2.mkdir(); - for (Integer i = 0; i < 16; i++) { - if (tm.map[i] != null) { + for (Integer i = 0; i < 16; i++) { + if (tm.map[i] != null) { - for (Integer j = 0; j < 16; j++) { - String local = j.toString() + ".dat"; + for (Integer j = 0; j < 16; j++) { + String local = j.toString() + ".dat"; - File tmp0 = new File(filepath + "\\" + i.toString()); - tmp0.mkdir(); + File tmp0 = new File(filepath + "\\" + i.toString()); + tmp0.mkdir(); - File tmp = new File(filepath + "\\" + i.toString() - + "\\" + local); - tmp.createNewFile(); - try (DataOutputStream out = new DataOutputStream( - new FileOutputStream(filepath + "\\" - + i.toString() + "\\" + local))) { - Set st = tm.map[i].keySet(); - for (String time : st) { + File tmp = new File(filepath + "\\" + i.toString() + + "\\" + local); + tmp.createNewFile(); + try (DataOutputStream out = new DataOutputStream( + new FileOutputStream(filepath + "\\" + + i.toString() + "\\" + local))) { + Set st = tm.map[i].keySet(); + for (String time : st) { - String tr = local.replaceAll(".dat", ""); - if ((time.hashCode() / 16 % 16) == Integer - .parseInt(tr)) { + String tr = local.replaceAll(".dat", ""); + if ((time.hashCode() / 16 % 16) == Integer + .parseInt(tr)) { - out.writeInt(time.length()); - out.writeChars(time); - out.writeInt(((String) tm.map[i].get(time)) - .length()); - out.writeChars((String) tm.map[i].get(time)); - } + out.writeInt(time.length()); + out.writeChars(time); + out.writeInt(((String) tm.map[i].get(time)) + .length()); + out.writeChars((String) tm.map[i].get(time)); + } - } + } - } catch (FileNotFoundException e) { - System.err.print("Not Found " + e.toString()); - } catch (IOException e) { - System.err.print("IOException " + e.toString()); - } - if (tmp.length() == 0) { - tmp.delete(); - } - } - File tmp0 = new File(filepath + "\\" + i.toString()); - if (tmp0.list().length == 0) - tmp0.delete(); - } - if (tm.map[i] == null) { - File delTable = new File(filepath + "\\" + i.toString()); + } catch (FileNotFoundException e) { + System.err.print("Not Found " + e.toString()); + } catch (IOException e) { + System.err.print("IOException " + e.toString()); + } + if (tmp.length() == 0) { + tmp.delete(); + } + } + File tmp0 = new File(filepath + "\\" + i.toString()); + if (tmp0.list().length == 0) { + tmp0.delete(); + } + } + if (tm.map[i] == null) { + File delTable = new File(filepath + "\\" + i.toString()); - delTable.delete(); - } - } - } - } + delTable.delete(); + } + } + } + } } From 2b10f9042e1bd468e0572a45179859c2514e4a37 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:46:12 +0300 Subject: [PATCH 12/17] Update TableWork.java --- .../MultiFileHashMap v2.0/TableWork.java | 306 +++++++++--------- 1 file changed, 154 insertions(+), 152 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java index bbd21bded..e632893d9 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableWork.java @@ -5,157 +5,159 @@ import java.util.*; public class TableWork { - public TableDriver t[]; - String homeDir; - int indicator; - int size; - - TableWork(int size, String homeDir) { - Reader rd = new Reader(); - this.size = 0; - File input = new File(homeDir); - String[] st; - st = input.list(); - if (st != null) { - for (String path : st) { - - File tm2 = new File(homeDir + "\\" + path); - - if (tm2.list() != null) { - - if (size == 0) { - t = new TableDriver[1]; - t[0] = new TableDriver(path); - t[0].map = rd.read(homeDir + "\\" + path); - - } else { - TableDriver temp[] = new TableDriver[t.length + 1]; - for (int k = 0; k < t.length; k++) - temp[k] = t[k]; - - temp[t.length] = new TableDriver(path); - temp[t.length].map = rd.read(homeDir + "\\" + path); - - t = temp; - } - size++; - } - - } - } - - this.homeDir = homeDir; - - indicator = -1; - } - - public int setDir = -1; // by default - - public int getset() { - return setDir; - } - - public int create(String tableToAdd) throws NullPointerException { - - if (t != null) { - for (int i = 0; i < t.length; i++) { - if (tableToAdd.equals(t[i].getName())) { - System.out.println("exist!"); - return 1; - } - } - ; - { - - TableDriver temp[] = new TableDriver[t.length + 1]; - for (int k = 0; k < t.length; k++) - temp[k] = t[k]; - - temp[t.length] = new TableDriver(tableToAdd); - t = temp; - // System.out.println("created"); - } - } - - if (t == null || size == 0) { - t = new TableDriver[1]; - t[0] = new TableDriver(tableToAdd); - } - System.out.println("created"); - size++; - return 0; // if added - } - - public void remove(File anyfile) { - if (!anyfile.exists()) - return; - if (anyfile.isDirectory()) { - for (File f : anyfile.listFiles()) - remove(f); - anyfile.delete(); - } else { - anyfile.delete(); - } - } - - public void drop(String tableToDel) throws NullPointerException, - IOException { - int haveDir = 0; - for (int i = 0; i < t.length; i++) { - if (t[i] != null && tableToDel.equals(t[i].getName()) == true) { - - if (t.length != 1) { - TableDriver tm = new TableDriver(); - t[i] = t[t.length - 1]; - t[t.length - 1] = null; - - } else - t[i] = null; - size--; - System.out.println(tableToDel + " dropped"); - File in = new File(homeDir); - String st[] = in.list(); - - if (st != null) { - for (String time : st) { - if (time.equals(tableToDel)) { - try { - in = new File(homeDir + "\\" + tableToDel); - - remove(in); - } catch (AccessControlException e) { - System.out.println("Access forbidden!"); - } - - } - - } - } - - // if was deleted - break; - } - - } - if (haveDir == 0) { - System.out.println("not exists"); - - } - - } - - public int userUse(String tablename) throws NullPointerException { - if (t != null && t[0] != null) - for (int i = 0; i < t.length; i++) { - if (t[i].getName().equals(tablename)) { - indicator = i; - System.out.println("use"); - return 0; - } - } - System.out.println("not exist"); - return 1; - - } + public TableDriver [] t ; + String homeDir; + int indicator; + int size; + + TableWork(int size, String homeDir) { + Reader rd = new Reader(); + this.size = 0; + File input = new File(homeDir); + String[] st; + st = input.list(); + if (st != null) { + for (String path : st) { + + File tm2 = new File(homeDir + "\\" + path); + + if (tm2.list() != null) { + + if (size == 0) { + t = new TableDriver[1]; + t[0] = new TableDriver(path); + t[0].map = rd.read(homeDir + "\\" + path); + + } else { + TableDriver [] temp = new TableDriver[t.length + 1]; + for (int k = 0; k < t.length; k++) { + temp[k] = t[k]; + } + temp[t.length] = new TableDriver(path); + temp[t.length].map = rd.read(homeDir + "\\" + path); + + t = temp; + } + size++; + } + + } + } + + this.homeDir = homeDir; + + indicator = -1; + } + + public int setDir = -1; // by default + + public int getset() { + return setDir; + } + + public int create(String tableToAdd) throws NullPointerException { + + if (t != null) { + for (int i = 0; i < t.length; i++) { + if (tableToAdd.equals(t[i].getName())) { + System.out.println("exist!"); + return 1; + } + } + { + TableDriver [] temp = new TableDriver[t.length + 1]; + for (int k = 0; k < t.length; k++) { + temp[k] = t[k]; + } + + temp[t.length] = new TableDriver(tableToAdd); + t = temp; + // System.out.println("created"); + } + } + + if (t == null || size == 0) { + t = new TableDriver[1]; + t[0] = new TableDriver(tableToAdd); + } + System.out.println("created"); + size++; + return 0; // if added + } + + public void remove(File anyfile) { + if (!anyfile.exists()) { + return; + } + if (anyfile.isDirectory()) { + for (File f : anyfile.listFiles()) { + remove(f); } + anyfile.delete(); + } else { + anyfile.delete(); + } + } + + public void drop(String tableToDel) throws NullPointerException, + IOException { + int haveDir = 0; + for (int i = 0; i < t.length; i++) { + if (t[i] != null && tableToDel.equals(t[i].getName())) { + + if (t.length != 1) { + TableDriver tm = new TableDriver(); + t[i] = t[t.length - 1]; + t[t.length - 1] = null; + + } else { + t[i] = null; + } + size--; + System.out.println(tableToDel + " dropped"); + File in = new File(homeDir); + String [] st = in.list(); + + if (st != null) { + for (String time : st) { + if (time.equals(tableToDel)) { + try { + in = new File(homeDir + "\\" + tableToDel); + + remove(in); + } catch (AccessControlException e) { + System.out.println("Access forbidden!"); + } + + } + + } + } + + // if was deleted + break; + } + + } + if (haveDir == 0) { + System.out.println("not exists"); + + } + + } + + public int userUse(String tablename) throws NullPointerException { + if (t != null && t[0] != null) { + for (int i = 0; i < t.length; i++) { + if (t[i].getName().equals(tablename)) { + indicator = i; + System.out.println("use"); + return 0; + } + } + } + System.out.println("not exist"); + return 1; + + } } From 1961c1a1d5167091279115ff84af39ed7aec3743 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:46:31 +0300 Subject: [PATCH 13/17] Update TableDriver.java --- .../MultiFileHashMap v2.0/TableDriver.java | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java index 9eb3f49e6..39f3aa4f9 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/TableDriver.java @@ -4,34 +4,35 @@ import java.util.Map; public class TableDriver { - private String name; - public Map[] map; - - TableDriver() { - - } - - TableDriver(String name) { - this.name = name; - map = (Map[]) new Map[16]; - for (int i = 0; i < 16; i++) { - map[i] = new HashMap(); - } - - } - - public int size() { - int summ = 0; - if (map != null) { - for (int i = 0; i < 16; i++) { - if (map[i] != null) - summ += map[i].size(); - } - } - return summ; - } - - public String getName() { - return name; - } + private String name; + public Map[] map; + + TableDriver() { + + } + + TableDriver(String name) { + this.name = name; + map = (Map[]) new Map[16]; + for (int i = 0; i < 16; i++) { + map[i] = new HashMap(); + } + + } + + public int size() { + int summ = 0; + if (map != null) { + for (int i = 0; i < 16; i++) { + if (map[i] != null) { + summ += map[i].size(); + } + } + } + return summ; + } + + public String getName() { + return name; + } } From d9ceca0698340cda151ce3add44a68b303c8ab30 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:46:48 +0300 Subject: [PATCH 14/17] Update Reader.java --- .../MultiFileHashMap v2.0/Reader.java | 166 +++++++++--------- 1 file changed, 80 insertions(+), 86 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java index 9e598431b..156b472fa 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Reader.java @@ -5,91 +5,85 @@ public class Reader { - private Map[] map; - - public Reader() { - - } - - public Map[] read(String filepath) { - - map = (Map[]) new Map[16]; - for (int ind = 0; ind < 16; ind++) { - map[ind] = new HashMap(); - } - - int flag = 0; - File alldir = new File(filepath); - String file[] = alldir.list(); - - for (Integer i = 0; i < 16; i++) { - for (int j = 0; j < file.length; j++) { - if (i.toString().equals(file[j].toString())) { - flag = 1; - break; - } - } - if (flag == 1) - - { - - 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) { - - } - } 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()); - } - } - } - } - flag = 0; - } - - return map; - } + private Map[] map; + + public Reader() { + + } + + public Map[] read(String filepath) { + + map = (Map[]) new Map[16]; + for (int ind = 0; ind < 16; ind++) { + map[ind] = new HashMap(); + } + + int flag = 0; + File alldir = new File(filepath); + String [] file = alldir.list(); + + for (Integer i = 0; i < 16; i++) { + for (int j = 0; j < file.length; j++) { + if (i.toString().equals(file[j].toString())) { + flag = 1; + break; + } + } + if (flag == 1) { + 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(), ""); + } 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()); + } + } + } + } + flag = 0; + } + + return map; + } } From 30c4f8ec66da0a4467adf2303eb97ff8c5ea96d2 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:47:06 +0300 Subject: [PATCH 15/17] Update ModeWork.java --- .../MultiFileHashMap v2.0/ModeWork.java | 191 +++++++++--------- 1 file changed, 92 insertions(+), 99 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java index e1cd6b820..9a38fe49f 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/ModeWork.java @@ -4,104 +4,97 @@ import java.util.*; class ModeWork { - Functional fn; - - ModeWork() { - fn = new Functional(); - } - - int stepMode(String[] array, TableWork f, int index) - throws NullPointerException, IOException { - - switch (array[0 + index]) { - case "put": - if (f.indicator == -1) - System.out.println("UNDEF"); - else - for (int i = 2 + index; i < array.length; i++) - - { - fn.put(f.t[f.indicator].map, - array[1 + index].hashCode() % 16, array[1 + index], - array[i]); - - } - return 2; - case "get": - if (f.indicator == -1) - System.out.println("UNDEF"); - else - fn.get(f.t[f.indicator].map, array[1 + index].hashCode() % 16, - array[1 + index]); - return 1; - case "list": - if (f.indicator == -1) - System.out.println("UNDEF"); - else { - fn.list(f.t[f.indicator].map); - } - return 1; - case "remove": - if (f.indicator == -1) - System.out.println("UNDEF"); - else - fn.remove(f.t[f.indicator].map, - array[1 + index].hashCode() % 16, array[1 + index]); - return 1; - case "create": - f.create(array[1 + index]); - - return 1; - case "drop": - f.drop(array[1 + index]); - - return 1; - case "use": - if (f.userUse(array[1 + index]) == 0) { - - } else { - - } - - return 1; - - case "show": - if (array[1].equals("tables")) - fn.showtables(f.t); - return 1; - case "exit": - return -1; - default: - System.out.println("Unknow operation. Fail."); - return 1; - } - } - - void usermode(Functional func, TableWork t) throws NullPointerException, - IOException, IllegalAccessException { - - 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, t, 0) == -1) { - break; - } - } - rd.close(); - } - - /* - * void interactive( Map[] 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; } } - * - * } - */ + Functional fn; + + ModeWork() { + fn = new Functional(); + } + + int stepMode(String[] array, TableWork f, int index) + throws NullPointerException, IOException { + + switch (array[0 + index]) { + case "put": + if (f.indicator == -1) { + System.out.println("UNDEF"); + } else { + for (int i = 2 + index; i < array.length; i++) { + fn.put(f.t[f.indicator].map, + array[1 + index].hashCode() % 16, array[1 + index], + array[i]); + } + } + return 2; + case "get": + if (f.indicator == -1) { + System.out.println("UNDEF"); + } else { fn.get(f.t[f.indicator].map, array[1 + index].hashCode() % 16, + array[1 + index]); + } + return 1; + case "list": + if (f.indicator == -1) { + System.out.println("UNDEF"); + } else { + fn.list(f.t[f.indicator].map); + } + return 1; + case "remove": + if (f.indicator == -1) { + System.out.println("UNDEF"); + } else { + fn.remove(f.t[f.indicator].map, + array[1 + index].hashCode() % 16, array[1 + index]); + } + return 1; + case "create": + f.create(array[1 + index]); + return 1; + case "drop": + f.drop(array[1 + index]); + + return 1; + case "use": + f.userUse(array[1 + index]); + return 1; + case "show": + if (array[1].equals("tables")) { + fn.showtables(f.t); + } + return 1; + case "exit": + return -1; + default: + System.out.println("Unknow operation. Fail."); + return 1; + } + } + + void usermode(Functional func, TableWork t) throws NullPointerException, + IOException, IllegalAccessException { + + 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, t, 0) == -1) { + break; + } + } + rd.close(); + } + + /* + * void interactive( Map[] 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; } } + * + * } + */ } From 174cbadb63dc4c5b83bd4663345451d2356a7564 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:47:24 +0300 Subject: [PATCH 16/17] Update JUnit.java --- .../MultiFileHashMap v2.0/JUnit.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java index 4deaac68b..1c8bdc222 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/JUnit.java @@ -4,22 +4,22 @@ import java.util.*; public class JUnit { - public static void main(String[] args) throws NullPointerException, - IOException, IllegalAccessException { - String path = System.getProperty("fizteh.db.dir").toString(); - TableWork tb = new TableWork(0, path); - Functional fn = new Functional(); + public static void main(String[] args) throws NullPointerException, + IOException, IllegalAccessException { + String path = System.getProperty("fizteh.db.dir").toString(); + TableWork tb = new TableWork(0, path); + Functional fn = new Functional(); - ModeWork mw = new ModeWork(); + ModeWork mw = new ModeWork(); - mw.usermode(fn, tb); - Write wr = new Write(); - if (tb.t != null) - for (int i = 0; i < tb.t.length; i++) { + mw.usermode(fn, tb); + Write wr = new Write(); + if (tb.t != null) { + for (int i = 0; i < tb.t.length; i++) { - wr.write(tb.t[i], tb); - } - - }; + wr.write(tb.t[i], tb); + } + } + }; } From d3cfeb27735097e29c4a148a566f23be578f77a5 Mon Sep 17 00:00:00 2001 From: AndrewTimokhin Date: Tue, 23 Dec 2014 23:47:50 +0300 Subject: [PATCH 17/17] Update Functional.java --- .../MultiFileHashMap v2.0/Functional.java | 132 +++++++++--------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java index 9bb68161b..319ea961c 100644 --- a/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java +++ b/src/ru/fizteh/fivt/students/AndrewTimokhin/MultiFileHashMap v2.0/Functional.java @@ -1,79 +1,81 @@ package ru; - + import java.io.IOException; import java.util.*; public class Functional { - void put(Map[] map, int i, String key, String value) { - if (i == -1) { - System.out.println("NotDefBD#"); - return; - } + void put(Map[] 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); - } + 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[] 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 time = map[i].keySet(); - for (String local : time) { - System.out.println(time); - } - } - } - return; - } + void list(Map[] 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 time = map[i].keySet(); + for (String local : time) { + System.out.println(time); + } + } + } + return; + } - void get(Map[] 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 get(Map[] 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[] 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 remove(Map[] 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) + 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()); - } - } + System.out.println("in table we have " + td[i].getName() + + " " + td[i].size()); + } + } + } + } }