use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class Transporter method createRequestPacket.
// --- REQUEST PACKET ---
public Tree createRequestPacket(Context ctx) {
FastBuildTree msg = new FastBuildTree(7);
// Add basic properties
msg.putUnsafe("ver", ServiceBroker.PROTOCOL_VERSION);
msg.putUnsafe("sender", nodeID);
msg.putUnsafe("id", ctx.id);
msg.putUnsafe("action", ctx.name);
// Add params and meta
if (ctx.params != null) {
msg.putUnsafe("params", ctx.params.asObject());
Tree meta = ctx.params.getMeta(false);
if (meta != null) {
msg.putUnsafe("meta", meta.asObject());
}
}
// Add opts
if (ctx.opts != null) {
msg.putUnsafe("timeout", ctx.opts.timeout);
}
// Return message
return msg;
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class JCacheCacher method get.
// --- CACHE METHODS ---
@Override
public Promise get(String key) {
try {
int pos = partitionPosition(key, true);
// Prefix is the name of the partition / region (eg.
// "user" from the "user.name" cache key)
String prefix = key.substring(0, pos);
javax.cache.Cache<String, byte[]> partition;
readLock.lock();
try {
partition = partitions.get(prefix);
} finally {
readLock.unlock();
}
if (partition != null) {
byte[] bytes = partition.get(key.substring(pos + 1));
if (bytes != null) {
Tree root = serializer.read(bytes);
Tree content = root.get(CONTENT);
if (content != null) {
return Promise.resolve(content);
}
return Promise.resolve(root);
}
}
} catch (Throwable cause) {
logger.warn("Unable to get data from JCache!", cause);
}
return Promise.resolve();
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class OHCacher method valueToBytes.
protected byte[] valueToBytes(Tree tree) throws Exception {
// Compress content
Tree root = new CheckedTree(Collections.singletonMap(CONTENT, tree.asObject()));
byte[] bytes = serializer.write(root);
boolean compressed;
if (compressAbove > 0 && bytes.length > compressAbove) {
bytes = compress(bytes, compressionLevel);
compressed = true;
} else {
compressed = false;
}
byte[] copy = new byte[bytes.length + 1];
System.arraycopy(bytes, 0, copy, 1, bytes.length);
if (compressed) {
// Compressed -> first byte = 1
copy[0] = (byte) 1;
}
return copy;
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class OHCacher method bytesToValue.
protected Tree bytesToValue(byte[] bytes) throws Exception {
// Decompress content
byte[] copy = new byte[bytes.length - 1];
System.arraycopy(bytes, 1, copy, 0, bytes.length - 1);
if (bytes[0] == 1) {
// First byte == 1 -> compressed
copy = decompress(copy);
}
Tree root = serializer.read(copy);
Tree content = root.get(CONTENT);
if (content != null) {
return content;
}
return root;
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class DefaultEventbus method receiveEvent.
// --- RECEIVE EVENT FROM REMOTE SERVICE ---
@Override
public void receiveEvent(Tree message) {
// Verify protocol version
if (checkVersion) {
String ver = message.get("ver", "unknown");
if (!ServiceBroker.PROTOCOL_VERSION.equals(ver)) {
logger.warn("Invalid protocol version (" + ver + ")!");
return;
}
}
// Get event property
String name = message.get("event", (String) null);
if (name == null || name.isEmpty()) {
logger.warn("Missing \"event\" property!");
return;
}
// Get data
Tree payload = message.get("data");
// Process events in Moleculer V2 style
Tree groupArray = message.get("groups");
Groups groups = null;
if (groupArray != null) {
int size = groupArray.size();
if (size > 0) {
String[] array = new String[groupArray.size()];
int i = 0;
for (Tree group : groupArray) {
array[i++] = group.asString();
}
groups = Groups.of(array);
}
}
// Emit or broadcast?
if (message.get("broadcast", true)) {
// Broadcast
broadcast(name, payload, groups, true);
} else {
// Emit
emit(name, payload, groups, true);
}
}
Aggregations