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

Иванов Иван, 397, JUnit #558

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 0 additions & 10 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java

This file was deleted.

12 changes: 6 additions & 6 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package ru.fizteh.fivt.students.ivan_ivanov.filemap;

import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell;
import ru.fizteh.fivt.students.ivan_ivanov.shell.Command;

import java.io.IOException;

public class Exit implements Command {
public class Exit implements Command<FileMapState> {

public String getName() {

public final String getName() {
return "exit";
}

public final void executeCmd(final Shell filemap, final String[] args) throws IOException {
Utils.write(((FileMap) filemap).getFileMapState().getDataBase(),
((FileMap) filemap).getFileMapState().getDataFile());
public void executeCmd(FileMapState inState, String[] args) throws IOException {

FileMapUtils.write(inState.getDataBase(), inState.getDataFile());
System.exit(0);
}
}
19 changes: 0 additions & 19 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor;
import ru.fizteh.fivt.students.ivan_ivanov.shell.Command;

public class FileMapExecutor extends Executor {
public class FileMapExecutor extends Executor<FileMapState> {

public FileMapExecutor() {

list();
}

public final void list() {
public void list() {

Command put = new Put();
mapOfCmd.put(put.getName(), put);
Command get = new Get();
mapOfCmd.put(get.getName(), get);
Command remove = new Remove();
mapOfCmd.put(remove.getName(), remove);
Command<FileMapState> exit = new Exit();
mapOfCmd.put(exit.getName(), exit);
Command list = new List();
mapOfCmd.put(list.getName(), list);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Этот класс лишний. В Main'е заведите массив из объектов {new Put(), new Get(), ...} и передайте его в класс Интерпретатор. Интерпретатор должен сам положить команды в свой мап.

Command exit = new Exit();
mapOfCmd.put(exit.getName(), exit);
}
}
29 changes: 16 additions & 13 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package ru.fizteh.fivt.students.ivan_ivanov.filemap;

import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell;

import java.io.File;
import java.io.IOException;

public class FileMapMain {
public static void main(final String[] args) throws IOException {
public static void main(String[] args) throws IOException {

String currentProperty = System.getProperty("db.file");
File base = new File(currentProperty);
if (!base.exists()) {
base.createNewFile();
String currentProperty = System.getProperty("fizteh.db.dir");
if (currentProperty == null) {
System.exit(-1);
}

File base = new File(currentProperty);
try {
if (!base.exists()) {
base.createNewFile();
}

base = base.getCanonicalFile().toPath().resolve("db.dat").toFile();
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
FileMapState startState = new FileMapState(base);
FileMapUtils.readDataBase(startState);
Shell<FileMapState> filemap = new Shell<FileMapState>(startState);

FileMapExecutor exec = new FileMapExecutor();

FileMap filemap = new FileMap(base);
FileMapExecutor exec = new FileMapExecutor();
try {
if (args.length > 0) {
filemap.batchState(args, exec);
} else {
Expand Down
22 changes: 13 additions & 9 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@

public class FileMapState {

public Map<String, String> dataBase;
public File dataFile;
private Map<String, String> dataBase;
private File dataFile;

public FileMapState(File currentFile) {

dataBase = new HashMap<String, String>();
dataFile = currentFile;
}

public Map<String, String> getDataBase() {

public final Map<String, String> getDataBase() {
return dataBase;
}

public final File getDataFile() {
public File getDataFile() {

return dataFile;
}

public FileMapState(final File currentFile) {
dataBase = new HashMap<String, String>();
dataFile = currentFile;
}

public void setDataBase(Map<String, String> map) {

dataBase = map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
import java.util.List;
import java.util.Map;


public class Utils {
public class FileMapUtils {

private static final long MAX_SIZE = 1024 * 1024;
private static final int POS_STEP = 5;
private static final int BUF_SIZE = 4096;

private static String readKey(final DataInputStream dataStream) throws IOException {
private static String readKey(DataInputStream dataStream) throws IOException {

ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
byte b = dataStream.readByte();
int length = 0;
while (b != 0) {
byteOutputStream.write(b);
b = dataStream.readByte();
try {
b = dataStream.readByte();
} catch (EOFException e) {
throw new IOException("wrong data format");
}
length++;
if (length > MAX_SIZE) {
throw new IOException("wrong data format");
Expand All @@ -29,12 +30,12 @@ private static String readKey(final DataInputStream dataStream) throws IOExcepti
if (length == 0) {
throw new IOException("wrong data format");
}

return byteOutputStream.toString(StandardCharsets.UTF_8.toString());
}

private static String readValue(final DataInputStream dis, final long offset1,
final long offset2, final long position, final long len) throws IOException {
private static String readValue(DataInputStream dis, long offset1,
long offset2, long position, long len) throws IOException {

dis.mark((int) len);
dis.skip(offset1 - position);
byte[] buffer = new byte[(int) (offset2 - offset1)];
Expand All @@ -44,30 +45,31 @@ private static String readValue(final DataInputStream dis, final long offset1,
return value;
}

public static void readDataBase(final FileMapState state) throws IOException {
public static void readDataBase(FileMapState state) throws IOException {

if (state.getDataFile().length() == 0) {
return;
}

InputStream currentStream = new FileInputStream(state.getDataFile());
BufferedInputStream bufferStream = new BufferedInputStream(currentStream, BUF_SIZE);
BufferedInputStream bufferStream = new BufferedInputStream(currentStream, 4096);
DataInputStream dataStream = new DataInputStream(bufferStream);

int fileLength = (int) state.getDataFile().length();

try {
int position = 0;
String key1 = readKey(dataStream);

position += key1.getBytes(StandardCharsets.UTF_8).length;
int offset1 = dataStream.readInt();
int firstOffset = offset1;
position += POS_STEP;

position += 5;
while (position != firstOffset) {
String key2 = readKey(dataStream);
position += key2.getBytes(StandardCharsets.UTF_8).length;
int offset2 = dataStream.readInt();
position += POS_STEP;
position += 5;
String value = readValue(dataStream, offset1, offset2, position, fileLength);
state.getDataBase().put(key1, value);
offset1 = offset2;
Expand All @@ -78,36 +80,42 @@ public static void readDataBase(final FileMapState state) throws IOException {
} finally {
closeStream(dataStream);
}

}

private static void closeStream(final Closeable stream) throws IOException {
private static void closeStream(Closeable stream) throws IOException {

stream.close();
}

public static void write(final Map<String, String> dataBase, final File currentFile) throws IOException {

OutputStream currentStream = new FileOutputStream(currentFile);
BufferedOutputStream bufferStream = new BufferedOutputStream(currentStream, BUF_SIZE);
DataOutputStream dataStream = new DataOutputStream(bufferStream);
long biasing = 0;

for (String key : dataBase.keySet()) {
biasing += key.getBytes(StandardCharsets.UTF_8).length + POS_STEP;
}
List<String> values = new ArrayList<String>(dataBase.keySet().size());
for (String key : dataBase.keySet()) {
String value = dataBase.get(key);
values.add(value);
dataStream.write(key.getBytes(StandardCharsets.UTF_8));
dataStream.writeByte(0);
dataStream.writeInt((int) biasing);
biasing += value.getBytes(StandardCharsets.UTF_8).length;
}
public static void write(Map<String, String> dataBase, File currentFile) throws IOException {

for (String value : values) {
dataStream.write(value.getBytes());
try {
FileOutputStream currentStream = new FileOutputStream(currentFile);
BufferedOutputStream bufferStream = new BufferedOutputStream(currentStream, 4096);
DataOutputStream dataStream = new DataOutputStream(bufferStream);
long biasing = 0;
try {
for (String key : dataBase.keySet()) {
biasing += key.getBytes(StandardCharsets.UTF_8).length + 5;
}
List<String> values = new ArrayList<String>(dataBase.keySet().size());
for (String key : dataBase.keySet()) {
String value = dataBase.get(key);
values.add(value);
dataStream.write(key.getBytes(StandardCharsets.UTF_8));
dataStream.writeByte(0);
dataStream.writeInt((int) biasing);
biasing += value.getBytes(StandardCharsets.UTF_8).length;
}

for (String value : values) {
dataStream.write(value.getBytes());
}
} finally {
closeStream(dataStream);
}
} catch (IOException e) {
throw new IOException("cannot write '" + currentFile.getName() + "'", e);
}
closeStream(dataStream);
}
}
17 changes: 11 additions & 6 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package ru.fizteh.fivt.students.ivan_ivanov.filemap;

import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell;
import ru.fizteh.fivt.students.ivan_ivanov.shell.Command;

import java.io.IOException;

public class Get implements Command {
public class Get implements Command<FileMapState> {

public String getName() {

public final String getName() {
return "get";
}

public final void executeCmd(final Shell filemap, final String[] args) throws IOException {
String key = args[0];
String value = ((FileMap) filemap).getFileMapState().getDataBase().get(key);
public void executeCmd(FileMapState inState, String[] args) throws IOException {

if (args.length != 1) {
System.out.println("incorrect number of arguments");
return;
}

String value = inState.getDataBase().get(args[0]);
if (value == null) {
System.out.println("not found");
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package ru.fizteh.fivt.students.ivan_ivanov.filemap;

import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell;
import ru.fizteh.fivt.students.ivan_ivanov.shell.Command;

import java.io.IOException;
import java.util.Set;

public class List implements Command {
public class List implements Command<FileMapState> {

public final String getName() {
return "list";
}

public final void executeCmd(final Shell filemap, final String[] args) throws IOException {
Set<String> keys = ((FileMap) filemap).getFileMapState().getDataBase().keySet();
public final void executeCmd(FileMapState inState, String[] args) throws IOException {
Set<String> keys = inState.getDataBase().keySet();
System.out.println(String.join(", ", keys));
}
}
Loading