[Assorted-commits] SF.net SVN: assorted:[1185] ydb/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-16 21:05:31
|
Revision: 1185 http://assorted.svn.sourceforge.net/assorted/?rev=1185&view=rev Author: yangzhang Date: 2009-02-16 21:05:26 +0000 (Mon, 16 Feb 2009) Log Message: ----------- - moved protobufs to ydb::pb - added ser Modified Paths: -------------- ydb/trunk/src/Makefile ydb/trunk/src/ydb.proto Added Paths: ----------- ydb/trunk/src/ser.cc ydb/trunk/src/ser.h Modified: ydb/trunk/src/Makefile =================================================================== --- ydb/trunk/src/Makefile 2009-02-15 03:22:34 UTC (rev 1184) +++ ydb/trunk/src/Makefile 2009-02-16 21:05:26 UTC (rev 1185) @@ -101,3 +101,6 @@ p2: p2.cc $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) $(OUTPUT_OPTION) + +ser: ser.cc ser.h ydb.o + $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) $(OUTPUT_OPTION) Added: ydb/trunk/src/ser.cc =================================================================== --- ydb/trunk/src/ser.cc (rev 0) +++ ydb/trunk/src/ser.cc 2009-02-16 21:05:26 UTC (rev 1185) @@ -0,0 +1,86 @@ +#include "ser.h" + +//#define USE_PB +using ydb::msg::reader; +using ydb::msg::writer; +using ydb::msg::stream; +using namespace commons; +using namespace std; +#ifdef USE_PB +using namespace ydb::pb; +#else +using namespace ydb::msg; +#endif + +#ifdef USE_PB +#define PBSWITCH(a,b) a +#define PBONLY(x) x +#define NPBONLY(x) +#else +#define PBSWITCH(a,b) b +#define PBONLY(x) +#define NPBONLY(x) x +#endif + +//template<typename TxnBatch, typename Txn, typename Op> +void run() +{ + array<char> a(1e8); + writer w(a); + reader r(a); + stream s(r,w); + string str; + const int nreps = 2; + + { + TxnBatch batch NPBONLY((s)); + for (int i = 0; i < nreps; ++i) { + w.mark(); + batch.Clear(); + NPBONLY(batch.start_txn()); + for (int t = 0; t < 2; ++t) { + Txn &txn = *batch.add_txn(); + txn.set_seqno(t + 5); + NPBONLY(txn.start_op()); + for (int o = 0; o < 2; ++o) { + Op &op = *txn.add_op(); + op.set_type (Op::del); + op.set_key (3 * (o+1)); + op.set_value(4 * (o+1)); + } + NPBONLY(txn.fin_op()); + } + NPBONLY(batch.fin_txn()); + cout << w.pos() << '/' << w.size() << endl; + PBONLY(check(batch.SerializeToString(&str))); + } + } + w.flush(); + + const bool show = true; + { + TxnBatch batch NPBONLY((s)); + for (int i = 0; i < nreps; ++i) { + batch.Clear(); + PBONLY(check(batch.ParseFromString(str))); + if (show) cout << "ntxn " << batch.txn_size() << endl; + for (int t = 0; t < batch.txn_size(); ++t) { + const Txn &txn = batch.txn(t); + if (show) cout << "txn seqno " << txn.seqno() << endl; + for (int o = 0; o < txn.op_size(); ++o) { + const Op &op = txn.op(o); + int otype = op.type(); + int okey = op.key(); + int oval = op.value(); + if (show) cout << "op type " << otype << " key " << okey << " value " << oval << endl; + } + } + } + } +} + +int main() +{ + run(); + return 0; +} Added: ydb/trunk/src/ser.h =================================================================== --- ydb/trunk/src/ser.h (rev 0) +++ ydb/trunk/src/ser.h 2009-02-16 21:05:26 UTC (rev 1185) @@ -0,0 +1,158 @@ +#ifndef YDB_MSG_H +#define YDB_MSG_H + +#include <commons/array.h> +#include <iomanip> +#include <iostream> +#include "ydb.pb.h" + +#define BEGIN_NAMESPACE(ns) namespace ns { +#define END_NAMESPACE } + +BEGIN_NAMESPACE(ydb) +BEGIN_NAMESPACE(msg) + +using namespace commons; +using namespace std; + +short unset = -1; + +using ydb::pb::Op_OpType; + +class writer +{ + private: + array<char> a_; + char *p_; + char *mark_; + array<char> &out_; + template<typename T> + void write_(T x, char *p) { + reserve(sizeof x, p); + *reinterpret_cast<T*>(p) = x; + } + public: + writer(array<char> &out) : a_(90), p_(a_.get()), mark_(p_), out_(out) {} + array<char> &buf() { return a_; } + size_t pos() { return p_ - mark_; } + size_t size() { return a_.size(); } + void mark() { mark_ = p_; /*skip<int>();*/ } + void flush() { memcpy(out_.get(), a_.get(), mark_ - a_.get()); } + void reserve(int n) { reserve(n, p_); } + void reserve(int n, char *p) { + if (p + n > a_.end()) { + flush(); + memmove(a_.get(), mark_, a_.end() - mark_); + size_t diff = mark_ - a_.get(); + mark_ -= diff; + p_ -= diff; + p -= diff; + } + } + void show() { + cout << (void*) p_; + for (size_t i = 0; i < a_.size(); ++i) + cout << " " << hex << setfill('0') << setw(2) << int(mark_[i]); + cout << endl; + cout << (void*) p_; + for (size_t i = 0; i < a_.size(); ++i) + cout << " " << setfill(' ') << setw(2) << (i == pos() ? "^^" : ""); + cout << endl; + } + template<typename T> void skip() { reserve(sizeof(T)); p_ += sizeof(T); } + template<typename T> void write(T x) { write_(x, p_); p_ += sizeof x; } + template<typename T> void write(T x, size_t off) { write_(x, mark_ + off); } +}; + +class reader +{ + private: + array<char> &a_; + const char *p_; + public: + reader(array<char> &a) : a_(a), p_(a.get()) {} + template<typename T> T read() { + T x = *reinterpret_cast<const T*>(p_); + p_ += sizeof(T); + return x; + } + void jump(ssize_t off) { p_ += off; } +}; + +class stream +{ + private: + reader &r_; + writer &w_; + public: + stream(reader &r, writer &w) : r_(r), w_(w) {} + reader &get_reader() { return r_; } + writer &get_writer() { return w_; } +}; + +class Op +{ + private: + stream &s_; + reader &r_; + writer &w_; + public: + static const Op_OpType read = ydb::pb::Op_OpType_read; + static const Op_OpType write = ydb::pb::Op_OpType_write; + static const Op_OpType del = ydb::pb::Op_OpType_del; + Op(stream &s) : s_(s), r_(s.get_reader()), w_(s.get_writer()) {} + void set_type (char x) { w_.write(x); } + void set_key (int x) { w_.write(x); } + void set_value(int x) { w_.write(x); } + char type() const { return r_.read<char>(); } + int key() const { return r_.read<int>(); } + int value() const { return r_.read<int>(); } +}; + +class Txn +{ + private: + stream &s_; + reader &r_; + writer &w_; + size_t off_; + Op op_; + mutable short nop_; + mutable int seqno_; + public: + Txn(stream &s) : + s_(s), r_(s.get_reader()), w_(s.get_writer()), off_(w_.pos()), op_(s), + nop_(unset) {} + void Clear() { w_.reserve(0*50); nop_ = unset; off_ = w_.pos(); } + void set_seqno(int x) { w_.write(x); } + int seqno() const { return r_.read<int>(); } + void start_op() { w_.skip<typeof(nop_)>(); } + Op *add_op() { if (nop_ == unset) nop_ = 0; ++nop_; return &op_; } + void fin_op() { w_.write(nop_, off_ + sizeof(int)); } + int op_size() const { if (nop_ == unset) nop_ = r_.read<typeof(nop_)>(); return nop_; } + const Op &op(int o) const { return op_; } +}; + +class TxnBatch +{ + private: + stream &s_; + reader &r_; + writer &w_; + size_t off_; + mutable Txn txn_; + mutable short ntxn_; + public: + TxnBatch(stream &s) : s_(s), r_(s.get_reader()), w_(s.get_writer()), off_(w_.pos()), txn_(s), ntxn_(unset) {} + void Clear() { w_.reserve(0*100); txn_.Clear(); ntxn_ = unset; off_ = w_.pos(); } + void start_txn() { w_.skip<typeof(ntxn_)>(); } + Txn *add_txn() { if (ntxn_ == unset) ntxn_ = 0; ++ntxn_; txn_.Clear(); return &txn_; } + void fin_txn() { w_.write(ntxn_, off_); } + int txn_size() const { if (ntxn_ == unset) ntxn_ = r_.read<typeof(ntxn_)>(); return ntxn_; } + const Txn &txn(int t) const { txn_.Clear(); return txn_; } +}; + +END_NAMESPACE +END_NAMESPACE + +#endif Modified: ydb/trunk/src/ydb.proto =================================================================== --- ydb/trunk/src/ydb.proto 2009-02-15 03:22:34 UTC (rev 1184) +++ ydb/trunk/src/ydb.proto 2009-02-16 21:05:26 UTC (rev 1185) @@ -1,3 +1,5 @@ +package ydb.pb; + option optimize_for = SPEED; // A socket address (host:port). This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |