TPIE

2362a60
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.h>
28 #include <tpie/tpie_assert.h>
30 #include <tpie/blocks/block.h>
31 #include <tpie/blocks/block_collection.h>
32 #include <list>
33 #include <map>
34 
35 namespace tpie {
36 
37 namespace blocks {
38 
44 private:
45  struct position_comparator {
46  bool operator()(const block_handle & a, const block_handle & b) const {
47  return a.position < b.position;
48  }
49  };
50 
51  typedef std::list<block_handle> block_list_t;
52 
53  struct block_information_t {
54  block_information_t() {}
55 
56  block_information_t(block * pointer, block_list_t::iterator iterator, bool dirty)
57  : pointer(pointer)
58  , iterator(iterator)
59  , dirty(dirty)
60  {}
61 
62  block * pointer;
63  block_list_t::iterator iterator;
64  bool dirty;
65  };
66 
67  typedef std::map<block_handle, block_information_t, position_comparator> block_map_t;
68 public:
76  block_collection_cache(std::string fileName, memory_size_type blockSize, memory_size_type maxSize, bool writeable);
77 
79 
80 
86 
91  void free_block(block_handle handle);
92 
93 private:
94  // make space for a new block in the cache
95  void prepare_cache();
96 
97  void add_to_cache(block_handle handle, block * b, bool dirty);
98 
99  // Register that item is now the most recently used one.
100  void used(block_information_t& item);
101 
102 public:
108  block * read_block(block_handle handle);
109 
115  void write_block(block_handle handle);
116 
117 private:
118  block_collection m_collection;
119  block_list_t m_blockList;
120  block_map_t m_blockMap;
121  memory_size_type m_curSize;
122  memory_size_type m_maxSize;
123  memory_size_type m_blockSize;
124 };
125 
126 } // blocks namespace
127 
128 } // tpie namespace
129 
130 #endif // _TPIE_BLOCKS_BLOCK_COLLECTION_CACHE_H
Defines the tp_assert macro.
A class to manage writing and reading of block to disk.
tpie_init and tpie_finish.
A class to manage writing and reading of block to disk.
Declare default file accessor.
void free_block(block_handle handle)
frees a block
block_collection_cache(std::string fileName, memory_size_type blockSize, memory_size_type maxSize, bool writeable)
Create a block collection.
block_handle get_free_block()
Allocates a new block.
void write_block(block_handle handle)
Writes the content of a block to disk.
block * read_block(block_handle handle)
Reads the content of a block from disk.