-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Тузиков Александр, ИТМО DWS, Stage 6 #217
Changes from all commits
fe2ee53
773e724
e899083
b3f5809
a13a743
da42951
0029cbd
25a784e
87f77fb
8355b8c
5913fc7
3141895
30be026
0783e70
c8f98e2
8c91869
b49a242
149bc54
4a55642
c4bc01f
74e6f56
21ef485
6dfb117
83ca308
1bf1d24
e835f26
42cfe60
939964f
c00bbb5
ab1f9d7
76c28ee
16e52a1
7432db0
7cc056a
c3268c5
24018f5
dfbb2f4
018b200
5245bc1
48d2322
153bb7f
62277c8
3241ea6
5669c04
37af132
d7d30af
2b41fcd
820a894
6cd09ce
0b17813
8b6f88c
d0cf8a3
48a0bce
540b67c
f69d073
4ff81db
56e2a5d
f583d82
5aaa874
bca9871
dbd1e8d
7f33f75
fd54b14
9a32617
37a2fbd
ba4137a
2695558
7437f7e
1a8e41d
d74c194
4207e95
09d5e2a
7eed192
2e2cfe1
1f536c0
0fc0074
902715f
d042ada
74692bb
5cb8448
6382e8f
81bc896
6293d52
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,74 @@ | ||
package ru.vk.itmo.test.tuzikovalexandr; | ||
|
||
import one.nio.http.Response; | ||
import one.nio.net.Session; | ||
import ru.vk.itmo.test.tuzikovalexandr.dao.EntryWithTimestamp; | ||
|
||
import java.io.IOException; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Iterator; | ||
|
||
public class StreamResponse extends Response { | ||
private final Iterator<EntryWithTimestamp<MemorySegment>> entries; | ||
|
||
public StreamResponse(String resultCode, Iterator<EntryWithTimestamp<MemorySegment>> entries) { | ||
super(resultCode); | ||
this.entries = entries; | ||
} | ||
|
||
public void stream(Session session) throws IOException { | ||
session.write(Constants.HEADER, 0, Constants.HEADER.length); | ||
|
||
processWrite(session); | ||
session.scheduleClose(); | ||
} | ||
|
||
private void next(Session session, MemorySegment key, MemorySegment value) throws IOException { | ||
byte[] keyInBytes = toByteArray(key); | ||
byte[] valueInBytes = toByteArray(value); | ||
|
||
byte[] entrySize = Integer.toHexString(keyInBytes.length + valueInBytes.length + Constants.NEW_LINE.length) | ||
.getBytes(StandardCharsets.UTF_8); | ||
|
||
byte[] resultValue = new byte[keyInBytes.length + valueInBytes.length + Constants.NEW_LINE.length | ||
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. Да, по аллокациям вообще ужас получился, страшно смотреть. Буду пробовать исправлять |
||
+ Constants.CRLF.length * 2 + entrySize.length]; | ||
|
||
ByteBuffer target = ByteBuffer.wrap(resultValue); | ||
target.put(entrySize); | ||
target.put(Constants.CRLF); | ||
target.put(keyInBytes); | ||
target.put(Constants.NEW_LINE); | ||
target.put(valueInBytes); | ||
target.put(Constants.CRLF); | ||
|
||
session.write(target.array(), 0, target.array().length); | ||
} | ||
|
||
private void processWrite(Session session) throws IOException { | ||
while (entries.hasNext()) { | ||
EntryWithTimestamp<MemorySegment> entry = entries.next(); | ||
|
||
next(session, entry.key(), entry.value()); | ||
} | ||
session.write(Constants.LAST_STRING, 0, Constants.LAST_STRING.length); | ||
} | ||
|
||
private <T> byte[] toByteArray(T segment) { | ||
if (segment == null) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
if (segment instanceof MemorySegment) { | ||
return ((MemorySegment) segment).toArray(ValueLayout.JAVA_BYTE); | ||
} | ||
|
||
if (segment instanceof String) { | ||
return ((String) segment).getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
return new byte[0]; | ||
} | ||
} |
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.
если были на разборе 6го этапа, то наверное уже поняли что в этом решении вы потенциально все данные можете запихать в память, если с сетью какие-то проблемы
Тут надо обратить внимание на то что под капотом у sesion.write() и попробовать реализовать иенно асинхронную запись
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.
То есть надо было оставаться на том же алгоритме, что и у one.nio? Что мы можем не до конца сразу все записать, а вернуться к этому потом?
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.
да, надо запоминать сколько мы записали, а далее по событию о том что сеть опять готова на запись - записать остальное