Skip to content

Instantly share code, notes, and snippets.

@suma
Created August 5, 2013 13:01
Show Gist options
  • Select an option

  • Save suma/6155752 to your computer and use it in GitHub Desktop.

Select an option

Save suma/6155752 to your computer and use it in GitHub Desktop.
Jubatus model interface, I'm thinking.
// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2013 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "model.hpp"
namespace jubatus {
namespace core {
namespace framework {
msgpack_writer::~msgpack_writer() {
}
model::~model() {
}
void model::compact() {
// not implemented, do nothing
}
void model::clear() {
// not implemented, do nothing
}
} // namespace framework
} // namespace core
} // namespace jubatus
// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2013 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef JUBATUS_CORE_FRAMEWORK_MODEL_HPP_
#define JUBATUS_CORE_FRAMEWORK_MODEL_HPP_
#include <msgpack.hpp>
namespace jubatus {
namespace core {
namespace framework {
class msgpack_writer {
public:
virtual ~msgpack_writer();
virtual void write(const char* buf, unsigned int len) = 0;
};
template <class T>
class stream_writer : public msgpack_writer {
public:
explicit stream_writer(T& stream)
: stream_(stream) {
}
void write(const char*buf, unsigned int len) {
stream_.write(buf, len);
}
T& stream() {
return stream_;
}
private:
T& stream_;
};
class model {
public:
virtual ~model();
// by msgpack
virtual void save(msgpack_writer&) = 0;
virtual void load(msgpack::object&) = 0;
// You can calculate only serialized size by msgpack_writer.
// TODO: virtual void check(msgpack::object&) const = 0;
// these can be empty, do nothing by default
virtual void compact();
virtual void clear();
};
} // namespace framework
} // namespace core
} // namespace jubatus
#endif // JUBATUS_CORE_FRAMEWORK_MODEL_HPP_
// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2013 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "model.hpp"
#include <gtest/gtest.h>
namespace jubatus {
namespace core {
namespace framework {
struct int_model {
int_model()
: value(0) {
}
int value;
MSGPACK_DEFINE(value);
void save(msgpack_writer& buf) {
msgpack::pack(buf, *this);
}
void load(msgpack::object& o) {
o.convert(this);
}
};
TEST(mixable, save_load) {
int_model m;
m.value = 30;
ASSERT_EQ(30, m.value);
// save model (value = 30)
msgpack::sbuffer sbuf;
stream_writer<msgpack::sbuffer> swriter(sbuf);
m.save(swriter);
ASSERT_LT(0, sbuf.size());
// change model value
m.value = 100;
msgpack::unpacked msg;
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
// load model (value = 30)
int_model load;
load.load(msg.get());
ASSERT_EQ(30, load.value);
}
} // namespace framework
} // namespace core
} // namespace jubatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment