Skip to content

Commit

Permalink
Avoid failure
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-friis committed Nov 10, 2024
1 parent 227e61a commit 597a9a3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
1 change: 0 additions & 1 deletion bindings/python/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
coll.add("inst1", inst1, force=True) # revert
assert coll.get("inst1") == inst1


# Test convinience functions
i1 = coll.get_id(inst1.uuid)
assert i1 == inst1
Expand Down
1 change: 0 additions & 1 deletion bindings/python/tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
person.name = "Neil Armstrong"
person.age = 39
person.skills = ["keping the head cold", "famous quotes"]
# person.incref()

# Map person to an instance of SimplePerson
simple = dlite.mapping("http://onto-ns.com/meta/0.1/SimplePerson", [person])
Expand Down
8 changes: 5 additions & 3 deletions src/dlite-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,12 @@ DLiteInstance *dlite_collection_get_new(const DLiteCollection *coll,
if (!inst) return NULL;
if (metaid) {
if (!(inst = dlite_mapping(metaid, (const DLiteInstance **)&inst, 1)))
errx(dliteMappingError,
"cannot map instance labeled '%s' to '%s'", label, metaid);
} else
return errx(dliteMappingError,
"cannot map instance labeled '%s' to '%s'",
label, metaid), NULL;
} else {
dlite_instance_incref(inst);
}
return inst;
}

Expand Down
48 changes: 44 additions & 4 deletions src/dlite-mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ DLiteInstance *mapping_map_rec(const DLiteMapping *m, Instances *instances)

/* Add new instance to `instances` */
assert(strcmp(inst->meta->uri, m->output_uri) == 0);
dlite_instance_incref(inst); // TODO: is this correct?
map_set(instances, inst->meta->uri, inst);

fail:
Expand Down Expand Up @@ -347,8 +348,8 @@ int set_inputs(Instances *inputs, const DLiteInstance **instances, int n)
const char *uri = instances[i]->meta->uri;
if (map_get(inputs, uri)) {
while (--i >= 0) {
dlite_instance_decref((DLiteInstance *)instances[i]);
map_remove(inputs, instances[i]->meta->uri);
dlite_instance_decref((DLiteInstance *)instances[i]);
}
return err(1, "more than one instance of the same metadata: %s", uri);
}
Expand All @@ -358,6 +359,20 @@ int set_inputs(Instances *inputs, const DLiteInstance **instances, int n)
return 0;
}

/* Decrease the refcount on each instance in inputs. */
int decref_inputs(Instances *inputs)
{
const char *uri;
map_iter_t iter = map_iter(inputs);
while ((uri = map_next(inputs, &iter))) {
DLiteInstance **instp = map_get(inputs, uri);
if (instp) dlite_instance_decref(*instp);
}
return 0;
}



/* Recursive help function that removes all references to input instances
found in `m`, from `inputs`. */
void remove_inputs_rec(const DLiteMapping *m, Instances *inputs)
Expand Down Expand Up @@ -430,22 +445,47 @@ DLiteInstance *dlite_mapping_map(const DLiteMapping *m,
DLiteInstance *dlite_mapping(const char *output_uri,
const DLiteInstance **instances, int n)
{
int i;
//int i;
DLiteInstance *inst=NULL;
DLiteMapping *m=NULL;
Instances inputs;

map_init(&inputs);

//int i;
//printf("\n");
//printf("<== dlite_mapping: in refcounts:\n");
//for (i=0; i<n; i++)
// printf(" - %s: refcount=%d\n",
// instances[i]->uuid, instances[i]->_refcount);

/* Increases refcount on each input instance */
if (set_inputs(&inputs, instances, n)) goto fail;
if (!(m = mapping_create_base(output_uri, &inputs))) goto fail;
inst = dlite_mapping_map(m, instances, n);

fail:
map_deinit(&inputs);
if (m) dlite_mapping_free(m);

//printf(" * inputs:\n");
//map_iter_t iter = map_iter(&inputs);
//const char *key;
//while ((key = map_next(&inputs, &iter))) {
// DLiteInstance *ins = *map_get(&inputs, key);
// printf(" - %s: %s: refcount=%d\n", key, ins->uuid, ins->_refcount);
//}

/* Decrease refcount */
for (i=0; i<n; i++) dlite_instance_decref((DLiteInstance *)instances[i]);
// FIXME: Are all added refcounts removed again
//for (i=0; i<n; i++) dlite_instance_decref((DLiteInstance *)instances[i]);
decref_inputs(&inputs);
map_deinit(&inputs);

//printf("==> dlite_mapping: out refcounts:\n");
//for (i=0; i<n; i++)
// printf(" - %s: refcount=%d\n",
// instances[i]->uuid, instances[i]->_refcount);
//printf(" inst (%s): refcount=%d\n", inst->uuid, inst->_refcount);

return inst;
}

0 comments on commit 597a9a3

Please sign in to comment.