~airpollution/fluidity/fluidity_airpollution

« back to all changes in this revision

Viewing changes to spatialindex-1.8.0/src/storagemanager/Buffer.cc

  • Committer: ziyouzhj
  • Date: 2013-12-09 16:51:29 UTC
  • Revision ID: ziyouzhj@gmail.com-20131209165129-ucoetc3u0atyy05c
airpolution

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
30
#include <spatialindex/SpatialIndex.h>
 
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");
 
50
                m_bWriteThrough = var.m_val.blVal;
 
51
        }
 
52
}
 
53
 
 
54
Buffer::~Buffer()
 
55
{
 
56
        flush();
 
57
}
 
58
 
 
59
void Buffer::flush()
 
60
{
 
61
        for (std::map<id_type, Entry*>::iterator 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, byte** data)
 
73
{
 
74
        std::map<id_type, Entry*>::iterator 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 byte[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 byte*>(*data)));
 
87
        }
 
88
}
 
89
 
 
90
void Buffer::storeByteArray(id_type& page, const uint32_t len, const byte* 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
                std::map<id_type, Entry*>::iterator 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
 
 
122
void Buffer::deleteByteArray(const id_type page)
 
123
{
 
124
        std::map<id_type, Entry*>::iterator it = m_buffer.find(page);
 
125
        if (it != m_buffer.end())
 
126
        {
 
127
                delete (*it).second;
 
128
                m_buffer.erase(it);
 
129
        }
 
130
 
 
131
        m_pStorageManager->deleteByteArray(page);
 
132
}
 
133
 
 
134
void Buffer::clear()
 
135
{
 
136
        for (std::map<id_type, Entry*>::iterator 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 byte*>(((*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
}