0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
IndexTables.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 
22 #include <Common/Compat.h>
23 #include "IndexTables.h"
24 
25 #include <Hypertable/Lib/KeySpec.h>
27 
28 #include <Common/StaticBuffer.h>
29 
30 #include <cstdio>
31 
32 using namespace Hypertable;
33 
34 void IndexTables::add(const Key &key, uint8_t flag,
35  const void *value, uint32_t value_len,
36  TableMutatorAsync *value_index_mutator,
37  TableMutatorAsync *qualifier_index_mutator) {
38 
39  if (value_index_mutator == 0 && qualifier_index_mutator == 0)
40  return;
41 
42  // now create the key for the index
43  KeySpec k;
44  k.timestamp = key.timestamp;
45  k.flag = flag;
46  k.column_family = "v1";
47 
48  // every \t in the original row key gets escaped
49  const char *escaped_row;
50  const char *escaped_qualifier;
51  size_t escaped_row_len;
52  size_t escaped_qualifier_len;
53  LoadDataEscape escape_row;
54  LoadDataEscape escape_qualifier;
55  LoadDataEscape escape_value;
56 
57  escape_row.escape(key.row, key.row_len, &escaped_row, &escaped_row_len);
58  escape_qualifier.escape(key.column_qualifier, key.column_qualifier_len,
59  &escaped_qualifier, &escaped_qualifier_len);
60 
61  // in a normal (non-qualifier) index the format of the new row
62  // key is "<value>\t<qualifier>\t<row>"
63  //
64  // if value has a 0 byte then we also have to escape it
65  if (value_index_mutator) {
66  size_t escaped_value_len;
67  const char *escaped_value;
68  escape_value.escape((const char *)value, (size_t)value_len, &escaped_value, &escaped_value_len);
69  //std::cout << escaped_value << std::endl;
70  StaticBuffer sb(4 + escaped_value_len + escaped_qualifier_len + escaped_row_len + 3);
71  char *p = (char *)sb.base;
73  p = tmp.append_to(p);
74  *p++ = ',';
75  memcpy(p, escaped_value, escaped_value_len);
76  p += escaped_value_len;
77  *p++ = '\t';
78  memcpy(p, escaped_qualifier, escaped_qualifier_len);
79  p += escaped_qualifier_len;
80  *p++ = '\t';
81  memcpy(p, escaped_row, escaped_row_len);
82  p += escaped_row_len;
83  *p++ = '\0';
84  k.row = sb.base;
85  k.row_len = p - 1 - (const char *)sb.base; /* w/o the terminating zero */
86 
87  // and insert it
88  value_index_mutator->set(k, 0, 0);
89  }
90 
91  // in a qualifier index the format of the new row key is "qualifier\trow"
92  if (qualifier_index_mutator) {
93  StaticBuffer sb(4 + key.column_qualifier_len + escaped_row_len + 2);
94  char *p = (char *)sb.base;
96  p = tmp.append_to(p);
97  *p++ = ',';
98  memcpy(p, escaped_qualifier, escaped_qualifier_len);
99  p += escaped_qualifier_len;
100  *p++ = '\t';
101  memcpy(p, escaped_row, escaped_row_len);
102  p += escaped_row_len;
103  *p++ = '\0';
104  k.row = sb.base;
105  k.row_len = p - 1 - (const char *)sb.base; /* w/o the terminating zero */
106 
107  // and insert it
108  qualifier_index_mutator->set(k, 0, 0);
109  }
110 }
int64_t timestamp
Definition: KeySpec.h:130
A memory buffer of static size.
Definition: StaticBuffer.h:45
int64_t timestamp
Definition: Key.h:134
const char * row
Definition: Key.h:129
void set(const KeySpec &key, const void *value, uint32_t value_len)
Inserts a cell into the table.
bool escape(const char *in_buf, size_t in_len, const char **out_bufp, size_t *out_lenp)
const void * row
Definition: KeySpec.h:125
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
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
uint32_t row_len
Definition: Key.h:131
Compatibility Macros for C/C++.
A memory buffer of static size.
Hypertable definitions
Provides access to internal components of opaque key.
Definition: Key.h:40
uint32_t column_qualifier_len
Definition: Key.h:132
char * append_to(char *p) const
Appends the converted number to the buffer specified, returns the forwarded pointer.
Definition: String.h:180
uint8_t column_family_code
Definition: Key.h:127
const char * column_qualifier
Definition: Key.h:130
const char * column_family
Definition: KeySpec.h:127