~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/core/spatialindex/storagemanager/Buffer.cc

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Spatial Index Library
 
2
//
 
3
// Copyright (C) 2002 Navel Ltd.
 
4
//
 
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.
 
9
//
 
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.
 
14
//
 
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
 
18
//
 
19
//  Email:
 
20
//    mhadji@gmail.com
 
21
 
 
22
#include "../spatialindex/SpatialIndexImpl.h"
 
23
 
 
24
#include "Buffer.h"
 
25
 
 
26
using namespace SpatialIndex::StorageManager;
 
27
using std::map;
 
28
 
 
29
Buffer::Buffer( IStorageManager& sm, Tools::PropertySet& ps ) :
 
30
    m_capacity( 10 ),
 
31
    m_bWriteThrough( false ),
 
32
    m_pStorageManager( &sm ),
 
33
    m_hits( 0 )
 
34
{
 
35
  Tools::Variant var = ps.getProperty( "Capacity" );
 
36
  if ( var.m_varType != Tools::VT_EMPTY )
 
37
  {
 
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;
 
40
  }
 
41
 
 
42
  var = ps.getProperty( "WriteThrough" );
 
43
  if ( var.m_varType != Tools::VT_EMPTY )
 
44
  {
 
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;
 
47
  }
 
48
}
 
49
 
 
50
Buffer::~Buffer()
 
51
{
 
52
  for ( map<long, Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); it++ )
 
53
  {
 
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 );
 
57
    delete e;
 
58
  }
 
59
}
 
60
 
 
61
void Buffer::loadByteArray( const long id, unsigned long& len, byte** data )
 
62
{
 
63
  map<long, Entry*>::iterator it = m_buffer.find( id );
 
64
 
 
65
  if ( it != m_buffer.end() )
 
66
  {
 
67
    m_hits++;
 
68
    Entry* e = ( *it ).second;
 
69
    len = e->m_length;
 
70
    *data = new byte[len];
 
71
    memcpy( *data, e->m_pData, len );
 
72
  }
 
73
  else
 
74
  {
 
75
    m_pStorageManager->loadByteArray( id, len, data );
 
76
    Entry* e = new Entry( len, ( const byte * ) *data );
 
77
    addEntry( id, e );
 
78
  }
 
79
}
 
80
 
 
81
void Buffer::storeByteArray( long& id, const unsigned long len, const byte* const data )
 
82
{
 
83
  if ( id == NewPage )
 
84
  {
 
85
    m_pStorageManager->storeByteArray( id, len, data );
 
86
    assert( m_buffer.find( id ) == m_buffer.end() );
 
87
    Entry* e = new Entry( len, data );
 
88
    addEntry( id, e );
 
89
  }
 
90
  else
 
91
  {
 
92
    if ( m_bWriteThrough )
 
93
    {
 
94
      m_pStorageManager->storeByteArray( id, len, data );
 
95
    }
 
96
 
 
97
    Entry* e = new Entry( len, data );
 
98
    if ( m_bWriteThrough == false ) e->m_bDirty = true;
 
99
 
 
100
    map<long, Entry*>::iterator it = m_buffer.find( id );
 
101
    if ( it != m_buffer.end() )
 
102
    {
 
103
      delete( *it ).second;
 
104
      ( *it ).second = e;
 
105
      if ( m_bWriteThrough == false ) m_hits++;
 
106
    }
 
107
    else
 
108
    {
 
109
      addEntry( id, e );
 
110
    }
 
111
  }
 
112
}
 
113
 
 
114
void Buffer::deleteByteArray( const long id )
 
115
{
 
116
  map<long, Entry*>::iterator it = m_buffer.find( id );
 
117
  if ( it != m_buffer.end() )
 
118
  {
 
119
    delete( *it ).second;
 
120
    m_buffer.erase( it );
 
121
  }
 
122
 
 
123
  m_pStorageManager->deleteByteArray( id );
 
124
}
 
125
 
 
126
void Buffer::clear()
 
127
{
 
128
  for ( map<long, Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); it++ )
 
129
  {
 
130
    if (( *it ).second->m_bDirty )
 
131
    {
 
132
      long id = ( *it ).first;
 
133
      m_pStorageManager->storeByteArray( id, (( *it ).second )->m_length, ( const byte * )(( *it ).second )->m_pData );
 
134
    }
 
135
 
 
136
    delete( *it ).second;
 
137
  }
 
138
 
 
139
  m_buffer.clear();
 
140
  m_hits = 0;
 
141
}
 
142
 
 
143
unsigned long Buffer::getHits()
 
144
{
 
145
  return m_hits;
 
146
}