From 3a175a5eb4179d89898376b232c20eabba7dab73 Mon Sep 17 00:00:00 2001 From: Podoba Alexander Date: Tue, 26 Sep 2023 14:14:03 +0300 Subject: [PATCH 1/6] - Dao and Factory implementing. --- .../itmo/podobaalexandr/InMemoryDaoImpl.java | 45 +++++++++++++++++++ .../itmo/test/podobaalexandr/FactoryImpl.java | 32 +++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java create mode 100644 src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java 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..d4de5a709 --- /dev/null +++ b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java @@ -0,0 +1,45 @@ +package ru.vk.itmo.podobaalexandr; + +import ru.vk.itmo.Dao; +import ru.vk.itmo.Entry; + +import java.lang.foreign.MemorySegment; +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) { + innerMap = innerMap.tailMap(from); + } + if (to != null) { + innerMap = innerMap.headMap(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) { + return o1.asByteBuffer().compareTo(o2.asByteBuffer()); + } + } + +} 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..82297ad1e --- /dev/null +++ b/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java @@ -0,0 +1,32 @@ +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.nio.charset.StandardCharsets; + +@DaoFactory +public class FactoryImpl implements DaoFactory.Factory> { + @Override + public Dao> createDao() { + return new InMemoryDaoImpl(); + } + + @Override + public String toString(MemorySegment memorySegment) { + return new String(memorySegment.asByteBuffer().array(), 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; + } +} From 872d4263154453dbdaf45e120d005ff73ff8fa2e Mon Sep 17 00:00:00 2001 From: Podoba Alexander Date: Tue, 26 Sep 2023 14:31:41 +0300 Subject: [PATCH 2/6] - Dao and Factory implementing. --- src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java index d4de5a709..ed5968844 100644 --- a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java +++ b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java @@ -10,7 +10,8 @@ import java.util.concurrent.ConcurrentSkipListMap; public class InMemoryDaoImpl implements Dao> { - ConcurrentNavigableMap> memorySegmentEntryMap = new ConcurrentSkipListMap<>(new MyComparator()); + ConcurrentNavigableMap> memorySegmentEntryMap + = new ConcurrentSkipListMap<>(new MyComparator()); @Override public Iterator> get(MemorySegment from, MemorySegment to) { From 165880d0dcb8d5e65b197f84d9151593c58edb0a Mon Sep 17 00:00:00 2001 From: Podoba Alexander Date: Tue, 26 Sep 2023 19:04:57 +0300 Subject: [PATCH 3/6] - Fix get(from, to) using if-else statements - Fix Comparator using mismatch and Long.compare() - Fix toString() using heapBase --- .../itmo/podobaalexandr/InMemoryDaoImpl.java | 19 +++++++++++++++---- .../itmo/test/podobaalexandr/FactoryImpl.java | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java index ed5968844..23ba17867 100644 --- a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java +++ b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java @@ -4,6 +4,7 @@ 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; @@ -16,12 +17,15 @@ public class InMemoryDaoImpl implements Dao> @Override public Iterator> get(MemorySegment from, MemorySegment to) { ConcurrentNavigableMap> innerMap = memorySegmentEntryMap; - if (from != null) { + + if (from != null && to == null) { innerMap = innerMap.tailMap(from); - } - if (to != null) { + } else if (from == null && to != null) { innerMap = innerMap.headMap(to); + } else if (from != null) { + innerMap = innerMap.subMap(from, to); } + return innerMap.values().iterator(); } @@ -39,7 +43,14 @@ private static class MyComparator implements Comparator { @Override public int compare(MemorySegment o1, MemorySegment o2) { - return o1.asByteBuffer().compareTo(o2.asByteBuffer()); + + if (o1.byteSize() == o2.byteSize()) { + long mismatch = o1.mismatch(o2); + return mismatch == -1 ? + 0 : o1.get(ValueLayout.JAVA_BYTE, mismatch) - o2.get(ValueLayout.JAVA_BYTE, mismatch); + } + + return Long.compare(o1.byteSize(), o2.byteSize()); } } diff --git a/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java b/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java index 82297ad1e..a6bd9ce83 100644 --- a/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java +++ b/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java @@ -17,7 +17,7 @@ public Dao> createDao() { @Override public String toString(MemorySegment memorySegment) { - return new String(memorySegment.asByteBuffer().array(), StandardCharsets.UTF_8); + return new String((byte[]) memorySegment.heapBase().orElseThrow(), StandardCharsets.UTF_8); } @Override From d1b44005783824edbaff8cb5ac21d3f6a5e3d0b2 Mon Sep 17 00:00:00 2001 From: Podoba Alexander Date: Wed, 27 Sep 2023 15:30:27 +0300 Subject: [PATCH 4/6] - Fix ordering --- .../ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java index 23ba17867..81054a1f1 100644 --- a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java +++ b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java @@ -44,13 +44,16 @@ private static class MyComparator implements Comparator { @Override public int compare(MemorySegment o1, MemorySegment o2) { - if (o1.byteSize() == o2.byteSize()) { + if (o1.byteSize() != 0 && o2.byteSize() != 0) { long mismatch = o1.mismatch(o2); - return mismatch == -1 ? - 0 : o1.get(ValueLayout.JAVA_BYTE, mismatch) - o2.get(ValueLayout.JAVA_BYTE, mismatch); + int diff = mismatch == -1 + ? 0 + : o1.get(ValueLayout.JAVA_BYTE, mismatch) - o2.get(ValueLayout.JAVA_BYTE, mismatch); + return diff == 0 ? Long.compare(o1.byteSize(), o2.byteSize()) : diff; + } else { + return Long.compare(o1.byteSize(), o2.byteSize()); } - return Long.compare(o1.byteSize(), o2.byteSize()); } } From 4336bc7ce8304c62c66fa73278209587c3c512f4 Mon Sep 17 00:00:00 2001 From: Podoba Alexander Date: Wed, 27 Sep 2023 15:47:21 +0300 Subject: [PATCH 5/6] - CodeClimate fix --- .../java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java index 81054a1f1..506096ee1 100644 --- a/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java +++ b/src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java @@ -44,16 +44,17 @@ 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) { - long mismatch = o1.mismatch(o2); int diff = mismatch == -1 ? 0 : o1.get(ValueLayout.JAVA_BYTE, mismatch) - o2.get(ValueLayout.JAVA_BYTE, mismatch); - return diff == 0 ? Long.compare(o1.byteSize(), o2.byteSize()) : diff; - } else { - return Long.compare(o1.byteSize(), o2.byteSize()); + return diff == 0 ? sizeDiff : diff; } + return sizeDiff; } } From 9067f3fba30918296e14761912f56383439c9972 Mon Sep 17 00:00:00 2001 From: Podoba Alexander Date: Wed, 27 Sep 2023 19:46:37 +0300 Subject: [PATCH 6/6] - Fixed orElseThrow in toString, adding orElse for another types of MemorySegment --- .../java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java b/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java index a6bd9ce83..3228df850 100644 --- a/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java +++ b/src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java @@ -6,8 +6,10 @@ 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 @@ -17,7 +19,8 @@ public Dao> createDao() { @Override public String toString(MemorySegment memorySegment) { - return new String((byte[]) memorySegment.heapBase().orElseThrow(), StandardCharsets.UTF_8); + return new String((byte[]) memorySegment.heapBase() + .orElse(memorySegment.toArray(ValueLayout.JAVA_BYTE)), StandardCharsets.UTF_8); } @Override