0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MetaLogEntityHeader.cc
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 #include <Common/Compat.h>
29 
30 #include "MetaLogEntity.h"
31 
32 #include <Common/Serialization.h>
33 #include <Common/Time.h>
34 
35 #include <ctime>
36 #include <string>
37 
38 using namespace Hypertable;
39 using namespace Hypertable::MetaLog;
40 using namespace std;
41 
42 int64_t EntityHeader::ms_next_id = 1;
45 
46 
48 
49 EntityHeader::EntityHeader(int32_t type_) : type(type_) {
50  {
51  lock_guard<mutex> lock(ms_mutex);
52  id = ms_next_id++;
53  }
54  timestamp = get_ts64();
55 }
56 
58  type = other.type;
59  checksum = other.checksum;
60  length = other.length;
61  id = other.id;
62  timestamp = other.timestamp;
63  flags = other.flags;
64  {
65  lock_guard<mutex> lock(ms_mutex);
66  if (ms_next_id <= other.id)
67  ms_next_id = other.id + 1;
68  }
69 }
70 
71 bool EntityHeader::operator<(const EntityHeader &other) const {
72  if (id == other.id)
73  return false;
74  else if (timestamp < other.timestamp)
75  return true;
76  else if (timestamp == other.timestamp && id < other.id)
77  return true;
78  return false;
79 }
80 
81 
82 void EntityHeader::encode(uint8_t **bufp) const {
87  Serialization::encode_i64(bufp, id);
89 }
90 
91 void EntityHeader::decode(const uint8_t **bufp, size_t *remainp) {
92  type = Serialization::decode_i32(bufp, remainp);
93  checksum = Serialization::decode_i32(bufp, remainp);
94  length = Serialization::decode_i32(bufp, remainp);
95  flags = Serialization::decode_i32(bufp, remainp);
96  id = Serialization::decode_i64(bufp, remainp);
97  timestamp = Serialization::decode_i64(bufp, remainp);
98 }
99 
100 
101 void EntityHeader::display(std::ostream &os) {
102  os << "type=" << type;
103  os << ",checksum=" << checksum;
104  os << ",id=" << id;
105  if (display_timestamp) {
106  time_t t = (time_t)(timestamp / 1000000000LL);
107  char buf[32];
108  struct tm tmval;
109  string timestr(asctime_r(gmtime_r(&t, &tmval), buf));
110  boost::trim_if(timestr, boost::is_any_of(" \t\n"));
111  os << ",timestamp=" << timestr;
112  }
113  os << ",flags=" << flags;
114  os << ",length=" << length;
115 }
static std::mutex ms_mutex
Mutex for serializing access to ms_next_id
static std::mutex mutex
Definition: Logger.cc:43
int32_t flags
Flags (either FLAG_REMOVE or 0)
int32_t length
Length of entity header plus serialized state.
void encode(uint8_t **bufp) const
Encodes (serializes) header to a buffer.
int64_t id
Unique ID of entity.
STL namespace.
bool operator<(const EntityHeader &other) const
Less than operator for comparing this header with another.
int64_t timestamp
Creation timestmp of entity header.
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
static int64_t ms_next_id
Global counter for generating unique entity IDs
int32_t type
Entity type defined within the context of a Definition
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Declarations for MetaLog::Entity.
Compatibility Macros for C/C++.
void encode_i64(uint8_t **bufp, uint64_t val)
Encode a 64-bit integer in little-endian order.
Functions to serialize/deserialize primitives to/from a memory buffer.
void display(std::ostream &os)
Display human-readable representation of header to an ostream.
Time related declarations.
Hypertable definitions
static bool display_timestamp
Controls whether or not timestamp is printed by display()
int32_t checksum
Checksum of serialized entity state
void decode(const uint8_t **bufp, size_t *remainp)
Decodes serialzed header from buffer.
int64_t get_ts64()
Returns the current time in nanoseconds as a 64bit number.
Definition: Time.cc:40
MetaLog framework.
Definition: MetaLog.h:44