Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.kaha.impl;
   18   
   19   import java.io.IOException;
   20   import java.util.LinkedList;
   21   import java.util.Map;
   22   import java.util.Set;
   23   import java.util.concurrent.ConcurrentHashMap;
   24   
   25   import org.apache.activemq.kaha.ContainerId;
   26   import org.apache.activemq.kaha.Marshaller;
   27   import org.apache.activemq.kaha.Store;
   28   import org.apache.activemq.kaha.StoreEntry;
   29   import org.apache.activemq.kaha.StoreLocation;
   30   import org.apache.activemq.kaha.impl.data.Item;
   31   import org.apache.activemq.kaha.impl.index.IndexItem;
   32   import org.apache.activemq.kaha.impl.index.IndexManager;
   33   import org.apache.commons.logging.Log;
   34   import org.apache.commons.logging.LogFactory;
   35   
   36   /**
   37    * A container of roots for other Containers
   38    * 
   39    * @version $Revision: 1.2 $
   40    */
   41   
   42   class IndexRootContainer {
   43       
   44       protected static final Marshaller ROOT_MARSHALLER = Store.OBJECT_MARSHALLER;
   45       private static final Log LOG = LogFactory.getLog(IndexRootContainer.class);
   46   
   47       protected IndexItem root;
   48       protected IndexManager indexManager;
   49       protected DataManager dataManager;
   50       protected Map<Object, StoreEntry> map = new ConcurrentHashMap<Object, StoreEntry>();
   51       protected LinkedList<StoreEntry> list = new LinkedList<StoreEntry>();
   52   
   53       IndexRootContainer(IndexItem root, IndexManager im, DataManager dfm) throws IOException {
   54           this.root = root;
   55           this.indexManager = im;
   56           this.dataManager = dfm;
   57           long nextItem = root.getNextItem();
   58           while (nextItem != Item.POSITION_NOT_SET) {
   59               StoreEntry item = indexManager.getIndex(nextItem);
   60               StoreLocation data = item.getKeyDataItem();
   61               Object key = dataManager.readItem(ROOT_MARSHALLER, data);
   62               map.put(key, item);
   63               list.add(item);
   64               nextItem = item.getNextItem();
   65               dataManager.addInterestInFile(item.getKeyFile());
   66           }
   67       }
   68   
   69       Set<Object> getKeys() {
   70           return map.keySet();
   71       }
   72   
   73       IndexItem addRoot(IndexManager containerIndexManager, ContainerId key) throws IOException {
   74           if (map.containsKey(key)) {
   75               removeRoot(containerIndexManager, key);
   76           }
   77   
   78           StoreLocation data = dataManager.storeDataItem(ROOT_MARSHALLER, key);
   79           IndexItem newRoot = indexManager.createNewIndex();
   80           newRoot.setKeyData(data);
   81           IndexItem containerRoot = containerIndexManager.createNewIndex();
   82           containerIndexManager.storeIndex(containerRoot);
   83           newRoot.setValueOffset(containerRoot.getOffset());
   84   
   85           IndexItem last = list.isEmpty() ? null : (IndexItem)list.getLast();
   86           last = last == null ? root : last;
   87           long prev = last.getOffset();
   88           newRoot.setPreviousItem(prev);
   89           indexManager.storeIndex(newRoot);
   90           last.setNextItem(newRoot.getOffset());
   91           indexManager.storeIndex(last);
   92           map.put(key, newRoot);
   93           list.add(newRoot);
   94           return containerRoot;
   95       }
   96   
   97       void removeRoot(IndexManager containerIndexManager, ContainerId key) throws IOException {
   98           StoreEntry oldRoot = map.remove(key);
   99           if (oldRoot != null) {
  100               dataManager.removeInterestInFile(oldRoot.getKeyFile());
  101               // get the container root
  102               IndexItem containerRoot = containerIndexManager.getIndex(oldRoot.getValueOffset());
  103               if (containerRoot != null) {
  104                   containerIndexManager.freeIndex(containerRoot);
  105               }
  106               int index = list.indexOf(oldRoot);
  107               IndexItem prev = index > 0 ? (IndexItem)list.get(index - 1) : root;
  108               prev = prev == null ? root : prev;
  109               IndexItem next = index < (list.size() - 1) ? (IndexItem)list.get(index + 1) : null;
  110               if (next != null) {
  111                   prev.setNextItem(next.getOffset());
  112                   next.setPreviousItem(prev.getOffset());
  113                   indexManager.updateIndexes(next);
  114               } else {
  115                   prev.setNextItem(Item.POSITION_NOT_SET);
  116               }
  117               indexManager.updateIndexes(prev);
  118               list.remove(oldRoot);
  119               indexManager.freeIndex((IndexItem)oldRoot);
  120           }
  121       }
  122   
  123       IndexItem getRoot(IndexManager containerIndexManager, ContainerId key) throws IOException {
  124           StoreEntry index = map.get(key);
  125           if (index != null) {
  126               return containerIndexManager.getIndex(index.getValueOffset());
  127           }
  128           return null;
  129       }
  130   
  131       boolean doesRootExist(Object key) {
  132           return map.containsKey(key);
  133       }
  134   
  135   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » [javadoc | source]