-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'refs/remotes/upstream/main' into stage-5
# Conflicts: # src/main/java/ru/vk/itmo/test/shishiginstepan/lua_scripts/random_rw_requests.lua # src/main/java/ru/vk/itmo/test/shishiginstepan/server/DistributedDao.java # src/main/java/ru/vk/itmo/test/shishiginstepan/server/RemoteDaoNode.java # src/main/java/ru/vk/itmo/test/shishiginstepan/server/Server.java # src/main/java/ru/vk/itmo/test/shishiginstepan/service/DatabaseService.java
- Loading branch information
Showing
389 changed files
with
321,352 additions
and
3,121 deletions.
There are no files selected for viewing
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
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
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
78 changes: 78 additions & 0 deletions
78
src/main/java/ru/vk/itmo/test/asvistukhin/RequestHandler.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,78 @@ | ||
package ru.vk.itmo.test.asvistukhin; | ||
|
||
import one.nio.http.Param; | ||
import one.nio.http.Request; | ||
import one.nio.http.Response; | ||
import ru.vk.itmo.test.asvistukhin.dao.Dao; | ||
import ru.vk.itmo.test.asvistukhin.dao.TimestampEntry; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class RequestHandler { | ||
private final Dao<MemorySegment, TimestampEntry<MemorySegment>> dao; | ||
|
||
public RequestHandler(Dao<MemorySegment, TimestampEntry<MemorySegment>> dao) { | ||
this.dao = dao; | ||
} | ||
|
||
public Response handle(Request request) { | ||
String id = request.getParameter("id="); | ||
return switch (request.getMethod()) { | ||
case Request.METHOD_GET -> get(id); | ||
case Request.METHOD_PUT -> put(id, request); | ||
case Request.METHOD_DELETE -> delete(id); | ||
default -> new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
}; | ||
} | ||
|
||
public Response get(@Param(value = "id", required = true) String id) { | ||
if (RequestWrapper.isEmptyParam(id)) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
TimestampEntry<MemorySegment> entry = dao.get(MemorySegment.ofArray(id.getBytes(StandardCharsets.UTF_8))); | ||
|
||
Response response; | ||
long timestamp; | ||
if (entry == null || entry.value() == null) { | ||
timestamp = (entry != null ? entry.timestamp() : 0); | ||
response = new Response(Response.NOT_FOUND, Response.EMPTY); | ||
response.addHeader(RequestWrapper.TIMESTAMP_STRING_HEADER + timestamp); | ||
} else { | ||
timestamp = entry.timestamp(); | ||
response = Response.ok(entry.value().toArray(ValueLayout.JAVA_BYTE)); | ||
response.addHeader(RequestWrapper.TIMESTAMP_STRING_HEADER + timestamp); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
public Response put(@Param(value = "id", required = true) String id, Request request) { | ||
if (RequestWrapper.isEmptyParam(id) || RequestWrapper.isEmptyRequest(request)) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
MemorySegment key = MemorySegment.ofArray(id.getBytes(StandardCharsets.UTF_8)); | ||
MemorySegment value = MemorySegment.ofArray(request.getBody()); | ||
dao.upsert(new TimestampEntry<>(key, value, System.currentTimeMillis())); | ||
|
||
return new Response(Response.CREATED, Response.EMPTY); | ||
} | ||
|
||
public Response delete(@Param(value = "id", required = true) String id) { | ||
if (RequestWrapper.isEmptyParam(id)) { | ||
return new Response(Response.BAD_REQUEST, Response.EMPTY); | ||
} | ||
|
||
dao.upsert( | ||
new TimestampEntry<>( | ||
MemorySegment.ofArray(id.getBytes(StandardCharsets.UTF_8)), | ||
null, | ||
System.currentTimeMillis()) | ||
); | ||
|
||
return new Response(Response.ACCEPTED, Response.EMPTY); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/ru/vk/itmo/test/asvistukhin/RequestWrapper.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,44 @@ | ||
package ru.vk.itmo.test.asvistukhin; | ||
|
||
import one.nio.http.Request; | ||
|
||
public class RequestWrapper { | ||
private static final String ID_PARAM = "id="; | ||
private static final String FROM_PARAM = "from="; | ||
private static final String ACK_PARAM = "ack="; | ||
public static final String SELF_HEADER = "X-Self"; | ||
public static final String NIO_TIMESTAMP_HEADER = "x-timestamp"; | ||
public static final String TIMESTAMP_STRING_HEADER = "X-Timestamp:"; | ||
public static final String NIO_TIMESTAMP_STRING_HEADER = "x-timestamp:"; | ||
|
||
public final String id; | ||
public final int from; | ||
public final int ack; | ||
|
||
public RequestWrapper(Request request, int clusterSize) throws IllegalArgumentException { | ||
String idString = request.getParameter(ID_PARAM); | ||
String fromString = request.getParameter(FROM_PARAM); | ||
String ackString = request.getParameter(ACK_PARAM); | ||
if (isEmptyParam(idString)) throw new IllegalArgumentException(); | ||
|
||
id = idString; | ||
try { | ||
from = isEmptyParam(fromString) ? clusterSize : Integer.parseInt(fromString); | ||
ack = isEmptyParam(ackString) ? (from + 1) / 2 : Integer.parseInt(ackString); | ||
} catch (NumberFormatException ex) { | ||
throw new IllegalArgumentException(ex); | ||
} | ||
|
||
if (from <= 0 || ack <= 0 || from < ack || clusterSize < from) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public static boolean isEmptyParam(String param) { | ||
return param == null || param.isEmpty(); | ||
} | ||
|
||
public static boolean isEmptyRequest(Request request) { | ||
return request.getBody() == null; | ||
} | ||
} |
Oops, something went wrong.