Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DanilDerut #342

Open
wants to merge 4 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
23 changes: 23 additions & 0 deletions homework-g599-derut/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>homework-g599-derut</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
27 changes: 27 additions & 0 deletions homework-g599-derut/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mipt-java-2016</artifactId>
<groupId>ru.mipt.java2016</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>homework-g599-derut</artifactId>
<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-base</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-tests</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import java.io.IOException;
import java.io.RandomAccessFile;

public class DoubleRead implements Serializer<Double> {

@Override
public void write(RandomAccessFile f, Double val) throws IOException {
f.writeDouble(val);
}

@Override
public Double read(RandomAccessFile f) throws IOException {
return f.readDouble();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import java.io.IOException;
import java.io.RandomAccessFile;

public class IntegerRead implements Serializer<Integer> {

@Override
public void write(RandomAccessFile f, Integer val) throws IOException {
f.writeInt(val);
}

@Override
public Integer read(RandomAccessFile f) throws IOException {
return f.readInt();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import ru.mipt.java2016.homework.base.task2.KeyValueStorage;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

class MyKVStorage<K, V> implements KeyValueStorage<K, V> {
private final RandomAccessFile dataBase;
private final Map<K, V> data = new HashMap<>();
private final Serializer<K> keyS;
private final Serializer<V> valS;
private boolean isClosed = true;
private int length = 0;

MyKVStorage(String path, Serializer<K> keyS, Serializer<V> valS) throws IOException {
this.keyS = keyS;
this.valS = valS;
Path directPath = Paths.get(path, "dataBase.db");
dataBase = new RandomAccessFile(directPath.toFile(), "rw");
isClosed = false;
if (dataBase.length() != 0) {
initialRead();
}
}

private void initialRead() throws IOException {
length = dataBase.readInt();
for (int i = 0; i < length; ++i) {
K key = keyS.read(dataBase);
V val = valS.read(dataBase);
data.put(key, val);
}
}

@Override
public synchronized V read(K key) {
checker();
return data.get(key);
}

@Override
public synchronized boolean exists(K key) {
checker();
return data.containsKey(key);

}

@Override
public synchronized void write(K key, V value) {
checker();
if (!exists(key)) {
++length;
}
data.put(key, value);

}

@Override
public synchronized void delete(K key) {
checker();
if (exists(key)) {
data.remove(key);
--length;
}
}

@Override
public Iterator<K> readKeys() {
checker();
return data.keySet().iterator();
}

@Override
public synchronized int size() {
checker();
return length;
}

@Override
public synchronized void close() throws IOException {
if (!isClosed) {
render();
}

}

public void render() throws IOException {
dataBase.seek(0);
dataBase.setLength(0);
IntegerRead intR = new IntegerRead();
intR.write(dataBase, length);
for (Map.Entry<K, V> entry : data.entrySet()) {
keyS.write(dataBase, entry.getKey());
valS.write(dataBase, entry.getValue());
}
dataBase.close();
isClosed = true;
}

private void checker() {
if (isClosed) {
throw new RuntimeException("Closed!!!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import java.io.IOException;
import java.io.RandomAccessFile;

public interface Serializer<T> {

void write(RandomAccessFile f, T val) throws IOException;

T read(RandomAccessFile f) throws IOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import java.io.IOException;
import java.io.RandomAccessFile;

public class StringReader implements Serializer<String> {

@Override
public void write(RandomAccessFile f, String val) throws IOException {
int length = val.length();
char[] c = val.toCharArray();
f.writeInt(length);
for (int i = 0; i < length; i++) {
f.writeChar(c[i]);
}

}

@Override
public String read(RandomAccessFile f) throws IOException {
int length = f.readInt();
char[] c = new char[length];
for (int i = 0; i < length; i++) {
c[i] = f.readChar();
}
return new String(c);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import ru.mipt.java2016.homework.base.task2.KeyValueStorage;
import ru.mipt.java2016.homework.tests.task2.AbstractSingleFileStorageTest;
import ru.mipt.java2016.homework.tests.task2.Student;
import ru.mipt.java2016.homework.tests.task2.StudentKey;

import java.io.IOException;

public class DataBaseTest extends AbstractSingleFileStorageTest {
@Override
protected KeyValueStorage<String, String> buildStringsStorage(String path) {
try {
return new MyKVStorage(path, new StringReader(), new StringReader());
} catch (IOException e) {
return null;
}
}

@Override
protected KeyValueStorage<Integer, Double> buildNumbersStorage(String path) {
try {

return new MyKVStorage(path, new IntegerRead(), new DoubleRead());
} catch (IOException e) {
return null;
}
}

@Override
protected KeyValueStorage<StudentKey, Student> buildPojoStorage(String path) {
try {
return new MyKVStorage(path, new StudentKReader(), new StudentReader());
} catch (IOException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import ru.mipt.java2016.homework.tests.task2.StudentKey;

import java.io.IOException;
import java.io.RandomAccessFile;

public class StudentKReader implements Serializer<StudentKey> {
@Override
public void write(RandomAccessFile f, StudentKey val) throws IOException {
IntegerRead intS = new IntegerRead();
StringReader stringS = new StringReader();
intS.write(f, val.getGroupId());
stringS.write(f, val.getName());

}

@Override
public StudentKey read(RandomAccessFile f) throws IOException {
IntegerRead intS = new IntegerRead();
StringReader stringS = new StringReader();
return new StudentKey(intS.read(f), stringS.read(f));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.mipt.java2016.homework.g599.derut.task2;

import ru.mipt.java2016.homework.tests.task2.Student;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;

public class StudentReader implements Serializer<Student> {
@Override
public void write(RandomAccessFile f, Student val) throws IOException {
IntegerRead intS = new IntegerRead();
StringReader stringS = new StringReader();
DoubleRead dS = new DoubleRead();

intS.write(f, val.getGroupId());
stringS.write(f, val.getName());
stringS.write(f, val.getHometown());
f.writeLong(val.getBirthDate().getTime());
boolean f1 = val.isHasDormitory();
if (f1)
intS.write(f, 1);
else
intS.write(f, 0);
dS.write(f, val.getAverageScore());

}

@Override
public Student read(RandomAccessFile f) throws IOException {
IntegerRead intS = new IntegerRead();
StringReader stringS = new StringReader();
DoubleRead dS = new DoubleRead();
int gID = intS.read(f);
String name = stringS.read(f);
String hmt = stringS.read(f);
long time = f.readLong();
Date d = new Date(time);
int f1 = intS.read(f);
boolean f2;
if (f1 == 0)
f2 = false;
else
f2 = true;
double as = dS.read(f);
return new Student(gID, name, hmt, d, f2, as);
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<module>homework-g596-grebenshchikova</module>
<module>homework-g597-kochukov</module>
<module>homework-g597-sigareva</module>
<module>homework-g599-derut</module>
<module>workshop-materials</module>

</modules>
Expand Down