IndexService.h
Go to the documentation of this file.
00001 /* 00002 * Copyright 2006-2009 Savarese Software Research Corporation 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * https://www.savarese.com/software/ApacheLicense-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00022 #ifndef __SSRC_WSPR_INDEX_INDEX_SERVICE_H 00023 #define __SSRC_WSPR_INDEX_INDEX_SERVICE_H 00024 00025 #include <ssrc/wispers/service/service.h> 00026 00027 __BEGIN_NS_SSRC_WSPR_INDEX 00028 00029 using std::string; 00030 using NS_SSRC_SPREAD::Message; 00031 using NS_SSRC_WISP_PROTOCOL::MessageInfo; 00032 using NS_SSRC_WISP_PROTOCOL::GroupMembershipDisable; 00033 00034 // TODO: If possible, don't make this a service. Make it a class 00035 // that gets aggregated. 00036 00040 template<typename MapType, typename IndexScheme = unsigned int, 00041 bool group_membership = GroupMembershipDisable> 00042 class IndexService : public service::ServiceProtocolProcessor { 00043 typedef service::ServiceProtocolProcessor super; 00044 friend class NS_SSRC_WISP_SERVICE::ServiceProtocolProcessor<typename super::packing_traits>; 00045 00046 public: 00047 static const bool GroupMembership = group_membership; 00048 00049 typedef MapType map_type; 00050 typedef IndexScheme index_scheme; 00051 00052 protected: 00053 00054 map_type _index; 00055 00056 template<index_scheme scheme, typename ResultTraits, typename QueryType> 00057 void process_query(const QueryType & msg, const MessageInfo & msginfo) 00058 { 00059 unsigned int i = msg.keys.size(); 00060 typename ResultTraits::parameter_type reply; 00061 00062 while(i-- > 0) { 00063 std::pair<typename map_type::template nth_index_iterator<scheme>::type, 00064 typename map_type::template nth_index_iterator<scheme>::type> 00065 range = equal_range<scheme>(msg.keys[i]); 00066 00067 while(range.first != range.second) { 00068 reply.result.insert(*range.first); 00069 ++range.first; 00070 } 00071 } 00072 00073 _caller.reply<ResultTraits>(msginfo.sender(), msginfo.token(), reply, 00074 Message::FIFOSelfDiscard); 00075 } 00076 00077 template<index_scheme scheme, typename SingleResultTraits, typename SingleQueryType> 00078 void process_single_query(const SingleQueryType & msg, 00079 const MessageInfo & msginfo) 00080 { 00081 typename map_type::template nth_index_iterator<scheme>::type it = 00082 find<scheme>(msg.key); 00083 00084 _caller. 00085 reply<SingleResultTraits>(msginfo.sender(), msginfo.token(), 00086 (it == end<scheme>() ? 00087 typename SingleResultTraits::parameter_type() : 00088 typename SingleResultTraits::parameter_type(*it)), 00089 Message::FIFOSelfDiscard); 00090 } 00091 00092 template<index_scheme scheme> 00093 typename map_type::template nth_index<scheme>::type & 00094 get_index() { 00095 return _index.template get<scheme>(); 00096 } 00097 00098 template<index_scheme scheme> 00099 const typename map_type::template nth_index<scheme>::type & 00100 get_index() const { 00101 return _index.template get<scheme>(); 00102 } 00103 00104 virtual void transition(typename super::State state) { 00105 super::transition(state); 00106 if(super::state() == super::Stopped) 00107 _index.clear(); 00108 } 00109 00110 explicit IndexService(typename super::caller_type & caller) : 00111 super(caller) 00112 { } 00113 00114 public: 00115 00119 virtual ~IndexService() { } 00120 00124 template<index_scheme scheme> 00125 bool empty() const { 00126 return get_index<scheme>().empty(); 00127 } 00128 00132 template<index_scheme scheme> 00133 typename map_type::template nth_index<scheme>::type::size_type 00134 size() const { 00135 return get_index<scheme>().size(); 00136 } 00137 00141 template<index_scheme scheme> 00142 typename map_type::template nth_index_iterator<scheme>::type 00143 begin() { 00144 return get_index<scheme>().begin(); 00145 } 00146 00150 template<index_scheme scheme> 00151 typename map_type::template nth_index_const_iterator<scheme>::type 00152 begin() const { 00153 return get_index<scheme>().begin(); 00154 } 00155 00159 template<index_scheme scheme> 00160 typename map_type::template nth_index_iterator<scheme>::type 00161 end() { 00162 return get_index<scheme>().end(); 00163 } 00164 00168 template<index_scheme scheme> 00169 typename map_type::template nth_index_const_iterator<scheme>::type 00170 end() const { 00171 return get_index<scheme>().end(); 00172 } 00173 00177 template<index_scheme scheme> 00178 std::pair<typename map_type::template nth_index_iterator<scheme>::type, bool> 00179 insert(const typename map_type::template nth_index<scheme>::type::value_type & value) 00180 { 00181 return get_index<scheme>().insert(value); 00182 } 00183 00187 template<index_scheme scheme> 00188 typename map_type::template nth_index<scheme>::type::size_type 00189 erase(const typename map_type::template nth_index<scheme>::type::key_type & key) 00190 { 00191 return get_index<scheme>().erase(key); 00192 } 00193 00194 template<index_scheme scheme> 00195 bool replace(typename map_type::template nth_index_iterator<scheme>::type position, 00196 const typename map_type::template nth_index<scheme>::type::value_type & value) 00197 { 00198 return get_index<scheme>().replace(position, value); 00199 } 00200 00204 template<index_scheme scheme, typename CompatibleKey> 00205 typename map_type::template nth_index_iterator<scheme>::type 00206 find(const CompatibleKey & key) const { 00207 return get_index<scheme>().find(key); 00208 } 00209 00213 template<index_scheme scheme, typename CompatibleKey> 00214 std::pair<typename map_type::template nth_index_iterator<scheme>::type, 00215 typename map_type::template nth_index_iterator<scheme>::type> 00216 equal_range(const CompatibleKey & key) const { 00217 return get_index<scheme>().equal_range(key); 00218 } 00219 00223 template<index_scheme scheme, typename CompatibleKey> 00224 typename map_type::template nth_index<scheme>::type::size_type 00225 count(const CompatibleKey & key) const { 00226 return get_index<scheme>().count(key); 00227 } 00228 }; 00229 00230 __END_NS_SSRC_WSPR_INDEX 00231 00232 #endif
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.