<commit_before>package org.jsonq.provider;

import org.jsonq.*;
import org.jsonq.util.*;
import java.util.*;

import static org.jsonq.JSONQConstants.*;

/**
 * Basic, generic implementation of the database API.
 */
public class DefaultDatabase implements Database {

	protected final Map<String,Store> _storeMap;
	protected final Map<String,StoreFactory> _providerMap;

	/**
	 * Constructor 
	 */
	public DefaultDatabase() {
		_storeMap = new HashMap<>(16);
		_providerMap = new HashMap<>(16);
	}

	/**
	 * Registers a new store provider with this Database.
	 *
	 * @param providerName the name of the store provider. This is used when provisioning a new
	 * store to determine which provider to use
	 * @param factory the factory which can create stores
	 */
	public void registerProvider( String providerName, StoreFactory factory ) {
		synchronized ( _providerMap ) {
			_providerMap.put( providerName, factory );
		}
	}

	/**
	 * Provisions a new store on this Database
	 *
	 * @param request the valid JSON/q request
	 *
	 * @return a Future representing the resultant JSON/q response
	 */
	public Future<JSONObject,JSONObject> provision( JSONObject request ) {
		Future<JSONObject,JSONObject> future = null; // TODO
		Scheduler.runAsync( new ProvisionCommand( future, request ) );
		return future;
	}

	/**
	 * Saves an item
	 *
	 * @param request the valid JSON/q request
	 *
	 * @return a Future representing the result of the save operation
	 */
	public Future<JSONObject,JSONObject> save( JSONObject request ) {
		return null;
	}

	//----------------------------------------
	// Commands
	//----------------------------------------

	/**
	 * Base class for DB commands
	 */
	public abstract class Command implements Runnable {

		protected final Future<JSONObject,JSONObject> _future;
		protected final JSONObject _request;

		/**
		 * Constructor 
		 */
		protected Command( Future<JSONObject,JSONObject> future, JSONObject request ) {
			_future = future;
			_request = request;

		}

		/**
		 * Signals that this command succeeded. Creates and sends the response
		 *
		 * @param payload the result payload to send
		 */
		protected void complete( JSONObject payload ) {
			complete( payload, true );
		}

		/**
		 * Signals that this command failed. Creates and sends the response
		 *
		 * @param error the error payload
		 */
		protected void fail( JSONObject error ) {
			complete( error, false );
		}

		/**
		 * Signals that this command failed. Creates and sends the response
		 *
		 * @param code the error code
		 * @param message the error message
		 * @param args any arguments which need to be added
		 */
		protected void fail( String code, String message, String... args ) {
			JSONObject error = JSONQ.createObject();
			error.put( "code", code );
			error.put( "message", message );
			if ( null != args && args.length > 0 ) {
				error.put( "args", Arrays.asList( args ));
			}
			complete( error, false );
		}

		/**
		 * Signals that this command completed
		 *
		 * @param payload the result payload to send
		 * @param success true if the command completed successfully
		 */
		private void complete( JSONObject payload, boolean success ) {
			JSONObject response = JSONQ.createObject();
			response.put( Response.REQUEST_ID, _request.getString( Request.ID ) );
			response.put( Response.SUCCESS, success );
			response.put( Response.PAYLOAD, payload );

			// TODO: set result on Future
		}
	}

	/**
	 * Runnable for provisioning a new store
	 */
	public class ProvisionCommand extends Command {

		/**
		 * Constructor 
		 */
		protected ProvisionCommand( Future<JSONObject,JSONObject> future, JSONObject request ) {
			super( future, request );
		}

		/**
		 * Called to execute this command
		 */
		public void run() {<commit_msg>Hook up future with real impl<commit_after>package org.jsonq.provider;

import org.jsonq.*;
import org.jsonq.util.*;
import java.util.*;

import static org.jsonq.JSONQConstants.*;

/**
 * Basic, generic implementation of the database API.
 */
public class DefaultDatabase implements Database {

	protected final Map<String,Store> _storeMap;
	protected final Map<String,StoreFactory> _providerMap;

	/**
	 * Constructor 
	 */
	public DefaultDatabase() {
		_storeMap = new HashMap<>(16);
		_providerMap = new HashMap<>(16);
	}

	/**
	 * Registers a new store provider with this Database.
	 *
	 * @param providerName the name of the store provider. This is used when provisioning a new
	 * store to determine which provider to use
	 * @param factory the factory which can create stores
	 */
	public void registerProvider( String providerName, StoreFactory factory ) {
		synchronized ( _providerMap ) {
			_providerMap.put( providerName, factory );
		}
	}

	/**
	 * Provisions a new store on this Database
	 *
	 * @param request the valid JSON/q request
	 *
	 * @return a Future representing the resultant JSON/q response
	 */
	public Future<JSONObject,JSONObject> provision( JSONObject request ) {
		FutureImpl<JSONObject,JSONObject> future = new FutureImpl<JSONObject,JSONObject>();
		Scheduler.runAsync( new ProvisionCommand( future, request ) );
		return future;
	}

	/**
	 * Saves an item
	 *
	 * @param request the valid JSON/q request
	 *
	 * @return a Future representing the result of the save operation
	 */
	public Future<JSONObject,JSONObject> save( JSONObject request ) {
		return null;
	}

	//----------------------------------------
	// Commands
	//----------------------------------------

	/**
	 * Base class for DB commands
	 */
	public abstract class Command implements Runnable {

		protected final FutureImpl<JSONObject,JSONObject> _future;
		protected final JSONObject _request;

		/**
		 * Constructor 
		 */
		protected Command( FutureImpl<JSONObject,JSONObject> future, JSONObject request ) {
			_future = future;
			_request = request;
		}

		/**
		 * Signals that this command succeeded. Creates and sends the response
		 *
		 * @param payload the result payload to send
		 */
		protected void complete( JSONObject payload ) {
			complete( payload, true );
		}

		/**
		 * Signals that this command failed. Creates and sends the response
		 *
		 * @param error the error payload
		 */
		protected void fail( JSONObject error ) {
			complete( error, false );
		}

		/**
		 * Signals that this command failed. Creates and sends the response
		 *
		 * @param code the error code
		 * @param message the error message
		 * @param args any arguments which need to be added
		 */
		protected void fail( String code, String message, String... args ) {
			JSONObject error = JSONQ.createObject();
			error.put( "code", code );
			error.put( "message", message );
			if ( null != args && args.length > 0 ) {
				error.put( "args", Arrays.asList( args ));
			}
			complete( error, false );
		}

		/**
		 * Signals that this command completed
		 *
		 * @param payload the result payload to send
		 * @param success true if the command completed successfully
		 */
		private void complete( JSONObject payload, boolean success ) {
			JSONObject response = JSONQ.createObject();
			response.put( Response.REQUEST_ID, _request.getString( Request.ID ) );
			response.put( Response.SUCCESS, success );
			response.put( Response.PAYLOAD, payload );

			if ( success ) {
				_future.complete( response );
			} else {
				_future.fail( response );
			}
		}
	}

	/**
	 * Runnable for provisioning a new store
	 */
	public class ProvisionCommand extends Command {

		/**
		 * Constructor 
		 */
		protected ProvisionCommand( FutureImpl<JSONObject,JSONObject> future, JSONObject request ) {
			super( future, request );
		}

		/**
		 * Called to execute this command
		 */
		public void run() {
{'fraction_non_alphanumeric': 0.11850691725398069, 'fraction_numerical': 0.0013051422605063951, 'mean_word_length': 3.001566579634465, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4552712', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2021, 'n_words': 737}