Merged asterix_lsm_stabilization upto r1547

git-svn-id: https://asterixdb.googlecode.com/svn/trunk/asterix@1622 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-common/pom.xml b/asterix-common/pom.xml
index a34460c..6356033 100644
--- a/asterix-common/pom.xml
+++ b/asterix-common/pom.xml
@@ -1,13 +1,12 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 	<modelVersion>4.0.0</modelVersion>
 	<parent>
 		<artifactId>asterix</artifactId>
 		<groupId>edu.uci.ics.asterix</groupId>
 		<version>0.0.4-SNAPSHOT</version>
 	</parent>
-	<groupId>edu.uci.ics.asterix</groupId>
 	<artifactId>asterix-common</artifactId>
-	<version>0.0.4-SNAPSHOT</version>
 	<build>
 		<plugins>
 			<plugin>
@@ -15,8 +14,9 @@
 				<artifactId>maven-compiler-plugin</artifactId>
 				<version>2.0.2</version>
 				<configuration>
-					<source>1.6</source>
-					<target>1.6</target>
+					<source>1.7</source>
+					<target>1.7</target>
+					<fork>true</fork>
 				</configuration>
 			</plugin>
 		</plugins>
@@ -25,22 +25,23 @@
 	<dependencies>
 		<dependency>
 			<groupId>edu.uci.ics.hyracks</groupId>
-			<artifactId>hyracks-algebricks-compiler</artifactId>
-			<version>0.2.0</version>
-			<scope>compile</scope>
-		</dependency>
-		<dependency>
-			<groupId>edu.uci.ics.hyracks</groupId>
-			<artifactId>hyracks-dataflow-common</artifactId>
-			<version>0.2.0</version>
-			<scope>compile</scope>
+			<artifactId>algebricks-compiler</artifactId>
 		</dependency>
 		<dependency>
 			<groupId>edu.uci.ics.hyracks</groupId>
 			<artifactId>hyracks-dataflow-std</artifactId>
-			<version>0.2.0</version>
+		</dependency>
+		<dependency>
+			<groupId>edu.uci.ics.asterix</groupId>
+			<artifactId>asterix-transactions</artifactId>
+			<version>0.0.4-SNAPSHOT</version>
+			<type>jar</type>
 			<scope>compile</scope>
 		</dependency>
+		<dependency>
+			<groupId>edu.uci.ics.hyracks</groupId>
+			<artifactId>hyracks-storage-am-lsm-common</artifactId>
+		</dependency>
 	</dependencies>
 
 </project>
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/annotations/RecordDataGenAnnotation.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/annotations/RecordDataGenAnnotation.java
index 8214b39..6b7a96b 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/annotations/RecordDataGenAnnotation.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/annotations/RecordDataGenAnnotation.java
@@ -1,6 +1,8 @@
 package edu.uci.ics.asterix.common.annotations;
 
-public class RecordDataGenAnnotation implements IRecordTypeAnnotation {
+import java.io.Serializable;
+
+public class RecordDataGenAnnotation implements IRecordTypeAnnotation, Serializable {
 
     private final IRecordFieldDataGen[] declaredFieldsDatagen;
     private final UndeclaredFieldsDataGen undeclaredFieldsDataGen;
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/api/AsterixAppContextInfoImpl.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/api/AsterixAppContextInfoImpl.java
new file mode 100644
index 0000000..0f1de56
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/api/AsterixAppContextInfoImpl.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2009-2012 by The Regents of the University of California
+ * Licensed 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 from
+ *
+ *     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.
+ */
+package edu.uci.ics.asterix.common.api;
+
+import edu.uci.ics.asterix.common.context.AsterixRuntimeComponentsProvider;
+import edu.uci.ics.asterix.common.dataflow.IAsterixApplicationContextInfo;
+import edu.uci.ics.hyracks.api.application.ICCApplicationContext;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndexLifecycleManagerProvider;
+import edu.uci.ics.hyracks.storage.common.IStorageManagerInterface;
+
+/*
+ * Acts as an holder class for IndexRegistryProvider, AsterixStorageManager
+ * instances that are accessed from the NCs. In addition an instance of ICCApplicationContext 
+ * is stored for access by the CC.
+ */
+public class AsterixAppContextInfoImpl implements IAsterixApplicationContextInfo {
+
+    private static AsterixAppContextInfoImpl INSTANCE;
+
+    private final ICCApplicationContext appCtx;
+
+    public static void initialize(ICCApplicationContext ccAppCtx) {
+        if (INSTANCE == null) {
+            INSTANCE = new AsterixAppContextInfoImpl(ccAppCtx);
+        }
+    }
+
+    private AsterixAppContextInfoImpl(ICCApplicationContext ccAppCtx) {
+        this.appCtx = ccAppCtx;
+    }
+
+    public static IAsterixApplicationContextInfo getInstance() {
+        return INSTANCE;
+    }
+
+    @Override
+    public IStorageManagerInterface getStorageManagerInterface() {
+        return AsterixRuntimeComponentsProvider.NOINDEX_PROVIDER;
+    }
+
+    @Override
+    public ICCApplicationContext getCCApplicationContext() {
+        return appCtx;
+    }
+
+    @Override
+    public IIndexLifecycleManagerProvider getIndexLifecycleManagerProvider() {
+        return AsterixRuntimeComponentsProvider.NOINDEX_PROVIDER;
+    }
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/DatasetConfig.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/DatasetConfig.java
index aff5dd2..cbe96f7 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/DatasetConfig.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/DatasetConfig.java
@@ -38,10 +38,12 @@
     }
 
     public enum IndexType {
-        BTREE,
-        KEYWORD,
-        QGRAM,
-        RTREE
+        BTREE,        
+        RTREE,
+        WORD_INVIX,
+        NGRAM_INVIX,
+        FUZZY_WORD_INVIX,
+        FUZZY_NGRAM_INVIX
     }
 
 }
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/GlobalConfig.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/GlobalConfig.java
index 201166e..1183b65 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/GlobalConfig.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/GlobalConfig.java
@@ -3,14 +3,13 @@
 import java.util.logging.Logger;
 
 public class GlobalConfig {
-
     public static final boolean DEBUG = true;
+
     public static final String ASTERIX_LOGGER_NAME = "edu.uci.ics.asterix";
+
     public static final Logger ASTERIX_LOGGER = Logger.getLogger(ASTERIX_LOGGER_NAME);
-    // public static Level ASTERIX_LOG_LEVEL = Level.FINEST;
 
     public static final String ASTERIX_LOGFILE_PATTERN = "%t/asterix.log";
-    // "%t/asterix%g.log";
 
     public static final String DEFAULT_CONFIG_FILE_NAME = "test.properties";
 
@@ -20,31 +19,41 @@
 
     public static final String WEB_SERVER_PORT_PROPERTY = "AsterixWebServerPort";
 
+    public static final String JSON_API_SERVER_PORT_PROPERTY = "AsterixJSONAPIServerPort";
+
     public static final String BUFFER_CACHE_PAGE_SIZE_PROPERTY = "BufferCachePageSize";
 
-    public static final String BUFFER_CACHE_SIZE_PROPERTY = "BufferCacheSize";
+    public static final String BUFFER_CACHE_NUM_PAGES_PROPERTY = "BufferCacheNumPages";
 
-    public static final int DEFAULT_BUFFER_CACHE_SIZE = 4096;
-
-    public static final String HYRACKS_APP_NAME = "asterix";
+    public static final int DEFAULT_BUFFER_CACHE_NUM_PAGES = 4096;
 
     public static final int DEFAULT_FRAME_SIZE = 32768;
 
     public static final String FRAME_SIZE_PROPERTY = "FrameSize";
 
     public static final float DEFAULT_BTREE_FILL_FACTOR = 1.00f;
+
     public static int DEFAULT_INPUT_DATA_COLUMN = 0;
 
-    // static {
-    // Handler h;
-    // try {
-    // h = new ConsoleHandler();
-    // h.setFormatter(new SysoutFormatter());
-    // } catch (Exception e) {
-    // h = new ConsoleHandler();
-    // }
-    // h.setLevel(ASTERIX_LOG_LEVEL);
-    // ASTERIX_LOGGER.addHandler(h);
-    // ASTERIX_LOGGER.setLevel(ASTERIX_LOG_LEVEL);
-    // }
+    public static int DEFAULT_INDEX_MEM_PAGE_SIZE = 32768;
+
+    public static int DEFAULT_INDEX_MEM_NUM_PAGES = 1000;
+
+    public static int getFrameSize() {
+        int frameSize = GlobalConfig.DEFAULT_FRAME_SIZE;
+        String frameSizeStr = System.getProperty(GlobalConfig.FRAME_SIZE_PROPERTY);
+        if (frameSizeStr != null) {
+            int fz = -1;
+            try {
+                fz = Integer.parseInt(frameSizeStr);
+            } catch (NumberFormatException nfe) {
+                GlobalConfig.ASTERIX_LOGGER.warning("Wrong frame size size argument. Picking default value ("
+                        + GlobalConfig.DEFAULT_FRAME_SIZE + ") instead.\n");
+            }
+            if (fz >= 0) {
+                frameSize = fz;
+            }
+        }
+        return frameSize;
+    }
 }
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/OptimizationConfUtil.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/OptimizationConfUtil.java
index 0a125b4..271f216 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/OptimizationConfUtil.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/OptimizationConfUtil.java
@@ -1,6 +1,6 @@
 package edu.uci.ics.asterix.common.config;
 
-import edu.uci.ics.hyracks.algebricks.core.rewriter.base.PhysicalOptimizationConfig; 
+import edu.uci.ics.hyracks.algebricks.core.rewriter.base.PhysicalOptimizationConfig;
 
 public class OptimizationConfUtil {
 
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContext.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContext.java
new file mode 100644
index 0000000..5070938
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContext.java
@@ -0,0 +1,219 @@
+package edu.uci.ics.asterix.common.context;
+
+import java.io.IOException;
+import java.util.logging.Level;
+
+import edu.uci.ics.asterix.common.config.GlobalConfig;
+import edu.uci.ics.asterix.transaction.management.exception.ACIDException;
+import edu.uci.ics.asterix.transaction.management.ioopcallbacks.LSMBTreeIOOperationCallbackFactory;
+import edu.uci.ics.asterix.transaction.management.ioopcallbacks.LSMInvertedIndexIOOperationCallbackFactory;
+import edu.uci.ics.asterix.transaction.management.ioopcallbacks.LSMRTreeIOOperationCallbackFactory;
+import edu.uci.ics.asterix.transaction.management.opcallbacks.IndexOperationTrackerFactory;
+import edu.uci.ics.asterix.transaction.management.resource.PersistentLocalResourceRepository;
+import edu.uci.ics.asterix.transaction.management.resource.PersistentLocalResourceRepositoryFactory;
+import edu.uci.ics.asterix.transaction.management.service.recovery.IAsterixAppRuntimeContextProvider;
+import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionSubsystem;
+import edu.uci.ics.hyracks.api.application.INCApplicationContext;
+import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
+import edu.uci.ics.hyracks.api.io.IIOManager;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndex;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndexLifecycleManager;
+import edu.uci.ics.hyracks.storage.am.common.dataflow.IndexLifecycleManager;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationCallbackProvider;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationScheduler;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMMergePolicy;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMOperationTrackerFactory;
+import edu.uci.ics.hyracks.storage.am.lsm.common.impls.SynchronousScheduler;
+import edu.uci.ics.hyracks.storage.common.buffercache.BufferCache;
+import edu.uci.ics.hyracks.storage.common.buffercache.ClockPageReplacementStrategy;
+import edu.uci.ics.hyracks.storage.common.buffercache.DelayPageCleanerPolicy;
+import edu.uci.ics.hyracks.storage.common.buffercache.HeapBufferAllocator;
+import edu.uci.ics.hyracks.storage.common.buffercache.IBufferCache;
+import edu.uci.ics.hyracks.storage.common.buffercache.ICacheMemoryAllocator;
+import edu.uci.ics.hyracks.storage.common.buffercache.IPageCleanerPolicy;
+import edu.uci.ics.hyracks.storage.common.buffercache.IPageReplacementStrategy;
+import edu.uci.ics.hyracks.storage.common.file.IFileMapManager;
+import edu.uci.ics.hyracks.storage.common.file.IFileMapProvider;
+import edu.uci.ics.hyracks.storage.common.file.ILocalResourceRepository;
+import edu.uci.ics.hyracks.storage.common.file.ILocalResourceRepositoryFactory;
+import edu.uci.ics.hyracks.storage.common.file.ResourceIdFactory;
+import edu.uci.ics.hyracks.storage.common.file.ResourceIdFactoryProvider;
+
+public class AsterixAppRuntimeContext {
+    private static final int DEFAULT_BUFFER_CACHE_PAGE_SIZE = 32768;
+    private static final int DEFAULT_LIFECYCLEMANAGER_MEMORY_BUDGET = 1024 * 1024 * 1024; // 1GB
+    private static final int DEFAULT_MAX_OPEN_FILES = Integer.MAX_VALUE;
+    private final INCApplicationContext ncApplicationContext;
+
+    private IIndexLifecycleManager indexLifecycleManager;
+    private IFileMapManager fileMapManager;
+    private IBufferCache bufferCache;
+    private TransactionSubsystem txnSubsystem;
+
+    private ILSMMergePolicy mergePolicy;
+    private ILSMOperationTrackerFactory lsmBTreeOpTrackerFactory;
+    private ILSMOperationTrackerFactory lsmRTreeOpTrackerFactory;
+    private ILSMOperationTrackerFactory lsmInvertedIndexOpTrackerFactory;
+    private ILSMIOOperationScheduler lsmIOScheduler;
+    private PersistentLocalResourceRepository localResourceRepository;
+    private ResourceIdFactory resourceIdFactory;
+    private IIOManager ioManager;
+    private boolean isShuttingdown;
+
+    public AsterixAppRuntimeContext(INCApplicationContext ncApplicationContext) {
+        this.ncApplicationContext = ncApplicationContext;
+    }
+
+    public void initialize() throws IOException, ACIDException {
+        int pageSize = getBufferCachePageSize();
+        int numPages = getBufferCacheNumPages();
+
+        fileMapManager = new AsterixFileMapManager();
+        ICacheMemoryAllocator allocator = new HeapBufferAllocator();
+        IPageReplacementStrategy prs = new ClockPageReplacementStrategy();
+        ioManager = ncApplicationContext.getRootContext().getIOManager();
+        indexLifecycleManager = new IndexLifecycleManager(DEFAULT_LIFECYCLEMANAGER_MEMORY_BUDGET);
+        IAsterixAppRuntimeContextProvider asterixAppRuntimeContextProvider = new AsterixAppRuntimeContextProviderForRecovery(
+                this);
+        txnSubsystem = new TransactionSubsystem(ncApplicationContext.getNodeId(), asterixAppRuntimeContextProvider);
+        IPageCleanerPolicy pcp = new DelayPageCleanerPolicy(600000);
+        bufferCache = new BufferCache(ioManager, allocator, prs, pcp, fileMapManager, pageSize, numPages,
+                DEFAULT_MAX_OPEN_FILES);
+
+        lsmIOScheduler = SynchronousScheduler.INSTANCE;
+        mergePolicy = new ConstantMergePolicy(3, this);
+        lsmBTreeOpTrackerFactory = new IndexOperationTrackerFactory(LSMBTreeIOOperationCallbackFactory.INSTANCE);
+        lsmRTreeOpTrackerFactory = new IndexOperationTrackerFactory(LSMRTreeIOOperationCallbackFactory.INSTANCE);
+        lsmInvertedIndexOpTrackerFactory = new IndexOperationTrackerFactory(
+                LSMInvertedIndexIOOperationCallbackFactory.INSTANCE);
+
+        ILocalResourceRepositoryFactory persistentLocalResourceRepositoryFactory = new PersistentLocalResourceRepositoryFactory(
+                ioManager);
+        localResourceRepository = (PersistentLocalResourceRepository) persistentLocalResourceRepositoryFactory
+                .createRepository();
+        resourceIdFactory = (new ResourceIdFactoryProvider(localResourceRepository)).createResourceIdFactory();
+        isShuttingdown = false;
+    }
+
+    public boolean isShuttingdown() {
+        return isShuttingdown;
+    }
+
+    public void setShuttingdown(boolean isShuttingdown) {
+        this.isShuttingdown = isShuttingdown;
+    }
+
+    private int getBufferCachePageSize() {
+        int pageSize = DEFAULT_BUFFER_CACHE_PAGE_SIZE;
+        String pageSizeStr = System.getProperty(GlobalConfig.BUFFER_CACHE_PAGE_SIZE_PROPERTY, null);
+        if (pageSizeStr != null) {
+            try {
+                pageSize = Integer.parseInt(pageSizeStr);
+            } catch (NumberFormatException nfe) {
+                if (GlobalConfig.ASTERIX_LOGGER.isLoggable(Level.WARNING)) {
+                    GlobalConfig.ASTERIX_LOGGER.warning("Wrong buffer cache page size argument. "
+                            + "Using default value: " + pageSize);
+                }
+            }
+        }
+
+        if (GlobalConfig.ASTERIX_LOGGER.isLoggable(Level.INFO)) {
+            GlobalConfig.ASTERIX_LOGGER.info("Buffer cache page size: " + pageSize);
+        }
+
+        return pageSize;
+    }
+
+    private int getBufferCacheNumPages() {
+        int numPages = GlobalConfig.DEFAULT_BUFFER_CACHE_NUM_PAGES;
+        String numPagesStr = System.getProperty(GlobalConfig.BUFFER_CACHE_NUM_PAGES_PROPERTY, null);
+        if (numPagesStr != null) {
+            try {
+                numPages = Integer.parseInt(numPagesStr);
+            } catch (NumberFormatException nfe) {
+                if (GlobalConfig.ASTERIX_LOGGER.isLoggable(Level.WARNING)) {
+                    GlobalConfig.ASTERIX_LOGGER.warning("Wrong buffer cache size argument. " + "Using default value: "
+                            + numPages);
+                }
+                return numPages;
+            }
+        }
+
+        if (GlobalConfig.ASTERIX_LOGGER.isLoggable(Level.INFO)) {
+            GlobalConfig.ASTERIX_LOGGER.info("Buffer cache size (number of pages): " + numPages);
+        }
+
+        return numPages;
+    }
+
+    public void deinitialize() throws HyracksDataException {
+        bufferCache.close();
+        for (IIndex index : indexLifecycleManager.getOpenIndexes()) {
+            index.deactivate();
+        }
+    }
+
+    public IBufferCache getBufferCache() {
+        return bufferCache;
+    }
+
+    public IFileMapProvider getFileMapManager() {
+        return fileMapManager;
+    }
+
+    public TransactionSubsystem getTransactionSubsystem() {
+        return txnSubsystem;
+    }
+
+    public IIndexLifecycleManager getIndexLifecycleManager() {
+        return indexLifecycleManager;
+    }
+
+    public ILSMMergePolicy getLSMMergePolicy() {
+        return mergePolicy;
+    }
+
+    public ILSMOperationTrackerFactory getLSMBTreeOperationTrackerFactory() {
+        return lsmBTreeOpTrackerFactory;
+    }
+
+    public ILSMOperationTrackerFactory getLSMRTreeOperationTrackerFactory() {
+        return lsmRTreeOpTrackerFactory;
+    }
+
+    public ILSMOperationTrackerFactory getLSMInvertedIndexOperationTrackerFactory() {
+        return lsmInvertedIndexOpTrackerFactory;
+    }
+
+    public ILSMIOOperationCallbackProvider getLSMBTreeIOOperationCallbackProvider() {
+        return AsterixRuntimeComponentsProvider.LSMBTREE_PROVIDER;
+    }
+
+    public ILSMIOOperationCallbackProvider getLSMRTreeIOOperationCallbackProvider() {
+        return AsterixRuntimeComponentsProvider.LSMRTREE_PROVIDER;
+    }
+
+    public ILSMIOOperationCallbackProvider getLSMInvertedIndexIOOperationCallbackProvider() {
+        return AsterixRuntimeComponentsProvider.LSMINVERTEDINDEX_PROVIDER;
+    }
+
+    public ILSMIOOperationCallbackProvider getNoOpIOOperationCallbackProvider() {
+        return AsterixRuntimeComponentsProvider.NOINDEX_PROVIDER;
+    }
+
+    public ILSMIOOperationScheduler getLSMIOScheduler() {
+        return lsmIOScheduler;
+    }
+
+    public ILocalResourceRepository getLocalResourceRepository() {
+        return localResourceRepository;
+    }
+
+    public ResourceIdFactory getResourceIdFactory() {
+        return resourceIdFactory;
+    }
+
+    public IIOManager getIOManager() {
+        return ioManager;
+    }
+}
\ No newline at end of file
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContextProviderForRecovery.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContextProviderForRecovery.java
new file mode 100644
index 0000000..8f2b96f
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContextProviderForRecovery.java
@@ -0,0 +1,104 @@
+package edu.uci.ics.asterix.common.context;
+
+import edu.uci.ics.asterix.transaction.management.service.recovery.IAsterixAppRuntimeContextProvider;
+import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionSubsystem;
+import edu.uci.ics.hyracks.api.io.IIOManager;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndexLifecycleManager;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationCallbackProvider;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationScheduler;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMMergePolicy;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMOperationTrackerFactory;
+import edu.uci.ics.hyracks.storage.common.buffercache.IBufferCache;
+import edu.uci.ics.hyracks.storage.common.file.IFileMapProvider;
+import edu.uci.ics.hyracks.storage.common.file.ILocalResourceRepository;
+import edu.uci.ics.hyracks.storage.common.file.ResourceIdFactory;
+
+public class AsterixAppRuntimeContextProviderForRecovery implements IAsterixAppRuntimeContextProvider {
+
+    private final AsterixAppRuntimeContext asterixAppRuntimeContext;
+
+    public AsterixAppRuntimeContextProviderForRecovery(AsterixAppRuntimeContext asterixAppRuntimeContext) {
+        this.asterixAppRuntimeContext = asterixAppRuntimeContext;
+    }
+
+    @Override
+    public IBufferCache getBufferCache() {
+        return asterixAppRuntimeContext.getBufferCache();
+    }
+
+    @Override
+    public IFileMapProvider getFileMapManager() {
+        return asterixAppRuntimeContext.getFileMapManager();
+    }
+
+    @Override
+    public TransactionSubsystem getTransactionSubsystem() {
+        return asterixAppRuntimeContext.getTransactionSubsystem();
+    }
+
+    @Override
+    public IIndexLifecycleManager getIndexLifecycleManager() {
+        return asterixAppRuntimeContext.getIndexLifecycleManager();
+    }
+
+    @Override
+    public ILSMMergePolicy getLSMMergePolicy() {
+        return asterixAppRuntimeContext.getLSMMergePolicy();
+    }
+
+    @Override
+    public ILSMIOOperationScheduler getLSMIOScheduler() {
+        return asterixAppRuntimeContext.getLSMIOScheduler();
+    }
+
+    @Override
+    public ILocalResourceRepository getLocalResourceRepository() {
+        return asterixAppRuntimeContext.getLocalResourceRepository();
+    }
+
+    @Override
+    public ResourceIdFactory getResourceIdFactory() {
+        return asterixAppRuntimeContext.getResourceIdFactory();
+    }
+
+    @Override
+    public IIOManager getIOManager() {
+        return asterixAppRuntimeContext.getIOManager();
+    }
+
+    @Override
+    public ILSMOperationTrackerFactory getLSMBTreeOperationTrackerFactory() {
+        return asterixAppRuntimeContext.getLSMBTreeOperationTrackerFactory();
+    }
+
+    @Override
+    public ILSMOperationTrackerFactory getLSMRTreeOperationTrackerFactory() {
+        return asterixAppRuntimeContext.getLSMRTreeOperationTrackerFactory();
+    }
+
+    @Override
+    public ILSMOperationTrackerFactory getLSMInvertedIndexOperationTrackerFactory() {
+        return asterixAppRuntimeContext.getLSMInvertedIndexOperationTrackerFactory();
+    }
+
+    @Override
+    public ILSMIOOperationCallbackProvider getLSMBTreeIOOperationCallbackProvider() {
+        return asterixAppRuntimeContext.getLSMBTreeIOOperationCallbackProvider();
+    }
+
+    @Override
+    public ILSMIOOperationCallbackProvider getLSMRTreeIOOperationCallbackProvider() {
+        return asterixAppRuntimeContext.getLSMRTreeIOOperationCallbackProvider();
+    }
+
+    @Override
+    public ILSMIOOperationCallbackProvider getLSMInvertedIndexIOOperationCallbackProvider() {
+        return asterixAppRuntimeContext.getLSMInvertedIndexIOOperationCallbackProvider();
+    }
+
+    @Override
+    public ILSMIOOperationCallbackProvider getNoOpIOOperationCallbackProvider() {
+        return asterixAppRuntimeContext.getNoOpIOOperationCallbackProvider();
+    }
+
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixFileMapManager.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixFileMapManager.java
new file mode 100644
index 0000000..5c6611b
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixFileMapManager.java
@@ -0,0 +1,61 @@
+package edu.uci.ics.asterix.common.context;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
+import edu.uci.ics.hyracks.api.io.FileReference;
+import edu.uci.ics.hyracks.storage.common.file.IFileMapManager;
+
+public class AsterixFileMapManager implements IFileMapManager {
+
+    private static final long serialVersionUID = 1L;
+    private Map<Integer, String> id2nameMap = new HashMap<Integer, String>();
+    private Map<String, Integer> name2IdMap = new HashMap<String, Integer>();
+    private int idCounter = 0;
+
+    @Override
+    public FileReference lookupFileName(int fileId) throws HyracksDataException {
+        String fName = id2nameMap.get(fileId);
+        if (fName == null) {
+            throw new HyracksDataException("No mapping found for id: " + fileId);
+        }
+        return new FileReference(new File(fName));
+    }
+
+    @Override
+    public int lookupFileId(FileReference fileRef) throws HyracksDataException {
+        String fileName = fileRef.getFile().getAbsolutePath();
+        Integer fileId = name2IdMap.get(fileName);
+        if (fileId == null) {
+            throw new HyracksDataException("No mapping found for name: " + fileName);
+        }
+        return fileId;
+    }
+
+    @Override
+    public boolean isMapped(FileReference fileRef) {
+        String fileName = fileRef.getFile().getAbsolutePath();
+        return name2IdMap.containsKey(fileName);
+    }
+
+    @Override
+    public boolean isMapped(int fileId) {
+        return id2nameMap.containsKey(fileId);
+    }
+
+    @Override
+    public void unregisterFile(int fileId) throws HyracksDataException {
+        String fileName = id2nameMap.remove(fileId);
+        name2IdMap.remove(fileName);
+    }
+
+    @Override
+    public void registerFile(FileReference fileRef) throws HyracksDataException {
+        Integer fileId = idCounter++;
+        String fileName = fileRef.getFile().getAbsolutePath();
+        id2nameMap.put(fileId, fileName);
+        name2IdMap.put(fileName, fileId);
+    }
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixRuntimeComponentsProvider.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixRuntimeComponentsProvider.java
new file mode 100644
index 0000000..fad0a38
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixRuntimeComponentsProvider.java
@@ -0,0 +1,96 @@
+package edu.uci.ics.asterix.common.context;
+
+import edu.uci.ics.asterix.transaction.management.ioopcallbacks.LSMBTreeIOOperationCallbackFactory;
+import edu.uci.ics.asterix.transaction.management.ioopcallbacks.LSMInvertedIndexIOOperationCallbackFactory;
+import edu.uci.ics.asterix.transaction.management.ioopcallbacks.LSMRTreeIOOperationCallbackFactory;
+import edu.uci.ics.asterix.transaction.management.opcallbacks.IndexOperationTracker;
+import edu.uci.ics.hyracks.api.context.IHyracksTaskContext;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndexLifecycleManager;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndexLifecycleManagerProvider;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationCallback;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationCallbackFactory;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationCallbackProvider;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationScheduler;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIOOperationSchedulerProvider;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIndex;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMMergePolicy;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMMergePolicyProvider;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMOperationTracker;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMOperationTrackerFactory;
+import edu.uci.ics.hyracks.storage.common.IStorageManagerInterface;
+import edu.uci.ics.hyracks.storage.common.buffercache.IBufferCache;
+import edu.uci.ics.hyracks.storage.common.file.IFileMapProvider;
+import edu.uci.ics.hyracks.storage.common.file.ILocalResourceRepository;
+import edu.uci.ics.hyracks.storage.common.file.ResourceIdFactory;
+
+public class AsterixRuntimeComponentsProvider implements IIndexLifecycleManagerProvider, IStorageManagerInterface,
+        ILSMIOOperationSchedulerProvider, ILSMMergePolicyProvider, ILSMOperationTrackerFactory,
+        ILSMIOOperationCallbackProvider {
+    private static final long serialVersionUID = 1L;
+
+    private final ILSMIOOperationCallbackFactory ioOpCallbackFactory;
+
+    public static final AsterixRuntimeComponentsProvider LSMBTREE_PROVIDER = new AsterixRuntimeComponentsProvider(
+            LSMBTreeIOOperationCallbackFactory.INSTANCE);
+    public static final AsterixRuntimeComponentsProvider LSMRTREE_PROVIDER = new AsterixRuntimeComponentsProvider(
+            LSMRTreeIOOperationCallbackFactory.INSTANCE);
+    public static final AsterixRuntimeComponentsProvider LSMINVERTEDINDEX_PROVIDER = new AsterixRuntimeComponentsProvider(
+            LSMInvertedIndexIOOperationCallbackFactory.INSTANCE);
+    public static final AsterixRuntimeComponentsProvider NOINDEX_PROVIDER = new AsterixRuntimeComponentsProvider(null);
+
+    private AsterixRuntimeComponentsProvider(ILSMIOOperationCallbackFactory ioOpCallbackFactory) {
+        this.ioOpCallbackFactory = ioOpCallbackFactory;
+    }
+
+    @Override
+    public ILSMOperationTracker createOperationTracker(ILSMIndex index) {
+        return new IndexOperationTracker(index, ioOpCallbackFactory);
+    }
+
+    @Override
+    public ILSMIOOperationCallback getIOOperationCallback(ILSMIndex index) {
+        return ((IndexOperationTracker) index.getOperationTracker()).getIOOperationCallback();
+    }
+
+    @Override
+    public ILSMIOOperationScheduler getIOScheduler(IHyracksTaskContext ctx) {
+        return ((AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext().getApplicationObject())
+                .getLSMIOScheduler();
+    }
+
+    @Override
+    public ILSMMergePolicy getMergePolicy(IHyracksTaskContext ctx) {
+        return ((AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext().getApplicationObject())
+                .getLSMMergePolicy();
+    }
+
+    @Override
+    public IBufferCache getBufferCache(IHyracksTaskContext ctx) {
+        return ((AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext().getApplicationObject())
+                .getBufferCache();
+    }
+
+    @Override
+    public IFileMapProvider getFileMapProvider(IHyracksTaskContext ctx) {
+        return ((AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext().getApplicationObject())
+                .getFileMapManager();
+    }
+
+    @Override
+    public ILocalResourceRepository getLocalResourceRepository(IHyracksTaskContext ctx) {
+        return ((AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext().getApplicationObject())
+                .getLocalResourceRepository();
+    }
+
+    @Override
+    public IIndexLifecycleManager getLifecycleManager(IHyracksTaskContext ctx) {
+        return ((AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext().getApplicationObject())
+                .getIndexLifecycleManager();
+    }
+
+    @Override
+    public ResourceIdFactory getResourceIdFactory(IHyracksTaskContext ctx) {
+        return ((AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext().getApplicationObject())
+                .getResourceIdFactory();
+    }
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/ConstantMergePolicy.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/ConstantMergePolicy.java
new file mode 100644
index 0000000..74b0aa6
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/ConstantMergePolicy.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2009-2012 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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.
+ */
+
+package edu.uci.ics.asterix.common.context;
+
+import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
+import edu.uci.ics.hyracks.storage.am.common.api.IndexException;
+import edu.uci.ics.hyracks.storage.am.common.impls.NoOpOperationCallback;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIndex;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMIndexAccessor;
+import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMMergePolicy;
+import edu.uci.ics.hyracks.storage.am.lsm.common.impls.NoOpIOOperationCallback;
+
+public class ConstantMergePolicy implements ILSMMergePolicy {
+
+    private final int threshold;
+    private final AsterixAppRuntimeContext ctx;
+
+    public ConstantMergePolicy(int threshold, AsterixAppRuntimeContext ctx) {
+        this.threshold = threshold;
+        this.ctx = ctx;
+    }
+
+    public void diskComponentAdded(final ILSMIndex index, int totalNumDiskComponents) throws HyracksDataException,
+            IndexException {
+        if (!ctx.isShuttingdown() && totalNumDiskComponents >= threshold) {
+            ILSMIndexAccessor accessor = (ILSMIndexAccessor) index.createAccessor(NoOpOperationCallback.INSTANCE,
+                    NoOpOperationCallback.INSTANCE);
+            accessor.scheduleMerge(NoOpIOOperationCallback.INSTANCE);
+        }
+    }
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/TransactionSubsystemProvider.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/TransactionSubsystemProvider.java
new file mode 100644
index 0000000..85e5375
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/TransactionSubsystemProvider.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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.
+ */
+
+package edu.uci.ics.asterix.common.context;
+
+import edu.uci.ics.asterix.transaction.management.service.transaction.ITransactionSubsystemProvider;
+import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionSubsystem;
+import edu.uci.ics.hyracks.api.context.IHyracksTaskContext;
+
+/**
+ * The purpose of this provider is to work around a cyclic dependency between asterix-common and asterix-transactions.
+ * The operation callbacks would depend on the AsterixAppRuntimeContext to get the transaction subsystem,
+ * while at the same time the AsterixAppRuntimeContext depends on asterix-transactions for the TransactionSubsystem.
+ */
+public class TransactionSubsystemProvider implements ITransactionSubsystemProvider {
+    @Override
+    public TransactionSubsystem getTransactionSubsystem(IHyracksTaskContext ctx) {
+        AsterixAppRuntimeContext runtimeCtx = (AsterixAppRuntimeContext) ctx.getJobletContext().getApplicationContext()
+                .getApplicationObject();
+        return runtimeCtx.getTransactionSubsystem();
+    }
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/dataflow/IAsterixApplicationContextInfo.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/dataflow/IAsterixApplicationContextInfo.java
new file mode 100644
index 0000000..401af28
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/dataflow/IAsterixApplicationContextInfo.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2009-2012 by The Regents of the University of California
+ * Licensed 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 from
+ *
+ *     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.
+ */
+package edu.uci.ics.asterix.common.dataflow;
+
+import edu.uci.ics.hyracks.api.application.ICCApplicationContext;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndexLifecycleManagerProvider;
+import edu.uci.ics.hyracks.storage.common.IStorageManagerInterface;
+
+/**
+ * Provides methods for obtaining the IIndexRegistryProvider, IStorageManager and
+ * ICCApplicationContext implementation.
+ */
+public interface IAsterixApplicationContextInfo {
+    public IIndexLifecycleManagerProvider getIndexLifecycleManagerProvider();
+
+    /**
+     * Returns an instance of the implementation for IStorageManagerInterface.
+     * 
+     * @return IStorageManagerInterface implementation instance
+     */
+    public IStorageManagerInterface getStorageManagerInterface();
+
+    /**
+     * Returns an instance of the implementation for ICCApplicationContext.
+     * 
+     * @return ICCApplicationContext implementation instance
+     */
+    public ICCApplicationContext getCCApplicationContext();
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/functions/FunctionArgumentsConstants.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/functions/FunctionArgumentsConstants.java
deleted file mode 100644
index 2a0ed70..0000000
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/functions/FunctionArgumentsConstants.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package edu.uci.ics.asterix.common.functions;
-
-public interface FunctionArgumentsConstants {
-    public static final String BTREE_INDEX = "btree";
-
-    public static final String RTREE_INDEX = "rtree";
-}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/functions/FunctionSignature.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/functions/FunctionSignature.java
new file mode 100644
index 0000000..188593c
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/functions/FunctionSignature.java
@@ -0,0 +1,53 @@
+package edu.uci.ics.asterix.common.functions;
+
+import java.io.Serializable;
+
+import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+
+public class FunctionSignature implements Serializable {
+    private final String namespace;
+    private final String name;
+    private final int arity;
+    private final String rep;
+
+    public FunctionSignature(String namespace, String name, int arity) {
+        this.namespace = namespace;
+        this.name = name;
+        this.arity = arity;
+        rep = namespace + "." + name + "@" + arity;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (!(o instanceof FunctionSignature)) {
+            return false;
+        } else {
+            FunctionSignature f = ((FunctionSignature) o);
+            return ((namespace != null && namespace.equals(f.getNamespace()) || (namespace == null && f.getNamespace() == null)))
+                    && name.equals(f.getName())
+                    && (arity == f.getArity() || arity == FunctionIdentifier.VARARGS || f.getArity() == FunctionIdentifier.VARARGS);
+        }
+    }
+
+    public String toString() {
+        return rep;
+    }
+
+    @Override
+    public int hashCode() {
+        return (namespace + "." + name).hashCode();
+    }
+
+    public String getNamespace() {
+        return namespace;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public int getArity() {
+        return arity;
+    }
+
+}
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/utils/UTF8CharSequence.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/utils/UTF8CharSequence.java
index fc5c930..a3ebbbd 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/utils/UTF8CharSequence.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/utils/UTF8CharSequence.java
@@ -1,63 +1,77 @@
 package edu.uci.ics.asterix.common.utils;
 
+import edu.uci.ics.hyracks.data.std.api.IValueReference;
 import edu.uci.ics.hyracks.data.std.primitive.UTF8StringPointable;
-import edu.uci.ics.hyracks.dataflow.common.data.accessors.IValueReference;
-import edu.uci.ics.hyracks.dataflow.common.data.util.StringUtils;
 
 public class UTF8CharSequence implements CharSequence {
 
-    private int start;
-    private int len;
-    private char[] buf;
+	private int start;
+	private int len;
+	private char[] buf;
 
-    public UTF8CharSequence(IValueReference valueRef, int start) {
-        reset(valueRef, start);
-    }
+	public UTF8CharSequence(IValueReference valueRef, int start) {
+		reset(valueRef, start);
+	}
 
-    public UTF8CharSequence() {
-    }
+	public UTF8CharSequence() {
+	}
 
-    @Override
-    public char charAt(int index) {
-        if (index >= len || index < 0) {
-            throw new IndexOutOfBoundsException("No index " + index + " for string of length " + len);
-        }
-        return buf[index];
-    }
+	@Override
+	public char charAt(int index) {
+		if (index >= len || index < 0) {
+			throw new IndexOutOfBoundsException("No index " + index
+					+ " for string of length " + len);
+		}
+		return buf[index];
+	}
 
-    @Override
-    public int length() {
-        return len;
-    }
+	@Override
+	public int length() {
+		return len;
+	}
 
-    @Override
-    public CharSequence subSequence(int start, int end) {
-        UTF8CharSequence carSeq = new UTF8CharSequence();
-        carSeq.len = end - start + 1;
-        carSeq.buf = new char[carSeq.len];
-        System.arraycopy(buf, start, carSeq.buf, 0, carSeq.len);
-        return carSeq;
-    }
+	@Override
+	public CharSequence subSequence(int start, int end) {
+		UTF8CharSequence carSeq = new UTF8CharSequence();
+		carSeq.len = end - start;
+		if (end != start) {
+			carSeq.buf = new char[carSeq.len];
+			System.arraycopy(buf, start, carSeq.buf, 0, carSeq.len);
+		}
+		return carSeq;
+	}
 
-    public void reset(IValueReference valueRef, int start) {
-        this.start = start;
-        resetLength(valueRef);
-        if (buf == null || buf.length < len) {
-            buf = new char[len];
-        }
-        int sStart = start + 2;
-        int c = 0;
-        int i = 0;
-        byte[] bytes = valueRef.getBytes();
-        while (c < len) {
-            buf[i++] = UTF8StringPointable.charAt(bytes, sStart + c);
-            c += UTF8StringPointable.charSize(bytes, sStart + c);
-        }
+	public void reset(IValueReference valueRef, int start) {
+		this.start = start;
+		resetLength(valueRef);
+		if (buf == null || buf.length < len) {
+			buf = new char[len];
+		}
+		int sStart = start + 2;
+		int c = 0;
+		int i = 0;
+		byte[] bytes = valueRef.getByteArray();
+		while (c < len) {
+			buf[i++] = UTF8StringPointable.charAt(bytes, sStart + c);
+			c += UTF8StringPointable.charSize(bytes, sStart + c);
+		}
 
-    }
+	}
 
-    private void resetLength(IValueReference valueRef) {
-        this.len = UTF8StringPointable.getUTFLen(valueRef.getBytes(), start);
-    }
+	private void resetLength(IValueReference valueRef) {
+		this.len = UTF8StringPointable.getUTFLength(valueRef.getByteArray(),
+				start);
+	}
+
+	@Override
+	public String toString() {
+		StringBuffer bf = new StringBuffer();
+		if (buf != null) {
+			for (int i = 0; i < buf.length; i++) {
+				bf.append(buf[i]);
+			}
+		}
+		return new String(bf);
+	}
 
 }