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.apache.camel.component.hawtdb;
018    
019    import org.apache.camel.Service;
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.fusesource.hawtbuf.Buffer;
023    import org.fusesource.hawtbuf.codec.BufferCodec;
024    import org.fusesource.hawtbuf.codec.IntegerCodec;
025    import org.fusesource.hawtbuf.codec.StringCodec;
026    import org.fusesource.hawtdb.api.BTreeIndexFactory;
027    import org.fusesource.hawtdb.api.OptimisticUpdateException;
028    import org.fusesource.hawtdb.api.SortedIndex;
029    import org.fusesource.hawtdb.api.Transaction;
030    import org.fusesource.hawtdb.api.TxPageFile;
031    import org.fusesource.hawtdb.api.TxPageFileFactory;
032    
033    /**
034     * Manages access to a shared <a href="http://hawtdb.fusesource.org/">HawtDB</a> file.
035     * <p/>
036     * Will by default not sync writes which allows it to be faster.
037     * You can force syncing by setting the sync option to <tt>true</tt>.
038     */
039    public class HawtDBFile extends TxPageFileFactory implements Service {
040    
041        private static final transient Log LOG = LogFactory.getLog(HawtDBFile.class);
042    
043        // the root which contains an index with name -> page for the real indexes
044        private static final BTreeIndexFactory<String, Integer> ROOT_INDEXES_FACTORY = new BTreeIndexFactory<String, Integer>();
045        // the real indexes where we store persisted data in buffers
046        private static final BTreeIndexFactory<Buffer, Buffer> INDEX_FACTORY = new BTreeIndexFactory<Buffer, Buffer>();
047    
048        private TxPageFile pageFile;
049    
050        static {
051            ROOT_INDEXES_FACTORY.setKeyCodec(StringCodec.INSTANCE);
052            ROOT_INDEXES_FACTORY.setValueCodec(IntegerCodec.INSTANCE);
053            ROOT_INDEXES_FACTORY.setDeferredEncoding(true);
054            INDEX_FACTORY.setKeyCodec(BufferCodec.INSTANCE);
055            INDEX_FACTORY.setValueCodec(BufferCodec.INSTANCE);
056            INDEX_FACTORY.setDeferredEncoding(true);
057        }
058    
059        public HawtDBFile() {
060            setSync(false);
061        }
062    
063        public void start() {
064            if (getFile() == null) {
065                throw new IllegalArgumentException("A file must be configured");
066            }
067    
068            if (LOG.isDebugEnabled()) {
069                LOG.debug("Starting HawtDB using file: " + getFile());
070            }
071    
072            open();
073            pageFile = getTxPageFile();
074    
075            execute(new Work<Boolean>() {
076                public Boolean execute(Transaction tx) {
077                    if (!tx.allocator().isAllocated(0)) {
078                        // if we just created the file, first allocated page should be 0
079                        ROOT_INDEXES_FACTORY.create(tx);
080                        LOG.info("Aggregation repository data store created using file: " + getFile());
081                    } else {
082                        SortedIndex<String, Integer> indexes = ROOT_INDEXES_FACTORY.open(tx);
083                        LOG.info("Aggregation repository data store loaded using file: " + getFile()
084                                + " containing " + indexes.size() + " repositories.");
085                    }
086                    return true;
087                }
088    
089                @Override
090                public String toString() {
091                    return "Allocation repository file: " + getFile();
092                }
093            });
094        }
095    
096        public void stop() {
097            if (LOG.isDebugEnabled()) {
098                LOG.debug("Stopping HawtDB using file: " + getFile());
099            }
100    
101            close();
102            pageFile = null;
103        }
104    
105        public <T> T execute(Work<T> work) {
106            if (LOG.isTraceEnabled()) {
107                LOG.trace("Executing work +++ start +++ " + work);
108            }
109    
110            Transaction tx = pageFile.tx();
111            T answer = doExecute(work, tx, pageFile);
112    
113            if (LOG.isTraceEnabled()) {
114                LOG.trace("Executing work +++ done  +++ " + work);
115            }
116            return answer;
117        }
118    
119        public SortedIndex<Buffer, Buffer> getRepositoryIndex(Transaction tx, String name, boolean create) {
120            SortedIndex<Buffer, Buffer> answer = null;
121    
122            SortedIndex<String, Integer> indexes = ROOT_INDEXES_FACTORY.open(tx);
123            Integer location = indexes.get(name);
124    
125            if (create && location == null) {
126                // create it..
127                SortedIndex<Buffer, Buffer> created = INDEX_FACTORY.create(tx);
128                int page = created.getIndexLocation();
129    
130                // add it to indexes so we can find it the next time
131                indexes.put(name, page);
132    
133                if (LOG.isDebugEnabled()) {
134                    LOG.debug("Created new repository index with name " + name + " at location " + page);
135                }
136    
137                answer = created;
138            } else if (location != null) {
139                if (LOG.isTraceEnabled()) {
140                    LOG.trace("Repository index with name " + name + " at location " + location);
141                }
142                answer = INDEX_FACTORY.open(tx, location);
143            }
144    
145            if (LOG.isTraceEnabled()) {
146                LOG.trace("Repository index with name " + name + " -> " + answer);
147            }
148            return answer;
149        }
150    
151        private static <T> T doExecute(Work<T> work, Transaction tx, TxPageFile page) {
152            T answer = null;
153    
154            boolean done = false;
155            int attempt = 0;
156            while (!done) {
157                try {
158                    // only log at DEBUG level if we are retrying
159                    if (attempt > 0 && LOG.isDebugEnabled()) {
160                        LOG.debug("Attempt " + attempt + " to execute work " + work);
161                    }
162                    attempt++;
163    
164                    // execute and get answer
165                    answer = work.execute(tx);
166    
167                    if (LOG.isTraceEnabled()) {
168                        LOG.trace("TX is read only: " + tx.isReadOnly() + " for executed work: " + work);
169                    }
170                    // commit work
171                    tx.commit();
172                    // and flush so we ensure data is spooled to disk
173                    page.flush();
174                    // and we are done
175                    done = true;
176                } catch (OptimisticUpdateException e) {
177                    // retry as we hit an optimistic update error
178                    LOG.warn("OptimisticUpdateException occurred at attempt " + attempt + " executing work " + work + ". Will do rollback and retry.");
179                    // no harm doing rollback before retry and no wait is needed
180                    tx.rollback();
181                } catch (RuntimeException e) {
182                    LOG.warn("Error executing work " + work + ". Will do rollback.", e);
183                    tx.rollback();
184                    throw e;
185                }
186            }
187    
188            return answer;
189        }
190    
191    }