diff --git a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java new file mode 100644 index 000000000..506096ee1 --- /dev/null +++ b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java @@ -0,0 +1,61 @@ +package ru.vk.itmo.podobaalexandr; + +import ru.vk.itmo.Dao; +import ru.vk.itmo.Entry; + +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; +import java.util.Comparator; +import java.util.Iterator; +import java.util.concurrent.ConcurrentNavigableMap; +import java.util.concurrent.ConcurrentSkipListMap; + +public class InMemoryDaoImpl implements Dao> { + ConcurrentNavigableMap> memorySegmentEntryMap + = new ConcurrentSkipListMap<>(new MyComparator()); + + @Override + public Iterator> get(MemorySegment from, MemorySegment to) { + ConcurrentNavigableMap> innerMap = memorySegmentEntryMap; + + if (from != null && to == null) { + innerMap = innerMap.tailMap(from); + } else if (from == null && to != null) { + innerMap = innerMap.headMap(to); + } else if (from != null) { + innerMap = innerMap.subMap(from, to); + } + + return innerMap.values().iterator(); + } + + @Override + public void upsert(Entry entry) { + memorySegmentEntryMap.put(entry.key(), entry); + } + + @Override + public Entry get(MemorySegment key) { + return memorySegmentEntryMap.get(key); + } + + private static class MyComparator implements Comparator { + + @Override + public int compare(MemorySegment o1, MemorySegment o2) { + + int sizeDiff = Long.compare(o1.byteSize(), o2.byteSize()); + long mismatch = o1.mismatch(o2); + + if (o1.byteSize() != 0 && o2.byteSize() != 0) { + int diff = mismatch == -1 + ? 0 + : o1.get(ValueLayout.JAVA_BYTE, mismatch) - o2.get(ValueLayout.JAVA_BYTE, mismatch); + return diff == 0 ? sizeDiff : diff; + } + + return sizeDiff; + } + } + +} diff --git a/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java b/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java new file mode 100644 index 000000000..3228df850 --- /dev/null +++ b/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java @@ -0,0 +1,35 @@ +package ru.vk.itmo.test.podobaalexandr; + +import ru.vk.itmo.Dao; +import ru.vk.itmo.Entry; +import ru.vk.itmo.podobaalexandr.InMemoryDaoImpl; +import ru.vk.itmo.test.DaoFactory; + +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; +import java.nio.charset.StandardCharsets; + +@SuppressWarnings("unused") +@DaoFactory +public class FactoryImpl implements DaoFactory.Factory> { + @Override + public Dao> createDao() { + return new InMemoryDaoImpl(); + } + + @Override + public String toString(MemorySegment memorySegment) { + return new String((byte[]) memorySegment.heapBase() + .orElse(memorySegment.toArray(ValueLayout.JAVA_BYTE)), StandardCharsets.UTF_8); + } + + @Override + public MemorySegment fromString(String data) { + return data == null ? null : MemorySegment.ofArray(data.getBytes(StandardCharsets.UTF_8)); + } + + @Override + public Entry fromBaseEntry(Entry baseEntry) { + return baseEntry; + } +}