This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into hw5
# Conflicts: # src/main/java/ru/vk/itmo/abramovilya/DaoImpl.java # src/main/java/ru/vk/itmo/abramovilya/DaoIterator.java # src/main/java/ru/vk/itmo/abramovilya/Storage.java # src/main/java/ru/vk/itmo/abramovilya/StorageFileWriter.java # src/main/java/ru/vk/itmo/test/abramovilya/DaoFactoryImpl.java
- Loading branch information
Showing
106 changed files
with
7,393 additions
and
1,622 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/ru/vk/itmo/bazhenovkirill/MemorySegmentUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ru.vk.itmo.bazhenovkirill; | ||
|
||
import ru.vk.itmo.BaseEntry; | ||
import ru.vk.itmo.Entry; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
|
||
public final class MemorySegmentUtils { | ||
|
||
private MemorySegmentUtils() { | ||
|
||
} | ||
|
||
public static MemorySegment getSlice(MemorySegment segment, long start, long end) { | ||
return segment.asSlice(start, end - start); | ||
} | ||
|
||
public static long startOfKey(MemorySegment segment, long inx) { | ||
return segment.get(ValueLayout.JAVA_LONG_UNALIGNED, inx * 2 * Long.BYTES); | ||
} | ||
|
||
public static long endOfKey(MemorySegment segment, long inx) { | ||
return normalize(startOfValue(segment, inx)); | ||
} | ||
|
||
public static MemorySegment getKey(MemorySegment segment, long inx) { | ||
return getSlice(segment, startOfKey(segment, inx), endOfKey(segment, inx)); | ||
} | ||
|
||
public static long startOfValue(MemorySegment segment, long inx) { | ||
return segment.get(ValueLayout.JAVA_LONG_UNALIGNED, Long.BYTES + inx * 2 * Long.BYTES); | ||
} | ||
|
||
public static long endOfValue(MemorySegment segment, long inx) { | ||
if (inx < recordsCount(segment) - 1) { | ||
return startOfKey(segment, inx + 1); | ||
} | ||
return segment.byteSize(); | ||
} | ||
|
||
public static Entry<MemorySegment> getEntry(MemorySegment segment, long inx) { | ||
MemorySegment key = getKey(segment, inx); | ||
MemorySegment value = getValue(segment, inx); | ||
return new BaseEntry<>(key, value); | ||
} | ||
|
||
public static MemorySegment getValue(MemorySegment segment, long inx) { | ||
long start = startOfValue(segment, inx); | ||
if (start < 0) { | ||
return null; | ||
} | ||
return getSlice(segment, start, endOfValue(segment, inx)); | ||
} | ||
|
||
public static long tombstone(long offset) { | ||
return 1L << 63 | offset; | ||
} | ||
|
||
public static long normalize(long offset) { | ||
return offset & ~(1L << 63); | ||
} | ||
|
||
public static long recordsCount(MemorySegment segment) { | ||
return indexSize(segment) / (2 * Long.BYTES); | ||
} | ||
|
||
public static long indexSize(MemorySegment segment) { | ||
return segment.get(ValueLayout.JAVA_LONG_UNALIGNED, 0); | ||
} | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
src/main/java/ru/vk/itmo/bazhenovkirill/MergeIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package ru.vk.itmo.bazhenovkirill; | ||
|
||
import java.util.Collection; | ||
import java.util.Comparator; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.PriorityQueue; | ||
|
||
public class MergeIterator<T> implements Iterator<T> { | ||
|
||
private final PriorityQueue<PeekIterator<T>> priorityQueue; | ||
|
||
private final Comparator<T> comparator; | ||
|
||
private PeekIterator<T> peek; | ||
|
||
private static class PeekIterator<T> implements Iterator<T> { | ||
|
||
private final Iterator<T> iterator; | ||
private T next; | ||
private final int id; | ||
|
||
public PeekIterator(int id, Iterator<T> iterator) { | ||
this.id = id; | ||
this.iterator = iterator; | ||
if (iterator.hasNext()) { | ||
next = iterator.next(); | ||
} | ||
} | ||
|
||
private T peek() { | ||
return next; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return next != null || iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
T curr = next; | ||
next = iterator.hasNext() ? iterator.next() : null; | ||
return curr; | ||
} | ||
} | ||
|
||
public MergeIterator(Collection<Iterator<T>> iterators, Comparator<T> comparator) { | ||
this.comparator = comparator; | ||
Comparator<PeekIterator<T>> peekComp = (o1, o2) -> comparator.compare(o1.peek(), o2.peek()); | ||
priorityQueue = new PriorityQueue<>( | ||
iterators.size(), | ||
peekComp.thenComparing(o -> -o.id) | ||
); | ||
|
||
int id = 0; | ||
for (Iterator<T> iterator : iterators) { | ||
if (iterator.hasNext()) { | ||
priorityQueue.add(new PeekIterator<>(id++, iterator)); | ||
} | ||
} | ||
} | ||
|
||
protected boolean skip(T t) { | ||
return t == null; | ||
} | ||
|
||
private PeekIterator<T> peek() { | ||
while (peek == null) { | ||
peek = priorityQueue.poll(); | ||
if (peek == null) { | ||
return null; | ||
} | ||
|
||
PeekIterator<T> next = priorityQueue.peek(); | ||
while (next != null && comparator.compare(peek.peek(), next.peek()) == 0) { | ||
PeekIterator<T> poll = priorityQueue.poll(); | ||
if (poll != null) { | ||
skipElement(poll); | ||
} | ||
next = priorityQueue.peek(); | ||
} | ||
|
||
if (!peek.hasNext()) { | ||
peek = null; | ||
continue; | ||
} | ||
|
||
if (skip(peek.peek())) { | ||
skipElement(peek); | ||
peek = null; | ||
} | ||
} | ||
|
||
return peek; | ||
} | ||
|
||
private void skipElement(PeekIterator<T> iterator) { | ||
iterator.next(); | ||
if (iterator.hasNext()) { | ||
priorityQueue.add(iterator); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return peek() != null; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
PeekIterator<T> peekIterator = peek(); | ||
if (peekIterator == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
T next = peek.next(); | ||
this.peek = null; | ||
if (peekIterator.hasNext()) { | ||
priorityQueue.add(peekIterator); | ||
} | ||
return next; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ru.vk.itmo.bazhenovkirill; | ||
|
||
public class Offset { | ||
long data; | ||
long index; | ||
|
||
Offset(long data, long index) { | ||
this.data = data; | ||
this.index = index; | ||
} | ||
} |
Oops, something went wrong.