0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Unique.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; either version 3
9  * of the 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 
24 #include "Unique.h"
25 
26 #include <Hypertable/Lib/Schema.h>
29 
30 namespace Hypertable { namespace HyperAppHelper {
31 
32 void
33 create_cell_unique(const TablePtr &table, const KeySpec &key,
34  String &guid)
35 {
36  ColumnFamilySpec *cf_spec;
37 
38  // check the schema - if TIME_ORDER is not descending then this function
39  // will not work
40  cf_spec=table->schema()->get_column_family(key.column_family);
41  if (!cf_spec)
43  "Unknown column family");
44  if (!cf_spec->get_option_time_order_desc())
46  "Column family is not chronological (use TIME_ORDER DESC)");
47 
48  // if the cell has no value: assign a new guid
49  if (guid.empty())
50  guid=generate_guid();
51 
52  // now insert the cell with a regular mutator
53  {
54  TableMutatorPtr mutator( table->create_mutator() );
55  mutator->set(key, guid);
56  mutator->flush();
57  }
58 
59  // and check if it was really inserted or if another client was faster
60  {
61  ScanSpecBuilder ssb;
62  ssb.add_row((const char *)key.row);
63  ssb.add_column(key.column_family);
64  ssb.set_cell_limit(1);
65  TableScannerPtr scanner( table->create_scanner(ssb.get()) );
66  Cell c;
67  if (!scanner->next(c)) {
69  "Inserted GUID was not found");
70  }
71  if (c.value_len!=guid.size() || memcmp(c.value, guid.c_str(), c.value_len)) {
73  "The inserted cell already exists and is not unique");
74  }
75  }
76 }
77 
78 }} // namespace HyperAppHelper, HyperTable
ScanSpec & get()
Returns the built ScanSpec object.
Definition: ScanSpec.h:566
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
bool get_option_time_order_desc() const
Gets time order desc option.
Column family specification.
void add_row(const string &str)
Adds a row to be returned in the scan.
Definition: ScanSpec.h:441
const void * row
Definition: KeySpec.h:125
std::shared_ptr< TableScanner > TableScannerPtr
Smart pointer to TableScanner.
Definition: TableScanner.h:124
Declarations for Schema.
String generate_guid()
Generates a new GUID.
Definition: Unique.h:44
std::shared_ptr< TableMutator > TableMutatorPtr
Smart pointer to TableMutator.
Definition: TableMutator.h:257
void set_cell_limit(int32_t n)
Sets the maximum number of cells to return.
Definition: ScanSpec.h:349
void create_cell_unique(const TablePtr &table, const KeySpec &key, String &guid)
Inserts a unique value into a table.
Definition: Unique.cc:33
Compatibility Macros for C/C++.
Helper class for building a ScanSpec.
Definition: ScanSpec.h:318
Hypertable definitions
void add_column(const string &str)
Adds a column family to be returned by the scan.
Definition: ScanSpec.h:408
uint32_t value_len
Definition: Cell.h:72
Encapsulates decomposed key and value.
Definition: Cell.h:32
const char * column_family
Definition: KeySpec.h:127
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
std::shared_ptr< Table > TablePtr
Definition: Table.h:53
const uint8_t * value
Definition: Cell.h:71