Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/index - IndexService.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 47 47 100.0 %
Date: 2012-04-09 Functions: 52 65 80.0 %
Branches: 32 54 59.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the IndexService class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_INDEX_INDEX_SERVICE_H
      23                 :            : #define __SSRC_WSPR_INDEX_INDEX_SERVICE_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers/service/service.h>
      26                 :            : 
      27                 :            : __BEGIN_NS_SSRC_WSPR_INDEX
      28                 :            : 
      29                 :            : using std::string;
      30                 :            : using NS_SSRC_SPREAD::Message;
      31                 :            : using NS_SSRC_WISP_PROTOCOL::MessageInfo;
      32                 :            : using NS_SSRC_WISP_PROTOCOL::GroupMembershipDisable;
      33                 :            : 
      34                 :            : // TODO: If possible, don't make this a service.  Make it a class
      35                 :            : // that gets aggregated.
      36                 :            : 
      37                 :            : /**
      38                 :            :  *
      39                 :            :  */
      40                 :            : template<typename MapType, typename IndexScheme = unsigned int,
      41                 :            :          bool group_membership = GroupMembershipDisable>
      42                 :            : class IndexService : public service::ServiceProtocolProcessor {
      43                 :            :   typedef service::ServiceProtocolProcessor super;
      44                 :            :   friend class NS_SSRC_WISP_SERVICE::ServiceProtocolProcessor<typename super::packing_traits>;
      45                 :            : 
      46                 :            : public:
      47                 :            :   static const bool GroupMembership = group_membership;
      48                 :            : 
      49                 :            :   typedef MapType map_type;
      50                 :            :   typedef IndexScheme index_scheme;
      51                 :            : 
      52                 :            : protected:
      53                 :            : 
      54                 :            :   map_type _index;
      55                 :            : 
      56                 :            :   template<index_scheme scheme, typename ResultTraits, typename QueryType>
      57                 :          5 :   void process_query(const QueryType & msg, const MessageInfo & msginfo)
      58                 :            :   {
      59                 :          5 :     unsigned int i = msg.keys.size();
      60                 :         10 :     typename ResultTraits::parameter_type reply;
      61                 :            : 
      62   [ +  +  +  + ]:         15 :     while(i-- > 0) {
      63                 :            :       std::pair<typename map_type::template nth_index_iterator<scheme>::type,
      64                 :            :                 typename map_type::template nth_index_iterator<scheme>::type>
      65   [ +  -  +  -  :          5 :         range = equal_range<scheme>(msg.keys[i]);
          +  -  +  -  +  
                -  +  - ]
      66                 :            : 
      67   [ +  -  +  +  :         14 :       while(range.first != range.second) {
             +  -  +  + ]
      68   [ +  -  +  -  :          4 :         reply.result.insert(*range.first);
             +  -  +  - ]
      69   [ +  -  +  - ]:          4 :         ++range.first;
      70                 :            :       }
      71                 :            :     }
      72                 :            : 
      73   [ +  -  +  - ]:          5 :     _caller.reply<ResultTraits>(msginfo.sender(), msginfo.token(), reply,
      74                 :            :                                 Message::FIFOSelfDiscard);
      75                 :          5 :   }
      76                 :            : 
      77                 :            :   template<index_scheme scheme, typename SingleResultTraits, typename SingleQueryType>
      78                 :          1 :   void process_single_query(const SingleQueryType & msg,
      79                 :            :                             const MessageInfo & msginfo)
      80                 :            :   {
      81                 :            :     typename map_type::template nth_index_iterator<scheme>::type it =
      82                 :          1 :       find<scheme>(msg.key);
      83                 :            : 
      84   [ -  +  +  - ]:          1 :     _caller.
      85                 :            :       reply<SingleResultTraits>(msginfo.sender(), msginfo.token(),
      86                 :            :       (it == end<scheme>() ?
      87                 :            :        typename SingleResultTraits::parameter_type() :
      88                 :            :        typename SingleResultTraits::parameter_type(*it)),
      89                 :            :                                 Message::FIFOSelfDiscard);
      90                 :          1 :   }
      91                 :            : 
      92                 :            :   template<index_scheme scheme>
      93                 :            :   typename map_type::template nth_index<scheme>::type &
      94                 :         87 :   get_index() {
      95                 :         87 :     return _index.template get<scheme>();
      96                 :            :   }
      97                 :            : 
      98                 :            :   template<index_scheme scheme>
      99                 :            :   const typename map_type::template nth_index<scheme>::type &
     100                 :         66 :   get_index() const {
     101                 :         66 :     return _index.template get<scheme>();
     102                 :            :   }
     103                 :            : 
     104                 :         23 :   virtual void transition(typename super::State state) {
     105                 :         23 :     super::transition(state);
     106         [ +  + ]:         23 :     if(super::state() == super::Stopped)
     107                 :         11 :       _index.clear();
     108                 :         23 :   }
     109                 :            : 
     110                 :         19 :   explicit IndexService(typename super::caller_type & caller) :
     111   [ +  -  +  - ]:         19 :     super(caller)
     112                 :         19 :   { }
     113                 :            : 
     114                 :            : public:
     115                 :            : 
     116                 :            :   /**
     117                 :            :    *
     118                 :            :    */
     119   [ +  -  -  + ]:         19 :   virtual ~IndexService() { }
     120                 :            : 
     121                 :            :   /**
     122                 :            :    *
     123                 :            :    */
     124                 :            :   template<index_scheme scheme>
     125                 :          3 :   bool empty() const {
     126                 :          3 :     return get_index<scheme>().empty();
     127                 :            :   }
     128                 :            : 
     129                 :            :   /**
     130                 :            :    *
     131                 :            :    */
     132                 :            :   template<index_scheme scheme>
     133                 :            :   typename map_type::template nth_index<scheme>::type::size_type
     134                 :         10 :   size() const {
     135                 :         10 :     return get_index<scheme>().size();
     136                 :            :   }
     137                 :            : 
     138                 :            :   /**
     139                 :            :    *
     140                 :            :    */
     141                 :            :   template<index_scheme scheme>
     142                 :            :   typename map_type::template nth_index_iterator<scheme>::type
     143                 :          5 :   begin() {
     144                 :          5 :     return get_index<scheme>().begin();
     145                 :            :   }
     146                 :            : 
     147                 :            :   /**
     148                 :            :    *
     149                 :            :    */
     150                 :            :   template<index_scheme scheme>
     151                 :            :   typename map_type::template nth_index_const_iterator<scheme>::type
     152                 :            :   begin() const {
     153                 :            :     return get_index<scheme>().begin();
     154                 :            :   }
     155                 :            : 
     156                 :            :   /**
     157                 :            :    *
     158                 :            :    */
     159                 :            :   template<index_scheme scheme>
     160                 :            :   typename map_type::template nth_index_iterator<scheme>::type
     161                 :         35 :   end() {
     162                 :         35 :     return get_index<scheme>().end();
     163                 :            :   }
     164                 :            : 
     165                 :            :   /**
     166                 :            :    *
     167                 :            :    */
     168                 :            :   template<index_scheme scheme>
     169                 :            :   typename map_type::template nth_index_const_iterator<scheme>::type
     170                 :            :   end() const {
     171                 :            :     return get_index<scheme>().end();
     172                 :            :   }
     173                 :            : 
     174                 :            :   /**
     175                 :            :    *
     176                 :            :    */
     177                 :            :   template<index_scheme scheme>
     178                 :            :   std::pair<typename map_type::template nth_index_iterator<scheme>::type, bool>
     179                 :         31 :   insert(const typename map_type::template nth_index<scheme>::type::value_type & value)
     180                 :            :   {
     181                 :         31 :     return get_index<scheme>().insert(value);
     182                 :            :   }
     183                 :            : 
     184                 :            :   /**
     185                 :            :    *
     186                 :            :    */
     187                 :            :   template<index_scheme scheme>
     188                 :            :   typename map_type::template nth_index<scheme>::type::size_type
     189                 :          5 :   erase(const typename map_type::template nth_index<scheme>::type::key_type & key)
     190                 :            :   {
     191                 :          5 :     return get_index<scheme>().erase(key);
     192                 :            :   }
     193                 :            : 
     194                 :            :   template<index_scheme scheme>
     195                 :          1 :   bool replace(typename map_type::template nth_index_iterator<scheme>::type position,
     196                 :            :                const typename map_type::template nth_index<scheme>::type::value_type & value)
     197                 :            :   {
     198                 :          1 :     return get_index<scheme>().replace(position, value);
     199                 :            :   }
     200                 :            : 
     201                 :            :   /**
     202                 :            :    *
     203                 :            :    */
     204                 :            :   template<index_scheme scheme, typename CompatibleKey>
     205                 :            :   typename map_type::template nth_index_iterator<scheme>::type
     206                 :         18 :   find(const CompatibleKey & key) const {
     207                 :         18 :     return get_index<scheme>().find(key);
     208                 :            :   }
     209                 :            : 
     210                 :            :   /**
     211                 :            :    *
     212                 :            :    */
     213                 :            :   template<index_scheme scheme, typename CompatibleKey>
     214                 :            :   std::pair<typename map_type::template nth_index_iterator<scheme>::type,
     215                 :            :             typename map_type::template nth_index_iterator<scheme>::type>
     216                 :          5 :   equal_range(const CompatibleKey & key) const {
     217                 :          5 :     return get_index<scheme>().equal_range(key);
     218                 :            :   }
     219                 :            : 
     220                 :            :   /**
     221                 :            :    *
     222                 :            :    */
     223                 :            :   template<index_scheme scheme, typename CompatibleKey>
     224                 :            :   typename map_type::template nth_index<scheme>::type::size_type
     225                 :         30 :   count(const CompatibleKey & key) const {
     226                 :         30 :     return get_index<scheme>().count(key);
     227                 :            :   }
     228                 :            : };
     229                 :            : 
     230                 :            : __END_NS_SSRC_WSPR_INDEX
     231                 :            : 
     232                 :            : #endif