<commit_before>package voldemort.store.parser;

import java.util.ArrayList;
import java.util.List;

import voldemort.utils.ByteUtils;
import voldemort.versioning.ClockEntry;
import voldemort.versioning.Occured;
import voldemort.versioning.VectorClock;

public class ClientNode {

    private String client;
    private VectorClock version;
    private ArrayList<AccessNode> accesses;

    public ClientNode(String client) {
        this.client = client;
        version = null;
        accesses = new ArrayList<AccessNode>();
    }

    public ClientNode(byte[] bytes, int offset) {
        int length = bytes[offset + 1];
        byte[] clientBytes = ByteUtils.copy(bytes, offset + 1, offset + length + 1);
        String c = ByteUtils.getString(clientBytes, "UTF-8");
        this.client = c;
        this.version = new VectorClock(bytes, offset + 1 + length);

    }

    public String getClient() {
        return client;
    }

    public VectorClock getVersion() {
        return version;
    }

    public int sizeInBytes() {
        return client.length() + 1 + version.sizeInBytes();
    }

    public byte[] toBytes() {
        byte[] clientBytes = ByteUtils.getBytes(client, "UTF=8");
        byte[] serialized = ByteUtils.cat(new byte[] { (byte) client.length() },
                                          clientBytes,
                                          version.toBytes());
        return serialized;
    }

    public void addAccess(AccessNode node) {
        if(client.equals(node.getClient()) && node.getOpType() == AccessType.SET) {
            accesses.add(node);
        }
    }

    public void setVersion(VectorClock v) {
        if(version == null) {
            version = v;
        } else {
            System.out.println("current version :" + version + " new version : " + v);
            List<ClockEntry> versions = v.getEntries();
            List<ClockEntry> clientVersions = version.getEntries();
            List<ClockEntry> newVersions = new ArrayList<ClockEntry>();
            int i = 0;
            int j = 0;
            while(i < versions.size() && j < clientVersions.size()) {
                ClockEntry v1 = versions.get(i);
                ClockEntry v2 = clientVersions.get(j);
                if(v1.getNodeId() == v2.getNodeId()) {
                    newVersions.add(new ClockEntry(v1.getNodeId(), Math.min(v1.getVersion(),
                                                                            v2.getVersion())));
                    i++;
                    j++;
                } else if(v1.getNodeId() < v2.getNodeId()) {<commit_msg>Fix bugs when transfering ClientNode bytes<commit_after>package voldemort.store.parser;

import java.util.ArrayList;
import java.util.List;

import voldemort.utils.ByteUtils;
import voldemort.versioning.ClockEntry;
import voldemort.versioning.Occured;
import voldemort.versioning.VectorClock;

public class ClientNode {

    private String client;
    private VectorClock version;
    private ArrayList<AccessNode> accesses;

    public ClientNode(String client) {
        this.client = client;
        version = null;
        accesses = new ArrayList<AccessNode>();
    }

    public ClientNode(byte[] bytes, int offset) {
        int length = bytes[offset];
        byte[] clientBytes = ByteUtils.copy(bytes, offset + 1, offset + length + 1);
        String c = ByteUtils.getString(clientBytes, "UTF-8");
        this.client = c;
        if(bytes[offset + length + 1] == 1)
            this.version = new VectorClock(bytes, offset + 2 + length);
        else
            this.version = null;

    }

    public String getClient() {
        return client;
    }

    public VectorClock getVersion() {
        return version;
    }

    public int sizeInBytes() {
        return client.length() + 2 + (version == null ? 0 : version.sizeInBytes());
    }

    public byte[] toBytes() {
        byte[] clientBytes = ByteUtils.getBytes(client, "UTF-8");

        byte[] serialized = ByteUtils.cat(new byte[] { (byte) client.length() },
                                          clientBytes,
                                          new byte[] { (byte) (version == null ? 0 : 1) },
                                          version == null ? new byte[0] : version.toBytes());
        return serialized;
    }

    public void addAccess(AccessNode node) {
        if(client.equals(node.getClient()) && node.getOpType() == AccessType.SET) {
            accesses.add(node);
        }
    }

    public void setVersion(VectorClock v) {
        if(version == null) {
            version = v;
        } else {
            // System.out.println("current version :" + version +
            // " new version : " + v);
            List<ClockEntry> versions = v.getEntries();
            List<ClockEntry> clientVersions = version.getEntries();
            List<ClockEntry> newVersions = new ArrayList<ClockEntry>();
            int i = 0;
            int j = 0;
            while(i < versions.size() && j < clientVersions.size()) {
                ClockEntry v1 = versions.get(i);
                ClockEntry v2 = clientVersions.get(j);
                if(v1.getNodeId() == v2.getNodeId()) {
                    newVersions.add(new ClockEntry(v1.getNodeId(), Math.min(v1.getVersion(),
                                                                            v2.getVersion())));
                    i++;
                    j++;
                } else if(v1.getNodeId() < v2.getNodeId()) {
{'fraction_non_alphanumeric': 0.11688311688311688, 'fraction_numerical': 0.0074995427108103165, 'mean_word_length': 1.8405194805194804, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2882894', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1446, 'n_words': 343}