0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TableMutatorIntervalHandler.cc
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 
26 #include "Common/Compat.h"
27 #include "Common/md5.h"
28 
29 #include "AsyncComm/Comm.h"
30 
31 #include "HyperAppHelper/Unique.h"
32 
33 #include "TableMutatorShared.h"
36 
37 using namespace Hypertable;
38 
40  ApplicationQueueInterface *app_queue,
41  TableMutatorShared *shared_mutator)
42  : active(true), complete(false), comm(comm), app_queue(app_queue), shared_mutator(shared_mutator) {
43 }
44 
46  char unique_hash[33];
47  uint32_t first_interval;
49 
50  md5_string(str.c_str(), unique_hash);
51 
52  memcpy(&first_interval, unique_hash, 4);
53 
54  first_interval %= shared_mutator->flush_interval();
55 
56  HT_ASSERT(comm->set_timer(first_interval, shared_from_this()) == Error::OK);
57 }
58 
59 
61 
62  if (active) {
63  TableMutatorIntervalHandlerPtr handler = static_pointer_cast<TableMutatorIntervalHandler>(shared_from_this());
64  app_queue->add(new TableMutatorFlushHandler(handler, event));
65  HT_ASSERT(comm->set_timer(shared_mutator->flush_interval(), shared_from_this()) == Error::OK);
66  }
67  else {
68  std::lock_guard<std::mutex> lock(mutex);
69  complete = true;
70  cond.notify_all();
71  }
72 }
73 
74 
76  std::lock_guard<std::mutex> lock(mutex);
77 
78  if (active)
80 }
Declarations for TableMutatorIntervalHandler.
Timer handler for periodically flushing a shared mutator.
virtual void handle(EventPtr &event)
Handles the timer interrupt.
bool active
Set to false to deactivate and prevent further timer interrupts.
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
Comm * comm
Pointer to AsyncComm subsystem.
String generate_guid()
Generates a new GUID.
Definition: Unique.h:44
#define HT_ASSERT(_e_)
Definition: Logger.h:396
std::mutex mutex
Mutex for serializing access to members
Compatibility Macros for C/C++.
std::condition_variable cond
Condition variable signalled when complete is set to true
void md5_string(const char *input, char output[33])
Calculates the hex string of MD5 of null terminated input.
Definition: md5.cc:384
TableMutatorIntervalHandler(Comm *comm, ApplicationQueueInterface *app_queue, TableMutatorShared *shared_mutator)
Constructor.
Hypertable definitions
void interval_flush()
Flush if necessary considering the flush interval.
TableMutatorShared * shared_mutator
Shared mutator to be periodically flushed.
bool complete
Indicates if final timer interrupt has completed.
Entry point to AsyncComm service.
Definition: Comm.h:61
void flush()
Flushes shared mutator shared_mutator.
Declarations for Comm.
int set_timer(uint32_t duration_millis, const DispatchHandlerPtr &handler)
Sets a timer for duration_millis milliseconds in the future.
Definition: Comm.cc:465
A TableMutator that can be shared from multiple threads and incidentally has an option to do periodic...
virtual void add(ApplicationHandler *app_handler)=0
Adds an application handler to queue.
md5 digest routines.
Abstract interface for application queue.
ApplicationQueueInterface * app_queue
Pointer to application queue.
std::shared_ptr< TableMutatorIntervalHandler > TableMutatorIntervalHandlerPtr
Smart pointer to TableMutatorIntervalHandler.