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

Павел Воропаев, 396, Parallel & Proxy #589

Open
wants to merge 2 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ru.fizteh.fivt.storage.structured.Table;
import ru.fizteh.fivt.students.pavel_voropaev.project.commands.DatabaseAbstractCommand;
import ru.fizteh.fivt.students.pavel_voropaev.project.commands.DatabaseInterpreterState;
import ru.fizteh.fivt.students.pavel_voropaev.project.custom_exceptions.TableDoesNotExistException;
import ru.fizteh.fivt.students.pavel_voropaev.project.custom_exceptions.ObjectDoesNotExistException;

import java.io.IOException;

Expand All @@ -22,7 +22,7 @@ public void exec(String[] param) throws IOException {

try {
state.getDatabase().removeTable(param[0]);
} catch (TableDoesNotExistException e) {
} catch (ObjectDoesNotExistException e) {
state.getOutputStream().println(param[0] + " not exists");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void exec(String[] param) {
out.println("not found");
} else {
out.println("found");
out.println("(" + Serializer.serialize(table, storeable, ' ', '\"') + ")");
out.println("(" + Serializer.serialize(storeable, " ", "\"") + ")");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void exec(String[] param) {
out.println("new");
} else {
out.println("overwrite");
out.println('(' + Serializer.serialize(table, value, ' ', '\"') + ')');
out.println('(' + Serializer.serialize(value, " ", "\"") + ')');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.fizteh.fivt.students.pavel_voropaev.project.custom_exceptions;

public class ObjectDoesNotExistException extends IllegalStateException {

public ObjectDoesNotExistException(String type, String name) {
super(type + " " + name + " not exists");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Database implements TableProvider {
private Map<String, Table> tables;
public class Database implements TableProvider, AutoCloseable {
private Map<String, MultiFileTable> tables;
private Path databasePath;
private ReadWriteLock lock;
private AtomicBoolean wasClosed;

public Database(String dbPath) {
wasClosed = new AtomicBoolean(false);
lock = new ReentrantReadWriteLock();

try {
databasePath = Paths.get(dbPath);
if (!Files.exists(databasePath)) {
Expand All @@ -44,7 +52,7 @@ public Database(String dbPath) {
throw new ContainsWrongFilesException(databasePath.toString());
}

Table currentTable = new MultiFileTable(databasePath, entry.getFileName().toString(), this);
MultiFileTable currentTable = new MultiFileTable(databasePath, entry.getFileName().toString(), this);
tables.put(entry.getFileName().toString(), currentTable);
}
} catch (IOException e) {
Expand All @@ -54,15 +62,23 @@ public Database(String dbPath) {

@Override
public Table getTable(String name) {
if (!isNameCorrect(name)) {
throw new InputMistakeException("Illegal table name: " + name);
lock.readLock().lock();
try {
checkClosed();
if (!isNameCorrect(name)) {
throw new InputMistakeException("Illegal table name: " + name);
}
return tables.get(name);
} finally {
lock.readLock().unlock();
}
return tables.get(name);
}

@Override
public Table createTable(String name, List<Class<?>> columnTypes) {
lock.writeLock().lock();
try {
checkClosed();
if (getTable(name) != null) {
return null;
}
Expand Down Expand Up @@ -95,33 +111,40 @@ public Table createTable(String name, List<Class<?>> columnTypes) {
throw new IOException("Cannot write table signature", e);
}

Table newTable = new MultiFileTable(databasePath, name, this);
MultiFileTable newTable = new MultiFileTable(databasePath, name, this);
tables.put(name, newTable);
return newTable;
} catch (IOException e) {
throw new InputMistakeException("Cannot create table: " + e.getMessage());
} finally {
lock.writeLock().unlock();
}
}

@Override
public void removeTable(String name) {
if (getTable(name) == null) {
throw new TableDoesNotExistException(name);
}

MultiFileTable table = (MultiFileTable) tables.get(name);
table.destroy();

tables.remove(name);
lock.writeLock().lock();
try {
Utils.rm(databasePath.resolve(name));
} catch (IOException e) {
throw new RuntimeException("Cannot remove directory (" + name + ") from disk: " + e.getMessage(), e);
checkClosed();
if (getTable(name) == null) {
throw new ObjectDoesNotExistException("Table", name);
}

tables.get(name).close();
tables.remove(name);
try {
Utils.rm(databasePath.resolve(name));
} catch (IOException e) {
throw new RuntimeException("Cannot remove directory (" + name + ") from disk: " + e.getMessage(), e);
}
} finally {
lock.writeLock().unlock();
}
}

@Override
public Storeable deserialize(Table table, String value) throws ParseException {
checkClosed();
if (table == null || value == null) {
throw new NullArgumentException("deserialize");
}
Expand All @@ -142,17 +165,20 @@ public Storeable deserialize(Table table, String value) throws ParseException {

@Override
public String serialize(Table table, Storeable value) throws ColumnFormatException {
return "[" + Serializer.serialize(table, value, ',', '\"') + "]";
checkClosed();
return "[" + Serializer.serialize(value, ",", "\"") + "]";
}

@Override
public Storeable createFor(Table table) {
checkClosed();
MultiFileTable databaseTable = (MultiFileTable) table;
return new TableEntry(databaseTable.signature);
}

@Override
public Storeable createFor(Table table, List<?> values) throws ColumnFormatException, IndexOutOfBoundsException {
checkClosed();
MultiFileTable databaseTable = (MultiFileTable) table;
Storeable storeable = new TableEntry(databaseTable.signature);

Expand All @@ -170,12 +196,48 @@ public Storeable createFor(Table table, List<?> values) throws ColumnFormatExcep

@Override
public List<String> getTableNames() {
List<String> list = new LinkedList<>();
list.addAll(tables.keySet());
return list;
lock.readLock().lock();
try {
checkClosed();
List<String> list = new LinkedList<>();
list.addAll(tables.keySet());
return list;
} finally {
lock.readLock().unlock();
}
}

private boolean isNameCorrect(String name) {
return (name != null) && !(name.matches(".*[</\"*%|\\\\:?>].*|.*\\."));
}

private void checkClosed() {
if (wasClosed.get()) {
throw new ObjectDoesNotExistException("Database", databasePath.toString());
}
}

void deleteNotify(String name) {
tables.remove(name);
}


@Override
public String toString() {
return getClass().getSimpleName() + "[" + databasePath.toAbsolutePath().toString() + "]";
}


@Override
public void close() throws Exception {
lock.writeLock().lock();
wasClosed.set(true);
try {
for (Map.Entry<String, MultiFileTable> entry : tables.entrySet()) {
entry.getValue().close();
}
} finally {
lock.writeLock().unlock();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package ru.fizteh.fivt.students.pavel_voropaev.project.database;

import ru.fizteh.fivt.storage.structured.TableProvider;
import ru.fizteh.fivt.storage.structured.TableProviderFactory;
import ru.fizteh.fivt.students.pavel_voropaev.project.custom_exceptions.ObjectDoesNotExistException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class DatabaseFactory implements TableProviderFactory {
public class DatabaseFactory implements TableProviderFactory, AutoCloseable {
private Map<String, Database> databases;
boolean wasClosed;

public DatabaseFactory() {
databases = new HashMap<>();
wasClosed = false;
}

@Override
public TableProvider create(String path) {
public Database create(String path) {
if (wasClosed) {
throw new ObjectDoesNotExistException("Provider", "");
}
if (path == null) {
throw new IllegalArgumentException("Database directory is null");
}
Expand All @@ -24,6 +36,16 @@ public TableProvider create(String path) {
throw new IllegalArgumentException("Cannot create database " + dirPath.toString(), e);
}
}
return new Database(path);
Database newDatabase = new Database(path);
databases.put(path, newDatabase);
return newDatabase;
}

@Override
public void close() throws Exception {
wasClosed = true;
for (Map.Entry<String, Database> entry : databases.entrySet()) {
entry.getValue().close();
}
}
}
Loading