GroupingPolicyEnum query_grouping_policy_for_object(Object* object) { // 先问新对象自己 GroupingPolicyEnum policy = get_policy(object); if (policy != GP_UNSPECIFIED) return policy; // 再问当前对象 SpmtThread* current_st = g_get_current_spmt_thread(); Object* current_object = current_st->get_current_object(); if (current_object) { policy = get_foreign_policy(current_object); if (policy != GP_UNSPECIFIED) return policy; } // 再问当前spmt线程 policy = current_st->get_policy(); if (policy != GP_UNSPECIFIED) return policy; // 最后采用缺省策略 return ::default_policy(object); }
// ---------------------------------------------------------------------------------- // // check if key exist in cluster // // def exists?(key, options = {}) // // params: // key - AerospikeC::Key object // options - hash of options: // policy: AerospikeC::Policy for read // // ------ // RETURN: // 1. true if exist // 2. false otherwise // static VALUE key_exists(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; as_record * rec = NULL; VALUE key; VALUE options; rb_scan_args(argc, argv, "11", &key, &options); if ( NIL_P(options) ) options = rb_hash_new(); as_key * k = rb_aero_KEY; as_policy_read * policy = get_policy(options); if ( ( status = aerospike_key_exists(as, &err, policy, k, &rec) ) != AEROSPIKE_OK ) { as_record_destroy(rec); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][exists?] success - false"), rb_aero_KEY_INFO); return Qfalse; } raise_as_error(err); } as_record_destroy(rec); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][exists?] success - true"), rb_aero_KEY_INFO); return Qtrue; }
void parse_net_options(list *options, network *net) { net->batch = option_find_int(options, "batch",1); net->learning_rate = option_find_float(options, "learning_rate", .001); net->momentum = option_find_float(options, "momentum", .9); net->decay = option_find_float(options, "decay", .0001); int subdivs = option_find_int(options, "subdivisions",1); net->time_steps = option_find_int_quiet(options, "time_steps",1); net->batch /= subdivs; net->batch *= net->time_steps; net->subdivisions = subdivs; net->h = option_find_int_quiet(options, "height",0); net->w = option_find_int_quiet(options, "width",0); net->c = option_find_int_quiet(options, "channels",0); net->inputs = option_find_int_quiet(options, "inputs", net->h * net->w * net->c); net->max_crop = option_find_int_quiet(options, "max_crop",net->w*2); net->min_crop = option_find_int_quiet(options, "min_crop",net->w); if(!net->inputs && !(net->h && net->w && net->c)) error("No input parameters supplied"); char *policy_s = option_find_str(options, "policy", "constant"); net->policy = get_policy(policy_s); if(net->policy == STEP){ net->step = option_find_int(options, "step", 1); net->scale = option_find_float(options, "scale", 1); } else if (net->policy == STEPS){ char *l = option_find(options, "steps"); char *p = option_find(options, "scales"); if(!l || !p) error("STEPS policy must have steps and scales in cfg file"); int len = strlen(l); int n = 1; int i; for(i = 0; i < len; ++i){ if (l[i] == ',') ++n; } int *steps = calloc(n, sizeof(int)); float *scales = calloc(n, sizeof(float)); for(i = 0; i < n; ++i){ int step = atoi(l); float scale = atof(p); l = strchr(l, ',')+1; p = strchr(p, ',')+1; steps[i] = step; scales[i] = scale; } net->scales = scales; net->steps = steps; net->num_steps = n; } else if (net->policy == EXP){ net->gamma = option_find_float(options, "gamma", 1); } else if (net->policy == SIG){ net->gamma = option_find_float(options, "gamma", 1); net->step = option_find_int(options, "step", 1); } else if (net->policy == POLY || net->policy == RANDOM){ net->power = option_find_float(options, "power", 1); } net->max_batches = option_find_int(options, "max_batches", 0); }
// ---------------------------------------------------------------------------------- // // removing record from the cluster // // def delete(key) // // params: // key - AerospikeC::Key object // options: // policy: AerospikeC::Policy for remove // // ------ // RETURN: // 1. true if deleted // 2. nil if AEROSPIKE_ERR_RECORD_NOT_FOUND // static VALUE delete_record(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; VALUE key; VALUE options; rb_scan_args(argc, argv, "11", &key, &options); if ( NIL_P(options) ) options = rb_hash_new(); as_key * k = rb_aero_KEY; as_policy_remove * policy = get_policy(options); if ( ( status = aerospike_key_remove(as, &err, policy, k) ) != AEROSPIKE_OK ) { if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][delete] success"), rb_aero_KEY_INFO); return Qtrue; }
// ---------------------------------------------------------------------------------- // // def touch(key, options = {}) // // params: // key - AeropsikeC::Key object // options - hash of options: // ttl: time to live record (default: 0) // policy: AerospikeC::Policy for operate // // ------ // RETURN: // 1. hash representing record header // 2. nil when AEROSPIKE_ERR_RECORD_NOT_FOUND // // @TODO options policy // static VALUE touch(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; VALUE key; VALUE options; rb_scan_args(argc, argv, "11", &key, &options); // default values for optional arguments if ( NIL_P(options) ) { options = rb_hash_new(); rb_hash_aset(options, ttl_sym, rb_zero); } else { if ( TYPE(rb_hash_aref(options, ttl_sym)) != T_FIXNUM ) { // check ttl option rb_raise(rb_aero_OptionError, "[AerospikeC::Client][put] ttl must be an integer"); } } as_key * k = rb_aero_KEY; as_record * rec = NULL; as_operations ops; as_operations_inita(&ops, 1); as_operations_add_touch(&ops); ops.ttl = FIX2INT( rb_hash_aref(options, ttl_sym) ); as_policy_operate * policy = get_policy(options); if ( ( status = aerospike_key_operate(as, &err, policy, k, &ops, &rec) ) != AEROSPIKE_OK ) { as_operations_destroy(&ops); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][touch] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } VALUE header = rb_hash_new(); rb_hash_aset(header, rb_str_new2("gen"), INT2FIX(rec->gen)); rb_hash_aset(header, rb_str_new2("expire_in"), INT2FIX(rec->ttl)); as_record_destroy(rec); as_operations_destroy(&ops); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][touch] success"), rb_aero_KEY_INFO); return header; }
// ---------------------------------------------------------------------------------- // // adding record to the cluster // // def put(key, bins, options = {}) // // params: // key - AerospikeC::Key object // bins - either hash {"bin name" => "value"} or AerospikeC::Record object // options - hash of options: // ttl: time to live record (default: 0) // policy: AerospikeC::Policy for write // // ------ // RETURN: // 1. true if completed succesfuly // 2. nil when AEROSPIKE_ERR_RECORD_NOT_FOUND // static VALUE put(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_status status; as_error err; aerospike * as = rb_aero_CLIENT; VALUE key; VALUE hash; VALUE options; rb_scan_args(argc, argv, "21", &key, &hash, &options); if ( NIL_P(options) ) options = rb_hash_new(); VALUE option_tmp; long len = rb_ary_len_long(hash); as_record rec; as_record_init(&rec, len); hash2record(hash, &rec); as_key * k = rb_aero_KEY; as_policy_write * policy = get_policy(options); if ( !NIL_P(options) ) { VALUE rb_ttl = rb_hash_aref(options, ttl_sym); if ( TYPE(rb_ttl) == T_FIXNUM ) { rec.ttl = FIX2INT( rb_ttl ); } } if ( ( status = aerospike_key_put(as, &err, policy, k, &rec) ) != AEROSPIKE_OK) { as_record_destroy(&rec); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][put] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } as_record_destroy(&rec); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][put] success"), rb_aero_KEY_INFO); return Qtrue; }
// ---------------------------------------------------------------------------------- // // execute udf on record // // def execute_udf(key, module_name, func_name, udf_args = [], options = {}) // // params: // key - AeropsikeC::Key object // module_name - string, registered module name // func_name - string, function name in module to execute // udf_args - arguments passed to udf // options: // policy - AerospikeC::ApplyPolicy // // ------ // RETURN: // 1. data returned from udf // static VALUE execute_udf(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; VALUE key; VALUE module_name; VALUE func_name; VALUE udf_args; VALUE options; rb_scan_args(argc, argv, "32", &key, &module_name, &func_name, &udf_args, &options); // default values for optional arguments if ( NIL_P(options) ) options = rb_hash_new(); if ( NIL_P(udf_args) ) udf_args = rb_ary_new(); as_key * k = rb_aero_KEY; as_arraylist * args = array2as_list(udf_args); as_policy_apply * policy = get_policy(options); as_val * res = NULL; char * c_module_name = StringValueCStr(module_name); char * c_func_name = StringValueCStr(func_name); if ( ( status = aerospike_key_apply(as, &err, policy, k, c_module_name, c_func_name, (as_list *)args, &res) ) != AEROSPIKE_OK ) { as_arraylist_destroy(args); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 3, rb_str_new2("[Client][execute_udf] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO, rb_aero_MOD_INFO); return Qnil; } raise_as_error(err); } VALUE result = as_val2rb_val(res); as_val_destroy(&res); as_arraylist_destroy(args); VALUE key_info = rb_aero_KEY_INFO; rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 3, rb_str_new2("[Client][execute_udf] success"), rb_aero_KEY_INFO, rb_aero_MOD_INFO); return result; }
// ---------------------------------------------------------------------------------- // // perform given operations on record in one call // // def operate(key, operations, options = {}) // // params: // key - AeropsikeC::Key object // operations - AeropsikeC::Operation object // options: // policy: AerospikeC::Policy for operate // // ------ // RETURN: // 1. hash representing record // 2. nil when AEROSPIKE_ERR_RECORD_NOT_FOUND // // static VALUE operate(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; VALUE key; VALUE operations; VALUE options; rb_scan_args(argc, argv, "21", &key, &operations, &options); if ( NIL_P(options) ) { options = rb_hash_new(); } as_key * k = rb_aero_KEY; VALUE is_aerospike_c_operation = rb_funcall(operations, rb_intern("is_a?"), 1, rb_aero_OPERATION); if ( is_aerospike_c_operation != Qtrue ) { rb_raise(rb_aero_OptionError, "[AerospikeC::Client][operate] use AerospikeC::Operation class to perform operations"); } as_operations * ops = rb_operations2as_operations(operations); as_record * rec = NULL; as_policy_operate * policy = get_policy(options); if ( ( status = aerospike_key_operate(as, &err, policy, k, ops, &rec) ) != AEROSPIKE_OK ) { as_operations_destroy(ops); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][operate] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } VALUE record = record2hash(rec); as_record_destroy(rec); as_operations_destroy(ops); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][operate] success"), rb_aero_KEY_INFO); return record; }
struct cmd_results *cmd_reject(int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, "reject", EXPECTED_MORE_THAN, 1))) { return error; } struct feature_policy *policy = get_policy(argv[0]); policy->features &= ~get_features(argc, argv, &error); if (error) { return error; } sway_log(L_DEBUG, "Permissions granted to %s for features %d", policy->program, policy->features); return cmd_results_new(CMD_SUCCESS, NULL, NULL); }
// ---------------------------------------------------------------------------------- // // def get_header(key, options = {}) // // params: // key - AerospikeC::Key object // options - hash of options: // policy: AerospikeC::Policy for read // // ------ // RETURN: // 1. hash representing record header // 2. nil when AEROSPIKE_ERR_RECORD_NOT_FOUND // static VALUE get_header(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; as_record * rec = NULL; VALUE key; VALUE options; rb_scan_args(argc, argv, "11", &key, &options); if ( NIL_P(options) ) options = rb_hash_new(); as_key * k = rb_aero_KEY; as_policy_read * policy = get_policy(options); if ( ( status = aerospike_key_exists(as, &err, policy, k, &rec) ) != AEROSPIKE_OK ) { as_record_destroy(rec); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][get_header] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } VALUE header = rb_hash_new(); rb_hash_aset(header, rb_str_new2("gen"), INT2FIX(rec->gen)); rb_hash_aset(header, rb_funcall(rb_aero_AerospikeC, rb_intern("ttl_name"), 0), INT2FIX(rec->ttl)); as_record_destroy(rec); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][get_header] success"), rb_aero_KEY_INFO); return header; }
/* * The main configuration routine. Its implementation is hugely inspired by the * the same routine implementation in Solaris NSCD. */ int parse_config_file(struct configuration *config, const char *fname, char const **error_str, int *error_line) { FILE *fin; char buffer[255]; char *fields[128]; int field_count, line_num, value; int res; TRACE_IN(parse_config_file); assert(config != NULL); assert(fname != NULL); fin = fopen(fname, "r"); if (fin == NULL) { TRACE_OUT(parse_config_file); return (-1); } res = 0; line_num = 0; memset(buffer, 0, sizeof(buffer)); while ((res == 0) && (fgets(buffer, sizeof(buffer) - 1, fin) != NULL)) { field_count = strbreak(buffer, fields, sizeof(fields)); ++line_num; if (field_count == 0) continue; switch (fields[0][0]) { case '#': case '\0': continue; case 'e': if ((field_count == 3) && (strcmp(fields[0], "enable-cache") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_yesno(fields[2])) != -1)) { enable_cache(config, fields[1], value); continue; } break; case 'd': if ((field_count == 2) && (strcmp(fields[0], "debug-level") == 0) && ((value = get_number(fields[1], 0, 10)) != -1)) { continue; } break; case 'p': if ((field_count == 3) && (strcmp(fields[0], "positive-time-to-live") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_number(fields[2], 0, -1)) != -1)) { set_positive_time_to_live(config, fields[1], value); continue; } else if ((field_count == 3) && (strcmp(fields[0], "positive-confidence-threshold") == 0) && ((value = get_number(fields[2], 1, -1)) != -1)) { set_positive_confidence_threshold(config, fields[1], value); continue; } else if ((field_count == 3) && (strcmp(fields[0], "positive-policy") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_policy(fields[2])) != -1)) { set_positive_policy(config, fields[1], value); continue; } else if ((field_count == 3) && (strcmp(fields[0], "perform-actual-lookups") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_yesno(fields[2])) != -1)) { set_perform_actual_lookups(config, fields[1], value); continue; } break; case 'n': if ((field_count == 3) && (strcmp(fields[0], "negative-time-to-live") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_number(fields[2], 0, -1)) != -1)) { set_negative_time_to_live(config, fields[1], value); continue; } else if ((field_count == 3) && (strcmp(fields[0], "negative-confidence-threshold") == 0) && ((value = get_number(fields[2], 1, -1)) != -1)) { set_negative_confidence_threshold(config, fields[1], value); continue; } else if ((field_count == 3) && (strcmp(fields[0], "negative-policy") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_policy(fields[2])) != -1)) { set_negative_policy(config, fields[1], value); continue; } break; case 's': if ((field_count == 3) && (strcmp(fields[0], "suggested-size") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_number(fields[2], 1, -1)) != -1)) { set_suggested_size(config, fields[1], value); continue; } break; case 't': if ((field_count == 2) && (strcmp(fields[0], "threads") == 0) && ((value = get_number(fields[1], 1, -1)) != -1)) { set_threads_num(config, value); continue; } break; case 'k': if ((field_count == 3) && (strcmp(fields[0], "keep-hot-count") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_number(fields[2], 0, -1)) != -1)) { set_keep_hot_count(config, fields[1], value); continue; } break; case 'c': if ((field_count == 3) && (strcmp(fields[0], "check-files") == 0) && (check_cachename(fields[1]) == 0) && ((value = get_yesno(fields[2])) != -1)) { check_files(config, fields[1], value); continue; } break; default: break; } LOG_ERR_2("config file parser", "error in file " "%s on line %d", fname, line_num); *error_str = "syntax error"; *error_line = line_num; res = -1; } fclose(fin); TRACE_OUT(parse_config_file); return (res); }
// ---------------------------------------------------------------------------------- // // getting record for specifed key // read all bins of record or given in specific_bins argument // // def get(key, specific_bins = nil, options = {}) // // params: // keys - Array of AerospikeC::Key objects // specific bins - Array of strings representing bin names // options - hash of options: // with_header: returns also generation and expire_in field (default: false) // policy: AerospikeC::Policy for read // // ------ // RETURN: // 1. hash representing record // 2. nil when AEROSPIKE_ERR_RECORD_NOT_FOUND // static VALUE get(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; as_record * rec = NULL; VALUE bins; VALUE key; VALUE specific_bins; VALUE options; rb_scan_args(argc, argv, "12", &key, &specific_bins, &options); // default values for optional arguments if ( NIL_P(specific_bins) ) specific_bins = Qnil; if ( NIL_P(options) ) { options = rb_hash_new(); rb_hash_aset(options, with_header_sym, Qfalse); } as_key * k = rb_aero_KEY; as_policy_read * policy = get_policy(options); // read specific bins if ( specific_bins != Qnil && rb_ary_len_int(specific_bins) > 0 ) { if ( TYPE(specific_bins) != T_ARRAY ) { rb_raise(rb_aero_OptionError, "[AerospikeC::Client][get] specific_bins must be an Array"); } char ** inputArray = rb_array2inputArray(specific_bins); // convert ruby array to char ** if ( ( status = aerospike_key_select(as, &err, policy, k, inputArray, &rec) ) != AEROSPIKE_OK) { as_record_destroy(rec); inputArray_destroy(inputArray); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][get] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } bins = record2hash(rec); bins = check_with_header(bins, options, rec); as_record_destroy(rec); inputArray_destroy(inputArray); // check_for_llist_workaround(self, key, bins); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][get] success"), rb_aero_KEY_INFO); return bins; } // read all bins if ( ( status = aerospike_key_get(as, &err, policy, k, &rec) ) != AEROSPIKE_OK) { as_record_destroy(rec); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][get] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } bins = record2hash(rec); bins = check_with_header(bins, options, rec); as_record_destroy(rec); // check_for_llist_workaround(self, key, bins); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][get] success"), rb_aero_KEY_INFO); return bins; }