0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ReplayPlan.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; version 2 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 
27 
28 #include <Common/Compat.h>
29 
30 #include "ReplayPlan.h"
31 
32 #include <Common/Serialization.h>
33 
34 using namespace std;
35 
36 namespace Hypertable {
37 namespace Lib {
38 namespace RangeServerRecovery {
39 
40 void ReplayPlan::insert(int32_t fragment, const String &location) {
41  FragmentReplayPlan entry(location, fragment);
42  auto &fragment_index = container.get<FragmentReplayPlanById>();
43  auto fragment_it = fragment_index.find(entry.fragment);
44  if (fragment_it != fragment_index.end())
45  container.erase(fragment_it);
46  container.insert(entry);
47 }
48 
49 void ReplayPlan::remove_location(const String &location) {
50  auto &location_index = container.get<FragmentReplayPlanByLocation>();
51  pair<FragmentReplayPlanLocationIndex::const_iterator, FragmentReplayPlanLocationIndex::const_iterator> bounds =
52  location_index.equal_range(location);
53  auto iter = bounds.first;
54  while (iter != bounds.second)
55  iter = location_index.erase(iter);
56 }
57 
58 void ReplayPlan::get_fragments(vector<int32_t> &fragments) const {
59  auto &fragment_index = container.get<FragmentReplayPlanById>();
60  for (auto & entry : fragment_index)
61  fragments.push_back(entry.fragment);
62 }
63 
64 void ReplayPlan::get_fragments(const String &location,
65  vector<int32_t> &fragments) const {
66  auto &location_index = container.get<FragmentReplayPlanByLocation>();
67  pair<FragmentReplayPlanLocationIndex::const_iterator, FragmentReplayPlanLocationIndex::const_iterator> bounds =
68  location_index.equal_range(location);
69  for (auto iter = bounds.first; iter != bounds.second; ++iter)
70  fragments.push_back(iter->fragment);
71 }
72 
73 void ReplayPlan::get_locations(StringSet &locations) const {
74  auto &location_index = container.get<FragmentReplayPlanByLocation>();
75  String last_location;
76  for (auto & entry : location_index) {
77  if (entry.location != last_location) {
78  last_location = entry.location;
79  locations.insert(last_location);
80  }
81  }
82 }
83 
84 bool ReplayPlan::get_location(int32_t fragment, String &location) const {
85  auto &fragment_index = container.get<FragmentReplayPlanById>();
86  auto fragment_it = fragment_index.find(fragment);
87  bool found = false;
88  if (fragment_it != fragment_index.end()) {
89  location = fragment_it->location;
90  found = true;
91  }
92  return found;
93 }
94 
95 uint8_t ReplayPlan::encoding_version() const {
96  return 1;
97 }
98 
99 size_t ReplayPlan::encoded_length_internal() const {
100  size_t length = 4;
101  auto &location_index = container.get<FragmentReplayPlanByLocation>();
102  for (auto & entry : location_index)
103  length += entry.encoded_length();
104  return length;
105 }
106 
126 void ReplayPlan::encode_internal(uint8_t **bufp) const {
127  auto &location_index = container.get<FragmentReplayPlanByLocation>();
128  Serialization::encode_i32(bufp, location_index.size());
129  for (auto & entry : location_index)
130  entry.encode(bufp);
131 }
132 
133 void ReplayPlan::decode_internal(uint8_t version, const uint8_t **bufp,
134  size_t *remainp) {
135  size_t count = Serialization::decode_i32(bufp, remainp);
136  for (size_t i=0; i<count; ++i) {
137  FragmentReplayPlan entry;
138  entry.decode(bufp, remainp);
139  container.insert(entry);
140  }
141 }
142 
143 }}}
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
STL namespace.
Declarations for ReplayPlan.
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
Functions to serialize/deserialize primitives to/from a memory buffer.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
Hypertable definitions