001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.fusesource.hawtdb.api; 018 019 import java.io.File; 020 021 import org.fusesource.hawtdb.internal.page.HawtPageFile; 022 import org.fusesource.hawtdb.internal.page.HawtTxPageFile; 023 024 /** 025 * A factory to create TxPageFile objects. 026 * 027 * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 028 */ 029 public class TxPageFileFactory { 030 031 private final PageFileFactory pageFileFactory = new PageFileFactory(); 032 private HawtTxPageFile txPageFile; 033 034 protected boolean drainOnClose; 035 protected boolean sync = true; 036 protected boolean useWorkerThread; 037 private int cacheSize = 1024; 038 039 public TxPageFileFactory() { 040 pageFileFactory.setHeaderSize(HawtTxPageFile.FILE_HEADER_SIZE); 041 } 042 043 /** 044 * Opens the TxPageFile object. A subsequent call to {@link #getTxPageFile()} will return 045 * the opened TxPageFile. 046 */ 047 public void open() { 048 if( getFile() == null ) { 049 throw new IllegalArgumentException("file property not set"); 050 } 051 boolean existed = getFile().isFile(); 052 pageFileFactory.open(); 053 if (txPageFile == null) { 054 txPageFile = new HawtTxPageFile(this, (HawtPageFile) pageFileFactory.getPageFile()); 055 if( existed ) { 056 txPageFile.recover(); 057 } else { 058 txPageFile.reset(); 059 } 060 } 061 } 062 063 /** 064 * Closes the previously opened PageFile object. Subsequent calls to 065 * {@link TxPageFileFactory#getTxPageFile()} will return null. 066 */ 067 public void close() { 068 if (txPageFile != null) { 069 txPageFile.suspend(true, false, drainOnClose); 070 txPageFile.flush(); 071 txPageFile.performBatches(); 072 txPageFile=null; 073 } 074 pageFileFactory.close(); 075 } 076 077 public boolean isSync() { 078 return sync; 079 } 080 081 public void setSync(boolean sync) { 082 this.sync = sync; 083 } 084 085 public TxPageFile getTxPageFile() { 086 return txPageFile; 087 } 088 089 public boolean isDrainOnClose() { 090 return drainOnClose; 091 } 092 093 public void setDrainOnClose(boolean drainOnClose) { 094 this.drainOnClose = drainOnClose; 095 } 096 097 public boolean isUseWorkerThread() { 098 return useWorkerThread; 099 } 100 101 public void setUseWorkerThread(boolean useWorkerThread) { 102 this.useWorkerThread = useWorkerThread; 103 } 104 105 public File getFile() { 106 return pageFileFactory.getFile(); 107 } 108 109 public int getMappingSegementSize() { 110 return pageFileFactory.getMappingSegementSize(); 111 } 112 113 public int getMaxPages() { 114 return pageFileFactory.getMaxPages(); 115 } 116 117 public short getPageSize() { 118 return pageFileFactory.getPageSize(); 119 } 120 121 public void setFile(File file) { 122 pageFileFactory.setFile(file); 123 } 124 125 public void setMappingSegementSize(int mappingSegementSize) { 126 pageFileFactory.setMappingSegementSize(mappingSegementSize); 127 } 128 129 public void setMaxFileSize(long size) { 130 pageFileFactory.setMaxFileSize(size); 131 } 132 133 public void setMaxPages(int maxPages) { 134 pageFileFactory.setMaxPages(maxPages); 135 } 136 137 public void setPageSize(short pageSize) { 138 pageFileFactory.setPageSize(pageSize); 139 } 140 141 public int getCacheSize() { 142 return cacheSize; 143 } 144 145 public void setCacheSize(int cacheSize) { 146 this.cacheSize = cacheSize; 147 } 148 }