|
| 1 | +package ru.vk.itmo.test.chebotinalexandr; |
| 2 | + |
| 3 | +import one.nio.http.HttpServer; |
| 4 | +import one.nio.http.HttpServerConfig; |
| 5 | +import one.nio.http.HttpSession; |
| 6 | +import one.nio.http.Param; |
| 7 | +import one.nio.http.Path; |
| 8 | +import one.nio.http.Request; |
| 9 | +import one.nio.http.RequestMethod; |
| 10 | +import one.nio.http.Response; |
| 11 | +import one.nio.server.AcceptorConfig; |
| 12 | +import ru.vk.itmo.ServiceConfig; |
| 13 | +import ru.vk.itmo.dao.BaseEntry; |
| 14 | +import ru.vk.itmo.dao.Dao; |
| 15 | +import ru.vk.itmo.dao.Entry; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.lang.foreign.MemorySegment; |
| 19 | +import java.lang.foreign.ValueLayout; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | + |
| 22 | +import static one.nio.http.Request.METHOD_DELETE; |
| 23 | +import static one.nio.http.Request.METHOD_GET; |
| 24 | +import static one.nio.http.Request.METHOD_PUT; |
| 25 | + |
| 26 | +public class StorageServer extends HttpServer { |
| 27 | + private static final String PATH = "/v0/entity"; |
| 28 | + private final Dao<MemorySegment, Entry<MemorySegment>> dao; |
| 29 | + |
| 30 | + public StorageServer(ServiceConfig config, Dao<MemorySegment, Entry<MemorySegment>> dao) throws IOException { |
| 31 | + super(createConfig(config)); |
| 32 | + this.dao = dao; |
| 33 | + } |
| 34 | + |
| 35 | + private static HttpServerConfig createConfig(ServiceConfig config) { |
| 36 | + HttpServerConfig httpServerConfig = new HttpServerConfig(); |
| 37 | + AcceptorConfig acceptorConfig = new AcceptorConfig(); |
| 38 | + acceptorConfig.reusePort = true; |
| 39 | + acceptorConfig.port = config.selfPort(); |
| 40 | + |
| 41 | + httpServerConfig.acceptors = new AcceptorConfig[] {acceptorConfig}; |
| 42 | + httpServerConfig.closeSessions = true; |
| 43 | + return httpServerConfig; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void handleDefault(Request request, HttpSession session) throws IOException { |
| 48 | + Response response = new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 49 | + session.sendResponse(response); |
| 50 | + } |
| 51 | + |
| 52 | + @Path("/hello") |
| 53 | + public Response hello() { |
| 54 | + return Response.ok("Hello, cruel world!".getBytes(StandardCharsets.UTF_8)); |
| 55 | + } |
| 56 | + |
| 57 | + @Path(PATH) |
| 58 | + @RequestMethod(METHOD_GET) |
| 59 | + public Response get(@Param("id") String id) { |
| 60 | + if (id == null || id.isEmpty()) { |
| 61 | + return new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 62 | + } |
| 63 | + |
| 64 | + Entry<MemorySegment> entry = dao.get(fromString(id)); |
| 65 | + if (entry == null) { |
| 66 | + return new Response(Response.NOT_FOUND, Response.EMPTY); |
| 67 | + } else { |
| 68 | + return Response.ok(toBytes(entry.value())); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Path(PATH) |
| 73 | + @RequestMethod(METHOD_PUT) |
| 74 | + public Response upsert(@Param("id") String id, Request request) { |
| 75 | + if (id == null || id.isEmpty()) { |
| 76 | + return new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 77 | + } |
| 78 | + |
| 79 | + Entry<MemorySegment> entry = new BaseEntry<>( |
| 80 | + fromString(id), |
| 81 | + fromBytes(request.getBody()) |
| 82 | + ); |
| 83 | + dao.upsert(entry); |
| 84 | + |
| 85 | + return new Response(Response.CREATED, Response.EMPTY); |
| 86 | + } |
| 87 | + |
| 88 | + @Path(PATH) |
| 89 | + @RequestMethod(METHOD_DELETE) |
| 90 | + public Response delete(@Param("id") String id) { |
| 91 | + if (id == null || id.isEmpty()) { |
| 92 | + return new Response(Response.BAD_REQUEST, Response.EMPTY); |
| 93 | + } |
| 94 | + |
| 95 | + Entry<MemorySegment> entry = new BaseEntry<>( |
| 96 | + fromString(id), |
| 97 | + null |
| 98 | + ); |
| 99 | + dao.upsert(entry); |
| 100 | + |
| 101 | + return new Response(Response.ACCEPTED, Response.EMPTY); |
| 102 | + } |
| 103 | + |
| 104 | + @Path(PATH) |
| 105 | + public Response post(@Param("id") String id) { |
| 106 | + //Post (in-place updating in LSM) is not supported |
| 107 | + return new Response(Response.METHOD_NOT_ALLOWED, Response.EMPTY); |
| 108 | + } |
| 109 | + |
| 110 | + private static MemorySegment fromString(String data) { |
| 111 | + return MemorySegment.ofArray(data.getBytes(StandardCharsets.UTF_8)); |
| 112 | + } |
| 113 | + |
| 114 | + private static MemorySegment fromBytes(byte[] bytes) { |
| 115 | + return MemorySegment.ofArray(bytes); |
| 116 | + } |
| 117 | + |
| 118 | + private static byte[] toBytes(MemorySegment segment) { |
| 119 | + return segment.toArray(ValueLayout.JAVA_BYTE); |
| 120 | + } |
| 121 | +} |
| 122 | + |
0 commit comments