libspatialindex API Reference  (git-trunk)
Buffer.cc
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libspatialindex - A C++ library for spatial indexing
3  * Author: Marios Hadjieleftheriou, mhadji@gmail.com
4  ******************************************************************************
5  * Copyright (c) 2002, Marios Hadjieleftheriou
6  *
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26 ******************************************************************************/
27 
28 #include <cstring>
29 
31 #include "Buffer.h"
32 
33 Buffer::Buffer(IStorageManager& sm, Tools::PropertySet& ps) :
34  m_capacity(10),
35  m_bWriteThrough(false),
36  m_pStorageManager(&sm),
37  m_u64Hits(0)
38 {
39  Tools::Variant var = ps.getProperty("Capacity");
40  if (var.m_varType != Tools::VT_EMPTY)
41  {
42  if (var.m_varType != Tools::VT_ULONG) throw Tools::IllegalArgumentException("Property Capacity must be Tools::VT_ULONG");
43  m_capacity = var.m_val.ulVal;
44  }
45 
46  var = ps.getProperty("WriteThrough");
47  if (var.m_varType != Tools::VT_EMPTY)
48  {
49  if (var.m_varType != Tools::VT_BOOL) throw Tools::IllegalArgumentException("Property WriteThrough must be Tools::VT_BOOL");
51  }
52 }
53 
55 {
56  flush();
57 }
58 
60 {
61  for (auto it = m_buffer.begin(); it != m_buffer.end(); ++it)
62  {
63  if ((*it).second->m_bDirty)
64  {
65  id_type page = (*it).first;
66  m_pStorageManager->storeByteArray(page, (*it).second->m_length, (*it).second->m_pData);
67  }
68  delete (*it).second;
69  }
70 }
71 
72 void Buffer::loadByteArray(const id_type page, uint32_t& len, uint8_t** data)
73 {
74  auto it = m_buffer.find(page);
75 
76  if (it != m_buffer.end())
77  {
78  ++m_u64Hits;
79  len = (*it).second->m_length;
80  *data = new uint8_t[len];
81  memcpy(*data, (*it).second->m_pData, len);
82  }
83  else
84  {
85  m_pStorageManager->loadByteArray(page, len, data);
86  addEntry(page, new Entry(len, static_cast<const uint8_t*>(*data)));
87  }
88 }
89 
90 void Buffer::storeByteArray(id_type& page, const uint32_t len, const uint8_t* const data)
91 {
92  if (page == NewPage)
93  {
94  m_pStorageManager->storeByteArray(page, len, data);
95  assert(m_buffer.find(page) == m_buffer.end());
96  addEntry(page, new Entry(len, data));
97  }
98  else
99  {
100  if (m_bWriteThrough)
101  {
102  m_pStorageManager->storeByteArray(page, len, data);
103  }
104 
105  Entry* e = new Entry(len, data);
106  if (m_bWriteThrough == false) e->m_bDirty = true;
107 
108  auto it = m_buffer.find(page);
109  if (it != m_buffer.end())
110  {
111  delete (*it).second;
112  (*it).second = e;
113  if (m_bWriteThrough == false) ++m_u64Hits;
114  }
115  else
116  {
117  addEntry(page, e);
118  }
119  }
120 }
121 
123 {
124  auto it = m_buffer.find(page);
125  if (it != m_buffer.end())
126  {
127  delete (*it).second;
128  m_buffer.erase(it);
129  }
130 
132 }
133 
135 {
136  for (auto it = m_buffer.begin(); it != m_buffer.end(); ++it)
137  {
138  if ((*it).second->m_bDirty)
139  {
140  id_type page = (*it).first;
141  m_pStorageManager->storeByteArray(page, ((*it).second)->m_length, static_cast<const uint8_t*>(((*it).second)->m_pData));
142  }
143 
144  delete (*it).second;
145  }
146 
147  m_buffer.clear();
148  m_u64Hits = 0;
149 }
150 
151 uint64_t Buffer::getHits()
152 {
153  return m_u64Hits;
154 }
virtual void deleteByteArray(const id_type id)=0
uint64_t getHits() override
Definition: Buffer.cc:151
VariantType m_varType
Definition: Tools.h:277
uint32_t ulVal
Definition: Tools.h:289
virtual void storeByteArray(id_type &id, const uint32_t len, const uint8_t *const data)=0
virtual void addEntry(id_type page, Entry *pEntry)=0
virtual void loadByteArray(const id_type id, uint32_t &len, uint8_t **data)=0
union Tools::Variant::@0 m_val
std::map< id_type, Entry * > m_buffer
Definition: Buffer.h:81
void storeByteArray(id_type &page, const uint32_t len, const uint8_t *const data) override
Definition: Buffer.cc:90
IStorageManager * m_pStorageManager
Definition: Buffer.h:80
void deleteByteArray(const id_type page) override
Definition: Buffer.cc:122
void loadByteArray(const id_type page, uint32_t &len, uint8_t **data) override
Definition: Buffer.cc:72
int64_t id_type
Definition: SpatialIndex.h:41
Variant getProperty(std::string property) const
Definition: Tools.cc:346
bool blVal
Definition: Tools.h:291