From 64de98c2cbdffa53cfe035ab5b331dea0ce2c020 Mon Sep 17 00:00:00 2001 From: Luis Remis Date: Fri, 29 Mar 2019 08:22:02 -0700 Subject: [PATCH 1/5] Add missing link to FindDescriptor --- utils/src/api_schema/api_schema.json | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/src/api_schema/api_schema.json b/utils/src/api_schema/api_schema.json index df29373e..c9a47fb0 100644 --- a/utils/src/api_schema/api_schema.json +++ b/utils/src/api_schema/api_schema.json @@ -581,6 +581,7 @@ "_ref": { "$ref": "#/definitions/refInt" }, "k_neighbors": { "$ref": "#/definitions/positiveInt" }, "results": { "$ref": "#/definitions/blockResults" }, + "link": { "$ref": "#/definitions/blockLink" }, "constraints": { "type": "object" }, "properties": { "type": "object" } }, From 623c2a1624157a0046bb84d4c79b6f887d0cfc2c Mon Sep 17 00:00:00 2001 From: Luis Remis Date: Fri, 29 Mar 2019 09:04:26 -0700 Subject: [PATCH 2/5] Fix FindDesc to avoid always returning id and label (#43) --- src/DescriptorsCommand.cc | 50 ++++++++++++++++++++++++++------------- src/DescriptorsCommand.h | 2 +- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/DescriptorsCommand.cc b/src/DescriptorsCommand.cc index bfdbcf18..b5df7ebd 100644 --- a/src/DescriptorsCommand.cc +++ b/src/DescriptorsCommand.cc @@ -540,11 +540,11 @@ int FindDescriptor::construct_protobuf( // Query for the Descriptors related to user-defined link // that match the user-defined constraints - // We will need to the the AND operation + // We will need to do the AND operation // on the construct_response. int desc_ref = get_value(cmd, "_ref", - query.get_available_reference()); + query.get_available_reference()); query.QueryNode( desc_ref, @@ -668,16 +668,31 @@ int FindDescriptor::construct_protobuf( return 0; } -void FindDescriptor::convert_properties(Json::Value& entities) +void FindDescriptor::convert_properties(Json::Value& entities, + Json::Value& list) { + bool flag_label = false; + bool flag_id = false; + + for (auto& prop : list) { + if (prop.asString() == "_label") { + flag_label = true; + } + if (prop.asString() == "_id") { + flag_id = true; + } + } + for (auto& element : entities) { if (element.isMember(VDMS_DESC_LABEL_PROP)) { - element["_label"] = element[VDMS_DESC_LABEL_PROP]; + if (flag_label) + element["_label"] = element[VDMS_DESC_LABEL_PROP]; element.removeMember(VDMS_DESC_LABEL_PROP); } if (element.isMember(VDMS_DESC_ID_PROP)) { - element["_id"] = element[VDMS_DESC_ID_PROP]; + if (flag_id) + element["_id"] = element[VDMS_DESC_ID_PROP]; element.removeMember(VDMS_DESC_ID_PROP); } } @@ -711,6 +726,7 @@ Json::Value FindDescriptor::construct_responses( } const Json::Value& results = cmd["results"]; + Json::Value list = get_value(results, "list"); // Case (1) if (cmd.isMember("link")) { @@ -719,6 +735,10 @@ Json::Value FindDescriptor::construct_responses( findDesc = json_responses[0]; + if (findDesc.isMember("entities")) { + convert_properties(findDesc["entities"], list); + } + if (findDesc["status"] != 0) { Json::Value return_error; return_error["status"] = RSCommand::Error; @@ -734,7 +754,7 @@ Json::Value FindDescriptor::construct_responses( findDesc = json_responses[1]; if (findDesc.isMember("entities")) { - convert_properties(findDesc["entities"]); + convert_properties(findDesc["entities"], list); } if (findDesc["status"] != 0) { @@ -783,14 +803,13 @@ Json::Value FindDescriptor::construct_responses( std::vector* distances; bool compute_distance = false; - if (results.isMember("list")) { - for (int i = 0; i < results["list"].size(); ++i) { - if (results["list"][i].asString() == "_distance") { - compute_distance = true; - break; - } + Json::Value list = get_value(results, "list"); + for (auto& prop : list) { + if (prop.asString() == "_distance") { + compute_distance = true; + break; } } @@ -817,8 +836,6 @@ Json::Value FindDescriptor::construct_responses( Json::Value entities = findDesc["entities"]; findDesc.removeMember("entities"); - convert_properties(entities); - for (int i = 0; i < (*ids).size(); ++i) { Json::Value desc_data; @@ -827,7 +844,7 @@ Json::Value FindDescriptor::construct_responses( bool pass_constraints = false; for (auto ent : entities) { - if (ent["_id"].asInt64() == d_id) { + if (ent[VDMS_DESC_ID_PROP].asInt64() == d_id) { desc_data = ent; pass_constraints = true; break; @@ -867,12 +884,13 @@ Json::Value FindDescriptor::construct_responses( findDesc["info"] = "VCL Exception"; return error(findDesc); } - } findDesc["entities"].append(desc_data); } + convert_properties(findDesc["entities"], list); + if (cache.isMember("cache_obj_id")) { // We remove the vectors associated with that entry to // free memory, without removing the entry from _cache_map diff --git a/src/DescriptorsCommand.h b/src/DescriptorsCommand.h index 84811447..0b35a45c 100644 --- a/src/DescriptorsCommand.h +++ b/src/DescriptorsCommand.h @@ -154,7 +154,7 @@ namespace VDMS{ { private: - void convert_properties(Json::Value& entities); + void convert_properties(Json::Value& entities, Json::Value& list); public: FindDescriptor(); From d734a82fc8f48fd8afd8996b68cbf4302e2a546e Mon Sep 17 00:00:00 2001 From: Luis Remis Date: Fri, 29 Mar 2019 09:58:25 -0700 Subject: [PATCH 3/5] Fix FindDesc by constraint with blob=true (#91) --- src/DescriptorsCommand.cc | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/DescriptorsCommand.cc b/src/DescriptorsCommand.cc index b5df7ebd..5d89c0f6 100644 --- a/src/DescriptorsCommand.cc +++ b/src/DescriptorsCommand.cc @@ -751,9 +751,43 @@ Json::Value FindDescriptor::construct_responses( assert(json_responses.size() == 2); + const Json::Value& set_response = json_responses[0]; + const Json::Value& set = set_response["entities"][0]; + + // These properties should always exist + assert(set.isMember(VDMS_DESC_SET_PATH_PROP)); + assert(set.isMember(VDMS_DESC_SET_DIM_PROP)); + std::string set_path = set[VDMS_DESC_SET_PATH_PROP].asString(); + int dim = set[VDMS_DESC_SET_DIM_PROP].asInt(); + findDesc = json_responses[1]; if (findDesc.isMember("entities")) { + + if (get_value(results, "blob", false)) { + + VCL::DescriptorSet* set = + _dm->get_descriptors_handler(set_path); + + for (auto& ent : findDesc["entities"]) { + long id = ent[VDMS_DESC_ID_PROP].asInt64(); + + try { + std::string* desc_blob = query_res.add_blobs(); + desc_blob->resize(sizeof(float) * dim); + + set->get_descriptors(&id, 1, + (float*)(*desc_blob).data()); + + } catch (VCL::Exception e) { + print_exception(e); + findDesc["status"] = RSCommand::Error; + findDesc["info"] = "VCL Exception"; + return error(findDesc); + } + } + } + convert_properties(findDesc["entities"], list); } From 51ce4b12785c9c873f5e73ed8b821d6b49826194 Mon Sep 17 00:00:00 2001 From: Luis Remis Date: Fri, 29 Mar 2019 09:59:43 -0700 Subject: [PATCH 4/5] Add more testing for FindDesc --- tests/python/TestFindDescriptors.py | 82 ++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/tests/python/TestFindDescriptors.py b/tests/python/TestFindDescriptors.py index 48240b92..d23ad63d 100644 --- a/tests/python/TestFindDescriptors.py +++ b/tests/python/TestFindDescriptors.py @@ -193,6 +193,86 @@ def test_findDescByConst_get_id(self): self.assertEqual(response[0]["FindDescriptor"] ["entities"][0]["myid"], 205) + def test_findDescByConst_blobTrue(self): + + # Add Set + set_name = "features_128d_4_findDescriptors_id_blob" + dims = 128 + total = 100 + self.create_set_and_insert(set_name, dims, total) + + db = self.create_connection() + + all_queries = [] + + finddescriptor = {} + finddescriptor["set"] = set_name + + constraints = {} + constraints["myid"] = ["==", 205] + finddescriptor["constraints"] = constraints + + results = {} + results["list"] = ["myid", "_label", "_id"] + results["blob"] = True + finddescriptor["results"] = results + + query = {} + query["FindDescriptor"] = finddescriptor + + all_queries = [] + all_queries.append(query) + + response, fv_array = db.query(all_queries) + + # Check success + self.assertEqual(response[0]["FindDescriptor"]["status"], 0) + self.assertEqual(response[0]["FindDescriptor"]["returned"], 1) + self.assertEqual(response[0]["FindDescriptor"] + ["entities"][0]["myid"], 205) + self.assertEqual(len(fv_array), 1) + self.assertEqual(len(fv_array[0]), dims*4) + + def test_findDescByConst_multiple_blobTrue(self): + + # Add Set + set_name = "features_128d_4_findDescriptors_m_blob" + dims = 128 + total = 100 + self.create_set_and_insert(set_name, dims, total) + + db = self.create_connection() + + all_queries = [] + + finddescriptor = {} + finddescriptor["set"] = set_name + + constraints = {} + constraints["myid"] = ["<=", 205] + finddescriptor["constraints"] = constraints + + results = {} + results["list"] = ["myid"] + results["blob"] = True + finddescriptor["results"] = results + + query = {} + query["FindDescriptor"] = finddescriptor + + all_queries = [] + all_queries.append(query) + + response, fv_array = db.query(all_queries) + + # Check success + self.assertEqual(response[0]["FindDescriptor"]["status"], 0) + self.assertEqual(response[0]["FindDescriptor"]["returned"], 6) + self.assertEqual(response[0]["FindDescriptor"] + ["entities"][5]["myid"], 200) + self.assertEqual(len(fv_array), 6) + self.assertEqual(len(fv_array[0]), dims*4) + def test_findDescByBlob(self): # Add Set @@ -416,7 +496,6 @@ def test_findDescByBlobUnusedRef(self): # descriptor_blob.append(x.tobytes()) # response, blob_array = db.query(all_queries, [descriptor_blob]) - # print(db.get_last_response_str()) # self.assertEqual(len(blob_array), kn) # self.assertEqual(descriptor_blob[0], blob_array[0]) @@ -553,6 +632,7 @@ def test_findDescByBlobWithLink(self): response, blob_array = db.query(all_queries, [descriptor_blob]) + self.assertEqual(len(blob_array), kn) # This checks that the received blobs is the same as the inserted. self.assertEqual(descriptor_blob[0], blob_array[0]) From 3379ab4fafd002de61bfae3625da49f2d3473cc9 Mon Sep 17 00:00:00 2001 From: Luis Remis Date: Mon, 1 Apr 2019 09:29:07 -0700 Subject: [PATCH 5/5] Fix blob check condition on FindDesc --- src/DescriptorsCommand.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DescriptorsCommand.cc b/src/DescriptorsCommand.cc index 5d89c0f6..c88f477e 100644 --- a/src/DescriptorsCommand.cc +++ b/src/DescriptorsCommand.cc @@ -898,7 +898,7 @@ Json::Value FindDescriptor::construct_responses( // desc_data["cache_id"] = Json::Int64((*ids)[i]); } - if (results.isMember("blob")) { + if (get_value(results, "blob", false)) { desc_data["blob"] = true;