Move database and HTTP libs over to new adapter model

This commit is contained in:
Jon Evans
2025-10-15 22:18:49 -04:00
parent 0752b43057
commit 0b2b6b2e77
9 changed files with 56 additions and 25 deletions
@@ -79,6 +79,8 @@ LIBRARY_RESULT<LIB_DATA*> SYMBOL_LIBRARY_MANAGER_ADAPTER::loadIfNeeded( const wx
SCH_IO* plugin = SCH_IO_MGR::FindPlugin( type );
wxCHECK( plugin, tl::unexpected( LIBRARY_ERROR( _( "Internal error" ) ) ) );
plugin->SetLibraryManagerAdapter( this );
std::lock_guard lock( aMutex );
aTarget[ row->Nickname() ].row = row;
@@ -90,8 +92,6 @@ LIBRARY_RESULT<LIB_DATA*> SYMBOL_LIBRARY_MANAGER_ADAPTER::loadIfNeeded( const wx
return &aTarget.at( aNickname );
}
wxLogTrace( traceLibraries, "Library %s not found in library table (%s)",
aNickname, magic_enum::enum_name( aScope ) );
return nullptr;
}
@@ -389,3 +389,20 @@ std::vector<std::pair<wxString, LIB_STATUS>> SYMBOL_LIBRARY_MANAGER_ADAPTER::Get
return ret;
}
int SYMBOL_LIBRARY_MANAGER_ADAPTER::GetModifyHash() const
{
int hash = 0;
for( const LIBRARY_TABLE_ROW* row : m_manager.Rows( LIBRARY_TABLE_TYPE::SYMBOL ) )
{
if( std::optional<const LIB_DATA*> result = fetchIfLoaded( row->Nickname() );
const LIB_DATA* rowData = *result )
{
hash += rowData->plugin->GetModifyHash();
}
}
return hash;
}
@@ -161,6 +161,8 @@ public:
static std::optional<SCH_IO_MGR::SCH_FILE_T> ParseLibType( const wxString& aType );
int GetModifyHash() const override;
protected:
void doPreload() override {}
+12 -7
View File
@@ -26,9 +26,11 @@
#include <boost/algorithm/string.hpp>
#include <libraries/symbol_library_manager_adapter.h>
#include <database/database_connection.h>
#include <database/database_lib_settings.h>
#include <fmt.h>
#include <ki_exception.h>
#include <lib_symbol.h>
#include <lib_id.h>
#include <symbol_lib_table.h>
@@ -36,8 +38,11 @@
#include "sch_io_database.h"
SCH_IO_DATABASE::SCH_IO_DATABASE() : SCH_IO( wxS( "Database library" ) ),
m_libTable( nullptr ),
SCH_IO_DATABASE::SCH_IO_DATABASE() :
SCH_IO( wxS( "Database library" ) ),
m_adapter( nullptr ),
m_settings(),
m_conn()
{
@@ -67,7 +72,7 @@ void SCH_IO_DATABASE::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
const wxString& aLibraryPath,
const std::map<std::string, UTF8>* aProperties )
{
wxCHECK_RET( m_libTable, "Database plugin missing library table handle!" );
wxCHECK_RET( m_adapter, "Database plugin missing library manager adapter handle!" );
ensureSettings( aLibraryPath );
ensureConnection();
cacheLib();
@@ -91,7 +96,7 @@ LIB_SYMBOL* SCH_IO_DATABASE::LoadSymbol( const wxString& aLibraryPath,
const wxString& aAliasName,
const std::map<std::string, UTF8>* aProperties )
{
wxCHECK( m_libTable, nullptr );
wxCHECK_MSG( m_adapter, nullptr, "Database plugin missing library manager adapter handle!" );
ensureSettings( aLibraryPath );
ensureConnection();
@@ -219,7 +224,7 @@ void SCH_IO_DATABASE::cacheLib()
{
long long currentTimestampSeconds = wxDateTime::Now().GetValue().GetValue() / 1000;
if( m_libTable->GetModifyHash() == m_cacheModifyHash
if( m_adapter->GetModifyHash() == m_cacheModifyHash
&& ( currentTimestampSeconds - m_cacheTimestamp ) < m_settings->m_Cache.max_age )
{
return;
@@ -269,7 +274,7 @@ void SCH_IO_DATABASE::cacheLib()
m_sanitizedNameMap = std::move( newSanitizedNameMap );
m_cacheTimestamp = currentTimestampSeconds;
m_cacheModifyHash = m_libTable->GetModifyHash();
m_cacheModifyHash = m_adapter->GetModifyHash();
}
void SCH_IO_DATABASE::ensureSettings( const wxString& aSettingsPath )
@@ -455,7 +460,7 @@ std::unique_ptr<LIB_SYMBOL> SCH_IO_DATABASE::loadSymbolFromRow( const wxString&
symbolId.Parse( std::any_cast<std::string>( aRow.at( aTable.symbols_col ) ) );
if( symbolId.IsValid() )
originalSymbol = m_libTable->LoadSymbol( symbolId );
originalSymbol = m_adapter->LoadSymbol( symbolId );
if( originalSymbol )
{
+4 -3
View File
@@ -28,6 +28,7 @@
#include <optional>
class LIBRARY_MANAGER_ADAPTER;
class DATABASE_LIB_SETTINGS;
struct DATABASE_LIB_TABLE;
@@ -76,9 +77,9 @@ public:
// Database libraries can never be written using the symbol editing API
bool IsLibraryWritable( const wxString& aLibraryPath ) override { return false; }
void SetLibTable( SYMBOL_LIB_TABLE* aTable ) override
void SetLibraryManagerAdapter( SYMBOL_LIBRARY_MANAGER_ADAPTER* aAdapter ) override
{
m_libTable = aTable;
m_adapter = aAdapter;
}
DATABASE_LIB_SETTINGS* Settings() const { return m_settings.get(); }
@@ -100,7 +101,7 @@ private:
static std::optional<bool> boolFromAny( const std::any& aVal );
SYMBOL_LIB_TABLE* m_libTable;
SYMBOL_LIBRARY_MANAGER_ADAPTER* m_adapter;
std::unique_ptr<DATABASE_LIB_SETTINGS> m_settings;
+5 -4
View File
@@ -25,13 +25,14 @@
#include <lib_symbol.h>
#include <symbol_lib_table.h>
#include <libraries/symbol_library_manager_adapter.h>
#include <http_lib/http_lib_connection.h>
#include "sch_io_http_lib.h"
SCH_IO_HTTP_LIB::SCH_IO_HTTP_LIB() :
SCH_IO( wxS( "HTTP library" ) ),
m_libTable( nullptr )
m_adapter( nullptr )
{
}
@@ -50,7 +51,7 @@ void SCH_IO_HTTP_LIB::EnumerateSymbolLib( wxArrayString& aSymbolNameList, const
void SCH_IO_HTTP_LIB::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList, const wxString& aLibraryPath,
const std::map<std::string, UTF8>* aProperties )
{
wxCHECK_RET( m_libTable, _( "httplib plugin missing library table handle!" ) );
wxCHECK_RET( m_adapter, "HTTP plugin missing library manager adapter handle!" );
ensureSettings( aLibraryPath );
ensureConnection();
@@ -95,7 +96,7 @@ void SCH_IO_HTTP_LIB::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
LIB_SYMBOL* SCH_IO_HTTP_LIB::LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
const std::map<std::string, UTF8>* aProperties )
{
wxCHECK( m_libTable, nullptr );
wxCHECK_MSG( m_adapter, nullptr, "HTTP plugin missing library manager adapter handle!" );
ensureSettings( aLibraryPath );
ensureConnection();
@@ -354,7 +355,7 @@ LIB_SYMBOL* SCH_IO_HTTP_LIB::loadSymbolFromPart( const wxString& aSymbolName,
symbolId.Parse( symbolIdStr );
if( symbolId.IsValid() )
originalSymbol = m_libTable->LoadSymbol( symbolId );
originalSymbol = m_adapter->LoadSymbol( symbolId );
if( originalSymbol )
{
+7 -2
View File
@@ -27,6 +27,8 @@
#include <sch_io/sch_io_mgr.h>
#include <wildcards_and_files_ext.h>
class LIBRARY_MANAGER_ADAPTER;
/**
* A KiCad HTTP library provides both symbol and footprint metadata, so there are "shim" plugins
* on both the symbol and footprint side of things that expose the database contents to the
@@ -68,7 +70,10 @@ public:
bool IsLibraryWritable( const wxString& aLibraryPath ) override { return false; }
void SetLibTable( SYMBOL_LIB_TABLE* aTable ) override { m_libTable = aTable; }
void SetLibraryManagerAdapter( SYMBOL_LIBRARY_MANAGER_ADAPTER* aAdapter ) override
{
m_adapter = aAdapter;
}
HTTP_LIB_SETTINGS* Settings() const { return m_settings.get(); }
@@ -92,7 +97,7 @@ private:
const HTTP_LIB_PART& aPart );
private:
SYMBOL_LIB_TABLE* m_libTable;
SYMBOL_LIBRARY_MANAGER_ADAPTER* m_adapter;
/// Generally will be null if no valid connection is established
std::unique_ptr<HTTP_LIB_CONNECTION> m_conn;
+5 -3
View File
@@ -32,6 +32,8 @@
#include <i18n_utility.h>
#include <wx/arrstr.h>
class SYMBOL_LIBRARY_MANAGER_ADAPTER;
/**
* Base class that schematic file and library loading and saving plugins should derive from.
* Implementations can provide either LoadSchematicFile() or SaveSchematicFile() functions,
@@ -351,10 +353,10 @@ public:
virtual const wxString& GetError() const;
/**
* Some library plugins need to have access to their parent library table.
* @param aTable is the table this plugin is registered within.
* Some library plugins need to interface with other loaded libraries.
* To do this, they receive a reference to the project-specific manager adapter.
*/
virtual void SetLibTable( SYMBOL_LIB_TABLE* aTable ) {}
virtual void SetLibraryManagerAdapter( SYMBOL_LIBRARY_MANAGER_ADAPTER* aAdapter ) {}
//-----</PUBLIC SCH_IO API>------------------------------------------------
-4
View File
@@ -81,7 +81,6 @@ bool SYMBOL_LIB_TABLE_ROW::Refresh()
plugin.reset( SCH_IO_MGR::FindPlugin( type ) );
SetLoaded( false );
plugin->SetLibTable( static_cast<SYMBOL_LIB_TABLE*>( GetParent() ) );
plugin->EnumerateSymbolLib( dummyList, GetFullURI( true ), GetProperties() );
SetLoaded( true );
return true;
@@ -366,8 +365,6 @@ SYMBOL_LIB_TABLE_ROW* SYMBOL_LIB_TABLE::FindRow( const wxString& aNickname, bool
if( !row->plugin )
row->setPlugin( SCH_IO_MGR::FindPlugin( row->type ) );
row->plugin->SetLibTable( this );
return row;
}
@@ -388,7 +385,6 @@ void SYMBOL_LIB_TABLE::LoadSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
row->SetOptions( row->GetOptions() + " " + PropPowerSymsOnly );
row->SetLoaded( false );
row->plugin->SetLibTable( this );
row->plugin->EnumerateSymbolLib( aSymbolList, row->GetFullURI( true ), row->GetProperties() );
row->SetLoaded( true );
+2
View File
@@ -98,6 +98,8 @@ public:
~LIBRARY_MANAGER_ADAPTER() override = default;
virtual int GetModifyHash() const { return 0; };
protected:
virtual void doPreload() = 0;