Skip to content

Commit

Permalink
Use checkpoint in maps to restore map state after error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed May 21, 2024
1 parent ed997ab commit 9a6c8de
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 63 deletions.
13 changes: 11 additions & 2 deletions src/cconfigspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,24 @@ _ccs_object_deserialize(
{
uint32_t version;
size_t size;
ccs_map_checkpoint_t map_checkpoint;
ccs_result_t err = CCS_RESULT_SUCCESS;
_ccs_object_deserialize_options_t opts = {NULL, CCS_FALSE, NULL, NULL,
NULL, NULL, NULL};
CCS_VALIDATE(_ccs_object_deserialize_options(
format, operation, args, &opts));
CCS_VALIDATE(_ccs_deserialize_header(
format, buffer_size, buffer, &size, &version));
CCS_VALIDATE(_ccs_object_deserialize_with_opts(
object_ret, format, version, buffer_size, buffer, &opts));
if (opts.map_values)
CCS_VALIDATE(_ccs_map_get_checkpoint(
opts.handle_map, &map_checkpoint));
CCS_VALIDATE_ERR_GOTO(err, _ccs_object_deserialize_with_opts(
object_ret, format, version, buffer_size, buffer, &opts), error);
return CCS_RESULT_SUCCESS;
error:
if (opts.map_values)
_ccs_map_rewind(opts.handle_map, map_checkpoint);
return err;
}

static inline ccs_result_t
Expand Down
23 changes: 6 additions & 17 deletions src/configuration_space_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ _ccs_deserialize_bin_configuration_space(
_ccs_object_deserialize_options_t new_opts = *opts;
ccs_result_t res = CCS_RESULT_SUCCESS;

new_opts.map_values = CCS_TRUE;
CCS_VALIDATE(ccs_create_map(&new_opts.handle_map));
if (!opts->map_values) {
new_opts.map_values = CCS_TRUE;
CCS_VALIDATE(ccs_create_map(&new_opts.handle_map));
}

_ccs_configuration_space_data_mock_t data = {
NULL, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL};
Expand All @@ -119,21 +121,7 @@ _ccs_deserialize_bin_configuration_space(
data.forbidden_clauses, data.feature_space, data.rng,
configuration_space_ret),
end);
if (opts->map_values) {
if (data.feature_space_handle)
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map,
data.feature_space_handle,
(ccs_object_t)data.feature_space),
err_configuration_space);
}
goto end;

err_configuration_space:
ccs_release_object(*configuration_space_ret);
*configuration_space_ret = NULL;
end:
if (data.feature_space)
ccs_release_object(data.feature_space);
Expand All @@ -153,7 +141,8 @@ _ccs_deserialize_bin_configuration_space(
ccs_release_object(data.forbidden_clauses[i]);
if (data.parameters)
free(data.parameters);
ccs_release_object(new_opts.handle_map);
if (!opts->map_values)
ccs_release_object(new_opts.handle_map);
return res;
}

Expand Down
5 changes: 1 addition & 4 deletions src/feature_space_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ _ccs_deserialize_bin_feature_space(
_ccs_object_deserialize_options_t *opts)
{
ccs_result_t res = CCS_RESULT_SUCCESS;
_ccs_object_deserialize_options_t new_opts = *opts;

new_opts.map_values = CCS_FALSE;
_ccs_context_data_mock_t data;
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_deserialize_bin_ccs_context_data(
&data, version, buffer_size, buffer, &new_opts),
&data, version, buffer_size, buffer, opts),
end);
CCS_VALIDATE_ERR_GOTO(
res,
Expand Down
39 changes: 39 additions & 0 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,42 @@ ccs_map_clear(ccs_map_t map)
CCS_OBJ_UNLOCK(map);
return CCS_RESULT_SUCCESS;
}

ccs_result_t
_ccs_map_get_checkpoint(
ccs_map_t map,
ccs_map_checkpoint_t *checkpoint_ret)
{
CCS_CHECK_OBJ(map, CCS_OBJECT_TYPE_MAP);
CCS_OBJ_RDLOCK(map);
_ccs_map_datum_t *map_datum = map->data->map;
if (map_datum) {
map_datum = (_ccs_map_datum_t *)
ELMT_FROM_HH(map_datum->hh.tbl, map_datum->hh.tbl->tail);
}
*checkpoint_ret = (void *)map_datum;
CCS_OBJ_UNLOCK(map);
return CCS_RESULT_SUCCESS;
}

ccs_result_t
_ccs_map_rewind(
ccs_map_t map,
ccs_map_checkpoint_t checkpoint)
{
CCS_CHECK_OBJ(map, CCS_OBJECT_TYPE_MAP);
_ccs_map_datum_t *head = (_ccs_map_datum_t *)checkpoint;
_ccs_map_datum_t *current, *tmp;
CCS_OBJ_WRLOCK(map);
if (head) {
head = (_ccs_map_datum_t *)head->hh.next;
} else {
head = map->data->map;
}
HASH_ITER(hh, head, current, tmp)
{
_ccs_map_remove(map->data, current);
}
CCS_OBJ_UNLOCK(map);
return CCS_RESULT_SUCCESS;
}
12 changes: 12 additions & 0 deletions src/map_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ struct _ccs_map_s {
_ccs_map_data_t *data;
};

typedef void * ccs_map_checkpoint_t;

ccs_result_t
_ccs_map_get_checkpoint(
ccs_map_t map,
ccs_map_checkpoint_t *checkpoint_ret);

ccs_result_t
_ccs_map_rewind(
ccs_map_t map,
ccs_map_checkpoint_t checkpoint);

#endif //_MAP_INTERNAL_H
46 changes: 6 additions & 40 deletions src/objective_space_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ _ccs_deserialize_bin_objective_space(
_ccs_object_deserialize_options_t *opts)
{
_ccs_object_deserialize_options_t new_opts = *opts;
ccs_datum_t datum;
ccs_result_t res = CCS_RESULT_SUCCESS;

new_opts.map_values = CCS_TRUE;
CCS_VALIDATE(ccs_create_map(&new_opts.handle_map));
if (!opts->map_values) {
new_opts.map_values = CCS_TRUE;
CCS_VALIDATE(ccs_create_map(&new_opts.handle_map));
}

_ccs_objective_space_data_mock_t data = {NULL, NULL, NULL, NULL, 0,
0, NULL, NULL, NULL};
Expand All @@ -103,42 +104,6 @@ _ccs_deserialize_bin_objective_space(
data.objective_types, objective_space_ret),
end);

if (opts && opts->map_values && opts->handle_map) {
if (data.feature_space_handle) {
CCS_VALIDATE_ERR_GOTO(
res,
ccs_map_get(
new_opts.handle_map,
ccs_object(data.feature_space_handle),
&datum),
err_objective_space);
CCS_REFUTE_ERR_GOTO(
res,
datum.type != CCS_DATA_TYPE_OBJECT ||
CCS_OBJ_TYPE(datum.value.o) !=
CCS_OBJECT_TYPE_FEATURE_SPACE,
CCS_RESULT_ERROR_INVALID_TYPE,
err_objective_space);
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map,
data.feature_space_handle,
datum.value.o),
err_objective_space);
}
CCS_VALIDATE_ERR_GOTO(
res,
_ccs_object_handle_check_add(
opts->handle_map, data.search_space_handle,
(ccs_object_t)data.search_space),
err_objective_space);
}
goto end;

err_objective_space:
ccs_release_object(*objective_space_ret);
*objective_space_ret = NULL;
end:
if (data.search_space)
ccs_release_object(data.search_space);
Expand All @@ -152,7 +117,8 @@ _ccs_deserialize_bin_objective_space(
ccs_release_object(data.objectives[i]);
if (data.parameters)
free(data.parameters);
ccs_release_object(new_opts.handle_map);
if (!opts->map_values)
ccs_release_object(new_opts.handle_map);
return res;
}

Expand Down

0 comments on commit 9a6c8de

Please sign in to comment.