forked from polis-vk/2024-highload-dht
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Далбеев Георгий / ИТМО DWS / Stage 1 (polis-vk#12)
* stage1 * stage1 * stage1 * report stage1 * fix issues --------- Co-authored-by: georgiidalbeev <[email protected]> Co-authored-by: Roman Mushchinskii <[email protected]>
- Loading branch information
Showing
46 changed files
with
5,773 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/ru/vk/itmo/test/georgiidalbeev/NewFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ru.vk.itmo.test.georgiidalbeev; | ||
|
||
import ru.vk.itmo.Service; | ||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.test.ServiceFactory; | ||
|
||
@ServiceFactory(stage = 1) | ||
public class NewFactory implements ServiceFactory.Factory { | ||
|
||
@Override | ||
public Service create(ServiceConfig config) { | ||
return new NewService(config); | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/main/java/ru/vk/itmo/test/georgiidalbeev/NewServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package ru.vk.itmo.test.georgiidalbeev; | ||
|
||
import one.nio.http.HttpServer; | ||
import one.nio.http.HttpServerConfig; | ||
import one.nio.http.HttpSession; | ||
import one.nio.http.Param; | ||
import one.nio.http.Path; | ||
import one.nio.http.Request; | ||
import one.nio.http.RequestMethod; | ||
import one.nio.http.Response; | ||
import one.nio.server.AcceptorConfig; | ||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.dao.BaseEntry; | ||
import ru.vk.itmo.dao.Dao; | ||
import ru.vk.itmo.dao.Entry; | ||
|
||
import java.io.IOException; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class NewServer extends HttpServer { | ||
|
||
private final Dao<MemorySegment, Entry<MemorySegment>> dao; | ||
private static final String PATH = "/v0/entity"; | ||
|
||
public NewServer(ServiceConfig config, Dao<MemorySegment, Entry<MemorySegment>> dao) throws IOException { | ||
super(configureServer(config)); | ||
this.dao = dao; | ||
} | ||
|
||
private static HttpServerConfig configureServer(ServiceConfig serviceConfig) { | ||
HttpServerConfig serverConfig = new HttpServerConfig(); | ||
AcceptorConfig acceptorConfig = new AcceptorConfig(); | ||
acceptorConfig.port = serviceConfig.selfPort(); | ||
acceptorConfig.reusePort = true; | ||
|
||
serverConfig.acceptors = new AcceptorConfig[] {acceptorConfig}; | ||
serverConfig.closeSessions = true; | ||
return serverConfig; | ||
} | ||
|
||
@Path(PATH) | ||
@RequestMethod(Request.METHOD_PUT) | ||
public Response putEntity(@Param(value = "id", required = true) String id, Request request) { | ||
MemorySegment key = validateId(id); | ||
if (key == null) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
try { | ||
Entry<MemorySegment> entry = new BaseEntry<>( | ||
key, | ||
MemorySegment.ofArray(request.getBody()) | ||
); | ||
|
||
dao.upsert(entry); | ||
} catch (Exception e) { | ||
return new Response(Response.INTERNAL_ERROR, Response.EMPTY); | ||
} | ||
|
||
return new Response(Response.CREATED, Response.EMPTY); | ||
} | ||
|
||
@Path(PATH) | ||
@RequestMethod(Request.METHOD_GET) | ||
public Response getEntity(@Param(value = "id", required = true) String id) { | ||
MemorySegment key = validateId(id); | ||
if (key == null) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
try { | ||
Entry<MemorySegment> entry = dao.get(key); | ||
|
||
if (entry == null) { | ||
return new Response(Response.NOT_FOUND, Response.EMPTY); | ||
} | ||
|
||
return Response.ok(entry.value().toArray(ValueLayout.JAVA_BYTE)); | ||
} catch (Exception e) { | ||
return new Response(Response.INTERNAL_ERROR, Response.EMPTY); | ||
} | ||
} | ||
|
||
@Path(PATH) | ||
@RequestMethod(Request.METHOD_DELETE) | ||
public Response deleteEntity(@Param(value = "id", required = true) String id) { | ||
MemorySegment key = validateId(id); | ||
if (key == null) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
try { | ||
Entry<MemorySegment> entry = new BaseEntry<>( | ||
key, | ||
null | ||
); | ||
|
||
dao.upsert(entry); | ||
} catch (Exception e) { | ||
return new Response(Response.INTERNAL_ERROR, Response.EMPTY); | ||
} | ||
|
||
return new Response(Response.ACCEPTED, Response.EMPTY); | ||
} | ||
|
||
@Path(PATH) | ||
public Response otherMethods() { | ||
return new Response(Response.METHOD_NOT_ALLOWED, Response.EMPTY); | ||
} | ||
|
||
@Override | ||
public void handleDefault(Request request, HttpSession session) throws IOException { | ||
Response response = new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
session.sendResponse(response); | ||
} | ||
|
||
private MemorySegment validateId(String id) { | ||
return (id == null || id.isEmpty()) ? null : MemorySegment.ofArray(id.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/ru/vk/itmo/test/georgiidalbeev/NewService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ru.vk.itmo.test.georgiidalbeev; | ||
|
||
import ru.vk.itmo.Service; | ||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.dao.Config; | ||
import ru.vk.itmo.dao.Dao; | ||
import ru.vk.itmo.dao.Entry; | ||
import ru.vk.itmo.test.reference.dao.ReferenceDao; | ||
|
||
import java.io.IOException; | ||
import java.lang.foreign.MemorySegment; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class NewService implements Service { | ||
|
||
private NewServer server; | ||
private final ServiceConfig config; | ||
private Dao<MemorySegment, Entry<MemorySegment>> dao; | ||
private static final long FLUSH_THRESHOLD = 5242880; | ||
|
||
public NewService(ServiceConfig config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> start() throws IOException { | ||
dao = new ReferenceDao(new Config(config.workingDir(), FLUSH_THRESHOLD)); | ||
server = new NewServer(config, dao); | ||
server.start(); | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> stop() throws IOException { | ||
server.stop(); | ||
dao.close(); | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ru.vk.itmo.test.georgiidalbeev; | ||
|
||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.test.smirnovandrew.MyService; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public final class Server { | ||
private static final String DIR = "tmp/dao"; | ||
|
||
private Server() { | ||
|
||
} | ||
|
||
@SuppressWarnings("FutureReturnValueIgnored") | ||
public static void main(String[] args) throws IOException { | ||
Path path = Path.of(DIR); | ||
if (!Files.exists(path)) { | ||
Files.createDirectories(path); | ||
} | ||
MyService service = new MyService( | ||
new ServiceConfig( | ||
8080, | ||
"http://localhost:8080", | ||
List.of("http://localhost:8080"), | ||
path | ||
) | ||
); | ||
|
||
service.start(); | ||
} | ||
} |
Binary file added
BIN
+37.1 KB
src/main/java/ru/vk/itmo/test/georgiidalbeev/async_profiler_result/delete.jfr
Binary file not shown.
Oops, something went wrong.