0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LocationCache.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_LocationCache_h
23 #define Hypertable_Lib_LocationCache_h
24 
25 #include "RangeLocationInfo.h"
26 
27 #include <Common/FlyweightString.h>
28 #include <Common/InetAddr.h>
29 #include <Common/StringExt.h>
30 
31 #include <cstring>
32 #include <ostream>
33 #include <map>
34 #include <mutex>
35 #include <set>
36 
37 namespace Hypertable {
38 
43  const char *table_name;
44  const char *end_row;
45  };
46 
50  inline bool operator<(const LocationCacheKey &x, const LocationCacheKey &y) {
51  int cmp = strcmp(x.table_name, y.table_name);
52  if (cmp)
53  return cmp < 0;
54  if (y.end_row == 0)
55  return (x.end_row == 0) ? false : true;
56  else if (x.end_row == 0)
57  return false;
58  return strcmp(x.end_row, y.end_row) < 0;
59  }
60 
64  inline bool operator==(const LocationCacheKey &x, const LocationCacheKey &y) {
65  if (strcmp(x.table_name, y.table_name))
66  return false;
67  if (x.end_row == 0)
68  return (y.end_row == 0) ? true : false;
69  else if (y.end_row == 0)
70  return false;
71  return !strcmp(x.end_row, y.end_row);
72  }
73 
77  inline bool operator!=(const LocationCacheKey &x, const LocationCacheKey &y) {
78  return !(x == y);
79  }
80 
81 
85  class LocationCache {
86  public:
89  struct Value {
90  struct Value *prev, *next;
91  std::map<LocationCacheKey, Value *>::iterator map_iter;
92  std::string start_row;
93  std::string end_row;
95  bool pegged;
96  };
97 
98  LocationCache(uint32_t max_entries) : m_mutex(), m_location_map(),
99  m_head(0), m_tail(0), m_max_entries(max_entries) { return; }
100  ~LocationCache();
101 
102  void insert(const char * table_name, RangeLocationInfo &range_loc_info,
103  bool pegged=false);
104  bool lookup(const char *table_name, const char *rowkey,
105  RangeLocationInfo *range_loc_infop, bool inclusive=false);
106  bool lookup(const char *table_name, const char *rowkey,
107  RangeAddrInfo *range_addr_infop, bool inclusive=false);
108  bool invalidate(const char *table_name, const char *rowkey);
109 
110  void invalidate_host(const std::string &hostname);
111 
112  void display(std::ostream &);
113 
114  private:
115  bool lookup(const char *table_name, const char *rowkey,
116  Value*& cacheval, bool inclusive);
117  void move_to_head(Value *cacheval);
118  void remove(Value *cacheval);
119 
120  const CommAddress *get_constant_address(const CommAddress &addr);
121 
124  bool operator()(const CommAddress *addr1, const CommAddress *addr2) const {
125  return *addr1 < *addr2;
126  }
127  };
128 
129  typedef std::map<LocationCacheKey, Value *> LocationMap;
130  typedef std::set<const CommAddress *, CommAddressPointerLt> AddressSet;
131 
133  LocationMap m_location_map;
134  AddressSet m_addresses;
137  uint32_t m_max_entries;
139  };
140 
142  typedef std::shared_ptr<LocationCache> LocationCachePtr;
143 
144 }
145 
146 
147 #endif // Hypertable_Lib_LocationCache_h
void display(std::ostream &)
static std::mutex mutex
Definition: Logger.cc:43
const CommAddress * get_constant_address(const CommAddress &addr)
void insert(const char *table_name, RangeLocationInfo &range_loc_info, bool pegged=false)
Insert.
This class acts as a cache of Range location information.
Definition: LocationCache.h:85
bool invalidate(const char *table_name, const char *rowkey)
Holds range start and end row plus location.
FlyweightString m_strings
The Flyweight string set stores duplicate strings efficiently.
Holds range location.
std::shared_ptr< LocationCache > LocationCachePtr
Smart pointer to LocationCache.
Flyweight string set.
void move_to_head(Value *cacheval)
MoveToHead.
std::set< const CommAddress *, CommAddressPointerLt > AddressSet
bool operator==(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:112
bool lookup(const char *table_name, const char *rowkey, RangeLocationInfo *range_loc_infop, bool inclusive=false)
Lookup.
bool operator!=(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:122
std::map< LocationCacheKey, Value * > LocationMap
bool operator<(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:128
Hypertable definitions
Key type for Range location cache.
Definition: LocationCache.h:42
STL Strict Weak Ordering for comparing CommAddress pointers.
Internet address wrapper classes and utility functions.
void invalidate_host(const std::string &hostname)
bool operator()(const CommAddress *addr1, const CommAddress *addr2) const
String extensions and helpers: sets, maps, append operators etc.
LocationCache(uint32_t max_entries)
Definition: LocationCache.h:98
Address abstraction to hold either proxy name or IPv4:port address.
Definition: CommAddress.h:52
std::map< LocationCacheKey, Value * >::iterator map_iter
Definition: LocationCache.h:91