<commit_before>//  Copyright 2011 <NAME>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

#include <zippylog/envelope.hpp>
#include <zippylog/platform.hpp>
#include <zippylog/message_registrar.hpp>

#include <google/protobuf/text_format.h>

#include <stdio.h>
#include <sstream>
#include <vector>

namespace zippylog {

using ::google::protobuf::Message;
using ::std::invalid_argument;
using ::std::string;
using ::std::stringstream;
using ::std::vector;
using ::zmq::message_t;

Envelope::Envelope() : messages(NULL), messages_size(0)
{
    platform::Time t;
    platform::TimeNow(t);

    this->envelope.set_create_time(t.epoch_micro);
}

Envelope::Envelope(message_t &msg, uint32 offset)
{
    if (!msg.size()) throw invalid_argument("0MQ message is empty");
    if (offset + 1 >= msg.size()) throw invalid_argument("specified offset is larger than message");

    this->InitializeFromBuffer((const void *)((char *)msg.data() + offset), msg.size() - offset);
}

Envelope::Envelope(const void * data, int size) : messages(NULL), messages_size(0)
{
    if (!data) throw invalid_argument("NULL data pointer");
    if (!size) throw invalid_argument("0 size data buffer");
    if (size < 0) throw invalid_argument("size <0: " + size);

    this->InitializeFromBuffer(data, size);
}

Envelope::Envelope(string const &s) :
    messages(NULL),
    messages_size(0)
{
    platform::Time t;
    platform::TimeNow(t);

    this->envelope.set_create_time(t.epoch_micro);
    this->envelope.set_string_value(s);
}

Envelope::~Envelope()
{
    if (this->messages) {
        for (int i = 0; i < this->messages_size; i++) {
            if (this->messages[i]) {
                delete this->messages[i];
            }
            this->messages[i] = NULL;
        }
    }

    delete [] this->messages;
    this->messages = NULL;
}

Envelope::Envelope(Envelope const &e)
{
    if (e.messages_size > 0) {
        this->messages_size = e.messages_size;
        this->messages = new Message *[this->messages_size];
        for (int i = 0; i < this->messages_size; i++) {
            this->messages[i] = NULL;
        }
    }
    else {
        this->messages_size = 0;
        this->messages = NULL;
    }

    this->envelope = e.envelope;
}

Envelope & Envelope::operator=(Envelope const &orig)
{
    if (this == &orig) return *this;

    if (orig.messages_size > 0) {
        this->messages_size = orig.messages_size;
        this->messages = new Message *[this->messages_size];
        for (int i = 0; i < this->messages_size; i++) {
            this->messages[i] = NULL;
        }
    }
    else {
        this->messages_size = 0;
        this->messages = NULL;
    }

    this->envelope = orig.envelope;

    return *this;
}

bool Envelope::operator==(Envelope const &other) const
{
    if (this == &other) return true;

    string s1, s2;
    this->Serialize(s1);
    other.Serialize(s2);

    return s1.compare(s2) == 0;
}

bool Envelope::operator!=(Envelope const &other) const
{
    return !(*this == other);
}

void Envelope::InitializeFromBuffer(const void * data, int size)
{
    if (!this->envelope.ParseFromArray(data, size)) {
        throw DeserializeException();
    }

    int count = this->MessageCount();

    this->messages = new Message *[count];
    this->messages_size = count;
    for (int i = 0; i < count; i++) {
        this->messages[i] = NULL;
    }
}

bool Envelope::Serialize(string &s) const
{
    return this->envelope.AppendToString(&s);
}

bool Envelope::AddMessage(Message &m, uint32 ns, uint32 enumeration)
{
    if (!this->messages) {
        assert(this->MessageCount() == 0);

        this->messages = new Message *[1];
        this->messages_size = 1;
        // don't bother caching it, cuz that would take memory
        this->messages[0] = NULL;
    }
    else {
        // we need to resize the array
        Message **old = this->messages;

        this->messages = new Message *[this->messages_size + 1];
        this->messages[this->messages_size+1] = NULL;

        for (int i = 0; i < this->messages_size; i++) {
            this->messages[i] = old[i];
        }

        delete [] old;
    }

    string buffer;
    if (!m.SerializeToString(&buffer)) return false;

    this->envelope.add_message_namespace(ns);
    this->envelope.add_message_type(enumeration);
    this->envelope.add_message(buffer);

    return true;
}

bool Envelope::ParseFromCodedInputStream(::google::protobuf::io::CodedInputStream &cis)<commit_msg>fix bugs in envelope message cache<commit_after>//  Copyright 2011 <NAME>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

#include <zippylog/envelope.hpp>
#include <zippylog/platform.hpp>
#include <zippylog/message_registrar.hpp>

#include <google/protobuf/text_format.h>

#include <stdio.h>
#include <sstream>
#include <vector>

namespace zippylog {

using ::google::protobuf::Message;
using ::std::invalid_argument;
using ::std::string;
using ::std::stringstream;
using ::std::vector;
using ::zmq::message_t;

Envelope::Envelope() : messages(NULL), messages_size(0)
{
    platform::Time t;
    platform::TimeNow(t);

    this->envelope.set_create_time(t.epoch_micro);
}

Envelope::Envelope(message_t &msg, uint32 offset)
{
    if (!msg.size()) throw invalid_argument("0MQ message is empty");
    if (offset + 1 >= msg.size()) throw invalid_argument("specified offset is larger than message");

    this->InitializeFromBuffer((const void *)((char *)msg.data() + offset), msg.size() - offset);
}

Envelope::Envelope(const void * data, int size) : messages(NULL), messages_size(0)
{
    if (!data) throw invalid_argument("NULL data pointer");
    if (!size) throw invalid_argument("0 size data buffer");
    if (size < 0) throw invalid_argument("size <0: " + size);

    this->InitializeFromBuffer(data, size);
}

Envelope::Envelope(string const &s) :
    messages(NULL),
    messages_size(0)
{
    platform::Time t;
    platform::TimeNow(t);

    this->envelope.set_create_time(t.epoch_micro);
    this->envelope.set_string_value(s);
}

Envelope::~Envelope()
{
    if (this->messages) {
        for (int i = 0; i < this->messages_size; i++) {
            if (this->messages[i]) {
                delete this->messages[i];
            }
            this->messages[i] = NULL;
        }

        delete [] this->messages;
    }
}

Envelope::Envelope(Envelope const &e)
    : messages_size(0), messages(NULL)
{
    if (e.messages_size > 0) {
        this->messages_size = e.messages_size;
        this->messages = new Message *[this->messages_size];
        for (int i = 0; i < this->messages_size; i++) {
            this->messages[i] = NULL;
        }
    }

    this->envelope.CopyFrom(e.envelope);
}

Envelope & Envelope::operator=(Envelope const &orig)
{
    if (this == &orig) return *this;

    if (orig.messages_size > 0) {
        this->messages_size = orig.messages_size;
        this->messages = new Message *[this->messages_size];
        for (int i = 0; i < this->messages_size; i++) {
            this->messages[i] = NULL;
        }
    }
    else {
        this->messages_size = 0;
        this->messages = NULL;
    }

    this->envelope = orig.envelope;

    return *this;
}

bool Envelope::operator==(Envelope const &other) const
{
    if (this == &other) return true;

    string s1, s2;
    this->Serialize(s1);
    other.Serialize(s2);

    return s1.compare(s2) == 0;
}

bool Envelope::operator!=(Envelope const &other) const
{
    return !(*this == other);
}

void Envelope::InitializeFromBuffer(const void * data, int size)
{
    if (!this->envelope.ParseFromArray(data, size)) {
        throw DeserializeException();
    }

    int count = this->MessageCount();

    this->messages = new Message *[count];
    this->messages_size = count;
    for (int i = 0; i < count; i++) {
        this->messages[i] = NULL;
    }
}

bool Envelope::Serialize(string &s) const
{
    return this->envelope.AppendToString(&s);
}

bool Envelope::AddMessage(Message &m, uint32 ns, uint32 enumeration)
{
    if (!this->messages) {
        assert(this->MessageCount() == 0);

        this->messages = new Message *[1];
        this->messages_size = 1;
        // don't bother caching it, cuz that would take memory
        this->messages[0] = NULL;
    }
    else {
        // we need to resize the array
        Message **old = this->messages;

        this->messages = new Message *[this->messages_size + 1];
        this->messages[this->messages_size] = NULL;

        for (int i = 0; i < this->messages_size; i++) {
            this->messages[i] = old[i];
        }

        this->messages_size++;

        delete [] old;
    }

    string buffer;
    if (!m.SerializeToString(&buffer)) return false;

    this->envelope.add_message_namespace(ns);
    this->envelope.add_message_type(enumeration);
    this->envelope.add_message(buffer);

    return true;
}

bool Envelope::ParseFromCodedInputStream(::google::protobuf::io::CodedInputStream &cis)
{'fraction_non_alphanumeric': 0.1447818218379248, 'fraction_numerical': 0.008747235069374623, 'mean_word_length': 3.0434959349593496, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1043481', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3107, 'n_words': 799}