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 1 commit
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/Factory.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 Factory implements ServiceFactory.Factory {

@Override
public Service create(ServiceConfig config) {
return new NewService(config);
}
}
121 changes: 121 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,121 @@
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.Config;
import ru.vk.itmo.dao.Entry;
import ru.vk.itmo.test.georgiidalbeev.dao.ReferenceDao;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.nio.charset.StandardCharsets;


public class NewServer extends HttpServer {

private final ReferenceDao dao;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему конкретный класс, а не интерфейс?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправил.

private static final long FIVE_MEGA_BYTES = 5242880;

public NewServer(ServiceConfig config) throws IOException {
super(configureServer(config));
dao = new ReferenceDao(new Config(config.workingDir(), FIVE_MEGA_BYTES));
}

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("/v0/entity")
@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);
}

Entry<MemorySegment> entry = new BaseEntry<>(
key,
MemorySegment.ofArray(request.getBody())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ключ провалидировали, но не хватает проверки тела на null. Иначе получается можно записать tombstone, тем самым удалив запись.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправил.

);

dao.upsert(entry);

return new Response(Response.CREATED, Response.EMPTY);
}

@Path("/v0/entity")
@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);
}

Entry<MemorySegment> entry = dao.get(key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь и ниже в delete не обрабатываются возможные исключения DAO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправил.


if (entry == null) {
return new Response(Response.NOT_FOUND, Response.EMPTY);
}

return Response.ok(entry.value().toArray(ValueLayout.JAVA_BYTE));
}

@Path("/v0/entity")
@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);
}

Entry<MemorySegment> entry = new BaseEntry<>(
key,
null
);

dao.upsert(entry);

return new Response(Response.ACCEPTED, Response.EMPTY);
}

@Path("/v0/entity")
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));
}

public void terminate() {
try {
dao.close();
} catch (IOException e) {
throw new UncheckedIOException("Error closing DAO: ", e);
}
}
}
31 changes: 31 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,31 @@
package ru.vk.itmo.test.georgiidalbeev;

import ru.vk.itmo.ServiceConfig;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

public class NewService implements ru.vk.itmo.Service {

private NewServer server;

private final ServiceConfig config;

public NewService(ServiceConfig config) {
this.config = config;
}

@Override
public CompletableFuture<Void> start() throws IOException {
server = new NewServer(config);
server.start();
return CompletableFuture.completedFuture(null);
}

@Override
public CompletableFuture<Void> stop() {
server.stop();
server.terminate();
return CompletableFuture.completedFuture(null);
}
}
20 changes: 20 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,20 @@
package ru.vk.itmo.test.georgiidalbeev;

import ru.vk.itmo.ServiceConfig;

import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

public class Server {

public static void main(String[] args) throws IOException {
NewServer server = new NewServer(new ServiceConfig(
8080,
"http://localhost",
List.of("http://localhost"),
Files.createTempDirectory("."))
);
server.start();
}
}
Binary file not shown.
Loading
Loading