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.activemq.kaha.impl.index;
018
019 import java.io.IOException;
020 import java.util.HashMap;
021 import java.util.Map;
022
023 import org.apache.activemq.kaha.IndexMBean;
024 import org.apache.activemq.kaha.Marshaller;
025 import org.apache.activemq.kaha.StoreEntry;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 /**
030 * Index implementation using a HashMap
031 *
032 *
033 */
034 public class VMIndex implements Index, IndexMBean {
035 private static final Logger LOG = LoggerFactory.getLogger(VMIndex.class);
036 private IndexManager indexManager;
037 private Map<Object, StoreEntry> map = new HashMap<Object, StoreEntry>();
038
039 public VMIndex(IndexManager manager) {
040 this.indexManager = manager;
041 }
042
043 /**
044 *
045 * @see org.apache.activemq.kaha.impl.index.Index#clear()
046 */
047 public void clear() {
048 map.clear();
049 }
050
051 /**
052 * @param key
053 * @return true if the index contains the key
054 * @see org.apache.activemq.kaha.impl.index.Index#containsKey(java.lang.Object)
055 */
056 public boolean containsKey(Object key) {
057 return map.containsKey(key);
058 }
059
060 /**
061 * @param key
062 * @return store entry
063 * @see org.apache.activemq.kaha.impl.index.Index#removeKey(java.lang.Object)
064 */
065 public StoreEntry remove(Object key) {
066 StoreEntry result = map.remove(key);
067 if (result != null) {
068 try {
069 result = indexManager.refreshIndex((IndexItem)result);
070 } catch (IOException e) {
071 LOG.error("Failed to refresh entry", e);
072 throw new RuntimeException("Failed to refresh entry");
073 }
074 }
075 return result;
076 }
077
078 /**
079 * @param key
080 * @param entry
081 * @see org.apache.activemq.kaha.impl.index.Index#store(java.lang.Object,
082 * org.apache.activemq.kaha.impl.index.IndexItem)
083 */
084 public void store(Object key, StoreEntry entry) {
085 map.put(key, entry);
086 }
087
088 /**
089 * @param key
090 * @return the entry
091 */
092 public StoreEntry get(Object key) {
093 StoreEntry result = map.get(key);
094 if (result != null) {
095 try {
096 result = indexManager.refreshIndex((IndexItem)result);
097 } catch (IOException e) {
098 LOG.error("Failed to refresh entry", e);
099 throw new RuntimeException("Failed to refresh entry");
100 }
101 }
102 return result;
103 }
104
105 /**
106 * @return true if the index is transient
107 */
108 public boolean isTransient() {
109 return true;
110 }
111
112 /**
113 * load indexes
114 */
115 public void load() {
116 }
117
118 /**
119 * unload indexes
120 */
121 public void unload() {
122 map.clear();
123 }
124
125 public void delete() throws IOException {
126 unload();
127 }
128
129 public void setKeyMarshaller(Marshaller marshaller) {
130 }
131
132 public int getSize() {
133 return map.size();
134 }
135 }