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    import java.io.IOException;
021    
022    import org.fusesource.hawtdb.internal.io.MemoryMappedFileFactory;
023    import org.fusesource.hawtdb.internal.page.HawtPageFile;
024    
025    /**
026     * A factory to create PageFile objects.
027     * 
028     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
029     */
030    public class PageFileFactory {
031    
032        private final MemoryMappedFileFactory mappedFileFactory = new MemoryMappedFileFactory();
033        private HawtPageFile pageFile;
034    
035        protected int headerSize = 0;
036        protected short pageSize = 512;
037        protected int maxPages = Integer.MAX_VALUE;
038    
039        public PageFile getPageFile() {
040            return pageFile;
041        }
042    
043        /**
044         * Opens the PageFile object. A subsequent call to {@link #getPageFile()} will return 
045         * the opened PageFile.
046         */
047        public void open() {
048            try {
049                mappedFileFactory.open();
050            } catch (IOException e) {
051                throw new IOPagingException(e);
052            }
053            if (pageFile == null) {
054                if( pageSize <= 0 ) {
055                    throw new IllegalArgumentException("pageSize property must be greater than 0");
056                }
057                if( maxPages <= 0 ) {
058                    throw new IllegalArgumentException("maxPages property must be greater than 0");
059                }
060                if( headerSize < 0 ) {
061                    throw new IllegalArgumentException("headerSize property cannot be negative.");
062                }
063                try {
064                    pageFile = new HawtPageFile(mappedFileFactory.getMemoryMappedFile(), pageSize, headerSize, maxPages);
065                } catch (IOException e) {
066                    throw new IOPagingException(e);
067                }
068            }
069        } 
070        
071        /**
072         * Closes the previously opened PageFile object.  Subsequent calls to 
073         * {@link PageFileFactory#getPageFile()} will return null. 
074         */
075        public void close() {
076            if (pageFile != null) {
077                pageFile = null;
078            }        
079            mappedFileFactory.close();
080        }
081    
082        public int getHeaderSize() {
083            return headerSize;
084        }
085        public void setHeaderSize(int headerSize) {
086            this.headerSize = headerSize;
087        }
088    
089        public short getPageSize() {
090            return pageSize;
091        }
092        public void setPageSize(short pageSize) {
093            this.pageSize = pageSize;
094        }
095    
096        public int getMaxPages() {
097            return maxPages;
098        }
099        public void setMaxPages(int maxPages) {
100            this.maxPages = maxPages;
101        }
102        public void setMaxFileSize(long size) {
103            setMaxPages( (int)((size-getHeaderSize())/getPageSize()) );
104        }
105        
106        public File getFile() {
107            return mappedFileFactory.getFile();
108        }
109    
110        public int getMappingSegementSize() {
111            return mappedFileFactory.getMappingSegementSize();
112        }
113    
114        public void setFile(File file) {
115            mappedFileFactory.setFile(file);
116        }
117    
118        public void setMappingSegementSize(int mappingSegementSize) {
119            mappedFileFactory.setMappingSegementSize(mappingSegementSize);
120        }
121    }