LCOV - code coverage report
Current view: top level - src - sortcache.h (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 7 8 87.5 %
Date: 2017-06-21 14:32:49 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2011  Gerd Lorscheid
       3             :  * Copyright (C) 2011-2017  Fulvio Benini
       4             :  *
       5             :  * This file is part of Scid (Shane's Chess Information Database).
       6             :  *
       7             :  * Scid is free software: you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation.
      10             :  *
      11             :  * Scid 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 Scid.  If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : /** @file
      21             :  * Defines the SortCache class, which sorts the games of an @e Index.
      22             :  */
      23             : 
      24             : #ifndef SORTCACHE_H
      25             : #define SORTCACHE_H
      26             : 
      27             : #include "common.h"
      28             : 
      29             : #if !CPP11_SUPPORT
      30             : #define MULTITHREADING_OFF
      31             : #endif
      32             : 
      33             : #ifndef MULTITHREADING_OFF
      34             : #include <atomic>
      35             : using std::atomic_bool;
      36             : #else
      37             : typedef bool atomic_bool;
      38             : #endif
      39             : 
      40             : class HFilter;
      41             : class Index;
      42             : class IndexEntry;
      43             : class NameBase;
      44             : 
      45             : /**
      46             :  * This class sorts games contained into an Index.
      47             :  * Multiple SortCache objects can be created for a single Index, allowing to
      48             :  * simultaneously sort the games by multiple criteria in an independent way.
      49             :  */
      50             : class SortCache {
      51             :         gamenumT nGames_;
      52             :         atomic_bool valid_fullMap_;
      53             :         atomic_bool th_interrupt_;
      54             :         bool partialHash_;
      55             :         gamenumT* fullMap_;
      56             :         void* th_;
      57             :         uint32_t* hash_;
      58             :         const Index* index_;
      59             :         const NameBase* nbase_;
      60             :         char criteria_[32];
      61             :         int refCount_;
      62             : 
      63             :         // Valid fields that can be used to sort the games.
      64             :         enum {
      65             :                 SORTING_date =         'd',
      66             :                 SORTING_year =         'y',
      67             :                 SORTING_event =        'e',
      68             :                 SORTING_site =         's',
      69             :                 SORTING_round =        'n',
      70             :                 SORTING_white =        'w',
      71             :                 SORTING_black =        'b',
      72             :                 SORTING_eco =          'o',
      73             :                 SORTING_result =       'r',
      74             :                 SORTING_moveCount =    'm',
      75             :                 SORTING_avgElo =       'R',
      76             :                 SORTING_country =      'c',
      77             :                 SORTING_deleted =      'D',
      78             :                 SORTING_eventdate =    'E',
      79             :                 SORTING_whiteelo =     'W',
      80             :                 SORTING_blackelo =     'B',
      81             :                 SORTING_commentcount = 'C',
      82             :                 SORTING_varcount =     'V',
      83             :                 SORTING_nagcount =     'A',
      84             :                 SORTING_resultwin =    '1',
      85             :                 SORTING_resultdraw =   '5',
      86             :                 SORTING_resultloss =   '0',
      87             :                 SORTING_rating =       'i',
      88             :                 SORTING_number =       'N',
      89             :                 SORTING_sentinel =    '\0'
      90             :         };
      91             : 
      92             : public:
      93             :         /**
      94             :          * Create a new SortCache object, builds the hash table, and asynchronously
      95             :          * sorts all the games.
      96             :          * @param idx:      valid pointer to an Index object, witch contains the
      97             :          *                  header data of the games to be sorted.
      98             :          * @param nb:       valid pointer to the NameBase corresponding to @e idx.
      99             :          * @param criteria: the list of fields by which games will be ordered.
     100             :          *                  Each field should be followed by '+' to indicate an
     101             :          *                  ascending order or by '-' for a descending order.
     102             :          * @param fullSort: start a background thread to asynchronously sort all
     103             :          *                  games, making subsequent uses of this object faster.
     104             :          * @returns a pointer to the new object in case of success, NULL otherwise.
     105             :          */
     106             :         static SortCache* create(const Index* idx, const NameBase* nb,
     107             :                                  const char* criteria);
     108             :         ~SortCache();
     109             : 
     110             :         /**
     111             :          * Notify the object that a game's header data has changed.
     112             :          * @param gameId: the id of the game whose data has been changed.
     113             :          */
     114             :         void checkForChanges(gamenumT gameId);
     115             : 
     116             :         /**
     117             :          * Interrupt any asynchronous operation. This function must be called before
     118             :          * modifying the Index or the NameBase associated with the SortCache.
     119             :          */
     120           0 :         void prepareForChanges() { th_interrupt(); }
     121             : 
     122             :         /**
     123             :          * Retrieve the sorted list of games' ids.
     124             :          * The behavior of this function is similar to the mySQL statement:
     125             :          *     SELECT gameId FROM idx WHERE filter(gameId) != 0 ORDER BY criteria
     126             :          *     LIMIT offset, row_count
     127             :          * @param row_offset:  the offset of the first row to return.
     128             :          *                     The offset of the initial row is 0.
     129             :          * @param row_count:   maximum number of rows to return.
     130             :          * @param filter:      a reference to a valid (!= NULL) HFilter object.
     131             :          *                     Games not included into the filter will be ignored.
     132             :          * @param[out] result: valid pointer to an array where the sorted list of
     133             :          *                     games will be stored (should be able to contain at
     134             :          *                     least @e row_count elements).
     135             :          * @returns the number of games' ids stored into @e result.
     136             :          */
     137             :         size_t select(size_t row_offset, size_t row_count, const HFilter& filter,
     138             :                       gamenumT* result) const;
     139             : 
     140             :         /**
     141             :          * Get the sorted position of a game.
     142             :          * @param gameId: the id of the game.
     143             :          * @param filter: a reference to a valid (!= NULL) HFilter object.
     144             :          *                Games not included into the filter will be ignored,
     145             :          *                and @e gameId must be included into the filter.
     146             :          * @returns the sorted position of @e gameId.
     147             :          */
     148             :         size_t sortedPosition(gamenumT gameId, const HFilter& filter) const;
     149             : 
     150       14559 :         int incrRef(int incr) { return refCount_ += incr; }
     151             : 
     152             : private:
     153             :         SortCache(const Index* idx, const NameBase* nbase);
     154             :         SortCache(const SortCache&);
     155             :         SortCache& operator=(const SortCache&);
     156             : 
     157             :         class CmpLess {
     158             :                 const SortCache* sc_;
     159             :         public:
     160         710 :                 CmpLess(const SortCache* sc) : sc_(sc) {}
     161             :                 bool operator()(gamenumT g1, gamenumT g2) const;
     162             :         };
     163             :         int fullCompare(gamenumT left, gamenumT right) const;
     164             : 
     165             :         uint32_t calcHash(gamenumT gameId);
     166             :         void generateHashCache();
     167             :         void sortAsynchronously();
     168             : 
     169       15133 :         void th_interrupt() {
     170       30266 :                 th_interrupt_ = true;
     171       15133 :                 th_join();
     172       30266 :                 th_interrupt_ = false;
     173       15133 :         }
     174             :         void th_join();
     175             :         void th_sort();
     176             : };
     177             : 
     178             : #endif

Generated by: LCOV version 1.12