Derecho  0.9
Distributed systems toolkit for RDMA
Object.hpp
Go to the documentation of this file.
1 #ifndef OBJECT_HPP
2 #define OBJECT_HPP
3 #include <chrono>
4 #include <iostream>
5 #include <map>
6 #include <memory>
7 #include <string.h>
8 #include <string>
9 #include <time.h>
10 #include <vector>
11 #include <optional>
12 #include <tuple>
13 
14 #include <derecho/conf/conf.hpp>
15 #include <derecho/core/derecho.hpp>
18 
19 using std::cout;
20 using std::endl;
21 using namespace persistent;
22 using namespace std::chrono_literals;
23 
24 namespace objectstore {
25 
27 public:
28  char* bytes;
29  std::size_t size;
30 
31  // constructor - copy to own the data
32  Blob(const char* const b, const decltype(size) s);
33 
34  // copy constructor - copy to own the data
35  Blob(const Blob& other);
36 
37  // move constructor - accept the memory from another object
38  Blob(Blob&& other);
39 
40  // default constructor - no data at all
41  Blob();
42 
43  // destructor
44  virtual ~Blob();
45 
46  // move evaluator:
47  Blob& operator=(Blob&& other);
48 
49  // copy evaluator:
50  Blob& operator=(const Blob& other);
51 
52  // serialization/deserialization supports
53  std::size_t to_bytes(char* v) const;
54 
55  std::size_t bytes_size() const;
56 
57  void post_object(const std::function<void(char const* const, std::size_t)>& f) const;
58 
60 
61  static std::unique_ptr<Blob> from_bytes(mutils::DeserializationManager*, const char* const v);
62 
63  // from_bytes_noalloc() implementation borrowed from mutils-serialization.
66  const char* const v,
68 };
69 
70 using OID = uint64_t;
71 #define INV_OID (0xffffffffffffffffLLU)
72 
74 public:
75  mutable std::tuple<persistent::version_t,uint64_t> ver; // object version
76  OID oid; // object_id
77  Blob blob; // the object
78 
79  bool operator==(const Object& other);
80 
81  bool is_valid() const;
82 
83  // constructor 0 : copy constructor
84  Object(const OID& _oid, const Blob& _blob);
85 
86  // constructor 0.5 : copy constructor
87  Object(const std::tuple<persistent::version_t,uint64_t> _ver, const OID& _oid, const Blob& _blob);
88 
89  // constructor 1 : copy consotructor
90  Object(const uint64_t _oid, const char* const _b, const std::size_t _s);
91 
92  // constructor 1.5 : copy constructor
93  Object(const std::tuple<persistent::version_t,uint64_t> _ver, const uint64_t _oid, const char* const _b, const std::size_t _s);
94 
95  // TODO: we need a move version for the deserializer.
96 
97  // constructor 2 : move constructor
98  Object(Object&& other);
99 
100  // constructor 3 : copy constructor
101  Object(const Object& other);
102 
103  // constructor 4 : default invalid constructor
104  Object();
105 
106  DEFAULT_SERIALIZATION_SUPPORT(Object, ver, oid, blob);
107 };
108 
109 inline std::ostream& operator<<(std::ostream& out, const Blob& b) {
110  out << "[size:" << b.size << ", data:" << std::hex;
111  if(b.size > 0) {
112  uint32_t i = 0;
113  for(i = 0; i < 8 && i < b.size; i++) {
114  out << " " << b.bytes[i];
115  }
116  if(i < b.size) {
117  out << "...";
118  }
119  }
120  out << std::dec << "]";
121  return out;
122 }
123 
124 inline std::ostream& operator<<(std::ostream& out, const Object& o) {
125  out << "Object{ver: 0x" << std::hex << std::get<0>(o.ver) << std::dec
126  << ", ts: " << std::get<1>(o.ver) << ", id:"
127  << o.oid << ", data:" << o.blob << "}";
128  return out;
129 }
130 
131 } // namespace objectstore
132 #endif //OBJECT_HPP
std::enable_if_t< std::is_base_of< ByteRepresentable CMA T >::value, std::unique_ptr< T > > from_bytes(DeserializationManager *ctx, char const *v)
Calls T::from_bytes(ctx,v) when T is a ByteRepresentable.
This file include all common types internal to derecho and not necessarily being known by a client pr...
std::size_t size
Definition: Object.hpp:29
std::enable_if_t< std::is_base_of< ByteRepresentable CMA std::decay_t< T > >::value, context_ptr< T > > from_bytes_noalloc(DeserializationManager *ctx, const char *v, context_ptr< std::decay_t< T >>=context_ptr< std::decay_t< T >>{})
Calls T::from_bytes_noalloc(ctx,v) when T is a ByteRepresentable.
#define DEFAULT_SERIALIZATION_SUPPORT(CLASS_NAME, CLASS_MEMBERS...)
THIS (below) is the only user-facing macro in this file.
A non-POD type which wishes to mark itself byte representable should extend this class.
auto bytes_size(const T &)
Just calls sizeof(T)
std::unique_ptr< T, ContextDeleter< T > > context_ptr
Definition: context_ptr.hpp:20
The manager for any RemoteDeserializationContexts.
bool operator==(const Opcode &lhs, const Opcode &rhs)
Definition: rpc_utils.hpp:72
std::tuple< persistent::version_t, uint64_t > ver
Definition: Object.hpp:75
uint64_t OID
Definition: Object.hpp:70
std::ostream & operator<<(std::ostream &out, const Object &o)
Definition: Object.hpp:124
std::enable_if_t< std::is_pod< BR >::value > post_object(const F &f, const BR &br, Args &&... args)
In-place serialization is also sometimes possible.
std::size_t to_bytes(const ByteRepresentable &b, char *v)
calls b.to_bytes(v) when b is a ByteRepresentable; calls std::memcpy() when b is POD.
void ensure_registered(mutils::DeserializationManager &)
Definition: Object.hpp:59