0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ResultCallback.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_ResultCallback_h
23 #define Hypertable_Lib_ResultCallback_h
24 
25 #include "ClientObject.h"
26 #include "ScanCells.h"
27 
28 #include <condition_variable>
29 #include <map>
30 #include <memory>
31 #include <mutex>
32 #include <vector>
33 
34 namespace Hypertable {
35 
36  using namespace Lib;
37 
38  class TableScannerAsync;
39  class TableMutatorAsync;
40 
43  class ResultCallback : public ClientObject {
44 
45  public:
46 
48 
49  virtual ~ResultCallback() {
50  wait_for_completion();
51  }
52 
56  virtual void register_scanner(TableScannerAsync *scanner) { }
57 
61  virtual void deregister_scanner(TableScannerAsync *scanner) { }
62 
66  virtual void register_mutator(TableMutatorAsync *mutator) { }
67 
71  virtual void deregister_mutator(TableMutatorAsync *mutator) { }
72 
77  virtual void completed() { }
78 
85  virtual void scan_ok(TableScannerAsync *scanner, ScanCellsPtr &cells)=0;
86 
95  virtual void scan_error(TableScannerAsync *scanner, int error, const std::string &error_msg,
96  bool eos)=0;
97 
103  virtual void update_ok(TableMutatorAsync *mutator)=0;
104 
112  virtual void update_error(TableMutatorAsync *mutator, int error, FailedMutations &failures)=0;
113 
118  std::unique_lock<std::mutex> lock(m_outstanding_mutex);
119  m_outstanding_cond.wait(lock, [this](){ return m_outstanding == 0; });
120  }
121 
126  std::lock_guard<std::mutex> lock(m_outstanding_mutex);
127  m_outstanding++;
128  }
129 
134  std::lock_guard<std::mutex> lock(m_outstanding_mutex);
135  HT_ASSERT(m_outstanding > 0);
136  m_outstanding--;
137  if (m_outstanding == 0) {
138  completed();
139  m_outstanding_cond.notify_all();
140  }
141  }
142 
146  bool is_done() {
147  std::lock_guard<std::mutex> lock(m_outstanding_mutex);
148  return m_outstanding == 0;
149  }
150 
151  protected:
152  int m_outstanding {};
154  std::condition_variable m_outstanding_cond;
155  };
156 
158  typedef std::shared_ptr<ResultCallback> ResultCallbackPtr;
159 
160 }
161 
162 #endif // Hypertable_Lib_ResultCallback_h
static std::mutex mutex
Definition: Logger.cc:43
Asynchronous table scanner.
virtual void register_mutator(TableMutatorAsync *mutator)
Hook for derived classes which want to keep track of scanners/mutators.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
std::shared_ptr< ScanCells > ScanCellsPtr
Smart pointer to ScanCells.
Definition: ScanCells.h:143
Declarations for ClientObject.
virtual void register_scanner(TableScannerAsync *scanner)
Hook for derived classes which want to keep track of scanners/mutators.
virtual void deregister_mutator(TableMutatorAsync *mutator)
Hook for derived classes which want to keep track of scanners/mutators.
Base class for Hypertable client objects.
Definition: ClientObject.h:44
Hypertable definitions
virtual void completed()
Callback method for completion, default one does nothing.
std::shared_ptr< ResultCallback > ResultCallbackPtr
Shared smart pointer to ResultCallback.
std::condition_variable m_outstanding_cond
Represents an open table.
virtual void deregister_scanner(TableScannerAsync *scanner)
Hook for derived classes which want to keep track of scanners/mutators.
void wait_for_completion()
Blocks till outstanding == 0.
std::vector< FailedMutation > FailedMutations
Definition: Cells.h:39