Skip to content

Commit

Permalink
Implement primitive builders.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Nov 6, 2017
1 parent 31fe28c commit 48a79c8
Show file tree
Hide file tree
Showing 35 changed files with 2,332 additions and 1,494 deletions.
250 changes: 126 additions & 124 deletions core/src/main/java/io/atomix/cluster/messaging/impl/ClusterMessage.java
Expand Up @@ -30,130 +30,132 @@
*/ */
public class ClusterMessage { public class ClusterMessage {


private final NodeId sender; private final NodeId sender;
private final MessageSubject subject; private final MessageSubject subject;
private final byte[] payload; private final byte[] payload;
private transient byte[] response; private transient byte[] response;


/** /**
* Creates a cluster message. * Creates a cluster message.
* *
* @param sender message sender * @param sender message sender
* @param subject message subject * @param subject message subject
* @param payload message payload * @param payload message payload
*/ */
public ClusterMessage(NodeId sender, MessageSubject subject, byte[] payload) { public ClusterMessage(NodeId sender, MessageSubject subject, byte[] payload) {
this.sender = sender; this.sender = sender;
this.subject = subject; this.subject = subject;
this.payload = payload; this.payload = payload;
}

/**
* Returns the id of the controller sending this message.
*
* @return message sender id.
*/
public NodeId sender() {
return sender;
}

/**
* Returns the message subject indicator.
*
* @return message subject
*/
public MessageSubject subject() {
return subject;
}

/**
* Returns the message payload.
*
* @return message payload.
*/
public byte[] payload() {
return payload;
}

/**
* Records the response to be sent to the sender.
*
* @param data response payload
*/
public void respond(byte[] data) {
response = data;
}

/**
* Returns the response to be sent to the sender.
*
* @return response bytes
*/
public byte[] response() {
return response;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("sender", sender)
.add("subject", subject)
.add("payload", ArraySizeHashPrinter.of(payload))
.toString();
}

@Override
public boolean equals(Object o) {
if (!(o instanceof ClusterMessage)) {
return false;
} }


/** ClusterMessage that = (ClusterMessage) o;
* Returns the id of the controller sending this message.
* return Objects.equals(this.sender, that.sender) &&
* @return message sender id. Objects.equals(this.subject, that.subject) &&
*/ Arrays.equals(this.payload, that.payload);
public NodeId sender() { }
return sender;
} /**

* Serializes this instance.
/** *
* Returns the message subject indicator. * @return bytes
* */
* @return message subject public byte[] getBytes() {
*/ byte[] senderBytes = sender.toString().getBytes(Charsets.UTF_8);
public MessageSubject subject() { byte[] subjectBytes = subject.name().getBytes(Charsets.UTF_8);
return subject; int capacity = 12 + senderBytes.length + subjectBytes.length + payload.length;
} ByteBuffer buffer = ByteBuffer.allocate(capacity);

buffer.putInt(senderBytes.length);
/** buffer.put(senderBytes);
* Returns the message payload. buffer.putInt(subjectBytes.length);
* buffer.put(subjectBytes);
* @return message payload. buffer.putInt(payload.length);
*/ buffer.put(payload);
public byte[] payload() { return buffer.array();
return payload; }
}

/**
/** * Decodes a new ClusterMessage from raw bytes.
* Records the response to be sent to the sender. *
* * @param bytes raw bytes
* @param data response payload * @return cluster message
*/ */
public void respond(byte[] data) { public static ClusterMessage fromBytes(byte[] bytes) {
response = data; ByteBuffer buffer = ByteBuffer.wrap(bytes);
} byte[] senderBytes = new byte[buffer.getInt()];

buffer.get(senderBytes);
/** byte[] subjectBytes = new byte[buffer.getInt()];
* Returns the response to be sent to the sender. buffer.get(subjectBytes);
* byte[] payloadBytes = new byte[buffer.getInt()];
* @return response bytes buffer.get(payloadBytes);
*/
public byte[] response() { return new ClusterMessage(new NodeId(new String(senderBytes, Charsets.UTF_8)),
return response; new MessageSubject(new String(subjectBytes, Charsets.UTF_8)),
} payloadBytes);

}
@Override
public String toString() { @Override
return MoreObjects.toStringHelper(getClass()) public int hashCode() {
.add("sender", sender) return Objects.hash(sender, subject, payload);
.add("subject", subject) }
.add("payload", ArraySizeHashPrinter.of(payload))
.toString();
}

@Override
public boolean equals(Object o) {
if (!(o instanceof ClusterMessage)) {
return false;
}

ClusterMessage that = (ClusterMessage) o;

return Objects.equals(this.sender, that.sender) &&
Objects.equals(this.subject, that.subject) &&
Arrays.equals(this.payload, that.payload);
}

/**
* Serializes this instance.
* @return bytes
*/
public byte[] getBytes() {
byte[] senderBytes = sender.toString().getBytes(Charsets.UTF_8);
byte[] subjectBytes = subject.name().getBytes(Charsets.UTF_8);
int capacity = 12 + senderBytes.length + subjectBytes.length + payload.length;
ByteBuffer buffer = ByteBuffer.allocate(capacity);
buffer.putInt(senderBytes.length);
buffer.put(senderBytes);
buffer.putInt(subjectBytes.length);
buffer.put(subjectBytes);
buffer.putInt(payload.length);
buffer.put(payload);
return buffer.array();
}

/**
* Decodes a new ClusterMessage from raw bytes.
* @param bytes raw bytes
* @return cluster message
*/
public static ClusterMessage fromBytes(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
byte[] senderBytes = new byte[buffer.getInt()];
buffer.get(senderBytes);
byte[] subjectBytes = new byte[buffer.getInt()];
buffer.get(subjectBytes);
byte[] payloadBytes = new byte[buffer.getInt()];
buffer.get(payloadBytes);

return new ClusterMessage(new NodeId(new String(senderBytes, Charsets.UTF_8)),
new MessageSubject(new String(subjectBytes, Charsets.UTF_8)),
payloadBytes);
}

@Override
public int hashCode() {
return Objects.hash(sender, subject, payload);
}
} }
13 changes: 7 additions & 6 deletions core/src/main/java/io/atomix/partition/Partitioner.java
Expand Up @@ -23,10 +23,11 @@
* @param <K> object type. * @param <K> object type.
*/ */
public interface Partitioner<K> { public interface Partitioner<K> {
/** /**
* Returns the {@link PartitionId} to which the specified object maps. * Returns the {@link PartitionId} to which the specified object maps.
* @param object object *
* @return partition identifier * @param object object
*/ * @return partition identifier
PartitionId hash(K object); */
PartitionId hash(K object);
} }
Expand Up @@ -86,7 +86,7 @@ public B withRelaxedReadConsistency() {
* *
* @return {@code true} if yes; {@code false} otherwise * @return {@code true} if yes; {@code false} otherwise
*/ */
public final boolean isReadOnly() { public final boolean readOnly() {
return readOnly; return readOnly;
} }


Expand All @@ -95,7 +95,7 @@ public final boolean isReadOnly() {
* *
* @return {@code true} if yes; {@code false} otherwise * @return {@code true} if yes; {@code false} otherwise
*/ */
public final boolean isRelaxedReadConsistency() { public final boolean relaxedReadConsistency() {
return relaxedReadConsistency; return relaxedReadConsistency;
} }


Expand All @@ -104,7 +104,7 @@ public final boolean isRelaxedReadConsistency() {
* *
* @return serializer * @return serializer
*/ */
public final Serializer getSerializer() { public final Serializer serializer() {
return serializer; return serializer;
} }


Expand All @@ -113,7 +113,7 @@ public final Serializer getSerializer() {
* *
* @return primitive name * @return primitive name
*/ */
public final String getName() { public final String name() {
return name; return name;
} }


Expand All @@ -122,7 +122,7 @@ public final String getName() {
* *
* @return primitive type * @return primitive type
*/ */
public final DistributedPrimitive.Type getPrimitiveType() { public final DistributedPrimitive.Type primitiveType() {
return type; return type;
} }


Expand Down

0 comments on commit 48a79c8

Please sign in to comment.