Search in sources :

Example 1 with Tree

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;
}
Also used : FastBuildTree(services.moleculer.util.FastBuildTree) FastBuildTree(services.moleculer.util.FastBuildTree) Tree(io.datatree.Tree)

Example 2 with Tree

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();
}
Also used : CheckedTree(services.moleculer.util.CheckedTree) Tree(io.datatree.Tree)

Example 3 with Tree

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;
}
Also used : CheckedTree(services.moleculer.util.CheckedTree) CheckedTree(services.moleculer.util.CheckedTree) Tree(io.datatree.Tree)

Example 4 with Tree

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;
}
Also used : CheckedTree(services.moleculer.util.CheckedTree) Tree(io.datatree.Tree)

Example 5 with Tree

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);
    }
}
Also used : CheckedTree(services.moleculer.util.CheckedTree) Tree(io.datatree.Tree)

Aggregations

Tree (io.datatree.Tree)60 FastBuildTree (services.moleculer.util.FastBuildTree)26 Test (org.junit.Test)12 NodeDescriptor (services.moleculer.transporter.tcp.NodeDescriptor)12 CheckedTree (services.moleculer.util.CheckedTree)9 TimeoutException (java.util.concurrent.TimeoutException)6 Promise (services.moleculer.Promise)6 CommonUtils.readTree (services.moleculer.util.CommonUtils.readTree)6 RemoteException (java.rmi.RemoteException)4 LinkedHashMap (java.util.LinkedHashMap)4 CallOptions (services.moleculer.context.CallOptions)4 Context (services.moleculer.context.Context)4 Annotation (java.lang.annotation.Annotation)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 NoSuchElementException (java.util.NoSuchElementException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)3 Action (services.moleculer.service.Action)3