libspatialindex API Reference  (git-trunk)
MemoryStorageManager.cc
Go to the documentation of this file.
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 <stdexcept>
29 #include <cstring>
30 
32 #include "MemoryStorageManager.h"
33 
34 using namespace SpatialIndex;
35 using namespace SpatialIndex::StorageManager;
36 
38 {
40  return sm;
41 }
42 
44 {
46  return returnMemoryStorageManager(ps);
47 }
48 
50 {
51 }
52 
54 {
55  for (std::vector<Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); ++it) delete *it;
56 }
57 
59 {
60 }
61 
62 void MemoryStorageManager::loadByteArray(const id_type page, uint32_t& len, uint8_t** data)
63 {
64  Entry* e;
65  try
66  {
67  e = m_buffer.at(page);
68  if (e == nullptr) throw InvalidPageException(page);
69  }
70  catch (std::out_of_range&)
71  {
72  throw InvalidPageException(page);
73  }
74 
75  len = e->m_length;
76  *data = new uint8_t[len];
77 
78  memcpy(*data, e->m_pData, len);
79 }
80 
81 void MemoryStorageManager::storeByteArray(id_type& page, const uint32_t len, const uint8_t* const data)
82 {
83  if (page == NewPage)
84  {
85  Entry* e = new Entry(len, data);
86 
87  if (m_emptyPages.empty())
88  {
89  m_buffer.push_back(e);
90  page = m_buffer.size() - 1;
91  }
92  else
93  {
94  page = m_emptyPages.top(); m_emptyPages.pop();
95  m_buffer[page] = e;
96  }
97  }
98  else
99  {
100  Entry* e_old;
101  try
102  {
103  e_old = m_buffer.at(page);
104  if (e_old == nullptr) throw InvalidPageException(page);
105  }
106  catch (std::out_of_range&)
107  {
108  throw InvalidPageException(page);
109  }
110 
111  Entry* e = new Entry(len, data);
112 
113  delete e_old;
114  m_buffer[page] = e;
115  }
116 }
117 
119 {
120  Entry* e;
121  try
122  {
123  e = m_buffer.at(page);
124  if (e == nullptr) throw InvalidPageException(page);
125  }
126  catch (std::out_of_range&)
127  {
128  throw InvalidPageException(page);
129  }
130 
131  m_buffer[page] = nullptr;
132  m_emptyPages.push(page);
133 
134  delete e;
135 }
void loadByteArray(const id_type page, uint32_t &len, uint8_t **data) override
void storeByteArray(id_type &page, const uint32_t len, const uint8_t *const data) override
SIDX_DLL IStorageManager * returnMemoryStorageManager(Tools::PropertySet &in)
SIDX_DLL IStorageManager * createNewMemoryStorageManager()
int64_t id_type
Definition: SpatialIndex.h:41