Skip to content
This repository was archived by the owner on Sep 27, 2022. It is now read-only.
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
10 changes: 0 additions & 10 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java

This file was deleted.

This file was deleted.

31 changes: 18 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,32 @@
package ru.fizteh.fivt.students.ivan_ivanov.filemap;
import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor;


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);

Executor exec = new Executor();

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);
}
}
24 changes: 0 additions & 24 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java

This file was deleted.

Loading