You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ra...@apache.org on 2007/01/25 14:58:39 UTC
svn commit: r499798 -
/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java
Author: rajdavies
Date: Thu Jan 25 05:58:38 2007
New Revision: 499798
URL: http://svn.apache.org/viewvc?view=rev&rev=499798
Log:
fix for http://issues.apache.org/activemq/browse/AMQ-1082
Modified:
incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java
Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java?view=diff&rev=499798&r1=499797&r2=499798
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java (original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java Thu Jan 25 05:58:38 2007
@@ -1,20 +1,17 @@
/**
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
+ * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
*/
+
package org.apache.activemq.kaha.impl.index;
import java.io.File;
@@ -23,18 +20,18 @@
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.util.LinkedList;
-
-
import org.apache.activemq.kaha.StoreEntry;
import org.apache.activemq.kaha.impl.DataManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+
/**
* Optimized Store reader
*
* @version $Revision: 1.1.1.1 $
*/
public final class IndexManager{
+
public static final String NAME_PREFIX="index-";
private static final Log log=LogFactory.getLog(IndexManager.class);
private final String name;
@@ -43,7 +40,6 @@
private RandomAccessFile indexFile;
private StoreIndexReader reader;
private StoreIndexWriter writer;
-
private DataManager redoLog;
private String mode;
private long length=0;
@@ -51,21 +47,21 @@
private IndexItem lastFree;
public IndexManager(File directory,String name,String mode,DataManager redoLog) throws IOException{
- this.directory = directory;
+ this.directory=directory;
this.name=name;
- this.mode = mode;
- this.redoLog = redoLog;
+ this.mode=mode;
+ this.redoLog=redoLog;
initialize();
}
public synchronized boolean isEmpty(){
- return lastFree == null &&length==0;
+ return lastFree==null&&length==0;
}
public synchronized IndexItem getIndex(long offset) throws IOException{
return reader.readItem(offset);
}
-
+
public synchronized IndexItem refreshIndex(IndexItem item) throws IOException{
reader.updateIndexes(item);
return item;
@@ -74,25 +70,23 @@
public synchronized void freeIndex(IndexItem item) throws IOException{
item.reset();
item.setActive(false);
- if (lastFree == null) {
+ if(lastFree==null){
firstFree=lastFree=item;
- }
- else {
+ }else{
lastFree.setNextItem(item.getOffset());
}
writer.updateIndexes(item);
-
}
public synchronized void storeIndex(IndexItem index) throws IOException{
writer.storeItem(index);
}
-
+
public synchronized void updateIndexes(IndexItem index) throws IOException{
- try {
- writer.updateIndexes(index);
- }catch(Throwable e) {
- log.error(name + " error updating indexes ",e);
+ try{
+ writer.updateIndexes(index);
+ }catch(Throwable e){
+ log.error(name+" error updating indexes ",e);
}
}
@@ -124,9 +118,8 @@
}
}
-
public synchronized boolean delete() throws IOException{
- firstFree = lastFree = null;
+ firstFree=lastFree=null;
if(indexFile!=null){
indexFile.close();
indexFile=null;
@@ -136,21 +129,19 @@
private synchronized IndexItem getNextFreeIndex() throws IOException{
IndexItem result=null;
- if (firstFree != null) {
- if (firstFree.equals(lastFree)) {
- result = firstFree;
+ if(firstFree!=null){
+ if(firstFree.equals(lastFree)){
+ result=firstFree;
firstFree=lastFree=null;
-
- }else {
- result = firstFree;
- firstFree = getIndex(firstFree.getNextItem());
- if (firstFree==null) {
+ }else{
+ result=firstFree;
+ firstFree=getIndex(firstFree.getNextItem());
+ if(firstFree==null){
lastFree=null;
}
}
result.reset();
}
-
return result;
}
@@ -161,35 +152,32 @@
public synchronized void setLength(long value){
this.length=value;
}
-
- public synchronized FileLock getLock() throws IOException {
+
+ public synchronized FileLock getLock() throws IOException{
return indexFile.getChannel().tryLock();
}
-
public String toString(){
return "IndexManager:("+NAME_PREFIX+name+")";
}
-
- protected void initialize() throws IOException {
+
+ protected void initialize() throws IOException{
file=new File(directory,NAME_PREFIX+name);
indexFile=new RandomAccessFile(file,mode);
reader=new StoreIndexReader(indexFile);
writer=new StoreIndexWriter(indexFile,name,redoLog);
long offset=0;
-
while((offset+IndexItem.INDEX_SIZE)<=indexFile.length()){
IndexItem index=reader.readItem(offset);
if(!index.isActive()){
index.reset();
- if (lastFree != null) {
+ if(lastFree!=null){
lastFree.setNextItem(index.getOffset());
updateIndexes(lastFree);
lastFree=index;
- }else {
+ }else{
lastFree=firstFree=index;
}
-
}
offset+=IndexItem.INDEX_SIZE;
}