libspatialindex API Reference  (git-trunk)
capi/Index.cc
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libsidx - A C API wrapper around libspatialindex
3  * Purpose: C++ objects to implement the index.
4  * Author: Howard Butler, hobu.inc@gmail.com
5  ******************************************************************************
6  * Copyright (c) 2009, Howard Butler
7  *
8  * All rights reserved.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27 ******************************************************************************/
28 
30 
31 SpatialIndex::ISpatialIndex* Index::CreateIndex()
32 {
33  using namespace SpatialIndex;
34 
35  ISpatialIndex* index = 0;
36 
37  Tools::Variant var;
38 
39  // Cache the result set limit
40  var = m_properties.getProperty("ResultSetLimit");
41  if (var.m_varType != Tools::VT_EMPTY)
42  {
43  if (var.m_varType != Tools::VT_LONGLONG)
44  throw std::runtime_error("Index::ResultSetLimit: "
45  "Property ResultSetLimit must be Tools::VT_LONGLONG");
46  m_resultSetLimit = var.m_val.llVal;
47  }
48  else
49  m_resultSetLimit = 0;
50 
51  // Cache the result set offset
52  var = m_properties.getProperty("ResultSetOffset");
53  if (var.m_varType != Tools::VT_EMPTY)
54  {
55  if (var.m_varType != Tools::VT_LONGLONG)
56  throw std::runtime_error("Index::ResultSetOffset: "
57  "Property ResultSetOffset must be Tools::VT_LONGLONG");
58  m_resultSetOffset = var.m_val.llVal;
59  }
60  else
61  m_resultSetOffset = 0;
62 
63  if (GetIndexType() == RT_RTree) {
64 
65  try {
66  index = RTree::returnRTree( *m_buffer, m_properties);
67  } catch (Tools::Exception& e) {
68  std::ostringstream os;
69  os << "Spatial Index Error: " << e.what();
70  throw std::runtime_error(os.str());
71  }
72  }
73 
74  else if (GetIndexType() == RT_MVRTree) {
75 
76  try {
77  index = MVRTree::returnMVRTree( *m_buffer, m_properties);
78  } catch (Tools::Exception& e) {
79  std::ostringstream os;
80  os << "Spatial Index Error: " << e.what();
81  throw std::runtime_error(os.str());
82  }
83  }
84 
85  else if (GetIndexType() == RT_TPRTree) {
86 
87  try {
88  index = TPRTree::returnTPRTree( *m_buffer,m_properties);
89  } catch (Tools::Exception& e) {
90  std::ostringstream os;
91  os << "Spatial Index Error: " << e.what();
92  throw std::runtime_error(os.str());
93  }
94  }
95 
96  return index;
97 }
98 
99 
100 Index::Index(const Tools::PropertySet& poProperties): m_properties(poProperties)
101 {
102  Setup();
103 
104  Initialize();
105 }
106 
107 
109 {
110  delete m_rtree;
111  delete m_buffer;
112  delete m_storage;
113 }
114 
115 Index::Index( const Tools::PropertySet& poProperties,
116  int (*readNext)(SpatialIndex::id_type *id,
117  double **pMin,
118  double **pMax,
119  uint32_t *nDimension,
120  const uint8_t **pData,
121  size_t *nDataLength))
122 : Index(poProperties, std::unique_ptr<DataStream>(new DataStream(readNext)))
123 {}
124 
125 Index::Index(const Tools::PropertySet& poProperties,
126  std::unique_ptr<SpatialIndex::IDataStream> ds)
127 : m_properties(poProperties)
128 {
129  using namespace SpatialIndex;
130 
131  Setup();
132 
133  m_storage = CreateStorage();
134  m_buffer = CreateIndexBuffer(*m_storage);
135 
136  SpatialIndex::id_type m_IdxIdentifier;
137 
138  // For memory storage ensure we do not write any files to disk during
139  // the bulk-loading phase
140  if (GetIndexStorage() == RT_Memory)
141  {
142  Tools::Variant sz;
144 
145  sz.m_val.ulVal = 1073741824;
146  m_properties.setProperty("ExternalSortBufferPageSize", sz);
147 
148  sz.m_val.ulVal = 2;
149  m_properties.setProperty("ExternalSortBufferTotalPages", sz);
150  }
151 
152  Tools::Variant var = m_properties.getProperty("IndexIdentifier");
153  if (var.m_varType != Tools::VT_EMPTY)
154  {
155  if (var.m_varType != Tools::VT_LONGLONG)
156  throw std::runtime_error("Index::Index (streaming): "
157  "Property IndexIdentifier must be Tools::VT_LONGLONG");
158 
159  m_IdxIdentifier = var.m_val.llVal;
160  }
161 
163  *ds,
164  *m_buffer,
165  m_properties,
166  m_IdxIdentifier);
167 }
168 
169 
171 {
172  using namespace SpatialIndex::StorageManager;
173  IBuffer* buffer = 0;
174  try {
175  if ( m_storage == 0 )
176  throw std::runtime_error("Storage was invalid to create index buffer");
177  buffer = returnRandomEvictionsBuffer(storage, m_properties);
178  } catch (Tools::Exception& e) {
179  std::ostringstream os;
180  os << "Spatial Index Error: " << e.what();
181  throw std::runtime_error(os.str());
182  }
183  return buffer;
184 }
185 
186 
187 SpatialIndex::IStorageManager* Index::CreateStorage()
188 {
189  using namespace SpatialIndex::StorageManager;
190 
191  SpatialIndex::IStorageManager* storage = 0;
192  std::string filename("");
193 
194  Tools::Variant var;
195  var = m_properties.getProperty("FileName");
196 
197  if (var.m_varType != Tools::VT_EMPTY)
198  {
199  if (var.m_varType != Tools::VT_PCHAR)
200  throw std::runtime_error("Index::CreateStorage: "
201  "Property FileName must be Tools::VT_PCHAR");
202 
203  filename = std::string(var.m_val.pcVal);
204  }
205 
206  if (GetIndexStorage() == RT_Disk) {
207  if (filename.empty()) {
208  std::ostringstream os;
209  os << "Spatial Index Error: filename was empty."
210  " Set IndexStorageType to RT_Memory";
211  throw std::runtime_error(os.str());
212  }
213  try {
214  storage = returnDiskStorageManager(m_properties);
215  return storage;
216  } catch (Tools::Exception& e) {
217  std::ostringstream os;
218  os << "Spatial Index Error: " << e.what();
219  throw std::runtime_error(os.str());
220  }
221 
222  } else if (GetIndexStorage() == RT_Memory) {
223 
224  try {
225  storage = returnMemoryStorageManager(m_properties);
226  return storage;
227  } catch (Tools::Exception& e) {
228  std::ostringstream os;
229  os << "Spatial Index Error: " << e.what();
230  throw std::runtime_error(os.str());
231  }
232 
233  } else if (GetIndexStorage() == RT_Custom) {
234 
235  try {
236  storage = returnCustomStorageManager(m_properties);
237  return storage;
238  } catch (Tools::Exception& e) {
239  std::ostringstream os;
240  os << "Spatial Index Error: " << e.what();
241  throw std::runtime_error(os.str());
242  }
243 
244  }
245  return storage;
246 }
247 
248 
249 
250 
251 void Index::Initialize()
252 {
253  m_storage = CreateStorage();
254  m_buffer = CreateIndexBuffer(*m_storage);
255  m_rtree = CreateIndex();
256 }
257 
258 void Index::Setup()
259 
260 {
261  m_buffer = 0;
262  m_storage = 0;
263  m_rtree = 0;
264  m_resultSetLimit = 0;
265  m_resultSetOffset = 0;
266 }
267 
269 {
270  Tools::Variant var;
271  var = m_properties.getProperty("IndexType");
272 
273  if (var.m_varType != Tools::VT_EMPTY)
274  {
275  if (var.m_varType != Tools::VT_ULONG)
276  throw std::runtime_error("Index::GetIndexType: "
277  "Property IndexType must be Tools::VT_ULONG");
278 
279  return static_cast<RTIndexType>(var.m_val.ulVal);
280  }
281 
282  // if we didn't get anything, we're returning an error condition
283  return RT_InvalidIndexType;
284 
285 }
287 {
288  Tools::Variant var;
290  var.m_val.ulVal = v;
291  m_properties.setProperty("IndexType", var);
292 }
293 
295 {
296 
297  Tools::Variant var;
298  var = m_properties.getProperty("IndexStorageType");
299 
300  if (var.m_varType != Tools::VT_EMPTY)
301  {
302  if (var.m_varType != Tools::VT_ULONG)
303  throw std::runtime_error("Index::GetIndexStorage: "
304  "Property IndexStorageType must be Tools::VT_ULONG");
305 
306  return static_cast<RTStorageType>(var.m_val.ulVal);
307  }
308 
309  // if we didn't get anything, we're returning an error condition
310  return RT_InvalidStorageType;
311 }
312 
314 {
315  Tools::Variant var;
317  var.m_val.ulVal = v;
318  m_properties.setProperty("IndexStorageType", var);
319 }
320 
322 {
323 
324  Tools::Variant var;
325  var = m_properties.getProperty("TreeVariant");
326 
327  if (var.m_varType != Tools::VT_EMPTY)
328  {
329  if (var.m_varType != Tools::VT_ULONG)
330  throw std::runtime_error("Index::GetIndexVariant: "
331  "Property TreeVariant must be Tools::VT_ULONG");
332 
333  return static_cast<RTIndexVariant>(var.m_val.ulVal);
334  }
335 
336  // if we didn't get anything, we're returning an error condition
337  return RT_InvalidIndexVariant;
338 }
339 
341 {
342  using namespace SpatialIndex;
343  Tools::Variant var;
344 
345  if (GetIndexType() == RT_RTree) {
346  var.m_val.ulVal = static_cast<RTree::RTreeVariant>(v);
347  m_properties.setProperty("TreeVariant", var);
348  } else if (GetIndexType() == RT_MVRTree) {
349  var.m_val.ulVal = static_cast<MVRTree::MVRTreeVariant>(v);
350  m_properties.setProperty("TreeVariant", var);
351  } else if (GetIndexType() == RT_TPRTree) {
352  var.m_val.ulVal = static_cast<TPRTree::TPRTreeVariant>(v);
353  m_properties.setProperty("TreeVariant", var);
354  }
355 }
356 
358 {
359  return m_resultSetOffset;
360 }
361 
363 {
364  Tools::Variant var;
366  var.m_val.llVal = v;
367  m_properties.setProperty("ResultSetOffset", var);
368  m_resultSetOffset = v;
369 }
370 
371 
373 {
374  return m_resultSetLimit;
375 }
376 
378 {
379  Tools::Variant var;
381  var.m_val.llVal = v;
382  m_properties.setProperty("ResultSetLimit", var);
383  m_resultSetLimit = v;
384 }
385 
387 {
388  m_rtree->flush();
389  m_storage->flush();
390 }
SpatialIndex::ISpatialIndex & index()
RTIndexType GetIndexType()
Definition: capi/Index.cc:268
void SetIndexVariant(RTIndexVariant v)
Definition: capi/Index.cc:340
Index(const Tools::PropertySet &poProperties)
Definition: capi/Index.cc:100
~Index()
Definition: capi/Index.cc:108
RTIndexVariant GetIndexVariant()
Definition: capi/Index.cc:321
int64_t GetResultSetOffset()
Definition: capi/Index.cc:357
void SetIndexStorage(RTStorageType v)
Definition: capi/Index.cc:313
void SetIndexType(RTIndexType v)
Definition: capi/Index.cc:286
RTStorageType GetIndexStorage()
Definition: capi/Index.cc:294
SpatialIndex::StorageManager::IBuffer & buffer()
void SetResultSetLimit(int64_t v)
Definition: capi/Index.cc:377
void SetResultSetOffset(int64_t v)
Definition: capi/Index.cc:362
int64_t GetResultSetLimit()
Definition: capi/Index.cc:372
void flush()
Definition: capi/Index.cc:386
virtual std::string what()=0
void setProperty(std::string property, Variant const &v)
Definition: Tools.cc:354
Variant getProperty(std::string property) const
Definition: Tools.cc:346
int64_t llVal
Definition: Tools.h:283
uint32_t ulVal
Definition: Tools.h:289
union Tools::Variant::@0 m_val
VariantType m_varType
Definition: Tools.h:277
char * pcVal
Definition: Tools.h:292
SIDX_DLL ISpatialIndex * returnMVRTree(IStorageManager &ind, Tools::PropertySet &in)
Definition: MVRTree.cc:144
SIDX_DLL ISpatialIndex * createAndBulkLoadNewRTree(BulkLoadMethod m, IDataStream &stream, IStorageManager &sm, double fillFactor, uint32_t indexCapacity, uint32_t leafCapacity, uint32_t dimension, RTreeVariant rv, id_type &indexIdentifier)
Definition: RTree.cc:190
SIDX_DLL ISpatialIndex * returnRTree(IStorageManager &ind, Tools::PropertySet &in)
Definition: RTree.cc:143
IStorageManager * returnCustomStorageManager(Tools::PropertySet &in)
SIDX_DLL IStorageManager * returnMemoryStorageManager(Tools::PropertySet &in)
SIDX_DLL IStorageManager * returnDiskStorageManager(Tools::PropertySet &in)
SIDX_DLL IBuffer * returnRandomEvictionsBuffer(IStorageManager &ind, Tools::PropertySet &in)
SIDX_DLL ISpatialIndex * returnTPRTree(IStorageManager &ind, Tools::PropertySet &in)
Definition: TPRTree.cc:140
int64_t id_type
Definition: SpatialIndex.h:41
@ VT_EMPTY
Definition: Tools.h:103
@ VT_LONGLONG
Definition: Tools.h:104
@ VT_PCHAR
Definition: Tools.h:101
@ VT_ULONG
Definition: Tools.h:97
RTIndexVariant
Definition: sidx_config.h:90
@ RT_InvalidIndexVariant
Definition: sidx_config.h:94
RTStorageType
Definition: sidx_config.h:82
@ RT_Memory
Definition: sidx_config.h:83
@ RT_Disk
Definition: sidx_config.h:84
@ RT_InvalidStorageType
Definition: sidx_config.h:86
@ RT_Custom
Definition: sidx_config.h:85
RTIndexType
Definition: sidx_config.h:74
@ RT_MVRTree
Definition: sidx_config.h:76
@ RT_InvalidIndexType
Definition: sidx_config.h:78
@ RT_TPRTree
Definition: sidx_config.h:77
@ RT_RTree
Definition: sidx_config.h:75