Skip to content

Commit

Permalink
Step-4. Correct Impl
Browse files Browse the repository at this point in the history
  • Loading branch information
atveritin committed Nov 1, 2023
1 parent b3dae75 commit a356e7e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
46 changes: 24 additions & 22 deletions src/main/java/ru/vk/itmo/tveritinalexandr/DiskStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,31 @@ protected boolean skip(Entry<MemorySegment> memorySegmentEntry) {
};
}

public void compact(Path storagePath, DiskStorage diskStorage, Iterator<Entry<MemorySegment>> firstIterator) throws IOException {
public void compact(Path storagePath, DiskStorage diskStorage, Iterator<Entry<MemorySegment>> inMemoryIterator)
throws IOException {
final Path indexFile = storagePath.resolve("index.idx");
final Path indexTmp = storagePath.resolve("index.tmp");
if (segmentList.isEmpty() && !firstIterator.hasNext()) return;

int fileName = 0;
if (segmentList.isEmpty() && !inMemoryIterator.hasNext()) return;

DiskStorage.saveForCompact(storagePath, diskStorage, firstIterator);
int maybeExistingFileName = 0;

while (fileName < segmentList.size()) {
Files.deleteIfExists(storagePath.resolve(String.valueOf(fileName)));
fileName ++;
}
DiskStorage.saveForCompact(storagePath, diskStorage, inMemoryIterator);

Files.deleteIfExists(storagePath.resolve("compacted.sst"));
while (maybeExistingFileName < segmentList.size()) {
Files.deleteIfExists(storagePath.resolve(String.valueOf(maybeExistingFileName)));
maybeExistingFileName ++;
}

Path source = storagePath.resolve(String.valueOf(segmentList.size()));
Path target = storagePath.resolve("compacted.sst");
System.out.println(Files.exists(target));
Files.createFile(target);
Files.deleteIfExists(storagePath.resolve("compacted"));

Path source = storagePath.resolve("compacting");
Path target = storagePath.resolve("compacted");
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);

Files.write(
indexTmp,
List.of("compacted.sst"),
List.of("compacted"),
StandardOpenOption.WRITE,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING
Expand All @@ -74,10 +73,15 @@ public void compact(Path storagePath, DiskStorage diskStorage, Iterator<Entry<Me
Files.move(indexTmp, indexFile, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
}

public static void saveForCompact(Path storagePath, DiskStorage diskStorage, Iterator<Entry<MemorySegment>> firstIterator)
// В методе есть много схожей логики с методом save(),
//но решил нарушить DRY, чтобы вам было удобнее проверять т.к. я взял референс 3-ого этапа
//и скорее всего весь PR будет зелёным)
public static void saveForCompact(Path storagePath, DiskStorage diskStorage,
Iterator<Entry<MemorySegment>> inMemoryIterator)
throws IOException {
final Path indexTmp = storagePath.resolve("index.tmp");
final Path indexFile = storagePath.resolve("index.idx");
final String compactingFileName = "compacting";

try {
Files.createFile(indexFile);
Expand All @@ -86,12 +90,10 @@ public static void saveForCompact(Path storagePath, DiskStorage diskStorage, Ite
}
List<String> existedFiles = Files.readAllLines(indexFile, StandardCharsets.UTF_8);

String newFileName = String.valueOf(existedFiles.size());

// index_size:
long dataSize = 0;
long count = 0;
var firstCycle = diskStorage.range(firstIterator, null, null);
var firstCycle = diskStorage.range(inMemoryIterator, null, null);
while (firstCycle.hasNext()) {
var entry = firstCycle.next();
dataSize += entry.key().byteSize();
Expand All @@ -106,7 +108,7 @@ public static void saveForCompact(Path storagePath, DiskStorage diskStorage, Ite

try (
FileChannel fileChannel = FileChannel.open(
storagePath.resolve(newFileName),
storagePath.resolve(compactingFileName),
StandardOpenOption.WRITE,
StandardOpenOption.READ,
StandardOpenOption.CREATE
Expand All @@ -120,11 +122,11 @@ public static void saveForCompact(Path storagePath, DiskStorage diskStorage, Ite
writeArena
);

// Решил попрыгать по страницам, но в 1 проход по итератору
// Прыгаем по страницам, но в 1 проход по итератору
// index_and_data:
long dataOffset = indexSize;
int indexOffset = 0;
var secondCycle = diskStorage.range(firstIterator, null, null);
var secondCycle = diskStorage.range(inMemoryIterator, null, null);
while (secondCycle.hasNext()){
var entry = secondCycle.next();
MemorySegment key = entry.key();
Expand All @@ -150,7 +152,7 @@ public static void saveForCompact(Path storagePath, DiskStorage diskStorage, Ite

List<String> list = new ArrayList<>(existedFiles.size() + 1);
list.addAll(existedFiles);
list.add(newFileName);
list.add(compactingFileName);
Files.write(
indexFile,
list,
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/ru/vk/itmo/tveritinalexandr/InMemoryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,14 @@ public Entry<MemorySegment> get(MemorySegment key) {

@Override
public void compact() throws IOException {
// flush();
diskStorage.compact(path, diskStorage, getInMemory(null, null));
}

// @Override
// public void flush() throws IOException {
// if (storage.isEmpty()) {
// return;
// }
// DiskStorage.save(path, storage.values());
// storage.clear();
// }
// Не был уверен, допускаются ли конкурентные вызовы flush
@Override
public synchronized void flush() throws IOException {
DiskStorage.save(path, storage.values());
}

@Override
public void close() throws IOException {
Expand All @@ -120,7 +116,7 @@ public void close() throws IOException {
arena.close();

if (!storage.isEmpty()) {
DiskStorage.save(path, storage.values());
flush();
}
}
}

0 comments on commit a356e7e

Please sign in to comment.