TPIE

11a2c2d
block_collection_cache.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet cino+=(0 :
3 // Copyright 2014, 2015, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef _TPIE_BLOCKS_BLOCK_COLLECTION_CACHE_H
25 #define _TPIE_BLOCKS_BLOCK_COLLECTION_CACHE_H
26 
27 #include <tpie/tpie_export.h>
28 #include <tpie/tpie.h>
29 #include <tpie/tpie_assert.h>
31 #include <tpie/blocks/block.h>
32 #include <tpie/blocks/block_collection.h>
33 #include <list>
34 #include <map>
35 
36 namespace tpie {
37 
38 namespace blocks {
39 
44 class TPIE_EXPORT block_collection_cache {
45 private:
46  struct position_comparator {
47  bool operator()(const block_handle & a, const block_handle & b) const {
48  return a.position < b.position;
49  }
50  };
51 
52  typedef std::list<block_handle> block_list_t;
53 
54  struct block_information_t {
55  block_information_t() {}
56 
57  block_information_t(block * pointer, block_list_t::iterator iterator, bool dirty)
58  : pointer(pointer)
59  , iterator(iterator)
60  , dirty(dirty)
61  {}
62 
63  block * pointer;
64  block_list_t::iterator iterator;
65  bool dirty;
66  };
67 
68  typedef std::map<block_handle, block_information_t, position_comparator> block_map_t;
69 public:
77  block_collection_cache(std::string fileName, memory_size_type blockSize, memory_size_type maxSize, bool writeable);
78 
80 
81 
86  block_handle get_free_block();
87 
92  void free_block(block_handle handle);
93 
94 private:
95  // make space for a new block in the cache
96  void prepare_cache();
97 
98  void add_to_cache(block_handle handle, block * b, bool dirty);
99 
100  // Register that item is now the most recently used one.
101  void used(block_information_t& item);
102 
103 public:
109  block * read_block(block_handle handle);
110 
116  void write_block(block_handle handle);
117 
118 private:
119  block_collection m_collection;
120  block_list_t m_blockList;
121  block_map_t m_blockMap;
122  memory_size_type m_curSize;
123  memory_size_type m_maxSize;
124  memory_size_type m_blockSize;
125 };
126 
127 } // blocks namespace
128 
129 } // tpie namespace
130 
131 #endif // _TPIE_BLOCKS_BLOCK_COLLECTION_CACHE_H
tpie_assert.h
tpie::blocks::block_collection_cache
A class to manage writing and reading of block to disk.
Definition: block_collection_cache.h:44
file_accessor.h
tpie::blocks::block_collection
A class to manage writing and reading of block to disk.
Definition: block_collection.h:40
tpie::array< char >
tpie.h
tpie::blocks::block_handle
Definition: block.h:32
tpie
Definition: access_type.h:26