0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Future.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; 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 Hypertable. If not, see <http://www.gnu.org/licenses/>
18  */
19 
20 #ifndef Hypertable_Lib_Future_h
21 #define Hypertable_Lib_Future_h
22 
23 #include "ResultCallback.h"
24 #include "Result.h"
25 
26 #include <list>
27 #include <map>
28 #include <memory>
29 
30 namespace Hypertable {
31 
32  using namespace std;
33 
34  class Future : public ResultCallback {
35  public:
36 
43  Future(size_t capacity=0) : m_capacity(capacity) { }
44 
45  virtual ~Future() { cancel(); }
46 
52  bool get(ResultPtr &result);
53 
61  bool get(ResultPtr &result, uint32_t timeout_ms, bool &timed_out);
62 
68  void cancel();
69 
70  void register_scanner(TableScannerAsync *scanner);
71 
72  void deregister_scanner(TableScannerAsync *scanner);
73 
74  void register_mutator(TableMutatorAsync *scanner);
75 
76  void deregister_mutator(TableMutatorAsync *scanner);
77 
81  bool is_full() {
82  std::lock_guard<std::mutex> lock(m_outstanding_mutex);
83  return !has_remaining_capacity();
84  }
85 
89  bool is_empty() {
90  std::lock_guard<std::mutex> lock(m_outstanding_mutex);
91  return _is_empty();
92  }
93 
97  bool is_cancelled() {
98  std::lock_guard<std::mutex> lock(m_outstanding_mutex);
99  return _is_cancelled();
100  }
101 
106  return !is_done();
107  }
108 
109  private:
110  friend class TableScannerAsync;
111  friend class TableMutator;
112  typedef list<ResultPtr> ResultQueue;
113 
114  void scan_ok(TableScannerAsync *scanner, ScanCellsPtr &cells);
115  void scan_error(TableScannerAsync *scanner, int error, const std::string &error_msg,
116  bool eos);
117  void update_ok(TableMutatorAsync *mutator);
118  void update_error(TableMutatorAsync *mutator, int error, FailedMutations &failures);
119 
120  size_t memory_used() {
121  return m_memory_used;
122  }
123 
125  if (!m_capacity)
126  // unbounded buffer
127  return true;
128  else
129  return m_memory_used < m_capacity;
130  }
131 
132  bool _is_empty() { return m_queue.empty(); }
133  bool _is_cancelled() const {
134  return m_cancelled;
135  }
136 
137  bool _is_done() {
138  return m_outstanding == 0;
139  }
140 
141  void enqueue(ResultPtr &result);
142 
143  ResultQueue m_queue;
144  size_t m_capacity {};
145  size_t m_memory_used {};
146  bool m_cancelled {};
147  typedef map<uint64_t, TableScannerAsync *> ScannerMap;
148  typedef map<uint64_t, TableMutatorAsync *> MutatorMap;
149  ScannerMap m_scanner_map;
150  MutatorMap m_mutator_map;
151  };
152  typedef std::shared_ptr<Future> FuturePtr;
153 }
154 
155 #endif // Hypertable_Lib_Future_h
bool _is_cancelled() const
Definition: Future.h:133
ResultQueue m_queue
Definition: Future.h:143
size_t memory_used()
Definition: Future.h:120
bool is_cancelled()
Checks whether the Future object has been cancelled.
Definition: Future.h:97
Asynchronous table scanner.
STL namespace.
std::shared_ptr< Result > ResultPtr
Smart pointer to Result.
Definition: Result.h:72
bool _is_empty()
Definition: Future.h:132
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
bool _is_done()
Definition: Future.h:137
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
Definition: TableMutator.h:55
std::shared_ptr< ScanCells > ScanCellsPtr
Smart pointer to ScanCells.
Definition: ScanCells.h:143
map< uint64_t, TableScannerAsync * > ScannerMap
Definition: Future.h:147
bool is_empty()
Checks whether the Future result queue is empty.
Definition: Future.h:89
list< ResultPtr > ResultQueue
Definition: Future.h:112
Hypertable definitions
map< uint64_t, TableMutatorAsync * > MutatorMap
Definition: Future.h:148
bool has_outstanding()
Checks whether there are any outstanding operations.
Definition: Future.h:105
std::shared_ptr< Future > FuturePtr
Definition: Future.h:152
Future(size_t capacity=0)
Future objects are used to access results from asynchronous scanners/mutators.
Definition: Future.h:43
MutatorMap m_mutator_map
Definition: Future.h:150
virtual ~Future()
Definition: Future.h:45
bool has_remaining_capacity()
Definition: Future.h:124
ScannerMap m_scanner_map
Definition: Future.h:149
Represents an open table.
bool is_full()
Checks whether the Future result queue is full.
Definition: Future.h:81
std::vector< FailedMutation > FailedMutations
Definition: Cells.h:39