0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BlockHeaderCommitLog.cc
Go to the documentation of this file.
1 /*
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 "BlockHeaderCommitLog.h"
31 
32 #include <Common/Error.h>
33 #include <Common/Logger.h>
34 #include <Common/Serialization.h>
35 
36 using namespace Hypertable;
37 using namespace Serialization;
38 
39 namespace {
40  const size_t EncodedLengths[BlockHeaderCommitLog::LatestVersion+1] = { 8, 16 };
41  const uint16_t BaseVersions[BlockHeaderCommitLog::LatestVersion+1] = { 0, 1 };
42 }
43 
45 
47  BlockHeader(BaseVersions[version]), m_revision(0), m_cluster_id(0),
48  m_version(version) {
49  HT_ASSERT(version <= LatestVersion);
50 }
51 
52 BlockHeaderCommitLog::BlockHeaderCommitLog(const char *magic,int64_t revision,
53  uint64_t cluster_id) :
54  BlockHeader(BaseVersions[LatestVersion], magic), m_revision(revision),
55  m_cluster_id(cluster_id), m_version(LatestVersion) {
56 }
57 
58 
60  return BlockHeader::encoded_length() + EncodedLengths[m_version];
61 }
62 
63 
64 void BlockHeaderCommitLog::encode(uint8_t **bufp) {
65  uint8_t *base = *bufp;
66  BlockHeader::encode(bufp);
67  encode_i64(bufp, m_revision);
68  if (m_version == 0)
69  *bufp += 2;
70  else
71  encode_i64(bufp, m_cluster_id);
73 }
74 
75 
76 void BlockHeaderCommitLog::decode(const uint8_t **bufp,
77  size_t *remainp) {
78 
79  BlockHeader::decode(bufp, remainp);
80  m_revision = decode_i64(bufp, remainp);
81 
82  // If version 0, skip old checksum field
83  if (m_version == 0) {
84  *bufp += 2;
85  *remainp -= 2;
86  return;
87  }
88 
89  m_cluster_id = decode_i64(bufp, remainp);
90 }
91 
92 
94  if (static_cast<const BlockHeader &>(*this) == other &&
95  m_version == other.m_version &&
96  m_revision == other.m_revision) {
97  if (m_version > 0)
98  return m_cluster_id == other.m_cluster_id;
99  return true;
100  }
101  return false;
102 }
virtual size_t encoded_length()
Returns length of serizlized block header.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Decodes commit log block header from memory location.
int64_t m_revision
Revision number of the most recent cell found in the block.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
BlockHeaderCommitLog(uint16_t version=LatestVersion)
Constructor with version number.
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
virtual void encode(uint8_t **bufp)
Encodes commit log block header to memory location.
Logging routines and macros.
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.
Declarations for BlockHeaderCommitLog.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Decodes serialized block header.
Definition: BlockHeader.cc:104
uint64_t m_cluster_id
Originating cluster ID.
Hypertable definitions
virtual size_t encoded_length()
Returns length of serizlized block header.
Definition: BlockHeader.cc:76
bool equals(const BlockHeaderCommitLog &other) const
Equality test.
void write_header_checksum(uint8_t *base)
Computes and writes checksum field.
Definition: BlockHeader.cc:62
Base class for block headers.
Definition: BlockHeader.h:48
Error codes, Exception handling, error logging.
uint16_t m_version
Serialization format version number
virtual void encode(uint8_t **bufp)
Encodes serialized representation of block header.
Definition: BlockHeader.cc:82