0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CommitLogBase.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_CommitLogBase_h
23 #define Hypertable_Lib_CommitLogBase_h
24 
25 #include "KeySpec.h"
26 #include "CommitLogBlockStream.h"
27 
28 #include <Common/String.h>
29 #include <Common/StringExt.h>
30 
31 #include <boost/algorithm/string.hpp>
32 
33 #include <deque>
34 #include <memory>
35 #include <mutex>
36 
37 namespace Hypertable {
38 
40  public:
43  void verify() {
44  HT_ASSERT(verification == 123456789LL);
45  }
46  bool remove_ok(StringSet &remove_ok_logs) {
47  return parent == 0 || remove_ok_logs.count(log_dir);
48  }
49  std::string to_str(StringSet &remove_ok_logs) {
50  std::string msg = format("FileInfo=(logdir=%s,num=%d,revision=%lld,references=%d,rmOk=%d)",
51  log_dir.c_str(), (int)num, (Lld)revision, (int)references,
52  (int)remove_ok_logs.count(log_dir));
53  if (parent)
54  msg += format(" parent=(logdir=%s,num=%d,revision=%lld,references=%d,rmOk=%d)",
55  parent->log_dir.c_str(), (int)parent->num, (Lld)parent->revision,
56  (int)parent->references, (int)remove_ok_logs.count(parent->log_dir));
57  return msg;
58  }
59  std::string log_dir;
60  uint32_t num {};
61  uint64_t size {};
62  int64_t revision {};
63  int64_t log_dir_hash {};
64  size_t references {};
68  int64_t verification {123456789LL};
69  };
70 
71  struct LtClfip {
73  return x->revision < y->revision;
74  }
75  };
76 
77  typedef std::deque<CommitLogFileInfo *> LogFragmentQueue;
78 
81  class CommitLogBase {
82  public:
83  CommitLogBase(const std::string &log_dir)
86 
87  boost::trim_right_if(m_log_dir, boost::is_any_of("/"));
88 
89  size_t lastslash = m_log_dir.find_last_of('/');
90 
91  m_log_name = (lastslash == String::npos) ? m_log_dir
92  : m_log_dir.substr(lastslash+1);
93  }
94 
99  void stitch_in(CommitLogBase *other) {
100  std::lock_guard<std::mutex>lock(m_mutex);
101  for (LogFragmentQueue::iterator iter = other->m_fragment_queue.begin();
102  iter != other->m_fragment_queue.end(); iter++)
103  m_fragment_queue.push_back(*iter);
104  other->m_fragment_queue.clear();
105  }
106 
107  std::string &get_log_dir() { return m_log_dir; }
108 
110 
111  bool empty() { std::lock_guard<std::mutex>lock(m_mutex); return m_fragment_queue.empty(); }
112 
114 
115  LogFragmentQueue &fragment_queue() { return m_fragment_queue; }
116 
118  while (finfo->parent)
119  finfo = finfo->parent;
120  return finfo->num;
121  }
122 
123  protected:
125  std::string m_log_dir;
126  std::string m_log_name;
127  LogFragmentQueue m_fragment_queue;
129  std::set<int64_t> m_linked_log_hashes;
131  };
132 
133  typedef std::shared_ptr<CommitLogBase> CommitLogBasePtr;
134 
135 }
136 
137 #endif // Hypertable_Lib_CommitLogBase_h
138 
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
void stitch_in(CommitLogBase *other)
This method assumes that the other commit log is not being concurrently used which is why it doesn't ...
Definition: CommitLogBase.h:99
static std::mutex mutex
Definition: Logger.cc:43
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
bool operator()(CommitLogFileInfo *x, CommitLogFileInfo *y) const
Definition: CommitLogBase.h:72
std::string & get_log_dir()
Abstraction for reading a stream of blocks from a commit log file.
static const int64_t TIMESTAMP_MIN
Definition: KeySpec.h:34
Declarations for CommitLogBlockStream.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
LogFragmentQueue & fragment_queue()
CommitLogBase(const std::string &log_dir)
Definition: CommitLogBase.h:83
std::deque< CommitLogFileInfo * > LogFragmentQueue
Definition: CommitLogBase.h:77
std::shared_ptr< CommitLogBase > CommitLogBasePtr
CommitLogBlockStream * block_stream
Definition: CommitLogBase.h:65
Hypertable definitions
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
std::string to_str(StringSet &remove_ok_logs)
Definition: CommitLogBase.h:49
bool remove_ok(StringSet &remove_ok_logs)
Definition: CommitLogBase.h:46
CommitLogFileInfo * parent
Definition: CommitLogBase.h:66
A String class based on std::string.
LogFragmentQueue m_fragment_queue
std::set< int64_t > m_linked_log_hashes
uint32_t toplevel_fragment_id(CommitLogFileInfo *finfo)
String extensions and helpers: sets, maps, append operators etc.