db: add date_type

This commit is contained in:
Avi Kivity
2015-01-06 15:25:50 +02:00
parent cfd86d70f8
commit 4b4f1606e6

View File

@@ -4,6 +4,7 @@
#include "database.hh"
#include "net/byteorder.hh"
#include "db_clock.hh"
bool
less_unsigned(const bytes& v1, const bytes& v2) {
@@ -128,12 +129,42 @@ struct boolean_type_impl : public simple_type_impl<bool> {
}
};
struct date_type_impl : public abstract_type {
date_type_impl() : abstract_type("date") {}
virtual void serialize(const boost::any& value, std::ostream& out) override {
auto v = boost::any_cast<db_clock::time_point>(value);
int64_t i = v.time_since_epoch().count();
i = net::hton(uint64_t(i));
out.write(reinterpret_cast<char*>(&i), 8);
}
virtual object_opt deserialize(std::istream& in) override {
int64_t tmp;
auto n = in.rdbuf()->sgetn(reinterpret_cast<char*>(&tmp), 8);
if (n == 0) {
return {};
}
if (n != 8) {
throw marshal_exception();
}
tmp = net::ntoh(uint64_t(tmp));
return boost::any(db_clock::time_point(db_clock::duration(tmp)));
}
virtual bool less(const bytes& b1, const bytes& b2) override {
// DateType has a bug where it compares the values as an unsigned type.
// Preserve this bug.
return default_less<db_clock::time_point>(b1, b2, [] (db_clock::time_point t1, db_clock::time_point t2) {
return uint64_t(t1.time_since_epoch().count() < t2.time_since_epoch().count());
});
}
};
thread_local shared_ptr<abstract_type> int_type(make_shared<int32_type_impl>());
thread_local shared_ptr<abstract_type> long_type(make_shared<long_type_impl>());
thread_local shared_ptr<abstract_type> ascii_type(make_shared<string_type_impl>("ascii"));
thread_local shared_ptr<abstract_type> bytes_type(make_shared<bytes_type_impl>());
thread_local shared_ptr<abstract_type> utf8_type(make_shared<string_type_impl>("utf8"));
thread_local shared_ptr<abstract_type> boolean_type(make_shared<boolean_type_impl>());
thread_local shared_ptr<abstract_type> date_type(make_shared<date_type_impl>());
partition::partition(column_family& cf)
: rows(key_compare(cf.clustering_key_type)) {