<commit_before>/**
 * 
 * Copyright 2005-2006 The Apache Software Foundation
 * 
 * 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 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.
 */
/**
 * Optimized writes to a RandomAcessFile
 * 
 * @version $Revision: 1.1.1.1 $
 */
package org.apache.activemq.kaha.impl;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.activemq.kaha.ListContainer;
import org.apache.activemq.kaha.MapContainer;
import org.apache.activemq.kaha.RuntimeStoreException;
import org.apache.activemq.kaha.Store;
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
/**
 * Optimized Store writer
 * 
 * @version $Revision: 1.1.1.1 $
 */
public class KahaStore implements Store{
    private static final String DEFAULT_CONTAINER_NAME = "data-container";
    private File directory;
    private DataManager rootData;
    private DataManager defaultContainerManager;
    private IndexManager indexManager;
    private IndexRootContainer mapsContainer;
    private IndexRootContainer listsContainer;
    private Map lists=new ConcurrentHashMap();
    private Map maps=new ConcurrentHashMap();
    private Map dataManagers = new ConcurrentHashMap();
    private boolean closed=false;
    private String name;
    private String mode;
    private boolean initialized;

    public KahaStore(String name,String mode) throws IOException{
        this.name=name;
        this.mode=mode;
        initialize();
    }

    public synchronized void close() throws IOException{
        if(!closed){
            closed=true;
            if(initialized){
                indexManager.close();
                rootData.close();
                defaultContainerManager.close();
            }
        }
    }

    public synchronized void force() throws IOException{
        if(initialized){
            indexManager.force();
            rootData.force();
            defaultContainerManager.force();
        }
    }

    public synchronized void clear() throws IOException{
        initialize();
        for(Iterator i=maps.values().iterator();i.hasNext();){
            BaseContainerImpl container=(BaseContainerImpl) i.next();
            container.clear();
        }
        for(Iterator i=lists.values().iterator();i.hasNext();){
            BaseContainerImpl container=(BaseContainerImpl) i.next();
            container.clear();
        }
        lists.clear();
        maps.clear();
    }

    public synchronized boolean delete() throws IOException{
        initialize();
        clear();
        boolean result=indexManager.delete();
        result&=rootData.delete();
        result&=defaultContainerManager.delete();
        initialized=false;
        return result;
    }

    public boolean doesMapContainerExist(Object id) throws IOException{
        initialize();
        return maps.containsKey(id);
    }

    public MapContainer getMapContainer(Object id) throws IOException{
        return getMapContainer(id, DEFAULT_CONTAINER_NAME);
    }
    
    public synchronized MapContainer getMapContainer(Object id, String dataContainerName) throws IOException{
        initialize();
       
        MapContainer result=(MapContainer) maps.get(id);
        if(result==null){
            DataManager dm = getDataManager(dataContainerName);
            ContainerId containerId = new ContainerId();
            containerId.setKey(id);
            containerId.setDataContainerPrefix(dataContainerName);
            IndexItem root=mapsContainer.addRoot(containerId);
            result=new MapContainerImpl(containerId,root,indexManager,dm);
            maps.put(containerId.getKey(),result);
        }
        return result;
    }

    public void deleteMapContainer(Object id) throws IOException{
        initialize();
        MapContainerImpl container=(MapContainerImpl) maps.remove(id);
        if(container!=null){
            container.clear();
            mapsContainer.removeRoot(container.getContainerId());
        }
    }

    public Set getMapContainerIds() throws IOException{
        initialize();
        return maps.keySet();
    }

    public boolean doesListContainerExist(Object id) throws IOException{
        initialize();
        return lists.containsKey(id);
    }

    public ListContainer getListContainer(Object id) throws IOException{
        return getListContainer(id,DEFAULT_CONTAINER_NAME);
    }
    
    public synchronized ListContainer getListContainer(Object id, String dataContainerName) throws IOException{
        initialize();
       
        ListContainer result=(ListContainer) lists.get(id);
        if(result==null){
            DataManager dm = getDataManager(dataContainerName);
            ContainerId containerId = new ContainerId();
            containerId.setKey(id);
            containerId.setDataContainerPrefix(dataContainerName);
            IndexItem root=listsContainer.addRoot(containerId);
            result=new ListContainerImpl(containerId,root,indexManager,dm);
            lists.put(containerId.getKey(),result);
        }
        return result;
    }

    public void deleteListContainer(Object id) throws IOException{
        initialize();
        ListContainerImpl container=(ListContainerImpl) lists.remove(id);
        if(container!=null){
            container.clear();
            listsContainer.removeRoot(container.getContainerId());
        }
    }

    public Set getListContainerIds() throws IOException{
        initialize();
        return lists.keySet();
    }

    protected void checkClosed(){
        if(closed){
            throw new RuntimeStoreException("The store is closed");
        }
    }

    protected synchronized void initialize() throws IOException{
        if(!initialized){
            initialized=true;
            directory=new File(name);
            directory.mkdirs();<commit_msg>Don't open data files again if the store has been closed.. on windows tests were failing since the open data files could not be deleted.<commit_after>/**
 * 
 * Copyright 2005-2006 The Apache Software Foundation
 * 
 * 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 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.
 */
/**
 * Optimized writes to a RandomAcessFile
 * 
 * @version $Revision: 1.1.1.1 $
 */
package org.apache.activemq.kaha.impl;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.activemq.kaha.ListContainer;
import org.apache.activemq.kaha.MapContainer;
import org.apache.activemq.kaha.RuntimeStoreException;
import org.apache.activemq.kaha.Store;
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
/**
 * Optimized Store writer
 * 
 * @version $Revision: 1.1.1.1 $
 */
public class KahaStore implements Store{
    private static final String DEFAULT_CONTAINER_NAME = "data-container";
    private File directory;
    private DataManager rootData;
    private DataManager defaultContainerManager;
    private IndexManager indexManager;
    private IndexRootContainer mapsContainer;
    private IndexRootContainer listsContainer;
    private Map lists=new ConcurrentHashMap();
    private Map maps=new ConcurrentHashMap();
    private Map dataManagers = new ConcurrentHashMap();
    private boolean closed=false;
    private String name;
    private String mode;
    private boolean initialized;

    public KahaStore(String name,String mode) throws IOException{
        this.name=name;
        this.mode=mode;
        initialize();
    }

    public synchronized void close() throws IOException{
        if(!closed){
            closed=true;
            if(initialized){
                indexManager.close();
                rootData.close();
                defaultContainerManager.close();
            }
        }
    }

    public synchronized void force() throws IOException{
        if(initialized){
            indexManager.force();
            rootData.force();
            defaultContainerManager.force();
        }
    }

    public synchronized void clear() throws IOException{
        initialize();
        for(Iterator i=maps.values().iterator();i.hasNext();){
            BaseContainerImpl container=(BaseContainerImpl) i.next();
            container.clear();
        }
        for(Iterator i=lists.values().iterator();i.hasNext();){
            BaseContainerImpl container=(BaseContainerImpl) i.next();
            container.clear();
        }
        lists.clear();
        maps.clear();
    }

    public synchronized boolean delete() throws IOException{
        initialize();
        clear();
        boolean result=indexManager.delete();
        result&=rootData.delete();
        result&=defaultContainerManager.delete();
        initialized=false;
        return result;
    }

    public boolean doesMapContainerExist(Object id) throws IOException{
        initialize();
        return maps.containsKey(id);
    }

    public MapContainer getMapContainer(Object id) throws IOException{
        return getMapContainer(id, DEFAULT_CONTAINER_NAME);
    }
    
    public synchronized MapContainer getMapContainer(Object id, String dataContainerName) throws IOException{
        initialize();
       
        MapContainer result=(MapContainer) maps.get(id);
        if(result==null){
            DataManager dm = getDataManager(dataContainerName);
            ContainerId containerId = new ContainerId();
            containerId.setKey(id);
            containerId.setDataContainerPrefix(dataContainerName);
            IndexItem root=mapsContainer.addRoot(containerId);
            result=new MapContainerImpl(containerId,root,indexManager,dm);
            maps.put(containerId.getKey(),result);
        }
        return result;
    }

    public void deleteMapContainer(Object id) throws IOException{
        initialize();
        MapContainerImpl container=(MapContainerImpl) maps.remove(id);
        if(container!=null){
            container.clear();
            mapsContainer.removeRoot(container.getContainerId());
        }
    }

    public Set getMapContainerIds() throws IOException{
        initialize();
        return maps.keySet();
    }

    public boolean doesListContainerExist(Object id) throws IOException{
        initialize();
        return lists.containsKey(id);
    }

    public ListContainer getListContainer(Object id) throws IOException{
        return getListContainer(id,DEFAULT_CONTAINER_NAME);
    }
    
    public synchronized ListContainer getListContainer(Object id, String dataContainerName) throws IOException{
        initialize();
       
        ListContainer result=(ListContainer) lists.get(id);
        if(result==null){
            DataManager dm = getDataManager(dataContainerName);
            ContainerId containerId = new ContainerId();
            containerId.setKey(id);
            containerId.setDataContainerPrefix(dataContainerName);
            IndexItem root=listsContainer.addRoot(containerId);
            result=new ListContainerImpl(containerId,root,indexManager,dm);
            lists.put(containerId.getKey(),result);
        }
        return result;
    }

    public void deleteListContainer(Object id) throws IOException{
        initialize();
        ListContainerImpl container=(ListContainerImpl) lists.remove(id);
        if(container!=null){
            container.clear();
            listsContainer.removeRoot(container.getContainerId());
        }
    }

    public Set getListContainerIds() throws IOException{
        initialize();
        return lists.keySet();
    }

    protected void checkClosed(){
        if(closed){
            throw new RuntimeStoreException("The store is closed");
        }
    }

    protected synchronized void initialize() throws IOException{
    	if( closed )
    		throw new IOException("Store has been closed.");
        if(!initialized){
            initialized=true;
            directory=new File(name);
            directory.mkdirs();
{'fraction_non_alphanumeric': 0.09085261686597297, 'fraction_numerical': 0.0031060723714862555, 'mean_word_length': 2.8002360578341694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2145176', 'n_tokens_mistral': 3168, 'n_tokens_neox': 3102, 'n_words': 831}