<commit_before>/*******************************************************************************
 *
 *    Copyright (C) 2015-2018 the BBoxDB project
 *  
 *    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. 
 *    
 *******************************************************************************/
package org.bboxdb.network.server.connection.handler.request;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

import org.bboxdb.distribution.partitioner.DistributionRegionState;
import org.bboxdb.distribution.partitioner.SpacePartitioner;
import org.bboxdb.distribution.partitioner.SpacePartitionerCache;
import org.bboxdb.distribution.region.DistributionRegion;
import org.bboxdb.distribution.zookeeper.DistributionGroupAdapter;
import org.bboxdb.distribution.zookeeper.DistributionRegionAdapter;
import org.bboxdb.distribution.zookeeper.ZookeeperClientFactory;
import org.bboxdb.distribution.zookeeper.ZookeeperException;
import org.bboxdb.network.packages.PackageEncodeException;
import org.bboxdb.network.packages.request.CreateDistributionGroupRequest;
import org.bboxdb.network.packages.response.ErrorResponse;
import org.bboxdb.network.packages.response.SuccessResponse;
import org.bboxdb.network.server.ErrorMessages;
import org.bboxdb.network.server.connection.ClientConnectionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateDistributionGroupHandler implements RequestHandler {
	
	/**
	 * The Logger
	 */
	private final static Logger logger = LoggerFactory.getLogger(CreateDistributionGroupHandler.class);
	

	@Override
	/**
	 * Create a new distribution group
	 */
	public boolean handleRequest(final ByteBuffer encodedPackage, 
			final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {

		String distributionGroup = null;
		
		try {
			final CreateDistributionGroupRequest createPackage = CreateDistributionGroupRequest.decodeTuple(encodedPackage);
			distributionGroup = createPackage.getDistributionGroup();
			logger.info("Create distribution group: {}", distributionGroup);
			
			final DistributionGroupAdapter distributionGroupAdapter 
				= ZookeeperClientFactory.getZookeeperClient().getDistributionGroupAdapter();
			
			final DistributionRegionAdapter distributionRegionAdapter 
				= ZookeeperClientFactory.getZookeeperClient().getDistributionRegionAdapter();
			
			final List<String> knownGroups = distributionGroupAdapter.getDistributionGroups();
			if(knownGroups.contains(distributionGroup)) {
				logger.error("Untable to create distributon group {}, already exists", distributionGroup);
				final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_DGROUP_EXISTS);
				clientConnectionHandler.writeResultPackage(responsePackage);
				return true;
			}
			
			distributionGroupAdapter.createDistributionGroup(distributionGroup, 
					createPackage.getDistributionGroupConfiguration());
			
			final SpacePartitioner spacePartitioner = SpacePartitionerCache.getInstance()
					.getSpacePartitionerForGroupName(distributionGroup);

			final DistributionRegion region = spacePartitioner.getRootNode();
			
			distributionRegionAdapter.setStateForDistributionRegion(region, DistributionRegionState.ACTIVE);
			
			clientConnectionHandler.writeResultPackage(new SuccessResponse(packageSequence));
		} catch (Exception e) {
			logger.warn("Error while create distribution group", e);
			
			deleteHalfWrittenDistributionGroup(distributionGroup);
			
			final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
			clientConnectionHandler.writeResultPackage(responsePackage);
		}
		
		return true;
	}

	/**
	 * Delete the half written distribution group in Zookeeper
	 * @param distributionGroup
	 */
	private void deleteHalfWrittenDistributionGroup(final String distributionGroup) {
		
		if(distributionGroup == null) {
			return;
		}
	
		try {
			final DistributionGroupAdapter distributionGroupZookeeperAdapter 
				= ZookeeperClientFactory.getZookeeperClient().getDistributionGroupAdapter();
			distributionGroupZookeeperAdapter.deleteDistributionGroup(distributionGroup);
		} catch (ZookeeperException e) {
			logger.error("Got an error during deletion of incorrect created distribution group", e);
		}
	
	}
}<commit_msg>Clear cached data<commit_after>/*******************************************************************************
 *
 *    Copyright (C) 2015-2018 the BBoxDB project
 *  
 *    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. 
 *    
 *******************************************************************************/
package org.bboxdb.network.server.connection.handler.request;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

import org.bboxdb.distribution.TupleStoreConfigurationCache;
import org.bboxdb.distribution.partitioner.DistributionRegionState;
import org.bboxdb.distribution.partitioner.SpacePartitioner;
import org.bboxdb.distribution.partitioner.SpacePartitionerCache;
import org.bboxdb.distribution.region.DistributionRegion;
import org.bboxdb.distribution.zookeeper.DistributionGroupAdapter;
import org.bboxdb.distribution.zookeeper.DistributionRegionAdapter;
import org.bboxdb.distribution.zookeeper.ZookeeperClientFactory;
import org.bboxdb.distribution.zookeeper.ZookeeperException;
import org.bboxdb.network.packages.PackageEncodeException;
import org.bboxdb.network.packages.request.CreateDistributionGroupRequest;
import org.bboxdb.network.packages.response.ErrorResponse;
import org.bboxdb.network.packages.response.SuccessResponse;
import org.bboxdb.network.server.ErrorMessages;
import org.bboxdb.network.server.connection.ClientConnectionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateDistributionGroupHandler implements RequestHandler {
	
	/**
	 * The Logger
	 */
	private final static Logger logger = LoggerFactory.getLogger(CreateDistributionGroupHandler.class);
	

	@Override
	/**
	 * Create a new distribution group
	 */
	public boolean handleRequest(final ByteBuffer encodedPackage, 
			final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {

		String distributionGroup = null;
		
		try {
			final CreateDistributionGroupRequest createPackage = CreateDistributionGroupRequest.decodeTuple(encodedPackage);
			distributionGroup = createPackage.getDistributionGroup();
			logger.info("Create distribution group: {}", distributionGroup);
			
			final DistributionGroupAdapter distributionGroupAdapter 
				= ZookeeperClientFactory.getZookeeperClient().getDistributionGroupAdapter();
			
			final DistributionRegionAdapter distributionRegionAdapter 
				= ZookeeperClientFactory.getZookeeperClient().getDistributionRegionAdapter();
			
			final List<String> knownGroups = distributionGroupAdapter.getDistributionGroups();
			if(knownGroups.contains(distributionGroup)) {
				logger.error("Untable to create distributon group {}, already exists", distributionGroup);
				final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_DGROUP_EXISTS);
				clientConnectionHandler.writeResultPackage(responsePackage);
				return true;
			}
			
			distributionGroupAdapter.createDistributionGroup(distributionGroup, 
					createPackage.getDistributionGroupConfiguration());
			
			final SpacePartitioner spacePartitioner = SpacePartitionerCache.getInstance()
					.getSpacePartitionerForGroupName(distributionGroup);

			final DistributionRegion region = spacePartitioner.getRootNode();
			
			distributionRegionAdapter.setStateForDistributionRegion(region, DistributionRegionState.ACTIVE);
			
			clientConnectionHandler.writeResultPackage(new SuccessResponse(packageSequence));
		} catch (Exception e) {
			logger.warn("Error while create distribution group", e);
			
			deleteHalfWrittenDistributionGroup(distributionGroup);
			
			final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
			clientConnectionHandler.writeResultPackage(responsePackage);
		}
		
		return true;
	}

	/**
	 * Delete the half written distribution group in Zookeeper
	 * @param distributionGroup
	 */
	private void deleteHalfWrittenDistributionGroup(final String distributionGroup) {
		
		if(distributionGroup == null) {
			return;
		}
	
		try {
			final DistributionGroupAdapter distributionGroupZookeeperAdapter 
				= ZookeeperClientFactory.getZookeeperClient().getDistributionGroupAdapter();
			distributionGroupZookeeperAdapter.deleteDistributionGroup(distributionGroup);
		} catch (ZookeeperException e) {
			logger.error("Got an error during deletion of incorrect created distribution group", e);
		} finally {
			TupleStoreConfigurationCache.getInstance().clear();
		}
	
	}
}
{'fraction_non_alphanumeric': 0.10316022544283414, 'fraction_numerical': 0.002818035426731079, 'mean_word_length': 5.727826675693974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '382919', 'n_tokens_mistral': 2750, 'n_tokens_neox': 2350, 'n_words': 554}