1
/******************************************************************************
2
* Project: libspatialindex - A C++ library for spatial indexing
3
* Author: Marios Hadjieleftheriou, mhadji@gmail.com
4
******************************************************************************
5
* Copyright (c) 2002, Marios Hadjieleftheriou
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:
16
* The above copyright notice and this permission notice shall be included
17
* in all copies or substantial portions of the Software.
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
******************************************************************************/
30
#include <spatialindex/SpatialIndex.h>
33
Buffer::Buffer(IStorageManager& sm, Tools::PropertySet& ps) :
35
m_bWriteThrough(false),
36
m_pStorageManager(&sm),
39
Tools::Variant var = ps.getProperty("Capacity");
40
if (var.m_varType != Tools::VT_EMPTY)
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;
46
var = ps.getProperty("WriteThrough");
47
if (var.m_varType != Tools::VT_EMPTY)
49
if (var.m_varType != Tools::VT_BOOL) throw Tools::IllegalArgumentException("Property WriteThrough must be Tools::VT_BOOL");
50
m_bWriteThrough = var.m_val.blVal;
61
for (std::map<id_type, Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); ++it)
63
if ((*it).second->m_bDirty)
65
id_type page = (*it).first;
66
m_pStorageManager->storeByteArray(page, (*it).second->m_length, (*it).second->m_pData);
72
void Buffer::loadByteArray(const id_type page, uint32_t& len, byte** data)
74
std::map<id_type, Entry*>::iterator it = m_buffer.find(page);
76
if (it != m_buffer.end())
79
len = (*it).second->m_length;
80
*data = new byte[len];
81
memcpy(*data, (*it).second->m_pData, len);
85
m_pStorageManager->loadByteArray(page, len, data);
86
addEntry(page, new Entry(len, static_cast<const byte*>(*data)));
90
void Buffer::storeByteArray(id_type& page, const uint32_t len, const byte* const data)
94
m_pStorageManager->storeByteArray(page, len, data);
95
assert(m_buffer.find(page) == m_buffer.end());
96
addEntry(page, new Entry(len, data));
102
m_pStorageManager->storeByteArray(page, len, data);
105
Entry* e = new Entry(len, data);
106
if (m_bWriteThrough == false) e->m_bDirty = true;
108
std::map<id_type, Entry*>::iterator it = m_buffer.find(page);
109
if (it != m_buffer.end())
113
if (m_bWriteThrough == false) ++m_u64Hits;
122
void Buffer::deleteByteArray(const id_type page)
124
std::map<id_type, Entry*>::iterator it = m_buffer.find(page);
125
if (it != m_buffer.end())
131
m_pStorageManager->deleteByteArray(page);
136
for (std::map<id_type, Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); ++it)
138
if ((*it).second->m_bDirty)
140
id_type page = (*it).first;
141
m_pStorageManager->storeByteArray(page, ((*it).second)->m_length, static_cast<const byte*>(((*it).second)->m_pData));
151
uint64_t Buffer::getHits()