<commit_before>/*******************************************************************************
 * Copyright 2013-2014 Aerospike, Inc.
 *
 * 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.
 ******************************************************************************/

#include <Python.h>
#include <stdbool.h>

#include <aerospike/as_error.h>
#include <aerospike/as_policy.h>
#include <aerospike/as_status.h>
#include "aerospike/as_scan.h"

#include "policy.h"

// Policy names
#define PY_POLICY_TIMEOUT "timeout"		// Number of milliseconds to wait
#define PY_POLICY_RETRY   "retry"		// Behavior of failed operations
#define PY_POLICY_KEY     "key"			// Behavior of the key
#define PY_POLICY_GEN     "gen"			// Behavior of the Generation value
#define PY_POLICY_EXISTS  "exists"		// Behavior for record existence

#define POLICY_INIT(__policy) \
	as_error_reset(err);\
if ( ! py_policy || py_policy == Py_None ) {\
	return err->code;\
}\
if ( ! PyDict_Check(py_policy) ) {\
	return as_error_update(err, AEROSPIKE_ERR_PARAM, "policy must be a dict");\
}\
__policy##_init(policy);\

#define POLICY_UPDATE() \
	*policy_p = policy;

#define POLICY_SET_FIELD(__field, __type) { \
	PyObject * py_field = PyDict_GetItemString(py_policy, #__field);\
	if ( py_field ) {\
		if ( PyInt_Check(py_field) ) {\
			policy->__field = (__type) PyInt_AsLong(py_field);\
		}\
		else {\
			return as_error_update(err, AEROSPIKE_ERR_PARAM, "%s is invalid", #__field);\
		}\
	}\
}

/*
 *******************************************************************************************************
 * Mapping of constant number to constant name string.
 *******************************************************************************************************
 */
static
AerospikeConstants aerospike_constants[] = {
	{ AS_POLICY_RETRY_NONE                 ,   "POLICY_RETRY_NONE" },
	{ AS_POLICY_RETRY_ONCE                 ,   "POLICY_RETRY_ONCE" },
	{ AS_POLICY_EXISTS_IGNORE              ,   "POLICY_EXISTS_IGNORE" },
	{ AS_POLICY_EXISTS_CREATE              ,   "POLICY_EXISTS_CREATE" },
	{ AS_POLICY_EXISTS_UPDATE              ,   "POLICY_EXISTS_UPDATE" },
	{ AS_POLICY_EXISTS_REPLACE             ,   "POLICY_EXISTS_REPLACE" },
	{ AS_POLICY_EXISTS_CREATE_OR_REPLACE   ,   "POLICY_EXISTS_CREATE_OR_REPLACE" },
	{ AS_UDF_TYPE_LUA                      ,   "UDF_TYPE_LUA" },
	{ AS_POLICY_KEY_DIGEST                 ,   "POLICY_KEY_DIGEST" },
	{ AS_POLICY_KEY_SEND                   ,   "POLICY_KEY_SEND" },
	{ AS_POLICY_GEN_IGNORE                 ,   "POLICY_GEN_IGNORE" },
	{ AS_POLICY_GEN_EQ                     ,   "POLICY_GEN_EQ" },
	{ AS_POLICY_GEN_GT                     ,   "POLICY_GEN_GT" },
	{ AS_SCAN_PRIORITY_AUTO                ,   "SCAN_PRIORITY_AUTO" },
	{ AS_SCAN_PRIORITY_LOW                 ,   "SCAN_PRIORITY_AUTO" },
	{ AS_SCAN_PRIORITY_MEDIUM              ,   "SCAN_PRIORITY_MEDIUM" },
	{ AS_SCAN_PRIORITY_HIGH                ,   "SCAN_PRIORITY_HIGH" },
	{ AS_SCAN_STATUS_COMPLETED             ,   "SCAN_STATUS_COMPLETED" },
	{ AS_SCAN_STATUS_ABORTED               ,   "SCAN_STATUS_ABORTED" },
	{ AS_SCAN_STATUS_UNDEF                 ,   "SCAN_STATUS_UNDEF" },
	{ AS_SCAN_STATUS_INPROGRESS            ,   "SCAN_STATUS_INPROGRESS" },
	{ AS_POLICY_REPLICA_MASTER             ,   "POLICY_REPLICA_MASTER" },
	{ AS_POLICY_REPLICA_ANY                 ,   "POLICY_REPLICA_ANY" },
	{ AS_POLICY_CONSISTENCY_LEVEL_ONE       ,   "POLICY_CONSISTENCY_ONE" },
	{ AS_POLICY_CONSISTENCY_LEVEL_ALL       ,   "POLICY_CONSISTENCY_ALL" },
	{ AS_POLICY_COMMIT_LEVEL_ALL            ,   "POLICY_COMMIT_LEVEL_ALL" },
	{ AS_POLICY_COMMIT_LEVEL_MASTER         ,   "POLICY_COMMIT_LEVEL_MASTER" }
};

/**
 * Function for setting scan parameters in scan.
 * Like Scan Priority, Percentage, Concurrent, Nobins
 *
 * @param err                   The as_error to be populated by the function
 *                              with the encountered error if any.
 * @scan_p                      Scan parameter.
 * @py_options                  The user's optional scan options.<commit_msg>False mapping of AS_SCAN_PRIORITY_LOW is resolved.<commit_after>/*******************************************************************************
 * Copyright 2013-2014 Aerospike, Inc.
 *
 * 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.
 ******************************************************************************/

#include <Python.h>
#include <stdbool.h>

#include <aerospike/as_error.h>
#include <aerospike/as_policy.h>
#include <aerospike/as_status.h>
#include "aerospike/as_scan.h"

#include "policy.h"

// Policy names
#define PY_POLICY_TIMEOUT "timeout"		// Number of milliseconds to wait
#define PY_POLICY_RETRY   "retry"		// Behavior of failed operations
#define PY_POLICY_KEY     "key"			// Behavior of the key
#define PY_POLICY_GEN     "gen"			// Behavior of the Generation value
#define PY_POLICY_EXISTS  "exists"		// Behavior for record existence

#define POLICY_INIT(__policy) \
	as_error_reset(err);\
if ( ! py_policy || py_policy == Py_None ) {\
	return err->code;\
}\
if ( ! PyDict_Check(py_policy) ) {\
	return as_error_update(err, AEROSPIKE_ERR_PARAM, "policy must be a dict");\
}\
__policy##_init(policy);\

#define POLICY_UPDATE() \
	*policy_p = policy;

#define POLICY_SET_FIELD(__field, __type) { \
	PyObject * py_field = PyDict_GetItemString(py_policy, #__field);\
	if ( py_field ) {\
		if ( PyInt_Check(py_field) ) {\
			policy->__field = (__type) PyInt_AsLong(py_field);\
		}\
		else {\
			return as_error_update(err, AEROSPIKE_ERR_PARAM, "%s is invalid", #__field);\
		}\
	}\
}

/*
 *******************************************************************************************************
 * Mapping of constant number to constant name string.
 *******************************************************************************************************
 */
static
AerospikeConstants aerospike_constants[] = {
	{ AS_POLICY_RETRY_NONE                 ,   "POLICY_RETRY_NONE" },
	{ AS_POLICY_RETRY_ONCE                 ,   "POLICY_RETRY_ONCE" },
	{ AS_POLICY_EXISTS_IGNORE              ,   "POLICY_EXISTS_IGNORE" },
	{ AS_POLICY_EXISTS_CREATE              ,   "POLICY_EXISTS_CREATE" },
	{ AS_POLICY_EXISTS_UPDATE              ,   "POLICY_EXISTS_UPDATE" },
	{ AS_POLICY_EXISTS_REPLACE             ,   "POLICY_EXISTS_REPLACE" },
	{ AS_POLICY_EXISTS_CREATE_OR_REPLACE   ,   "POLICY_EXISTS_CREATE_OR_REPLACE" },
	{ AS_UDF_TYPE_LUA                      ,   "UDF_TYPE_LUA" },
	{ AS_POLICY_KEY_DIGEST                 ,   "POLICY_KEY_DIGEST" },
	{ AS_POLICY_KEY_SEND                   ,   "POLICY_KEY_SEND" },
	{ AS_POLICY_GEN_IGNORE                 ,   "POLICY_GEN_IGNORE" },
	{ AS_POLICY_GEN_EQ                     ,   "POLICY_GEN_EQ" },
	{ AS_POLICY_GEN_GT                     ,   "POLICY_GEN_GT" },
	{ AS_SCAN_PRIORITY_AUTO                ,   "SCAN_PRIORITY_AUTO" },
	{ AS_SCAN_PRIORITY_LOW                 ,   "SCAN_PRIORITY_LOW" },
	{ AS_SCAN_PRIORITY_MEDIUM              ,   "SCAN_PRIORITY_MEDIUM" },
	{ AS_SCAN_PRIORITY_HIGH                ,   "SCAN_PRIORITY_HIGH" },
	{ AS_SCAN_STATUS_COMPLETED             ,   "SCAN_STATUS_COMPLETED" },
	{ AS_SCAN_STATUS_ABORTED               ,   "SCAN_STATUS_ABORTED" },
	{ AS_SCAN_STATUS_UNDEF                 ,   "SCAN_STATUS_UNDEF" },
	{ AS_SCAN_STATUS_INPROGRESS            ,   "SCAN_STATUS_INPROGRESS" },
	{ AS_POLICY_REPLICA_MASTER             ,   "POLICY_REPLICA_MASTER" },
	{ AS_POLICY_REPLICA_ANY                 ,   "POLICY_REPLICA_ANY" },
	{ AS_POLICY_CONSISTENCY_LEVEL_ONE       ,   "POLICY_CONSISTENCY_ONE" },
	{ AS_POLICY_CONSISTENCY_LEVEL_ALL       ,   "POLICY_CONSISTENCY_ALL" },
	{ AS_POLICY_COMMIT_LEVEL_ALL            ,   "POLICY_COMMIT_LEVEL_ALL" },
	{ AS_POLICY_COMMIT_LEVEL_MASTER         ,   "POLICY_COMMIT_LEVEL_MASTER" }
};

/**
 * Function for setting scan parameters in scan.
 * Like Scan Priority, Percentage, Concurrent, Nobins
 *
 * @param err                   The as_error to be populated by the function
 *                              with the encountered error if any.
 * @scan_p                      Scan parameter.
 * @py_options                  The user's optional scan options.
{'fraction_non_alphanumeric': 0.16823761508110477, 'fraction_numerical': 0.00263042525208242, 'mean_word_length': 3.0937640197397935, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '4965367', 'n_tokens_mistral': 3199, 'n_tokens_neox': 3072, 'n_words': 574}