0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MetaLogEntity.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
28 #ifndef Hypertable_Lib_MetaLogEntity_h
29 #define Hypertable_Lib_MetaLogEntity_h
30 
31 #include "MetaLogEntityHeader.h"
32 
33 #include <Common/Logger.h>
34 #include <Common/Serializable.h>
35 
36 #include <boost/algorithm/string.hpp>
37 
38 #include <iostream>
39 #include <memory>
40 #include <mutex>
41 
42 namespace Hypertable {
43 
44  namespace MetaLog {
45 
50  class Reader;
51  class Writer;
52 
62  class Entity : public std::enable_shared_from_this<Entity>, public Serializable {
63  public:
64 
70  Entity(int32_t type);
71 
78  Entity(const EntityHeader &header_);
79 
81  virtual ~Entity() { }
82 
84 
91  virtual void decode(const uint8_t **bufp, size_t *remainp,
92  uint16_t definition_version) {
93  HT_ASSERT(!"Not Implemented");
94  }
95 
97  void lock() { m_mutex.lock(); }
98 
100  void unlock() { m_mutex.unlock(); }
101 
113  }
114 
124  return (header.flags & EntityHeader::FLAG_REMOVE) != 0;
125  }
126 
132  virtual const std::string name() = 0;
133 
137  int64_t id() const { return header.id; }
138 
142  virtual void display(std::ostream &os) { }
143 
144  friend std::ostream &operator <<(std::ostream &os, Entity &entity);
145  friend class Reader;
146  friend class Writer;
147 
148  protected:
149 
159  void encode_entry(uint8_t **bufp);
160 
163 
166  };
167 
169  typedef std::shared_ptr<Entity> EntityPtr;
170 
178  inline std::ostream &
179  operator <<(std::ostream &os, Entity &entity) {
180  os << "{MetaLog::Entity " << entity.name() << " header={";
181  entity.header.display(os);
182  os << "} payload={";
183  entity.display(os);
184  os << "}";
185  return os;
186  }
187 
188  namespace EntityType {
189  enum {
190  RECOVER = 0x00000001
191  };
192  }
193 
207  class EntityRecover : public Entity {
208  public:
209 
213  EntityRecover() : Entity(EntityType::RECOVER) { }
214 
215  size_t encoded_length() const override { return 0; }
216 
217  void encode(uint8_t **bufp) const override { }
218 
219  void decode(const uint8_t **bufp, size_t *remainp) override {}
220 
221  void decode(const uint8_t **bufp, size_t *remainp,
222  uint16_t definition_version) override {}
223 
228  const std::string name() override { return "Recover"; }
229 
230  private:
231 
232  uint8_t encoding_version() const override { return 0; }
233 
234  size_t encoded_length_internal() const override { return 0; }
235 
236  void encode_internal(uint8_t **bufp) const override { }
237 
238  void decode_internal(uint8_t version, const uint8_t **bufp,
239  size_t *remainp) override { }
240 
241  };
243  }
244 }
245 
246 #endif // Hypertable_Lib_MetaLogEntity_h
static std::mutex mutex
Definition: Logger.cc:43
friend std::ostream & operator<<(std::ostream &os, Entity &entity)
ostream shift function for Entity objects.
int32_t flags
Flags (either FLAG_REMOVE or 0)
int32_t length
Length of entity header plus serialized state.
size_t encoded_length() const override
Returns serialized object length.
void encode_internal(uint8_t **bufp) const override
Writes serialized representation of object to a buffer.
std::shared_ptr< Entity > EntityPtr
Smart pointer to Entity.
void encode(uint8_t **bufp) const override
Writes serialized representation of object to a buffer.
int64_t id
Unique ID of entity.
std::mutex m_mutex
Mutex for serializing access to members
void decode(const uint8_t **bufp, size_t *remainp) override
Reads serialized representation of object from a buffer.
EntityHeader header
Entity header
void decode(const uint8_t **bufp, size_t *remainp, uint16_t definition_version) override
Decodes serialized entity state.
void mark_for_removal()
Marks entity for removal.
virtual ~Entity()
Destructor.
Definition: MetaLogEntity.h:81
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Base class for MetaLog entities.
Definition: MetaLogEntity.h:62
Recover entity used for sanity checking.
uint8_t encoding_version() const override
Returns encoding version.
Logging routines and macros.
Writes a MetaLog.
Definition: MetaLogWriter.h:66
void display(std::ostream &os)
Display human-readable representation of header to an ostream.
bool marked_for_removal()
Checks if entity is marked for removal.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
Declarations for Serializable.
void lock()
Locks the entity's mutex.
Definition: MetaLogEntity.h:97
Hypertable definitions
virtual void decode(const uint8_t **bufp, size_t *remainp, uint16_t definition_version)
Decodes serialized entity state.
Definition: MetaLogEntity.h:91
virtual const std::string name()=0
Returns the name of the entity.
Mixin class that provides a standard serialization interface.
Definition: Serializable.h:65
virtual void display(std::ostream &os)
Prints a textual representation of the entity state to an ostream.
int64_t id() const
Returns the entity ID.
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Reads serialized representation of object from a buffer.
void encode_entry(uint8_t **bufp)
Encodes entity header plus serialized state.
size_t encoded_length_internal() const override
Returns internal serialized length.
const std::string name() override
Returns the name of the entity.
Entity(int32_t type)
Constructor from entity type.
int32_t checksum
Checksum of serialized entity state
void unlock()
Unlocks the entity's mutex.
Declarations for MetaLog::EntityHeader.
std::ostream & operator<<(std::ostream &os, Entity &entity)
ostream shift function for Entity objects.