-
Notifications
You must be signed in to change notification settings - Fork 48
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} |
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; | ||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ключ провалидировали, но не хватает проверки тела на null. Иначе получается можно записать tombstone, тем самым удалив запись. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь и ниже в delete не обрабатываются возможные исключения DAO. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} |
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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему конкретный класс, а не интерфейс?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Исправил.