0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FragmentData.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 
26 
27 #include <Common/Compat.h>
28 #include "FragmentData.h"
29 
31 
34 
35 using namespace std;
36 using namespace Hypertable;
37 
38 FragmentData::~FragmentData() {
39  Global::memory_tracker->subtract(m_memory_consumption);
40 }
41 
43  m_data.push_back(event);
44  int64_t memory_added = sizeof(Event) + event->payload_len;
45  Global::memory_tracker->add(memory_added);
46  m_memory_consumption += memory_added;
47  return;
48 }
49 
50 void FragmentData::clear() {
51  m_data.clear();
52  Global::memory_tracker->subtract(m_memory_consumption);
53  m_memory_consumption = 0;
54 }
55 
56 
57 void FragmentData::merge(TableIdentifier &table, RangePtr &range,
58  CommitLogPtr &log) {
59 
60  Key key;
61  SerializedKey serkey;
62  ByteString value;
63  const uint8_t *mod, *mod_end;
64  DynamicBuffer dbuf;
65  int64_t latest_revision;
66  int64_t total_bytes = 0;
67  size_t kv_pairs = 0;
68 
69  // de-serialize all objects
70  for (auto &event : m_data) {
71  const uint8_t *ptr = event->payload;
72  size_t remain = event->payload_len;
74  params.decode(&ptr, &remain);
75 
76  dbuf.clear();
77  dbuf.ensure(table.encoded_length() + remain);
78  table.encode(&dbuf.ptr);
79 
80  latest_revision = TIMESTAMP_MIN;
81 
82  mod = (const uint8_t *)ptr;
83  mod_end = mod + remain;
84 
85  total_bytes += remain;
86 
87  while (mod < mod_end) {
88  serkey.ptr = mod;
89  value.ptr = mod + serkey.length();
90  HT_ASSERT(serkey.ptr <= mod_end && value.ptr <= mod_end);
91  HT_ASSERT(key.load(serkey));
92 
93  if (key.revision > latest_revision)
94  latest_revision = key.revision;
95 
96  range->add(key, value);
97 
98  // skip to next kv pair
99  value.next();
100  dbuf.add_unchecked((const void *)mod, value.ptr-mod);
101  kv_pairs++;
102  mod = value.ptr;
103  }
104 
105  HT_ASSERT(dbuf.ptr-dbuf.base <= (long)dbuf.size);
106 
107  if (remain)
108  log->write(ClusterId::get(), dbuf, latest_revision, Filesystem::Flags::NONE);
109  }
110 
111  HT_INFOF("Just added %d key/value pairs (%lld bytes)",
112  (int)kv_pairs, (Lld)total_bytes);
113 }
Network communication event.
Definition: Event.h:54
Declarations for FragmentData.
Declarations for PhantomUpdate request parameters.
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
uint8_t * ptr
Pointer to the end of the used part of the buffer.
void add(const Key &key, uint8_t flag, const void *value, uint32_t value_len, TableMutatorAsync *value_index_mutator, TableMutatorAsync *qualifier_index_mutator)
Definition: IndexTables.cc:34
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
static const int64_t TIMESTAMP_MIN
Definition: KeySpec.h:34
A class managing one or more serializable ByteStrings.
Definition: ByteString.h:47
#define HT_ASSERT(_e_)
Definition: Logger.h:396
virtual void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: Serializable.cc:64
uint32_t size
The size of the allocated memory buffer (base)
Compatibility Macros for C/C++.
bool load(const SerializedKey &key)
Parses the opaque key and loads the components into the member variables.
Definition: Key.cc:158
T get(const String &name)
Retrieves a configuration value.
Definition: Config.h:82
size_t length() const
Retrieves the length of the serialized string.
Definition: ByteString.h:62
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
const uint8_t * ptr
The pointer to the serialized data.
Definition: ByteString.h:121
Hypertable definitions
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
void clear()
Clears the buffer.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
Provides access to internal components of opaque key.
Definition: Key.h:40
std::shared_ptr< Range > RangePtr
Smart pointer to Range.
Definition: Range.h:404
uint8_t * base
Pointer to the allocated memory buffer.
std::shared_ptr< CommitLog > CommitLogPtr
Smart pointer to CommitLog.
Definition: CommitLog.h:223
int64_t revision
Definition: Key.h:135
Request parameters for phantom update function.
Definition: PhantomUpdate.h:49
void ensure(size_t len)
Ensure space for additional data Will grow the space to 1.5 of the needed space with existing data un...
Definition: DynamicBuffer.h:82
uint8_t * add_unchecked(const void *data, size_t len)
Adds additional data without boundary checks.
Declarations for ClusterId.
uint8_t * next()
Retrieves the next serialized String in the buffer.
Definition: ByteString.h:71