PgnDatabase.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file PgnDatabase.h This file contains the declaration of class pgn::Database.
4 //
5 // Copyright (C) 2010, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef PGNDATABASE_H
25 #define PGNDATABASE_H
26 
27 #ifndef USE_PCH
28 #include <string>
29 #include <glibmm/refptr.h>
30 #include <glibmm/dispatcher.h>
31 #include <giomm/file.h>
32 #endif
33 
34 #include "Referenceable.h"
35 #include "MemoryBlockList.h"
36 
37 namespace cwchess {
38 namespace pgn {
39 
40 using util::MemoryBlockList;
41 using util::MemoryBlockNode;
42 
43 class Database : public util::Referenceable {
44 
45  enum state_type {
46  white_space,
47  string_token,
48  comment_token
49  };
50 
51  private:
52  bool M_saw_carriage_return;
53  size_t M_line_wrapped;
54  int M_number_of_lines;
55  size_t M_number_of_characters;
56  state_type M_state;
57  unsigned char const* M_search_table;
58  unsigned char const* M_search_table_storeR;
59  static unsigned char* S_state_tables[11];
60 
61  protected:
62  MemoryBlockList* M_buffer; //!< Linked list of blocks with valid data.
63  Glib::RefPtr<MemoryBlockNode> M_new_block; //!< Temporary storage for new block that is being read and not linked yet.
64  //! Constructor.
65  Database(void) : M_buffer(NULL), M_saw_carriage_return(false), M_line_wrapped(0),
66  M_number_of_lines(0), M_number_of_characters(0), M_state(white_space) { }
67 
68  /** @brief Process next data block.
69  *
70  * This function is called for all subsequent blocks of data during the initialization of the Database object.
71  * /
72  void process_next_data_block(char const* data, size_t size);
73  public:
74  //! @brief Return the path name of the database.
75  virtual std::string get_path(void) const = 0;
76 
77  int number_of_lines(void) const { return M_number_of_lines; }
78  size_t number_of_characters(void) const { return M_number_of_characters; }
79 };
80 
81 class DatabaseSeekable : public Database {
82  public:
83  typedef sigc::slot<void, size_t> SlotOpenFinished;
84  // This is the minimum blocksize needed to reach a speed of 130 MB/s.
85  // The 64 is to take MemoryBlockNode (32 bytes) and a possible malloc overhead into account.
86  // That means we're not reading an integral number of disk blocks at a time, but that
87  // turns out to make no difference (on my machine).
88  static size_t const S_buffer_size = 6 * 4096 - 64;
89  private:
90  Glib::RefPtr<Gio::File> M_file;
91  Glib::RefPtr<Gio::Cancellable> M_cancellable;
92  gsize M_bytes_read;
93  Glib::RefPtr<Gio::FileInputStream> M_file_input_stream;
94  SlotOpenFinished M_slot_open_finished;
95  Glib::Thread* M_read_thread;
96  Glib::Dispatcher M_processing_finished;
97  public:
98  static Glib::RefPtr<Database> open(std::string const& path, SlotOpenFinished const& slot)
99  { return Glib::RefPtr<Database>(new DatabaseSeekable(path, slot)); }
100  protected:
101  DatabaseSeekable(std::string const& path, SlotOpenFinished const& slot_open_finished) :
102  M_file(Gio::File::create_for_path(path)), M_cancellable(Gio::Cancellable::create()),
103  M_bytes_read(0), M_slot_open_finished(slot_open_finished) { load(); }
104  virtual ~DatabaseSeekable();
105  private:
106  void load(void);
107  void read_async_open_ready(Glib::RefPtr<Gio::AsyncResult>& result);
108  static void read_async_ready(GObject* source_object, GAsyncResult* async_res, gpointer user_data);
109  void read_async_ready(GObject* source_object, GAsyncResult* async_res);
110  void need_more_data(void);
111  void processing_finished(void);
112 
113  //! @brief Return the path name of the database.
114  virtual std::string get_path(void) const { M_file->get_path(); }
115 
116  private:
117  void read_thread(void);
118 };
119 
120 } // namespace pgn
121 } // namespace cwchess
122 
123 #endif // PGNDATABASE_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
Glib::RefPtr< MemoryBlockNode > M_new_block
Definition: PgnDatabase.h:63
Database(void)
Constructor.
Definition: PgnDatabase.h:65
virtual std::string get_path(void) const =0
Return the path name of the database.
This file contains the declaration of class MemoryBlockList.
MemoryBlockList * M_buffer
Linked list of blocks with valid data.
Definition: PgnDatabase.h:62
This file contains the declaration of class Referenceable.
void process_next_data_block(char const* data, size_t size)
Process next data block.
Definition: PgnDatabase.cc:48

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.