1
/* Copyright (c) 2001-2005, The HSQL Development Group
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* Neither the name of the HSQL Development Group nor the names of its
15
* contributors may be used to endorse or promote products derived from this
16
* software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
/* Copyright (c) 2001-2005, The HSQL Development Group
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* Neither the name of the HSQL Development Group nor the names of its
15
* contributors may be used to endorse or promote products derived from this
16
* software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
32
package org.hsqldb.persist;
34
34
import java.io.File;
111
112
protected long maxCacheBytes; // number of bytes
112
113
protected Cache cache;
114
public DataFileCache(Database db, String fileName,
115
String backupName) throws HsqlException {
117
this.fileName = fileName;
118
this.backupFileName = backupName;
120
fa = database.getFileAccess();
115
public DataFileCache(Database db,
116
String baseFileName) throws HsqlException {
118
initParams(db, baseFileName);
124
120
cache = new Cache(this);
128
124
* initial external parameters are set here.
130
protected void initParams() throws HsqlException {
126
protected void initParams(Database database,
127
String baseFileName) throws HsqlException {
129
fileName = baseFileName + ".data";
130
backupFileName = baseFileName + ".backup";
131
this.database = database;
132
fa = database.getFileAccess();
132
134
int cacheScale = database.getProperties().getIntegerProperty(
133
135
HsqlDatabaseProperties.hsqldb_cache_scale, 14, 8, 18);
272
281
StopWatch sw = new StopWatch();
283
appLog.sendLine(SimpleLog.LOG_NORMAL,
284
"DataFileCache.close(" + write + ") : start");
276
288
Trace.printSystemOut("saveAll: " + sw.elapsedTime());
279
dataFile.seek(LONG_EMPTY_SIZE);
280
dataFile.writeLong(freeBlocks.getLostBlocksSize());
283
dataFile.seek(LONG_FREE_POS_POS);
284
dataFile.writeLong(fileFreePosition);
287
dataFile.seek(FLAGS_POS);
289
int flag = BitMap.set(0, FLAG_ISSAVED);
292
flag = BitMap.set(flag, FLAG_ROWINFO);
289
appLog.sendLine(SimpleLog.LOG_NORMAL,
290
"DataFileCache.close() : save data");
292
if (fileModified || freeBlocks.isModified()) {
295
dataFile.seek(LONG_EMPTY_SIZE);
296
dataFile.writeLong(freeBlocks.getLostBlocksSize());
299
dataFile.seek(LONG_FREE_POS_POS);
300
dataFile.writeLong(fileFreePosition);
303
dataFile.seek(FLAGS_POS);
305
int flag = BitMap.set(0, FLAG_ISSAVED);
308
flag = BitMap.set(flag, FLAG_ROWINFO);
311
dataFile.writeInt(flag);
312
appLog.sendLine(SimpleLog.LOG_NORMAL,
313
"DataFileCache.close() : flags");
316
if (dataFile.length() != fileFreePosition) {
317
dataFile.seek(fileFreePosition);
320
appLog.sendLine(SimpleLog.LOG_NORMAL,
321
"DataFileCache.close() : seek end");
322
Trace.printSystemOut("pos and flags: "
295
dataFile.writeInt(flag);
298
dataFile.seek(fileFreePosition);
299
Trace.printSystemOut("pos and flags: " + sw.elapsedTime());
302
327
if (dataFile != null) {
303
328
dataFile.close();
329
appLog.sendLine(SimpleLog.LOG_NORMAL,
330
"DataFileCache.close() : close");
326
353
void postClose(boolean keep) throws HsqlException {
355
SimpleLog appLog = database.logger.appLog;
328
357
if (cacheReadonly) {
362
appLog.sendLine(SimpleLog.LOG_NORMAL,
363
"DataFileCache.postClose(" + keep + ") : start");
334
366
database.getProperties().setProperty(
335
367
HsqlDatabaseProperties.hsqldb_cache_version,
336
368
HsqlDatabaseProperties.VERSION_STRING_1_7_0);
337
369
database.getProperties().save();
370
appLog.sendLine(SimpleLog.LOG_NORMAL,
371
"DataFileCache.postClose() : save props");
339
373
if (fileModified) {
343
377
fa.removeElement(backupFileName);
378
appLog.sendLine(SimpleLog.LOG_NORMAL,
379
"DataFileCache.postClose() : delete backup");
344
380
deleteOrResetFreePos(database, fileName);
381
appLog.sendLine(SimpleLog.LOG_NORMAL,
382
"DataFileCache.postClose() : delete file");
346
384
} catch (IOException e) {
347
385
throw new HsqlException(
510
public synchronized int getStorageSize(int i) throws HsqlException {
513
CachedObject value = cache.get(i);
516
return value.getStorageSize();
520
} catch (IOException e) {
521
database.logger.appLog.logContext(e);
523
throw Trace.error(Trace.DATA_FILE_ERROR,
524
Trace.DataFileCache_makeRow, new Object[] {
555
public synchronized int getStorageSize(int i) throws IOException {
557
CachedObject value = cache.get(i);
560
return value.getStorageSize();
530
566
public synchronized CachedObject get(int i, PersistentStore store,