comparison Framework/Plugins/IndexBackend.cpp @ 161:2ccde9c7311b optimized-routes

added new optimized REST routes. this is a temporary work to try to speed up some routes (used by LRO). This way, we avoid another app to access the Orthanc DB and we skip the plugin SDK update for a very specific route
author Alain Mazy <alain@mazy.be>
date Fri, 10 Jul 2020 13:26:47 +0200
parents 275e14f57f1e
children 6f83b74373d3
comparison
equal deleted inserted replaced
160:84b6fc6f6b9b 161:2ccde9c7311b
28 #include "GlobalProperties.h" 28 #include "GlobalProperties.h"
29 29
30 #include <Compatibility.h> // For std::unique_ptr<> 30 #include <Compatibility.h> // For std::unique_ptr<>
31 #include <Logging.h> 31 #include <Logging.h>
32 #include <OrthancException.h> 32 #include <OrthancException.h>
33 33 #include <boost/algorithm/string/join.hpp>
34 34
35 namespace OrthancDatabases 35 namespace OrthancDatabases
36 { 36 {
37 static std::string ConvertWildcardToLike(const std::string& query) 37 static std::string ConvertWildcardToLike(const std::string& query)
38 { 38 {
1950 1950
1951 statement.Execute(args); 1951 statement.Execute(args);
1952 } 1952 }
1953 } 1953 }
1954 1954
1955 void IndexBackend::GetStudyInstancesIds(std::list<std::string>& target /*out*/,
1956 std::string& publicStudyId)
1957 {
1958 DatabaseManager::CachedStatement statement(
1959 STATEMENT_FROM_HERE, manager_,
1960 "SELECT instances.publicid FROM resources instances"
1961 " INNER JOIN resources series ON instances.parentid = series.internalid"
1962 " INNER JOIN resources studies ON series.parentid = studies.internalid"
1963 " WHERE studies.publicId = ${id}"
1964 );
1965
1966 statement.SetReadOnly(true);
1967 statement.SetParameterType("id", ValueType_Utf8String);
1968
1969 Dictionary args;
1970 args.SetUtf8Value("id", publicStudyId);
1971
1972 ReadListOfStrings(target, statement, args);
1973 }
1974
1975
1976 void IndexBackend::GetStudyInstancesMetadata(std::map<std::string, std::map<int32_t, std::string>>& target /*out*/,
1977 std::string& publicStudyId,
1978 std::list<int32_t> metadataTypes)
1979 {
1980 {
1981 std::string sql = "SELECT instances.publicid, metadata.type, metadata.value FROM resources instances "
1982 "LEFT JOIN metadata ON metadata.id = instances.internalid "
1983 "INNER JOIN resources series ON instances.parentid = series.internalid "
1984 "INNER JOIN resources studies ON series.parentid = studies.internalid "
1985 " WHERE studies.publicId = ${id} ";
1986
1987
1988 if (metadataTypes.size() != 0)
1989 {
1990 std::list<std::string> metadataTypesStrings;
1991 for (std::list<int32_t>::const_iterator m = metadataTypes.begin(); m != metadataTypes.end(); m++)
1992 {
1993 metadataTypesStrings.push_back(boost::lexical_cast<std::string>(*m));
1994 }
1995
1996 std::string metadataTypesFilter = boost::algorithm::join(metadataTypesStrings, ",");
1997 sql = sql + " AND metadata.type IN (" + metadataTypesFilter + ")";
1998 }
1999
2000 DatabaseManager::StandaloneStatement statement(manager_, sql);
2001
2002 statement.SetReadOnly(true);
2003 statement.SetParameterType("id", ValueType_Utf8String);
2004
2005 Dictionary args;
2006 args.SetUtf8Value("id", publicStudyId);
2007
2008 statement.Execute(args);
2009
2010 target.clear();
2011
2012 if (!statement.IsDone())
2013 {
2014 if (statement.GetResultFieldsCount() != 3)
2015 {
2016 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
2017 }
2018
2019 while (!statement.IsDone())
2020 {
2021 std::string instanceId = ReadString(statement, 0);
2022 int32_t type = ReadInteger32(statement, 1);
2023 std::string value = ReadString(statement, 2);
2024
2025 if (target.find(instanceId) == target.end())
2026 {
2027 target[instanceId] = std::map<std::int32_t, std::string>();
2028 }
2029 target[instanceId][type] = value;
2030
2031 statement.Next();
2032 }
2033 }
2034 }
2035 }
2036
1955 2037
1956 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in 1.3.1 2038 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in 1.3.1
1957 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 4) 2039 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 4)
1958 // New primitive since Orthanc 1.5.4 2040 // New primitive since Orthanc 1.5.4
1959 bool IndexBackend::LookupResourceAndParent(int64_t& id, 2041 bool IndexBackend::LookupResourceAndParent(int64_t& id,