1
/* -*- tab-width: 4 -*-
3
* Electric(tm) VLSI Design System
5
* File: OverflowPageStorage.java
7
* Copyright (c) 2009 Sun Microsystems and Static Free Software
9
* Electric(tm) is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 3 of the License, or
12
* (at your option) any later version.
14
* Electric(tm) is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public License
20
* along with Electric(tm); see the file COPYING. If not, write to
21
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22
* Boston, Mass 02111-1307, USA.
24
package com.sun.electric.database.geometry.btree;
27
* Combines two PageStorage objects, using the first until the "high
28
* water mark" is exceeded, then moving to the other. Generally the
29
* first PageStorage is "small but fast" and the second is "large but
32
* Might not be thread-safe.
34
public class OverflowPageStorage extends PageStorage {
36
private final PageStorage ps1;
37
private final PageStorage ps2;
38
private final int highWaterMark;
39
private boolean overflowed = false;
41
/** Note that highWaterMark is in BYTES, not pages */
42
public OverflowPageStorage(PageStorage ps1, PageStorage ps2, long highWaterMark) {
43
super(Math.max(ps1.getPageSize(), ps1.getPageSize()));
46
this.highWaterMark = (int)(highWaterMark / getPageSize());
49
public int getNumPages() { return overflowed ? ps2.getNumPages() : ps1.getNumPages(); }
50
public synchronized int createPage() {
51
if (overflowed) return ps2.createPage();
52
if (ps1.getNumPages() < highWaterMark) return ps1.createPage();
53
byte[] buf = new byte[getPageSize()];
54
for(int i=0; i<ps1.getNumPages(); i++) {
55
while(ps2.getNumPages() < i+1) ps2.createPage();
56
ps1.readPage(i, buf, 0);
57
ps2.writePage(i, buf, 0);
61
return ps2.createPage();
63
public void fsync(int pageid) { if (overflowed) ps2.fsync(pageid); else ps1.fsync(pageid); }
64
public void fsync() { if (overflowed) ps2.fsync(); else ps1.fsync(); }
65
public void writePage(int pageid, byte[] buf, int ofs) {
66
if (overflowed) ps2.writePage(pageid, buf, ofs);
67
else ps1.writePage(pageid, buf, ofs);
69
public void readPage(int pageid, byte[] buf, int ofs) {
70
if (overflowed) ps2.readPage(pageid, buf, ofs);
71
else ps1.readPage(pageid, buf, ofs);
74
public synchronized void close() { if (overflowed) ps2.close(); else ps1.close(); }
b'\\ No newline at end of file'