-
Notifications
You must be signed in to change notification settings - Fork 69
FileMap with JUnit (#Задание4) #598
base: master
Are you sure you want to change the base?
Changes from 15 commits
da7a17c
5e0ca7b
895e6fe
1cbccab
7cbea81
10bf88b
1e0693a
e162d7d
b615130
f412af5
512eb42
228acff
ec5d7cd
93aca57
44da6c6
b5fae6b
e74a8c7
7ad5cb3
45290a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.DataBase; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Класс @class FactoryImplements отвечает за фабрику по созданию таблиц. | ||
| * Имплиментируя интерфейс TableProviderFactory, переопределяет метод по | ||
| * созданию провайдера базы данных. | ||
| * | ||
| * @author Timokhin Andrew | ||
| */ | ||
| public class FactoryImplements implements TableProviderFactory { | ||
|
|
||
| @Override | ||
| public TableProvider create(String dir) { | ||
| if (dir == null) { | ||
| throw new IllegalArgumentException( | ||
| "String representing directory is null"); | ||
| } | ||
| try { | ||
| return new TableProviderImplements(dir); | ||
| } catch (IOException exception) { | ||
| throw new RuntimeException(exception); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.DataBase; | ||
|
|
||
| public class KeyNullAndNotFound extends Exception { | ||
|
|
||
| public KeyNullAndNotFound(String description) { | ||
| super(description); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.DataBase; | ||
|
|
||
| import java.io.DataInputStream; | ||
| import java.io.EOFException; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| /** | ||
| * Класс @class Reader отвечает за физическое чтение данных из файловой системы. | ||
| * | ||
| * | ||
| * @author Timokhin Andrew | ||
| */ | ||
| public class Reader { | ||
|
|
||
| static final int TOTAL_SUB_STRING = 16; | ||
| static final String DIRECT = ".dir"; | ||
| private final String directory; | ||
|
|
||
| public Reader(String directory) { | ||
| this.directory = directory; | ||
| } | ||
|
|
||
| private void readKeyValue(DataInputStream rd, StringBuilder string) throws IOException { | ||
|
Collaborator
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. Параметры-значения лишь усложняют использование функции. Убери второй параметр. Эта функция должна возвращать прочитанное значение.
Contributor
Author
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. f |
||
| int length = rd.readInt(); | ||
| for (int k = 0; k < length; k++) { | ||
| string | ||
| .append(rd.readChar()); | ||
|
Collaborator
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. Лишний перевод строки
Contributor
Author
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. f |
||
| } | ||
| } | ||
|
|
||
| public void read(TableProviderImplements tp) throws IOException { | ||
| StringBuilder keyBuilder = new StringBuilder(); | ||
| StringBuilder valueBuilder = new StringBuilder(); | ||
| int length; | ||
|
Collaborator
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. Лишняя локальная переменная. Если NetBeans тебе их не подкрашивает, то перейди на более функциональную IDE.
Contributor
Author
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. f |
||
| String path = tp.getDir(); | ||
| boolean dbExist = false; | ||
| File testDir = new File(path); | ||
| if (testDir.list() != null) { | ||
| for (String time : testDir.list()) { | ||
| Path pathToFile = Paths.get(path, time); | ||
| File checkDir = new File(pathToFile.toString()); | ||
| if (checkDir.isDirectory()) { | ||
| dbExist = true; | ||
| tp.createTable(time); | ||
| TableImplement dataBase = (TableImplement) tp.getTable(time); | ||
| dataBase.setPath(tp.getDir()); | ||
| for (int i = 0; i < TOTAL_SUB_STRING; i++) { | ||
| Integer numberDir = new Integer(i); | ||
|
Collaborator
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. Эта переменная не нужна. Просто прибавляй число к строке:
Contributor
Author
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. f |
||
| Path pathToDb = Paths.get(path, time, | ||
| numberDir.toString() + DIRECT); | ||
| File locDB = new File(pathToDb.toString()); | ||
| if (locDB.exists()) { | ||
| for (String file : locDB.list()) { | ||
| Path pathToLocalDb = Paths.get( | ||
| locDB.getAbsolutePath(), file); | ||
| try (DataInputStream rd = new DataInputStream( | ||
| new FileInputStream( | ||
| pathToLocalDb.toString()))) { | ||
| while (true) { | ||
| try { | ||
| readKeyValue(rd, keyBuilder); | ||
| readKeyValue(rd, valueBuilder); | ||
|
|
||
| dataBase.put( | ||
| keyBuilder.toString(), | ||
| valueBuilder.toString()); | ||
| dataBase.getBackup().put( | ||
| keyBuilder.toString(), | ||
| valueBuilder.toString()); | ||
| keyBuilder.replace(0, | ||
| keyBuilder.length(), ""); | ||
| valueBuilder.replace(0, | ||
| valueBuilder.length(), ""); | ||
| } catch (EOFException e) { | ||
| rd.close(); | ||
| break; | ||
| } | ||
|
|
||
| } | ||
| } catch (FileNotFoundException exceptionFileNotFound) { | ||
| throw new RuntimeException( | ||
| exceptionFileNotFound); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (!dbExist) { | ||
|
||
| return; | ||
| } | ||
|
|
||
| return; | ||
| } | ||
| } | ||
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.
DIRECTORY_SUFFIX
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.
f