This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Andrew Timokhin, JUnit #576
Open
AndrewTimokhin
wants to merge
17
commits into
dkomanov:master
Choose a base branch
from
AndrewTimokhin:proper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
da7a17c
MultiDataBase v1.0
AndrewTimokhin 5e0ca7b
v2
AndrewTimokhin 2e29807
Update Functional.java
AndrewTimokhin a7f6d16
Update JUnit.java
AndrewTimokhin 0db17ac
Update ModeWork.java
AndrewTimokhin 6e955ec
Update Reader.java
AndrewTimokhin f73ddbd
Update TableDriver.java
AndrewTimokhin 52efcdb
Update TableWork.java
AndrewTimokhin 2a4e6ea
Update Write.java
AndrewTimokhin 2599217
Update Functional.java
AndrewTimokhin d42c884
Update Write.java
AndrewTimokhin 2b10f90
Update TableWork.java
AndrewTimokhin 1961c1a
Update TableDriver.java
AndrewTimokhin d9ceca0
Update Reader.java
AndrewTimokhin 30c4f8e
Update ModeWork.java
AndrewTimokhin 174cbad
Update JUnit.java
AndrewTimokhin d3cfeb2
Update Functional.java
AndrewTimokhin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Functional.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ru; | ||
|
||
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()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/MainClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
|
||
} | ||
} | ||
|
82 changes: 82 additions & 0 deletions
82
src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/ModeWork.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } } | ||
* | ||
* } | ||
*/ | ||
|
||
} | ||
|
79 changes: 79 additions & 0 deletions
79
src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
111 changes: 111 additions & 0 deletions
111
src/ru/fizteh/fivt/students/AndrewTimokhin/MultiDataBase/TableWork.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Неправильное имя пакет. Имя пакета должно совпадать с путём до папки.