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
25
#include "../spatialindex/SpatialIndexImpl.h"
27
#include "RandomEvictionsBuffer.h"
30
// following two functions don't exist natively on windows so we'll emulate them
31
// drand48() returns real number x where 0 <= x < 1
32
// srand48() initializes generator
34
double drand48( void ) { return ( double ) rand() / ( double )( RAND_MAX + 1 ); }
35
void srand48( long seed ) { srand( seed ); }
38
using namespace SpatialIndex;
39
using namespace SpatialIndex::StorageManager;
42
IBuffer* SpatialIndex::StorageManager::returnRandomEvictionsBuffer( IStorageManager& sm, Tools::PropertySet& ps )
44
IBuffer* b = new RandomEvictionsBuffer( sm, ps );
48
IBuffer* SpatialIndex::StorageManager::createNewRandomEvictionsBuffer( IStorageManager& sm, unsigned int capacity, bool bWriteThrough )
51
Tools::PropertySet ps;
53
var.m_varType = Tools::VT_ULONG;
54
var.m_val.ulVal = capacity;
55
ps.setProperty( "Capacity", var );
57
var.m_varType = Tools::VT_BOOL;
58
var.m_val.blVal = bWriteThrough;
59
ps.setProperty( "WriteThrough", var );
61
return returnRandomEvictionsBuffer( sm, ps );
64
RandomEvictionsBuffer::RandomEvictionsBuffer( IStorageManager& sm, Tools::PropertySet& ps ) : Buffer( sm, ps )
66
srand48(( long )time( NULL ) );
69
RandomEvictionsBuffer::~RandomEvictionsBuffer()
73
void RandomEvictionsBuffer::addEntry( long id, Entry* e )
75
assert( m_buffer.size() <= m_capacity );
77
if ( m_buffer.size() == m_capacity ) removeEntry();
78
assert( m_buffer.find( id ) == m_buffer.end() );
79
m_buffer.insert( std::pair<long, Entry*>( id, e ) );
82
void RandomEvictionsBuffer::removeEntry()
84
if ( m_buffer.size() == 0 ) return;
86
unsigned int entry = ( unsigned int ) floor((( double ) m_buffer.size() ) * drand48() );
88
map<long, Entry*>::iterator it = m_buffer.begin();
89
for ( unsigned int cIndex = 0; cIndex < entry; cIndex++ ) it++;
91
if (( *it ).second->m_bDirty )
93
long id = ( *it ).first;
94
m_pStorageManager->storeByteArray( id, (( *it ).second )->m_length, ( const byte * )(( *it ).second )->m_pData );