libspatialindex API Reference  (git-trunk)
RandomEvictionsBuffer.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 #include <ctime>
28 #include <cstdlib>
29 #include <cmath>
30 
31 #ifndef HAVE_SRAND48
33 #endif
34 
36 #include "RandomEvictionsBuffer.h"
37 
38 using namespace SpatialIndex;
39 using namespace SpatialIndex::StorageManager;
40 
42 {
43  IBuffer* b = new RandomEvictionsBuffer(sm, ps);
44  return b;
45 }
46 
48 {
49  Tools::Variant var;
51 
53  var.m_val.ulVal = capacity;
54  ps.setProperty("Capacity", var);
55 
57  var.m_val.blVal = bWriteThrough;
58  ps.setProperty("WriteThrough", var);
59 
60  return returnRandomEvictionsBuffer(sm, ps);
61 }
62 
64 {
65  srand48(static_cast<uint32_t>(time(nullptr)));
66 }
67 
69 = default;
70 
72 {
73  assert(m_buffer.size() <= m_capacity);
74 
75  if (m_buffer.size() == m_capacity) removeEntry();
76  assert(m_buffer.find(page) == m_buffer.end());
77  m_buffer.insert(std::pair<id_type, Entry*>(page, e));
78 }
79 
81 {
82  if (m_buffer.size() == 0) return;
83 
84  double random;
85 
86  random = drand48();
87 
88  uint32_t entry = static_cast<uint32_t>(floor(((double) m_buffer.size()) * random));
89 
90  std::map<id_type, Entry*>::iterator it = m_buffer.begin();
91  for (uint32_t cIndex = 0; cIndex < entry; cIndex++) ++it;
92 
93  if ((*it).second->m_bDirty)
94  {
95  id_type page = (*it).first;
96  m_pStorageManager->storeByteArray(page, ((*it).second)->m_length, (const uint8_t *) ((*it).second)->m_pData);
97  }
98 
99  delete (*it).second;
100  m_buffer.erase(it);
101 }
VariantType m_varType
Definition: Tools.h:277
uint32_t ulVal
Definition: Tools.h:289
void setProperty(std::string property, Variant const &v)
Definition: Tools.cc:354
virtual void storeByteArray(id_type &id, const uint32_t len, const uint8_t *const data)=0
RandomEvictionsBuffer(IStorageManager &, Tools::PropertySet &ps)
union Tools::Variant::@0 m_val
void srand48(long int seed) __THROW
std::map< id_type, Entry * > m_buffer
Definition: Buffer.h:81
IStorageManager * m_pStorageManager
Definition: Buffer.h:80
SIDX_DLL IBuffer * createNewRandomEvictionsBuffer(IStorageManager &in, uint32_t capacity, bool bWriteThrough)
int64_t id_type
Definition: SpatialIndex.h:41
bool blVal
Definition: Tools.h:291
SIDX_DLL IBuffer * returnRandomEvictionsBuffer(IStorageManager &ind, Tools::PropertySet &in)
void addEntry(id_type page, Buffer::Entry *pEntry) override
double drand48(void) __THROW
Definition: rand48.cc:145