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