Skip to content

Commit

Permalink
Task3.v4.3 (And more)
Browse files Browse the repository at this point in the history
  • Loading branch information
gerasartem committed Dec 16, 2016
1 parent 5e587f4 commit 6e21785
Showing 1 changed file with 52 additions and 36 deletions.
Expand Up @@ -82,7 +82,12 @@ public V load(K key) throws Exception {

@Override
public V read(K key) {
checkClosed();
readLock.lock();
try {
checkClosed();
} finally {
readLock.unlock();
}
writeLock.lock();
try {
return cache.get(key);
Expand All @@ -95,9 +100,9 @@ public V read(K key) {

@Override
public boolean exists(K key) {
checkClosed();
readLock.lock();
try {
checkClosed();
return offsetTable.containsKey(key);
} finally {
readLock.unlock();
Expand All @@ -106,7 +111,12 @@ public boolean exists(K key) {

@Override
public void write(K key, V value) {
checkClosed();
readLock.lock();
try {
checkClosed();
} finally {
readLock.unlock();
}
writeLock.lock();
try {
offsetTableIsUpdated = true;
Expand All @@ -129,7 +139,12 @@ public void write(K key, V value) {

@Override
public void delete(K key) {
checkClosed();
readLock.lock();
try {
checkClosed();
} finally {
readLock.unlock();
}
writeLock.lock();
try {
offsetTableIsUpdated = true;
Expand All @@ -146,9 +161,9 @@ public void delete(K key) {

@Override
public Iterator<K> readKeys() {
checkClosed();
readLock.lock();
try {
checkClosed();
return offsetTable.keySet().iterator();
} finally {
readLock.unlock();
Expand All @@ -157,9 +172,9 @@ public Iterator<K> readKeys() {

@Override
public int size() {
checkClosed();
readLock.lock();
try {
checkClosed();
return offsetTable.size();
} finally {
readLock.unlock();
Expand All @@ -168,7 +183,12 @@ public int size() {

@Override
public void close() throws IOException {
checkClosed();
readLock.lock();
try {
checkClosed();
} finally {
readLock.unlock();
}
writeLock.lock();
try {
isClosed = true;
Expand All @@ -189,13 +209,8 @@ public void close() throws IOException {
}

private void checkClosed() {
readLock.lock();
try {
if (isClosed) {
throw new RuntimeException("Storage is closed");
}
} finally {
readLock.unlock();
if (isClosed) {
throw new RuntimeException("Storage is closed");
}
}

Expand Down Expand Up @@ -242,35 +257,36 @@ private void writeField(K key, V value) throws IOException {
}

private void refreshStorageFile() throws IOException {
if (oldNoteCounter > REFRESH_CONST * offsetTable.size()) {
try (StorageFileIO storageFileIO = this.storageFileIO.open()) {
storageLength = 0;
long oldFileOffset = 0;
for (int keySize = storageFileIO.copyReadSize();
keySize > 0; keySize = storageFileIO.copyReadSize()) {

ByteBuffer keyCode = storageFileIO.copyReadField(keySize);
int valueSize = storageFileIO.copyReadSize();
ByteBuffer valueCode = storageFileIO.copyReadField(valueSize);
K key = keySerializer.deserialize(keyCode);
if (oldNoteCounter <= REFRESH_CONST * offsetTable.size()) {
return;
}
try (StorageFileIO storageFileIO = this.storageFileIO.open()) {
storageLength = 0;
long oldFileOffset = 0;
for (int keySize = storageFileIO.copyReadSize();
keySize > 0; keySize = storageFileIO.copyReadSize()) {

if (offsetTable.containsKey(key) && offsetTable.get(key).equals(oldFileOffset)) {
ByteBuffer keyCode = storageFileIO.copyReadField(keySize);
int valueSize = storageFileIO.copyReadSize();
ByteBuffer valueCode = storageFileIO.copyReadField(valueSize);
K key = keySerializer.deserialize(keyCode);

storageFileIO.writeSize(keySize);
storageFileIO.writeField(keyCode);
storageLength += INT_SIZE + keySize;
if (offsetTable.containsKey(key) && offsetTable.get(key).equals(oldFileOffset)) {

offsetTable.put(key, storageLength);
storageFileIO.writeSize(keySize);
storageFileIO.writeField(keyCode);
storageLength += INT_SIZE + keySize;

storageFileIO.writeSize(valueSize);
storageFileIO.writeField(valueCode);
storageLength += INT_SIZE + valueSize;
}
offsetTable.put(key, storageLength);

oldFileOffset += 2 * INT_SIZE + keySize + valueSize;
storageFileIO.writeSize(valueSize);
storageFileIO.writeField(valueCode);
storageLength += INT_SIZE + valueSize;
}
oldNoteCounter = 0;

oldFileOffset += 2 * INT_SIZE + keySize + valueSize;
}
oldNoteCounter = 0;
}
}
}
Expand Down

0 comments on commit 6e21785

Please sign in to comment.