Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Абрамов Илья, ИТМО ФИТиП М33341, HW5 #261

Open
wants to merge 97 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
c251b4e
Add Dao impl, add DaoFactory impl
IlyaAbramovv Sep 24, 2023
8d05af4
Remove static imports
IlyaAbramovv Sep 26, 2023
b155a32
Satisfy codeclimate
IlyaAbramovv Sep 26, 2023
091e5ec
Merge branch 'main' into main
incubos Sep 27, 2023
5f6ee6a
Merge branch 'main' into main
IlyaAbramovv Sep 28, 2023
df3a8cb
Merge branch 'main' into main
IlyaAbramovv Sep 29, 2023
76ac568
Merge remote-tracking branch 'upstream/main' into hw2
IlyaAbramovv Sep 30, 2023
1aecc06
First attempt
IlyaAbramovv Oct 1, 2023
833f036
Make reading with mapped memory
IlyaAbramovv Oct 1, 2023
3fd8ce3
Refactor code
IlyaAbramovv Oct 1, 2023
745f0ec
Merge branch 'polis-vk:main' into hw2
IlyaAbramovv Oct 1, 2023
e90d58f
Satisfy codeclimate
IlyaAbramovv Oct 1, 2023
e5ba827
Satisfy codeclimate
IlyaAbramovv Oct 1, 2023
789ca94
Merge remote-tracking branch 'upstream/main' into hw2
IlyaAbramovv Oct 2, 2023
232825f
Merge branch 'main' into hw2
IlyaAbramovv Oct 2, 2023
ecd33e3
Merge branch 'main' into hw2
daniil-ushkov Oct 3, 2023
5aee110
Merge branch 'main' into hw2
IlyaAbramovv Oct 4, 2023
a537415
Merge branch 'main' into hw2
IlyaAbramovv Oct 4, 2023
ea7bf15
Remove redundant condition
IlyaAbramovv Oct 4, 2023
0931c16
Merge remote-tracking branch 'upstream/main' into hw3
IlyaAbramovv Oct 7, 2023
f483d7d
Merge remote-tracking branch 'upstream/main' into hw3
IlyaAbramovv Oct 8, 2023
f4e6bce
Merge remote-tracking branch 'upstream/main' into hw3
IlyaAbramovv Oct 12, 2023
2df562b
Add some logic to hw3
IlyaAbramovv Oct 13, 2023
d961a45
Add some logic to hw3
IlyaAbramovv Oct 14, 2023
45d5027
Passes all tests except OOM
IlyaAbramovv Oct 15, 2023
9aeea20
Extract methods
IlyaAbramovv Oct 15, 2023
5159443
Attempt 1 (passes tests)
IlyaAbramovv Oct 15, 2023
bdfcc15
Add index above index file, implement binary search in get for a sing…
IlyaAbramovv Oct 16, 2023
4985a2d
Implement binary search in iterator get
IlyaAbramovv Oct 16, 2023
d1539f3
Rename InMemoryDao -> DaoImpl
IlyaAbramovv Oct 16, 2023
915e07f
Remove redundant indexIndex file logic
IlyaAbramovv Oct 17, 2023
5a11ed5
Rename Index -> SSTable, cleanup iterator code
IlyaAbramovv Oct 17, 2023
228d601
All sstable files are now mapped in constructor
IlyaAbramovv Oct 17, 2023
f619f87
Merge remote-tracking branch 'upstream/main' into hw3
IlyaAbramovv Oct 17, 2023
10cd428
Introduce DaoIterator class
IlyaAbramovv Oct 17, 2023
7c16c6d
Add TableEntry classes
IlyaAbramovv Oct 17, 2023
0d852d8
Refactor code
IlyaAbramovv Oct 17, 2023
a7dad9a
Change DaoImpl constructor signature
IlyaAbramovv Oct 17, 2023
a635a88
Refactor code
IlyaAbramovv Oct 18, 2023
0e92e91
Change entryNum size from long to int
IlyaAbramovv Oct 18, 2023
58e4b24
Merge remote-tracking branch 'upstream/main' into hw3
IlyaAbramovv Oct 18, 2023
584d849
Remove debug methods from DaoFactoryImpl
IlyaAbramovv Oct 18, 2023
e31bd9d
Satisfy codeclimate
IlyaAbramovv Oct 18, 2023
0d18e58
Satisfy codeclimate
IlyaAbramovv Oct 18, 2023
e85a34f
Satisfy codeclimate
IlyaAbramovv Oct 18, 2023
ec17263
Merge remote-tracking branch 'upstream/main' into hw4
IlyaAbramovv Oct 22, 2023
0f1a8e4
Hw4 Passes tests
IlyaAbramovv Oct 22, 2023
12faa86
Extract writeIteratorIntoFile method
IlyaAbramovv Oct 22, 2023
df0d1e4
Consistent state is restored if db was dropped during compacting
IlyaAbramovv Oct 24, 2023
f77cff6
Merge remote-tracking branch 'upstream/main' into hw4
IlyaAbramovv Oct 31, 2023
1b0450a
HW4 done
IlyaAbramovv Oct 31, 2023
84af74d
Introduce StorageFileWriter delegate
IlyaAbramovv Oct 31, 2023
3453bd5
Make constants private
IlyaAbramovv Oct 31, 2023
58adb4a
Fix hw3 issues
IlyaAbramovv Nov 1, 2023
c46f2ba
Satisfy codeclimate
IlyaAbramovv Nov 1, 2023
36c21db
Merge branch 'main' into hw4
incubos Nov 2, 2023
35e2e16
Merge branch 'main' into hw4
IlyaAbramovv Nov 3, 2023
f5dbcd2
Empty commit in case to restart codeclimate
IlyaAbramovv Nov 3, 2023
98eb995
Empty commit in case to restart codeclimate
IlyaAbramovv Nov 3, 2023
d26b61c
Merge remote-tracking branch 'upstream/main' into hw4
IlyaAbramovv Nov 5, 2023
75667df
Restart CI
IlyaAbramovv Nov 5, 2023
5862760
Add flush on maxThresholdBytes excess
IlyaAbramovv Nov 7, 2023
c00b078
Prettify flush()
IlyaAbramovv Nov 7, 2023
11cb076
Merge remote-tracking branch 'upstream/main' into hw5
IlyaAbramovv Nov 7, 2023
eda7bc5
Create ConcurrentMapWithSize
IlyaAbramovv Nov 8, 2023
26ff22c
Implement background flush
IlyaAbramovv Nov 8, 2023
3e122da
Change reentrantLock to semaphore
IlyaAbramovv Nov 8, 2023
e4d984d
Merge remote-tracking branch 'upstream/main' into hw5
IlyaAbramovv Nov 9, 2023
f8c68ef
Implement background compact
IlyaAbramovv Nov 9, 2023
ebfeee9
Merge remote-tracking branch 'upstream/main' into hw5
IlyaAbramovv Nov 12, 2023
c0639c8
Add some locks, DaoException is now thrown instead of OOM
IlyaAbramovv Nov 14, 2023
a94f642
Remove FileChannel lists as redundant
IlyaAbramovv Nov 14, 2023
99cb74f
Move arena handling to Storage class
IlyaAbramovv Nov 14, 2023
2df5f0b
Change arena from shared to auto
IlyaAbramovv Nov 14, 2023
cef7207
Fix finishCompact issues
IlyaAbramovv Nov 20, 2023
90eaf6b
Extract Compactor delegate
IlyaAbramovv Nov 20, 2023
5b7aba1
Merge remote-tracking branch 'upstream/main' into hw5
IlyaAbramovv Nov 20, 2023
70686e4
Satisfy codeclimate
IlyaAbramovv Nov 20, 2023
97c76d8
Satisfy codeclimate
IlyaAbramovv Nov 20, 2023
605145a
Merge branch 'main' into hw5
incubos Nov 21, 2023
8396a44
Merge branch 'main' into hw5
incubos Nov 21, 2023
8c5c1db
Merge branch 'main' into hw5
atimofeyev Nov 22, 2023
2486154
Merge branch 'main' into hw5
atimofeyev Nov 22, 2023
87563d7
Flush is now executed in the background
IlyaAbramovv Nov 22, 2023
ef9ab21
Merge remote-tracking branch 'upstream/main' into hw5
IlyaAbramovv Nov 22, 2023
90257bd
Merge remote-tracking branch 'origin/hw5' into hw5
IlyaAbramovv Nov 22, 2023
2f9b630
Now close() works properly
IlyaAbramovv Nov 22, 2023
e2aafc4
Change flushLock from semaphore to atomicBoolean
IlyaAbramovv Nov 22, 2023
a740061
Move size calc out of lock
IlyaAbramovv Nov 22, 2023
f9a98a0
Calculate map size on fly
IlyaAbramovv Nov 22, 2023
ed20aca
Satisfy codeclimate
IlyaAbramovv Nov 22, 2023
f9316c3
Merge branch 'main' into hw5
atimofeyev Nov 22, 2023
6834529
One more try
IlyaAbramovv Nov 22, 2023
7fa9b9f
Merge backgroundCompactQueue and backgroundFlushQueue
IlyaAbramovv Nov 22, 2023
a35d9ff
Change something
IlyaAbramovv Nov 22, 2023
c2acea0
Merge remote-tracking branch 'upstream/main' into hw5
IlyaAbramovv Dec 11, 2023
8e4a164
Merge branch 'main' into hw5
Dalvikk Dec 26, 2023
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
5 changes: 5 additions & 0 deletions src/main/java/ru/vk/itmo/abramovilya/CompactionState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.vk.itmo.abramovilya;

enum CompactionState {
DELETE_REMAINING, MOVE_COMPACTED, NO_COMPACTION
}
146 changes: 146 additions & 0 deletions src/main/java/ru/vk/itmo/abramovilya/Compactor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package ru.vk.itmo.abramovilya;

import ru.vk.itmo.BaseEntry;
import ru.vk.itmo.Entry;

import java.io.IOException;
import java.lang.foreign.MemorySegment;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Iterator;

class Compactor {
private final Storage storage;
CompactionState compactionState;

Compactor(Storage storage) {
this.storage = storage;
this.compactionState = CompactionState.NO_COMPACTION;
}

CompactionState intToCompactionState(int i) {
for (CompactionState cs : CompactionState.values()) {
if (cs.ordinal() == i) {
return cs;
}
}
throw new IllegalArgumentException("Unknown state: " + i);
}

// compact вызывается из dao однопоточно
void compact(Iterator<Entry<MemorySegment>> iterator1,
Iterator<Entry<MemorySegment>> iterator2,
int sstablesToCompact) throws IOException {
if (!Files.exists(storage.compactedTablesAmountFilePath)) {
Files.createFile(storage.compactedTablesAmountFilePath);
}
Files.writeString(storage.compactedTablesAmountFilePath,
String.valueOf(sstablesToCompact),
StandardOpenOption.WRITE);

Entry<Long> storageIndexSize = calcCompactedSStableIndexSize(iterator1);
Path compactingSStablePath =
storage.storagePath.resolve(Storage.SSTABLE_BASE_NAME + Storage.COMPACTING_SUFFIX);
Path compactingIndexPath =
storage.storagePath.resolve(Storage.INDEX_BASE_NAME + Storage.COMPACTING_SUFFIX);
StorageFileWriter.writeIteratorIntoFile(storageIndexSize.key(),
storageIndexSize.value(),
iterator2,
compactingSStablePath,
compactingIndexPath);

Path compactedSStablePath =
storage.storagePath.resolve(Storage.SSTABLE_BASE_NAME + Storage.COMPACTED_SUFFIX);
Path compactedIndexPath =
storage.storagePath.resolve(Storage.INDEX_BASE_NAME + Storage.COMPACTED_SUFFIX);
Files.move(compactingSStablePath, compactedSStablePath, StandardCopyOption.ATOMIC_MOVE);
Files.move(compactingIndexPath, compactedIndexPath, StandardCopyOption.ATOMIC_MOVE);

compactionState = CompactionState.DELETE_REMAINING;
if (!Files.exists(storage.compactionStateFilePath)) {
Files.createFile(storage.compactionStateFilePath);
}
Files.writeString(storage.compactionStateFilePath,
String.valueOf(compactionState.ordinal()),
StandardOpenOption.WRITE);
}

void finishCompact(int compactedSStablesAmount) throws IOException {
if (compactionState == CompactionState.DELETE_REMAINING) {
for (int i = 0; i < compactedSStablesAmount; i++) {
Files.deleteIfExists(storage.storagePath.resolve(Storage.SSTABLE_BASE_NAME + i));
Files.deleteIfExists(storage.storagePath.resolve(Storage.INDEX_BASE_NAME + i));
}
compactionState = CompactionState.MOVE_COMPACTED;
Files.writeString(storage.compactionStateFilePath,
String.valueOf(compactionState.ordinal()),
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
}

if (compactionState == CompactionState.MOVE_COMPACTED) {
Path compactedSStablePath =
storage.storagePath.resolve(Storage.SSTABLE_BASE_NAME + Storage.COMPACTED_SUFFIX);
if (Files.exists(compactedSStablePath)) {
Files.move(compactedSStablePath,
storage.storagePath.resolve(Storage.SSTABLE_BASE_NAME + 0),
StandardCopyOption.ATOMIC_MOVE,
StandardCopyOption.REPLACE_EXISTING);
}
Path compactedIndexPath =
storage.storagePath.resolve(Storage.INDEX_BASE_NAME + Storage.COMPACTED_SUFFIX);

if (Files.exists(compactedIndexPath)) {
Files.move(compactedIndexPath,
storage.storagePath.resolve(Storage.INDEX_BASE_NAME + 0),
StandardCopyOption.ATOMIC_MOVE,
StandardCopyOption.REPLACE_EXISTING);
}
}

int totalSStables = Integer.parseInt(Files.readString(storage.metaFilePath));

storage.sstableMappedList = new ArrayList<>();
storage.indexMappedList = new ArrayList<>();

for (int i = compactedSStablesAmount; i < totalSStables; i++) {
Path oldSStablePath = storage.storagePath.resolve(Storage.SSTABLE_BASE_NAME + i);
Path newSStablePath = storage.storagePath.resolve(
Storage.SSTABLE_BASE_NAME + convertOldFileNumToNew(i, compactedSStablesAmount));
Path oldIndexPath = storage.storagePath.resolve(Storage.INDEX_BASE_NAME + i);
Path newIndexPath = storage.storagePath.resolve(
Storage.INDEX_BASE_NAME + convertOldFileNumToNew(i, compactedSStablesAmount));

if (Files.exists(oldSStablePath)) {
Files.move(oldSStablePath, newSStablePath, StandardCopyOption.ATOMIC_MOVE);
}
if (Files.exists(oldIndexPath)) {
Files.move(oldIndexPath, newIndexPath, StandardCopyOption.ATOMIC_MOVE);
}
}

int newTotalSStables = convertOldFileNumToNew(totalSStables, compactedSStablesAmount);
storage.fillFileRepresentationLists(newTotalSStables);

Files.writeString(storage.metaFilePath, String.valueOf(newTotalSStables));
Files.deleteIfExists(storage.compactedTablesAmountFilePath);
}

private static int convertOldFileNumToNew(int oldNum, int compactedSStablesNum) {
return oldNum - compactedSStablesNum + 1;
}

private Entry<Long> calcCompactedSStableIndexSize(Iterator<Entry<MemorySegment>> iterator) {
long storageSize = 0;
long indexSize = 0;
while (iterator.hasNext()) {
Entry<MemorySegment> entry = iterator.next();
storageSize += entry.key().byteSize() + entry.value().byteSize() + 2 * Long.BYTES;
indexSize += Integer.BYTES + Long.BYTES;
}
return new BaseEntry<>(storageSize, indexSize);
}
}
13 changes: 13 additions & 0 deletions src/main/java/ru/vk/itmo/abramovilya/DaoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.vk.itmo.abramovilya;

public class DaoException extends RuntimeException {
public DaoException(String cause) {
super(cause);
}

public static final class DaoMemoryException extends DaoException {
public DaoMemoryException(String cause) {
super(cause);
}
}
}
Loading
Loading