1
// Spatial Index Library
3
// Copyright (C) 2002 Navel Ltd.
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include "../spatialindex/SpatialIndexImpl.h"
26
using namespace SpatialIndex::StorageManager;
29
Buffer::Buffer( IStorageManager& sm, Tools::PropertySet& ps ) :
31
m_bWriteThrough( false ),
32
m_pStorageManager( &sm ),
35
Tools::Variant var = ps.getProperty( "Capacity" );
36
if ( var.m_varType != Tools::VT_EMPTY )
38
if ( var.m_varType != Tools::VT_ULONG ) throw Tools::IllegalArgumentException( "Property Capacity must be Tools::VT_ULONG" );
39
m_capacity = var.m_val.ulVal;
42
var = ps.getProperty( "WriteThrough" );
43
if ( var.m_varType != Tools::VT_EMPTY )
45
if ( var.m_varType != Tools::VT_BOOL ) throw Tools::IllegalArgumentException( "Property WriteThrough must be Tools::VT_BOOL" );
46
m_bWriteThrough = var.m_val.blVal;
52
for ( map<long, Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); it++ )
54
Entry* e = ( *it ).second;
55
long id = ( *it ).first;
56
if ( e->m_bDirty ) m_pStorageManager->storeByteArray( id, e->m_length, e->m_pData );
61
void Buffer::loadByteArray( const long id, unsigned long& len, byte** data )
63
map<long, Entry*>::iterator it = m_buffer.find( id );
65
if ( it != m_buffer.end() )
68
Entry* e = ( *it ).second;
70
*data = new byte[len];
71
memcpy( *data, e->m_pData, len );
75
m_pStorageManager->loadByteArray( id, len, data );
76
Entry* e = new Entry( len, ( const byte * ) *data );
81
void Buffer::storeByteArray( long& id, const unsigned long len, const byte* const data )
85
m_pStorageManager->storeByteArray( id, len, data );
86
assert( m_buffer.find( id ) == m_buffer.end() );
87
Entry* e = new Entry( len, data );
92
if ( m_bWriteThrough )
94
m_pStorageManager->storeByteArray( id, len, data );
97
Entry* e = new Entry( len, data );
98
if ( m_bWriteThrough == false ) e->m_bDirty = true;
100
map<long, Entry*>::iterator it = m_buffer.find( id );
101
if ( it != m_buffer.end() )
103
delete( *it ).second;
105
if ( m_bWriteThrough == false ) m_hits++;
114
void Buffer::deleteByteArray( const long id )
116
map<long, Entry*>::iterator it = m_buffer.find( id );
117
if ( it != m_buffer.end() )
119
delete( *it ).second;
120
m_buffer.erase( it );
123
m_pStorageManager->deleteByteArray( id );
128
for ( map<long, Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); it++ )
130
if (( *it ).second->m_bDirty )
132
long id = ( *it ).first;
133
m_pStorageManager->storeByteArray( id, (( *it ).second )->m_length, ( const byte * )(( *it ).second )->m_pData );
136
delete( *it ).second;
143
unsigned long Buffer::getHits()