Skip to content
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 1 #12

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/ru/vk/itmo/test/georgiidalbeev/NewFactory.java
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 src/main/java/ru/vk/itmo/test/georgiidalbeev/NewServer.java
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 src/main/java/ru/vk/itmo/test/georgiidalbeev/NewService.java
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);
}
}
35 changes: 35 additions & 0 deletions src/main/java/ru/vk/itmo/test/georgiidalbeev/Server.java
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 not shown.
Loading
Loading