-
Notifications
You must be signed in to change notification settings - Fork 78
[IGNORE] #39
base: main
Are you sure you want to change the base?
[IGNORE] #39
Changes from all commits
3a175a5
872d426
165880d
a781ad9
d1b4400
4336bc7
9067f3f
6f9db69
8d6fe99
04e4b03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MemorySegment, Entry<MemorySegment>> { | ||
ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> memorySegmentEntryMap | ||
= new ConcurrentSkipListMap<>(new MyComparator()); | ||
|
||
@Override | ||
public Iterator<Entry<MemorySegment>> get(MemorySegment from, MemorySegment to) { | ||
ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лишняя аллокация в этом методе. ищи где There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А теперь очень сложные ифы (условно). Можно сделать в начале !=null && !=null. Но это вкусовщина, можешь забить. Если исправил замечание - надо на коммент ответить в чем заключалась ошибка (чтобы я понял, правильно ли ты меня понял) и объяснить исправление. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так как у нас два отдельных if, в которых создаётся новая map, то может быть случай, когда у нас выполняются два условия и будут две аллокации, а теперь только одна. |
||
} | ||
|
||
@Override | ||
public void upsert(Entry<MemorySegment> entry) { | ||
memorySegmentEntryMap.put(entry.key(), entry); | ||
} | ||
|
||
@Override | ||
public Entry<MemorySegment> get(MemorySegment key) { | ||
return memorySegmentEntryMap.get(key); | ||
} | ||
|
||
private static class MyComparator implements Comparator<MemorySegment> { | ||
|
||
@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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тоже сложный иф тоже вкусовщина |
||
return diff == 0 ? sizeDiff : diff; | ||
} | ||
|
||
return sizeDiff; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MemorySegment, Entry<MemorySegment>> { | ||
@Override | ||
public Dao<MemorySegment, Entry<MemorySegment>> 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<MemorySegment> fromBaseEntry(Entry<MemorySegment> baseEntry) { | ||
return baseEntry; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
почему ты хранишь в начале ключ, а потом ентри, где тоже содержится ключ