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

Commit

Permalink
fix codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
axothy committed Jan 3, 2024
1 parent 5369a89 commit d8fa37d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/main/java/ru/vk/itmo/chebotinalexandr/MurmurHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;


/**
* taken from <a href="https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/utils/MurmurHash.java#L178">...</a>.
* remade for MemorySegment
* generates 64-bit hash
*/

public final class MurmurHash {
public static final int DEFAULT_SEED = 104729;
public static final VarHandle LITTLE_ENDIAN_LONG =
Expand Down Expand Up @@ -123,6 +123,7 @@ public static long[] hash64(MemorySegment key, int offset, int length, long seed
k1 *= c2;
h1 ^= k1;
// fall through
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import ru.vk.itmo.Entry;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.UncheckedIOException;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/ru/vk/itmo/chebotinalexandr/SSTableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import ru.vk.itmo.BaseEntry;
import ru.vk.itmo.Entry;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

public final class SSTableUtils {
public static final long TOMBSTONE = -1;
Expand Down Expand Up @@ -122,6 +127,20 @@ private static Entry<MemorySegment> get(MemorySegment sstable, long index, long
}
}

public static void deleteOldSSTables(Path basePath, String extension) throws IOException {
try (Stream<Path> stream = Files.list(basePath)) {
stream
.filter(path -> path.toString().endsWith(extension))
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}

public static long entryByteSize(Entry<MemorySegment> entry) {
if (entry.value() == null) {
return entry.key().byteSize();
Expand Down
28 changes: 11 additions & 17 deletions src/main/java/ru/vk/itmo/chebotinalexandr/SSTablesStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static ru.vk.itmo.chebotinalexandr.SSTableUtils.*;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.BLOOM_FILTER_HASH_FUNCTIONS_OFFSET;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.BLOOM_FILTER_LENGTH_OFFSET;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.COMPACTION_NOT_FINISHED_TAG;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.ENTRIES_SIZE_OFFSET;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.OLDEST_SS_TABLE_INDEX;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.TOMBSTONE;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.binarySearch;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.deleteOldSSTables;
import static ru.vk.itmo.chebotinalexandr.SSTableUtils.entryByteSize;

public class SSTablesStorage {
private static final String SSTABLE_NAME = "sstable_";
Expand Down Expand Up @@ -88,7 +96,7 @@ private static void restoreCompaction(Path basePath, Arena arena) {
if (tag == COMPACTION_NOT_FINISHED_TAG) {
Files.delete(pathTmp);
} else {
deleteOldSSTables(basePath);
deleteOldSSTables(basePath, SSTABLE_EXTENSION);
Files.move(pathTmp, pathTmp.resolveSibling(SSTABLE_NAME + OLDEST_SS_TABLE_INDEX + SSTABLE_EXTENSION),
StandardCopyOption.ATOMIC_MOVE);
}
Expand Down Expand Up @@ -245,20 +253,6 @@ private static List<Path> getPaths(Path basePath) throws IOException {
}
}

private static void deleteOldSSTables(Path basePath) throws IOException {
try (Stream<Path> stream = Files.list(basePath)) {
stream
.filter(path -> path.toString().endsWith(SSTABLE_EXTENSION))
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}

public MemorySegment compact(Iterator<Entry<MemorySegment>> iterator,
long sizeForCompaction, long entryCount, long bfLength) throws IOException {
Path path = basePath.resolve(SSTABLE_NAME + ".tmp");
Expand Down Expand Up @@ -302,7 +296,7 @@ public MemorySegment compact(Iterator<Entry<MemorySegment>> iterator,

memorySegment.set(ValueLayout.JAVA_LONG_UNALIGNED, ENTRIES_SIZE_OFFSET, entryCount);

deleteOldSSTables(basePath);
deleteOldSSTables(basePath, SSTABLE_EXTENSION);
Files.move(path, path.resolveSibling(SSTABLE_NAME + OLDEST_SS_TABLE_INDEX + SSTABLE_EXTENSION),
StandardCopyOption.ATOMIC_MOVE);

Expand Down

0 comments on commit d8fa37d

Please sign in to comment.