projects
/
demos
/
kafka
/
chat
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
feat: Prepared the application for sharding
[demos/kafka/chat]
/
src
/
main
/
java
/
de
/
juplo
/
kafka
/
chat
/
backend
/
domain
/
ChatRoom.java
diff --git
a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java
b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java
index
58efa54
..
4496585
100644
(file)
--- a/
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java
+++ b/
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java
@@
-11,6
+11,8
@@
import reactor.core.publisher.Sinks;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.*;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
@Slf4j
@Slf4j
@@
-18,27
+20,36
@@
import java.util.*;
@ToString(of = { "id", "name" })
public class ChatRoom
{
@ToString(of = { "id", "name" })
public class ChatRoom
{
+ public final static Pattern VALID_USER = Pattern.compile("^[a-z0-9-]{2,}$");
@Getter
private final UUID id;
@Getter
private final String name;
@Getter
private final UUID id;
@Getter
private final String name;
+ @Getter
+ private final int shard;
private final Clock clock;
private final ChatRoomService service;
private final int bufferSize;
private Sinks.Many<Message> sink;
private final Clock clock;
private final ChatRoomService service;
private final int bufferSize;
private Sinks.Many<Message> sink;
+
public ChatRoom(
UUID id,
String name,
public ChatRoom(
UUID id,
String name,
+ int shard,
Clock clock,
ChatRoomService service,
int bufferSize)
{
this.id = id;
this.name = name;
Clock clock,
ChatRoomService service,
int bufferSize)
{
this.id = id;
this.name = name;
+ this.shard = shard;
this.clock = clock;
this.service = service;
this.bufferSize = bufferSize;
this.clock = clock;
this.service = service;
this.bufferSize = bufferSize;
+ // @RequiredArgsConstructor unfortunately not possible, because
+ // the `bufferSize` is not set, if `createSink()` is called
+ // from the variable declaration!
this.sink = createSink();
}
this.sink = createSink();
}
@@
-48,6
+59,10
@@
public class ChatRoom
String user,
String text)
{
String user,
String text)
{
+ Matcher matcher = VALID_USER.matcher(user);
+ if (!matcher.matches())
+ throw new InvalidUsernameException(user);
+
Message.MessageKey key = Message.MessageKey.of(user, id);
return service
.getMessage(key)
Message.MessageKey key = Message.MessageKey.of(user, id);
return service
.getMessage(key)
@@
-56,7
+71,7
@@
public class ChatRoom
: Mono.error(() -> new MessageMutationException(existing, text)))
.switchIfEmpty(
Mono
: Mono.error(() -> new MessageMutationException(existing, text)))
.switchIfEmpty(
Mono
- .
just(
service.persistMessage(key, LocalDateTime.now(clock), text))
+ .
fromSupplier(() ->
service.persistMessage(key, LocalDateTime.now(clock), text))
.doOnNext(m ->
{
Sinks.EmitResult result = sink.tryEmitNext(m);
.doOnNext(m ->
{
Sinks.EmitResult result = sink.tryEmitNext(m);