From 329bb577cbdd86de693f33ba40a84aad2218dd49 Mon Sep 17 00:00:00 2001 From: Brice Videau Date: Wed, 2 Oct 2024 14:42:20 -0500 Subject: [PATCH] Add CCS_DESERIALIZE_OPTION_MAP_HANDLES option. Fix map bug in ruby. --- bindings/python/cconfigspace/base.py | 11 ++++++++--- bindings/ruby/lib/cconfigspace/base.rb | 16 ++++++++++------ bindings/ruby/lib/cconfigspace/map.rb | 2 +- include/cconfigspace/base.h | 10 ++++++++-- src/cconfigspace.c | 3 +-- src/cconfigspace_deserialize.h | 5 +++++ tests/test_configuration_space.c | 2 +- tests/test_feature_space.c | 2 +- tests/test_random_features_tuner.c | 2 +- tests/test_random_tree_tuner.c | 2 +- tests/test_random_tuner.c | 2 +- tests/test_user_defined_features_tuner.c | 1 + tests/test_user_defined_tree_tuner.c | 1 + tests/test_user_defined_tuner.c | 1 + 14 files changed, 41 insertions(+), 19 deletions(-) diff --git a/bindings/python/cconfigspace/base.py b/bindings/python/cconfigspace/base.py index dcbafcc0..c8709657 100644 --- a/bindings/python/cconfigspace/base.py +++ b/bindings/python/cconfigspace/base.py @@ -395,6 +395,7 @@ class DeserializeOption(CEnumeration): _members_ = [ ('END', 0), 'HANDLE_MAP', + 'MAP_HANDLES', 'VECTOR_CALLBACK', 'NON_BLOCKING', 'DATA_CALLBACK' @@ -556,7 +557,7 @@ def serialize(self, format = 'binary', path = None, file_descriptor = None, call return v @classmethod - def deserialize(cls, format = 'binary', handle_map = None, vector_callback = None, path = None, buffer = None, file_descriptor = None, callback = None): + def deserialize(cls, format = 'binary', handle_map = None, map_handles = False, vector_callback = None, path = None, buffer = None, file_descriptor = None, callback = None): if format != 'binary': raise Error(Result(Result.ERROR_INVALID_VALUE)) mode_count = 0; @@ -568,8 +569,12 @@ def deserialize(cls, format = 'binary', handle_map = None, vector_callback = Non mode_count += 1 if not mode_count == 1: raise Error(Result(Result.ERROR_INVALID_VALUE)) + if map_handles and not handle_map: + raise Error(Result(Result.ERROR_INVALID_VALUE)) o = ccs_object(0) options = [DeserializeOption.END] + if map_handles: + options = [DeserializeOption.MAP_HANDLES] + options if handle_map: options = [DeserializeOption.HANDLE_MAP, handle_map.handle] + options if vector_callback: @@ -692,8 +697,8 @@ def _register_serialize_callback(handle, callback_data): _register_destroy_callback(handle) _data_store[value]['serialize_calback'] = callback_data -def deserialize(format = 'binary', handle_map = None, path = None, buffer = None, file_descriptor = None, vector_callback = None, vector_callback_data = None, callback = None, callback_data = None): - return Object.deserialize(format = format, handle_map = handle_map, path = path, buffer = buffer, file_descriptor = file_descriptor, vector_callback = vector_callback, callback = callback) +def deserialize(format = 'binary', handle_map = None, map_handles = False, path = None, buffer = None, file_descriptor = None, vector_callback = None, vector_callback_data = None, callback = None, callback_data = None): + return Object.deserialize(format = format, handle_map = handle_map, map_handles = map_handles, path = path, buffer = buffer, file_descriptor = file_descriptor, vector_callback = vector_callback, callback = callback) def _set_destroy_callback(handle, callback): if callback is None: diff --git a/bindings/ruby/lib/cconfigspace/base.rb b/bindings/ruby/lib/cconfigspace/base.rb index 5931efa6..5caf1571 100644 --- a/bindings/ruby/lib/cconfigspace/base.rb +++ b/bindings/ruby/lib/cconfigspace/base.rb @@ -198,7 +198,8 @@ def read_ccs_result_t DatumFlags = bitmask FFI::Type::UINT32, :ccs_datum_flags_t, [ :CCS_DATUM_FLAG_TRANSIENT, - :CCS_DATUM_FLAG_UNPOOLED ] + :CCS_DATUM_FLAG_UNPOOLED, + :CCS_DATUM_FLAG_ID ] NumericType = enum FFI::Type::INT32, :ccs_numeric_type_t, [ :CCS_NUMERIC_TYPE_INT, DataType.to_native(:CCS_DATA_TYPE_INT, nil), @@ -227,6 +228,7 @@ def read_ccs_numeric_type_t DeserializeOptions = enum FFI::Type::INT32, :ccs_deserialize_option_t, [ :CCS_DESERIALIZE_OPTION_END, 0, :CCS_DESERIALIZE_OPTION_HANDLE_MAP, + :CCS_DESERIALIZE_OPTION_MAP_HANDLES, :CCS_DESERIALIZE_OPTION_VECTOR_CALLBACK, :CCS_DESERIALIZE_OPTION_NON_BLOCKING, :CCS_DESERIALIZE_OPTION_DATA_CALLBACK ] @@ -733,7 +735,7 @@ def serialize(format: :binary, path: nil, file_descriptor: nil, callback: nil) return result end - def self.deserialize(format: :binary, handle_map: nil, path: nil, buffer: nil, file_descriptor: nil, vector_callback: nil, vector_callback_data: nil, callback: nil) + def self.deserialize(format: :binary, handle_map: nil, map_handles: false, path: nil, buffer: nil, file_descriptor: nil, vector_callback: nil, vector_callback_data: nil, callback: nil) raise CCSError, :CCS_RESULT_ERROR_INVALID_VALUE if format != :binary format = :CCS_SERIALIZE_FORMAT_BINARY mode_count = 0 @@ -741,9 +743,11 @@ def self.deserialize(format: :binary, handle_map: nil, path: nil, buffer: nil, f mode_count += 1 if buffer mode_count += 1 if file_descriptor raise CCSError, :CCS_RESULT_ERROR_INVALID_VALUE unless mode_count == 1 + raise CCSError, :CCS_RESULT_ERROR_INVALID_VALUE if map_handles && !handle_map ptr = MemoryPointer::new(:ccs_object_t) options = [] options.concat [:ccs_deserialize_option_t, :CCS_DESERIALIZE_OPTION_HANDLE_MAP, :ccs_map_t, handle_map.handle] if handle_map + options.concat [:ccs_deserialize_option_t, :CCS_DESERIALIZE_OPTION_MAP_HANDLES] if map_handles if vector_callback cb_wrapper = CCS.get_deserialize_vector_callback_wrapper(&vector_callback) options.concat [:ccs_deserialize_option_t, :CCS_DESERIALIZE_OPTION_VECTOR_CALLBACK, :ccs_object_deserialize_vector_callback, cb_wrapper, :value, vector_callback_data] @@ -786,7 +790,7 @@ def user_data=(ud) end - @@data_store = Hash.new { |h, k| h[k] = { callbacks: [], user_data: nil, serialize_calback: nil, strings: [] } } + @@data_store = Hash.new { |h, k| h[k] = { callbacks: [], user_data: nil, serialize_callback: nil, strings: [] } } # Delete wrappers are responsible for deregistering the object data_store def self.register_vector(handle, vector_data) @@ -832,7 +836,7 @@ def self.register_callback(handle, callback_data) def self.register_serialize_callback(handle, callback_data) value = handle.address register_destroy_callback(handle) unless @@data_store.include?(value) - @@data_store[value][:serialize_calback] = callback_data + @@data_store[value][:serialize_callback] = callback_data end def self.set_destroy_callback(handle, &block) @@ -913,8 +917,8 @@ def self.set_serialize_callback(handle, &block) register_serialize_callback(handle, cb_data) end - def self.deserialize(format: :binary, handle_map: nil, path: nil, buffer: nil, file_descriptor: nil, vector_callback: nil, callback: nil) - return CCS::Object.deserialize(format: format, handle_map: handle_map, path: path, buffer: buffer, file_descriptor: file_descriptor, vector_callback: vector_callback, callback: callback) + def self.deserialize(format: :binary, handle_map: nil, map_handles: false, path: nil, buffer: nil, file_descriptor: nil, vector_callback: nil, callback: nil) + return CCS::Object.deserialize(format: format, handle_map: handle_map, map_handles: map_handles, path: path, buffer: buffer, file_descriptor: file_descriptor, vector_callback: vector_callback, callback: callback) end end diff --git a/bindings/ruby/lib/cconfigspace/map.rb b/bindings/ruby/lib/cconfigspace/map.rb index b37c3de9..b1aada16 100644 --- a/bindings/ruby/lib/cconfigspace/map.rb +++ b/bindings/ruby/lib/cconfigspace/map.rb @@ -51,7 +51,7 @@ def include?(key) def pairs sz = size - return [] if count == 0 + return [] if sz == 0 keys = MemoryPointer::new(:ccs_datum_t, sz) values = MemoryPointer::new(:ccs_datum_t, sz) CCS.error_check CCS.ccs_map_get_pairs(@handle, sz, keys, values, nil) diff --git a/include/cconfigspace/base.h b/include/cconfigspace/base.h index 98e60d07..dcd3f31f 100644 --- a/include/cconfigspace/base.h +++ b/include/cconfigspace/base.h @@ -1025,10 +1025,16 @@ enum ccs_deserialize_option_e { /** * The next parameter is a ccs_handle_map_t object that must contain * the mappings required to deserialize an object (usually bindings or - * expressions). I given, will also add a mapping between the object - * original handle and its current handle. + * expressions). */ CCS_DESERIALIZE_OPTION_HANDLE_MAP, + /** + * Secifies that handles have to be mapped. If given, + * #CCS_DESERIALIZE_OPTION_HANDLE_MAP must be set and the given map + * will be updated to contain the mapping between serialized object + * handles and deserialized object handles. + */ + CCS_DESERIALIZE_OPTION_MAP_HANDLES, /** * The next parameter is a pointer to a callback of type * ccs_object_deserialize_vector_callback_t and its user data, that diff --git a/src/cconfigspace.c b/src/cconfigspace.c index fba0da5a..d2db2308 100644 --- a/src/cconfigspace.c +++ b/src/cconfigspace.c @@ -709,7 +709,6 @@ _ccs_object_deserialize_file_descriptor( va_list args) { ccs_result_t res = CCS_RESULT_SUCCESS; - int fd; int non_blocking; size_t header_size; ssize_t offset; @@ -717,7 +716,7 @@ _ccs_object_deserialize_file_descriptor( NULL, NULL, NULL}; _ccs_file_descriptor_state_t state = {NULL, 0, NULL, 0, -1, 0}; _ccs_file_descriptor_state_t *pstate = NULL; - fd = va_arg(args, int); + int fd = va_arg(args, int); CCS_VALIDATE(_ccs_object_deserialize_options( format, CCS_SERIALIZE_OPERATION_FILE_DESCRIPTOR, args, &opts)); non_blocking = !!(opts.ppfd_state); diff --git a/src/cconfigspace_deserialize.h b/src/cconfigspace_deserialize.h index aeada3d0..71130071 100644 --- a/src/cconfigspace_deserialize.h +++ b/src/cconfigspace_deserialize.h @@ -52,6 +52,8 @@ _ccs_object_deserialize_options( case CCS_DESERIALIZE_OPTION_HANDLE_MAP: opts->handle_map = va_arg(args, ccs_map_t); CCS_CHECK_OBJ(opts->handle_map, CCS_OBJECT_TYPE_MAP); + break; + case CCS_DESERIALIZE_OPTION_MAP_HANDLES: opts->map_values = CCS_TRUE; break; case CCS_DESERIALIZE_OPTION_VECTOR_CALLBACK: @@ -83,6 +85,9 @@ _ccs_object_deserialize_options( } opt = (ccs_deserialize_option_t)va_arg(args, int32_t); } + CCS_REFUTE( + opts->map_values && !opts->handle_map, + CCS_RESULT_ERROR_INVALID_VALUE); return CCS_RESULT_SUCCESS; } diff --git a/tests/test_configuration_space.c b/tests/test_configuration_space.c index 8c1a4ff6..fe1e79b8 100644 --- a/tests/test_configuration_space.c +++ b/tests/test_configuration_space.c @@ -393,7 +393,7 @@ test_deserialize(void) (ccs_object_t *)&space, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, - CCS_DESERIALIZE_OPTION_END); + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_END); assert(err == CCS_RESULT_SUCCESS); err = ccs_map_get(map, ccs_object(space_ref), &d); diff --git a/tests/test_feature_space.c b/tests/test_feature_space.c index 75a0e3d9..dcf305c9 100644 --- a/tests/test_feature_space.c +++ b/tests/test_feature_space.c @@ -293,7 +293,7 @@ test_deserialize(void) (ccs_object_t *)&feature_space, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, - CCS_DESERIALIZE_OPTION_END); + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_END); assert(err == CCS_RESULT_SUCCESS); err = ccs_context_get_parameter_by_name( diff --git a/tests/test_random_features_tuner.c b/tests/test_random_features_tuner.c index b2c4644f..3a08eac7 100644 --- a/tests/test_random_features_tuner.c +++ b/tests/test_random_features_tuner.c @@ -148,7 +148,7 @@ test(void) (ccs_object_t *)&tuner_copy, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, - CCS_DESERIALIZE_OPTION_END); + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_END); assert(err == CCS_RESULT_SUCCESS); err = ccs_map_get(map, ccs_object((ccs_object_t)tuner), &d); diff --git a/tests/test_random_tree_tuner.c b/tests/test_random_tree_tuner.c index 35f43910..12dc9613 100644 --- a/tests/test_random_tree_tuner.c +++ b/tests/test_random_tree_tuner.c @@ -146,7 +146,7 @@ test(void) (ccs_object_t *)&tuner_copy, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, - CCS_DESERIALIZE_OPTION_END); + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_END); assert(err == CCS_RESULT_SUCCESS); err = ccs_tuner_get_history(tuner_copy, NULL, 100, history, &count); diff --git a/tests/test_random_tuner.c b/tests/test_random_tuner.c index f7e5e055..cb22b8b4 100644 --- a/tests/test_random_tuner.c +++ b/tests/test_random_tuner.c @@ -89,7 +89,7 @@ test(void) (ccs_object_t *)&tuner_copy, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, - CCS_DESERIALIZE_OPTION_END); + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_END); assert(err == CCS_RESULT_SUCCESS); err = ccs_tuner_get_history(tuner_copy, NULL, 100, history, &count); diff --git a/tests/test_user_defined_features_tuner.c b/tests/test_user_defined_features_tuner.c index 4f3a90a5..a9bb75c7 100644 --- a/tests/test_user_defined_features_tuner.c +++ b/tests/test_user_defined_features_tuner.c @@ -289,6 +289,7 @@ test(void) (ccs_object_t *)&tuner_copy, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_VECTOR_CALLBACK, &deserialize_vector_callback, (void *)NULL, CCS_DESERIALIZE_OPTION_END); diff --git a/tests/test_user_defined_tree_tuner.c b/tests/test_user_defined_tree_tuner.c index 258b34b7..23a6daf7 100644 --- a/tests/test_user_defined_tree_tuner.c +++ b/tests/test_user_defined_tree_tuner.c @@ -300,6 +300,7 @@ test(void) (ccs_object_t *)&tuner_copy, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_VECTOR_CALLBACK, &deserialize_vector_callback, (void *)NULL, CCS_DESERIALIZE_OPTION_END); diff --git a/tests/test_user_defined_tuner.c b/tests/test_user_defined_tuner.c index ad857e4b..e3e8eb1c 100644 --- a/tests/test_user_defined_tuner.c +++ b/tests/test_user_defined_tuner.c @@ -243,6 +243,7 @@ test(void) (ccs_object_t *)&tuner_copy, CCS_SERIALIZE_FORMAT_BINARY, CCS_SERIALIZE_OPERATION_MEMORY, buff_size, buff, CCS_DESERIALIZE_OPTION_HANDLE_MAP, map, + CCS_DESERIALIZE_OPTION_MAP_HANDLES, CCS_DESERIALIZE_OPTION_VECTOR_CALLBACK, &deserialize_vector_callback, (void *)NULL, CCS_DESERIALIZE_OPTION_END);