0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Cells.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 
22 #ifndef Hypertable_Lib_Cells_h
23 #define Hypertable_Lib_Cells_h
24 
25 #include "Cell.h"
26 
28 #include <Common/StringExt.h>
29 
30 #include <memory>
31 #include <vector>
32 #include <set>
33 
34 namespace Hypertable {
35 
37  typedef std::vector<Cell, CellAlloc> Cells;
38  typedef std::pair<Cell, int> FailedMutation;
39  typedef std::vector<FailedMutation> FailedMutations;
40 
41  class CellsBuilder {
42  public:
43  CellsBuilder(size_t size_hint = 128)
44  : m_size_hint(size_hint) {
45  // initialization in the initializer list it will fail on dtor
48  m_cells.reserve(size_hint);
49  }
50 
52  m_str_set.clear();
53  m_cells.clear();
54  m_arena.free();
55  }
56 
57  size_t size() const {
58  return m_cells.size();
59  }
60 
61  size_t memory_used() const {
62  return m_arena.used();
63  }
64 
65  void get_cell(Cell &cc, size_t ii) {
66  cc = m_cells[ii];
67  }
68 
69  void add(const Cell &cell, bool own = true) {
70  if (!own) {
71  m_cells.push_back(cell);
72  return;
73  }
74 
75  Cell copy;
76 
77  if (cell.row_key)
78  copy.row_key = m_arena.dup(cell.row_key);
79 
80  if (cell.column_family)
82 
83  if (cell.column_qualifier)
85 
86  copy.timestamp = cell.timestamp;
87  copy.revision = cell.revision;
88 
89  if (cell.value) {
90  copy.value = (uint8_t *)m_arena.dup(cell.value, cell.value_len);
91  copy.value_len = cell.value_len;
92  }
93  copy.flag = cell.flag;
94  m_cells.push_back(copy);
95  }
96 
97  void get(Cells &cells) { cells = m_cells; }
98  Cells &get() { return m_cells; }
99  const Cells &get() const { return m_cells; }
100 
101  void clear() {
102  m_str_set.clear();
103  m_cells.clear();
104  m_arena.free();
105  m_cells.reserve(m_size_hint);
106  }
107 
108  void copy_failed_mutations(const FailedMutations &src, FailedMutations &dst) {
109  clear();
110  dst.clear();
111  size_t ii=0;
112  for (const auto &v : src) {
113  add(v.first);
114  dst.push_back(std::make_pair(m_cells[ii], v.second));
115  ++ii;
116  }
117  }
118  protected:
120  typedef std::set<const char*, LtCstr, CstrSetAlloc> CstrSet;
121 
123  Cells m_cells;
124  CstrSet m_str_set;
125  size_t m_size_hint;
126 
127  inline const char* get_or_add(const char* s) {
128  CstrSet::iterator it = m_str_set.find(s);
129  if (it == m_str_set.end()) {
130  it = m_str_set.insert(m_arena.dup(s)).first;
131  }
132  return *it;
133  }
134  };
135 
137  typedef std::shared_ptr<CellsBuilder> CellsBuilderPtr;
138 
139 }
140 
141 #endif // Hypertable_Lib_Cells_h
std::vector< Cell, CellAlloc > Cells
Definition: Cells.h:37
std::pair< Cell, int > FailedMutation
Definition: Cells.h:38
const char * column_qualifier
Definition: Cell.h:68
void free()
Free the whole arena.
Definition: PageArena.h:312
PageArena memory allocator for STL classes.
The PageArenaAllocator is a STL allocator based on PageArena.
void get_cell(Cell &cc, size_t ii)
Definition: Cells.h:65
CellsBuilder(size_t size_hint=128)
Definition: Cells.h:43
size_t size() const
Definition: Cells.h:57
size_t memory_used() const
Definition: Cells.h:61
CharT * dup(const CharT *s)
Duplicate a null terminated string; memory is allocated from the pool.
Definition: PageArena.h:274
uint64_t revision
Definition: Cell.h:70
void copy_failed_mutations(const FailedMutations &src, FailedMutations &dst)
Definition: Cells.h:108
The PageArena allocator is simple and fast, avoiding individual mallocs/frees.
Definition: PageArena.h:69
const char * row_key
Definition: Cell.h:66
std::shared_ptr< CellsBuilder > CellsBuilderPtr
Smart pointer to CellsBuilder.
Definition: Cells.h:137
Hypertable definitions
size_t used() const
Statistic accessors - returns used bytes.
Definition: PageArena.h:351
void copy(TableDumper &, CellsBuilder &)
Definition: TableDumper.cc:129
PageArenaAllocator< Cell > CellAlloc
Definition: Cells.h:36
const char * column_family
Definition: Cell.h:67
std::set< const char *, LtCstr, CstrSetAlloc > CstrSet
Definition: Cells.h:120
uint32_t value_len
Definition: Cell.h:72
PageArenaAllocator< const char * > CstrSetAlloc
Definition: Cells.h:119
uint8_t flag
Definition: Cell.h:73
void add(const Cell &cell, bool own=true)
Definition: Cells.h:69
Encapsulates decomposed key and value.
Definition: Cell.h:32
String extensions and helpers: sets, maps, append operators etc.
const uint8_t * value
Definition: Cell.h:71
std::vector< FailedMutation > FailedMutations
Definition: Cells.h:39
const char * get_or_add(const char *s)
Definition: Cells.h:127
int64_t timestamp
Definition: Cell.h:69