001: // Spatial Index Library
002: //
003: // Copyright (C) 2002 Navel Ltd.
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // Contact information:
020: // Mailing address:
021: // Marios Hadjieleftheriou
022: // University of California, Riverside
023: // Department of Computer Science
024: // Surge Building, Room 310
025: // Riverside, CA 92521
026: //
027: // Email:
028: // marioh@cs.ucr.edu
029: package org.geotools.caching.spatialindex.storagemanager;
030:
031: import java.util.*;
032:
033: public abstract class Buffer implements IBuffer {
034: int m_capacity = 10;
035: boolean m_bWriteThrough = false;
036: IStorageManager m_storageManager = null;
037: HashMap m_buffer = new HashMap();
038: long m_hits = 0;
039:
040: public Buffer(IStorageManager sm, int capacity,
041: boolean bWriteThrough) {
042: m_storageManager = sm;
043: m_capacity = capacity;
044: m_bWriteThrough = bWriteThrough;
045: }
046:
047: abstract void addEntry(int id, Entry entry);
048:
049: abstract void removeEntry();
050:
051: public byte[] loadByteArray(final int id) {
052: byte[] ret = null;
053: Entry e = (Entry) m_buffer.get(new Integer(id));
054:
055: if (e != null) {
056: m_hits++;
057:
058: ret = new byte[e.m_data.length];
059: System.arraycopy(e.m_data, 0, ret, 0, e.m_data.length);
060: } else {
061: ret = m_storageManager.loadByteArray(id);
062: e = new Entry(ret);
063: addEntry(id, e);
064: }
065:
066: return ret;
067: }
068:
069: public int storeByteArray(final int id, final byte[] data) {
070: int ret = id;
071:
072: if (id == NewPage) {
073: ret = m_storageManager.storeByteArray(id, data);
074:
075: Entry e = new Entry(data);
076: addEntry(ret, e);
077: } else {
078: if (m_bWriteThrough) {
079: m_storageManager.storeByteArray(id, data);
080: }
081:
082: Entry e = (Entry) m_buffer.get(new Integer(id));
083:
084: if (e != null) {
085: e.m_data = new byte[data.length];
086: System.arraycopy(data, 0, e.m_data, 0, data.length);
087:
088: if (m_bWriteThrough == false) {
089: e.m_bDirty = true;
090: m_hits++;
091: } else {
092: e.m_bDirty = false;
093: }
094: } else {
095: e = new Entry(data);
096:
097: if (m_bWriteThrough == false) {
098: e.m_bDirty = true;
099: }
100:
101: addEntry(id, e);
102: }
103: }
104:
105: return ret;
106: }
107:
108: public void deleteByteArray(final int id) {
109: Integer ID = new Integer(id);
110: Entry e = (Entry) m_buffer.get(ID);
111:
112: if (e != null) {
113: m_buffer.remove(ID);
114: }
115:
116: m_storageManager.deleteByteArray(id);
117: }
118:
119: public void flush() {
120: Iterator it = m_buffer.entrySet().iterator();
121:
122: while (it.hasNext()) {
123: Map.Entry me = (Map.Entry) it.next();
124: Entry e = (Entry) me.getValue();
125: int id = ((Integer) me.getKey()).intValue();
126:
127: if (e.m_bDirty) {
128: m_storageManager.storeByteArray(id, e.m_data);
129: }
130: }
131:
132: m_storageManager.flush();
133: }
134:
135: public void clear() {
136: Iterator it = m_buffer.entrySet().iterator();
137:
138: while (it.hasNext()) {
139: Map.Entry me = (Map.Entry) it.next();
140: Entry e = (Entry) me.getValue();
141:
142: if (e.m_bDirty) {
143: int id = ((Integer) me.getKey()).intValue();
144: m_storageManager.storeByteArray(id, e.m_data);
145: }
146: }
147:
148: m_buffer.clear();
149: m_hits = 0;
150: }
151:
152: public long getHits() {
153: return m_hits;
154: }
155:
156: class Entry {
157: byte[] m_data = null;
158: boolean m_bDirty = false;
159:
160: Entry(final byte[] d) {
161: m_data = new byte[d.length];
162: System.arraycopy(d, 0, m_data, 0, d.length);
163: }
164: }
165: } // Buffer
|