Skip to content

Commit

Permalink
fix for code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yulalenk committed Feb 25, 2024
1 parent 50248fe commit 1173547
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
72 changes: 39 additions & 33 deletions src/main/java/ru/vk/itmo/test/alenkovayulya/ServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
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 ru.vk.itmo.test.alenkovayulya.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;
import java.util.function.Supplier;

public class ServerImpl extends HttpServer {

private final ReferenceDao referenceDao;
private final Dao<MemorySegment, Entry<MemorySegment>> referenceDao;

public ServerImpl(ServiceConfig serviceConfig, ReferenceDao referenceDao) throws IOException {
public ServerImpl(ServiceConfig serviceConfig,
Dao<MemorySegment, Entry<MemorySegment>> referenceDao) throws IOException {
super(createServerConfig(serviceConfig));
this.referenceDao = referenceDao;
}
Expand All @@ -43,36 +44,43 @@ private static HttpServerConfig createServerConfig(ServiceConfig serviceConfig)
@Path("/v0/entity")
@RequestMethod(Request.METHOD_GET)
public Response getEntity(@Param(value = "id", required = true) String id) {
if (isEmptyId(id)) {
return new Response(Response.BAD_REQUEST, Response.EMPTY);
}
Entry<MemorySegment> value = referenceDao.get(convertBytesToMemorySegment(id.getBytes(StandardCharsets.UTF_8)));
return handleException(() -> {
if (isEmptyId(id)) {
return new Response(Response.BAD_REQUEST, Response.EMPTY);
}
Entry<MemorySegment> value = referenceDao.get(
convertBytesToMemorySegment(id.getBytes(StandardCharsets.UTF_8)));

return value == null ? new Response(Response.NOT_FOUND, Response.EMPTY)
: Response.ok(value.value().toArray(ValueLayout.JAVA_BYTE));
return value == null ? new Response(Response.NOT_FOUND, Response.EMPTY)
: Response.ok(value.value().toArray(ValueLayout.JAVA_BYTE));
});
}

@Path("/v0/entity")
@RequestMethod(Request.METHOD_PUT)
public Response putEntity(@Param(value = "id", required = true) String id, Request request) {
if (isEmptyId(id)) {
return new Response(Response.BAD_REQUEST, Response.EMPTY);
}
referenceDao.upsert(new BaseEntry<>(
convertBytesToMemorySegment(id.getBytes(StandardCharsets.UTF_8)),
convertBytesToMemorySegment(request.getBody())));
return new Response(Response.CREATED, Response.EMPTY);
return handleException(() -> {
if (isEmptyId(id)) {
return new Response(Response.BAD_REQUEST, Response.EMPTY);
}
referenceDao.upsert(new BaseEntry<>(
convertBytesToMemorySegment(id.getBytes(StandardCharsets.UTF_8)),
convertBytesToMemorySegment(request.getBody())));
return new Response(Response.CREATED, Response.EMPTY);
});
}

@Path("/v0/entity")
@RequestMethod(Request.METHOD_DELETE)
public Response deleteEntity(@Param(value = "id", required = true) String id) {
if (isEmptyId(id)) {
return new Response(Response.BAD_REQUEST, Response.EMPTY);
}
referenceDao.upsert(new BaseEntry<>(
convertBytesToMemorySegment(id.getBytes(StandardCharsets.UTF_8)), null));
return new Response(Response.ACCEPTED, Response.EMPTY);
return handleException(() -> {
if (isEmptyId(id)) {
return new Response(Response.BAD_REQUEST, Response.EMPTY);
}
referenceDao.upsert(new BaseEntry<>(
convertBytesToMemorySegment(id.getBytes(StandardCharsets.UTF_8)), null));
return new Response(Response.ACCEPTED, Response.EMPTY);
});
}

@Override
Expand All @@ -83,21 +91,19 @@ public void handleDefault(Request request, HttpSession session) throws IOExcepti
}
}

@Override
public synchronized void stop() {
super.stop();
try {
referenceDao.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private boolean isEmptyId(String id) {
return id.isEmpty() && id.isBlank();
}

private MemorySegment convertBytesToMemorySegment(byte[] byteArray) {
return MemorySegment.ofArray(byteArray);
}

private Response handleException(Supplier<Response> runnable) {
try {
return runnable.get();
} catch (Exception exception) {
return new Response(Response.INTERNAL_ERROR, Response.EMPTY);
}
}
}
9 changes: 6 additions & 3 deletions src/main/java/ru/vk/itmo/test/alenkovayulya/ServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
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.ServiceFactory;
import ru.vk.itmo.test.alenkovayulya.dao.ReferenceDao;

import java.io.IOException;
import java.lang.foreign.MemorySegment;
import java.util.concurrent.CompletableFuture;

public class ServiceImpl implements Service {

private ReferenceDao referenceDao;
private Dao<MemorySegment, Entry<MemorySegment>> referenceDao;
private ServerImpl server;
private final ServiceConfig config;

Expand All @@ -21,15 +24,15 @@ public ServiceImpl(ServiceConfig config) {
}

@Override
public CompletableFuture<Void> start() throws IOException {
public synchronized CompletableFuture<Void> start() throws IOException {
referenceDao = new ReferenceDao(new Config(config.workingDir(), 1024 * 1024 * 1024));
server = new ServerImpl(config, referenceDao);
server.start();
return CompletableFuture.completedFuture(null);
}

@Override
public CompletableFuture<Void> stop() throws IOException {
public synchronized CompletableFuture<Void> stop() throws IOException {
server.stop();
referenceDao.close();
return CompletableFuture.completedFuture(null);
Expand Down

0 comments on commit 1173547

Please sign in to comment.