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

Kotsyrba Alina 397 JUnit, Storeable, Parallel #528

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/ru/fizteh/fivt/students/kotsurba/junit/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.fizteh.fivt.students.kotsurba.junit;

import ru.fizteh.fivt.storage.strings.Table;
import ru.fizteh.fivt.storage.strings.TableProvider;

public class Context {
public TableProvider provider;
Copy link
Contributor

Choose a reason for hiding this comment

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

Уместнее подобные переменные закрыть. Используй private.

Copy link
Author

Choose a reason for hiding this comment

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

нет, я их использую

Copy link
Collaborator

Choose a reason for hiding this comment

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

Сделай их private, сгенерируй геттеры-сеттеры

public Table table;

public Context(TableProvider newProvider) {
provider = newProvider;
table = null;
}

public int getChanges() {
if (table != null) {
return ((DataBase) table).getNewKeys();
}
return 0;
}
}
224 changes: 224 additions & 0 deletions src/ru/fizteh/fivt/students/kotsurba/junit/DataBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package ru.fizteh.fivt.students.kotsurba.junit;

import ru.fizteh.fivt.storage.strings.Table;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public final class DataBase implements Table {

private String name;
private String dataBaseDirectory;
private DataBaseFile[] files;

public static final int DIRECTORY_COUNT = 16;
public static final int FILES_COUNT = 16;

public final class DirFile {
private int nDir;
private int nFile;

public DirFile(int key) {
key = Math.abs(key);
nDir = key % DIRECTORY_COUNT;
nFile = (key / DIRECTORY_COUNT) % FILES_COUNT;
}

public DirFile(final int newDir, final int newFile) {
nDir = newDir;
nFile = newFile;
}

private String getNDirectory() {
return Integer.toString(nDir) + ".dir";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Суффиксы вынести в константы

}

private String getNFile() {
return Integer.toString(nFile) + ".dat";
}

public int getId() {
return nDir * DIRECTORY_COUNT + nFile;
}
}

public DataBase(final String dbDirectory) {
name = new File(dbDirectory).getName();
dataBaseDirectory = dbDirectory;
isCorrect();
files = new DataBaseFile[DIRECTORY_COUNT * FILES_COUNT];
loadFiles();
}

private void checkNames(final String[] dirs, final String secondName) {
for (String dir : dirs) {
String[] name = dir.split("\\.");
if (name.length != 2 || !name[1].equals(secondName)) {
throw new MultiDataBaseException(dataBaseDirectory + " wrong file in path " + dir);
}

int firstName;
try {
firstName = Integer.parseInt(name[0]);
} catch (NumberFormatException e) {
throw new MultiDataBaseException(dataBaseDirectory + " wrong file first name " + dir);
}

if ((firstName < 0) || firstName > FILES_COUNT - 1) {
throw new MultiDataBaseException(dataBaseDirectory + " wrong file first name " + dir);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Повтор строки. Вынести в константы

}
}
}

private void isCorrectDirectory(final String dirName) {
File file = new File(dirName);
if (file.isFile()) {
throw new MultiDataBaseException(dirName + " isn't a directory!");
}
String[] dirs = file.list();
checkNames(dirs, "dat");
for (String dir : dirs) {
if (new File(dirName, dir).isDirectory()) {
throw new MultiDataBaseException(dirName + File.separator + dir + " isn't a file!");
}
}
}

private void isCorrect() {
File file = new File(dataBaseDirectory);
if (file.isFile()) {
throw new MultiDataBaseException(dataBaseDirectory + " isn't directory!");
}

String[] dirs = file.list();
checkNames(dirs, "dir");
Copy link
Collaborator

Choose a reason for hiding this comment

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

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

for (String dir : dirs) {
isCorrectDirectory(dataBaseDirectory + File.separator + dir);
}
}

private void tryDeleteDirectory(final String name) {
File file = new File(dataBaseDirectory + File.separator + name);
Copy link
Contributor

Choose a reason for hiding this comment

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

Стоит обернуть этот блок в try catch

Copy link
Author

Choose a reason for hiding this comment

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

что тут может произойти?

Copy link
Contributor

Choose a reason for hiding this comment

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

Может кинуть NullPointerException

Copy link
Author

Choose a reason for hiding this comment

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

не может, т.к. это private метод, и предполагается его корректное использование внутри класса. на данный момент он вызывается только в одном методе, где точно не может быть подан null.

if (file.exists()) {
if (file.list().length == 0) {
if (!file.delete()) {
throw new DataBaseException("Cannot delete a directory!");
}
}
}
}

private String getFullName(final DirFile node) {
return dataBaseDirectory + File.separator + node.getNDirectory() + File.separator + node.getNFile();
}

public void loadFiles() {
for (int i = 0; i < DIRECTORY_COUNT; ++i) {
for (int j = 0; j < FILES_COUNT; ++j) {
DirFile node = new DirFile(i, j);
DataBaseFile file = new DataBaseFile(getFullName(node), node.nDir, node.nFile);
files[node.getId()] = file;
}
}
}

private void checkKey(final String key) {
if ((key == null) || (key.trim().length() == 0)) {
throw new IllegalArgumentException("Wrong key!");
}
}

public void drop() {
for (byte i = 0; i < DIRECTORY_COUNT; ++i) {
for (byte j = 0; j < FILES_COUNT; ++j) {
File file = new File(getFullName(new DirFile(i, j)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Стоит обернуть этот блок в try catch

if (file.exists()) {
if (!file.delete()) {
throw new DataBaseException("Cannot delete a file!");
}
}
}
tryDeleteDirectory(Integer.toString(i) + ".dir");
}
}

@Override
public String getName() {
return name;
}

@Override
public String put(final String keyStr, final String valueStr) {
checkKey(keyStr);
checkKey(valueStr);
DirFile node = new DirFile(keyStr.getBytes()[0]);
DataBaseFile file = files[node.getId()];
return file.put(keyStr, valueStr);
}

@Override
public String get(final String keyStr) {
checkKey(keyStr);
DirFile node = new DirFile(keyStr.getBytes()[0]);
DataBaseFile file = files[node.getId()];
return file.get(keyStr);
}

@Override
public String remove(final String keyStr) {
checkKey(keyStr);
DirFile node = new DirFile(keyStr.getBytes()[0]);
DataBaseFile file = files[node.getId()];
return file.remove(keyStr);
}

@Override
public int commit() {
int allNew = 0;
for (int i = 0; i < DIRECTORY_COUNT * FILES_COUNT; ++i) {
allNew += files[i].getNewKeys();
files[i].commit();
}
return allNew;
}

@Override
public int size() {
int allSize = 0;
for (int i = 0; i < DIRECTORY_COUNT * FILES_COUNT; ++i) {
allSize += files[i].getSize();
}
return allSize;
}

@Override
public int rollback() {
int allCanceled = 0;
for (int i = 0; i < DIRECTORY_COUNT * FILES_COUNT; ++i) {
allCanceled += files[i].getNewKeys();
files[i].rollback();
}
return allCanceled;
}

public int getNewKeys() {
int allNewSize = 0;
for (int i = 0; i < DIRECTORY_COUNT * FILES_COUNT; ++i) {
allNewSize += files[i].getNewKeys();
}
return allNewSize;
}

@Override
public List<String> list() {
List result = new ArrayList<>();
for (DataBaseFile dbf : files) {
if (dbf.getSize() != 0) {
List<String> ans = dbf.getAllKeys();
result.addAll(ans);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.fizteh.fivt.students.kotsurba.junit;

public class DataBaseException extends Error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

extends Exception

public DataBaseException(final String message) {
super(message);
}
}
Loading