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        }
090    
091        public void stop() {
092            if (LOG.isDebugEnabled()) {
093                LOG.debug("Stopping HawtDB using file: " + getFile());
094            }
095    
096            close();
097            pageFile = null;
098        }
099    
100        public <T> T execute(Work<T> work) {
101            if (LOG.isTraceEnabled()) {
102                LOG.trace("Executing work +++ start +++ " + work);
103            }
104    
105            Transaction tx = pageFile.tx();
106            T answer = doExecute(work, tx, pageFile);
107    
108            if (LOG.isTraceEnabled()) {
109                LOG.trace("Executing work +++ done  +++ " + work);
110            }
111            return answer;
112        }
113    
114        public SortedIndex<Buffer, Buffer> getRepositoryIndex(Transaction tx, String name, boolean create) {
115            SortedIndex<Buffer, Buffer> answer = null;
116    
117            SortedIndex<String, Integer> indexes = ROOT_INDEXES_FACTORY.open(tx);
118            Integer location = indexes.get(name);
119    
120            if (create && location == null) {
121                // create it..
122                SortedIndex<Buffer, Buffer> created = INDEX_FACTORY.create(tx);
123                int page = created.getIndexLocation();
124    
125                // add it to indexes so we can find it the next time
126                indexes.put(name, page);
127    
128                if (LOG.isDebugEnabled()) {
129                    LOG.debug("Created new repository index with name " + name + " at location " + page);
130                }
131    
132                answer = created;
133            } else if (location != null) {
134                if (LOG.isTraceEnabled()) {
135                    LOG.trace("Repository index with name " + name + " at location " + location);
136                }
137                answer = INDEX_FACTORY.open(tx, location);
138            }
139    
140            if (LOG.isTraceEnabled()) {
141                LOG.trace("Repository index with name " + name + " -> " + answer);
142            }
143            return answer;
144        }
145    
146        private static <T> T doExecute(Work<T> work, Transaction tx, TxPageFile page) {
147            T answer = null;
148    
149            boolean done = false;
150            int attempt = 0;
151            while (!done) {
152                try {
153                    // only log at DEBUG level if we are retrying
154                    if (attempt > 0 && LOG.isDebugEnabled()) {
155                        LOG.debug("Attempt " + attempt + " to execute work " + work);
156                    }
157                    attempt++;
158    
159                    answer = work.execute(tx);
160                    // commit work
161                    tx.commit();
162                    // and flush so we ensure data is spooled to disk
163                    page.flush();
164                    // and we are done
165                    done = true;
166                } catch (OptimisticUpdateException e) {
167                    // retry as we hit an optimistic update error
168                    LOG.warn("OptimisticUpdateException occurred at attempt " + attempt + " executing work " + work + " will do rollback and retry.");
169                    // no harm doing rollback before retry and no wait is needed
170                    tx.rollback();
171                } catch (RuntimeException e) {
172                    LOG.warn("Error executing work " + work + " will do rollback.", e);
173                    tx.rollback();
174                    throw e;
175                }
176            }
177    
178            return answer;
179        }
180    
181    }