01: // Spatial Index Library
02: //
03: // Copyright (C) 2002 Navel Ltd.
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the GNU Lesser General Public
07: // License as published by the Free Software Foundation; either
08: // version 2.1 of the License, or (at your option) any later version.
09: //
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: // Contact information:
20: // Mailing address:
21: // Marios Hadjieleftheriou
22: // University of California, Riverside
23: // Department of Computer Science
24: // Surge Building, Room 310
25: // Riverside, CA 92521
26: //
27: // Email:
28: // marioh@cs.ucr.edu
29: package org.geotools.caching.spatialindex.storagemanager;
30:
31: import java.util.*;
32:
33: public class RandomEvictionsBuffer extends Buffer {
34: Random m_random = new Random();
35:
36: public RandomEvictionsBuffer(IStorageManager sm, int capacity,
37: boolean bWriteThrough) {
38: super (sm, capacity, bWriteThrough);
39: }
40:
41: void addEntry(int id, Entry e) {
42: assert m_buffer.size() <= m_capacity;
43:
44: if (m_buffer.size() == m_capacity) {
45: removeEntry();
46: }
47:
48: m_buffer.put(new Integer(id), e);
49: }
50:
51: void removeEntry() {
52: if (m_buffer.size() == 0) {
53: return;
54: }
55:
56: int entry = m_random.nextInt(m_buffer.size());
57:
58: Iterator it = m_buffer.entrySet().iterator();
59:
60: for (int cIndex = 0; cIndex < (entry - 1); cIndex++)
61: it.next();
62:
63: Map.Entry me = (Map.Entry) it.next();
64: Entry e = (Entry) me.getValue();
65: int id = ((Integer) me.getKey()).intValue();
66:
67: if (e.m_bDirty) {
68: m_storageManager.storeByteArray(id, e.m_data);
69: }
70:
71: m_buffer.remove(new Integer(id));
72: }
73: } // RandomEvictionsBuffer
|