<commit_before>package com.tinkerpop.rexster.gremlin.converter;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.util.structures.Row;
import com.tinkerpop.pipes.util.structures.Table;
import com.tinkerpop.rexster.Tokens;
import org.msgpack.MessagePack;
import org.msgpack.packer.BufferPacker;
import org.msgpack.packer.Packer;
import org.msgpack.type.NilValue;
import org.msgpack.type.ValueFactory;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Converts a result from Gremlin to a byte array encoded by MsgPack.
 */
public class MsgPackResultConverter implements ResultConverter<byte[]> {
    private final MessagePack msgpack = new MessagePack();

    public byte[] convert(final Object result) throws Exception {
        final BufferPacker packer = msgpack.createBufferPacker(1024);
        prepareOutput(result, packer);
        return packer.toByteArray();
    }

    private void prepareOutput(final Object object, final Packer packer) throws Exception {
        if (object == null) {
            packer.write(ValueFactory.createNilValue());
        } else if (object instanceof String || object instanceof Number || object instanceof Boolean) {
            packer.write(object);
        } else if (object instanceof Element) {
            final Element element = (Element) object;
            final Set<String> propertyKeys = element.getPropertyKeys();
            final int propertySize = propertyKeys.size();
            final boolean isVertex = element instanceof Vertex;
            final int elementSize = (isVertex ? 2 : 5) + ((propertySize > 0) ? 1 : 0);

            packer.writeMapBegin(elementSize);
            packer.write(Tokens._ID);
            packer.write(element.getId());<commit_msg>RexPro msgpack serializes element ids better.<commit_after>package com.tinkerpop.rexster.gremlin.converter;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.util.structures.Row;
import com.tinkerpop.pipes.util.structures.Table;
import com.tinkerpop.rexster.Tokens;
import org.msgpack.MessagePack;
import org.msgpack.packer.BufferPacker;
import org.msgpack.packer.Packer;
import org.msgpack.type.NilValue;
import org.msgpack.type.ValueFactory;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Converts a result from Gremlin to a byte array encoded by MsgPack.
 */
public class MsgPackResultConverter implements ResultConverter<byte[]> {
    private final MessagePack msgpack = new MessagePack();

    public byte[] convert(final Object result) throws Exception {
        final BufferPacker packer = msgpack.createBufferPacker(1024);
        prepareOutput(result, packer);
        return packer.toByteArray();
    }

    private void prepareOutput(final Object object, final Packer packer) throws Exception {
        if (object == null) {
            packer.write(ValueFactory.createNilValue());
        } else if (object instanceof String || object instanceof Number || object instanceof Boolean) {
            packer.write(object);
        } else if (object instanceof Element) {
            final Element element = (Element) object;
            final Set<String> propertyKeys = element.getPropertyKeys();
            final int propertySize = propertyKeys.size();
            final boolean isVertex = element instanceof Vertex;
            final int elementSize = (isVertex ? 2 : 5) + ((propertySize > 0) ? 1 : 0);

            packer.writeMapBegin(elementSize);
            packer.write(Tokens._ID);
            final Object id = element.getId();
            if (id.getClass().isPrimitive()) {
                packer.write(id);
            } else {
                packer.write(id.toString());
            }
{'fraction_non_alphanumeric': 0.09827887253679221, 'fraction_numerical': 0.004489897730107259, 'mean_word_length': 3.3025751072961373, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4050036', 'n_tokens_mistral': 1141, 'n_tokens_neox': 1098, 'n_words': 282}