winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

SQLiteIndexSource.cpp (5030B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "Microsoft/SQLiteIndexSource.h"
      5 #include "Microsoft/SQLiteIndexSourceV1.h"
      6 #include "Microsoft/SQLiteIndexSourceV2.h"
      7 #include "Microsoft/PreIndexedPackageSourceFactory.h"
      8 #include <winget/ManifestYamlParser.h>
      9 #include <winget/PackageVersionDataManifest.h>
     10 
     11 using namespace AppInstaller::Utility;
     12 
     13 
     14 namespace AppInstaller::Repository::Microsoft
     15 {
     16     namespace details
     17     {
     18         SourceReference::SourceReference(const std::shared_ptr<SQLiteIndexSource>& source) :
     19             m_source(source) {}
     20 
     21         std::shared_ptr<SQLiteIndexSource> SourceReference::GetReferenceSource() const
     22         {
     23             std::shared_ptr<SQLiteIndexSource> source = m_source.lock();
     24             THROW_HR_IF(E_NOT_VALID_STATE, !source);
     25             return source;
     26         }
     27     }
     28 
     29     SQLiteIndexSource::SQLiteIndexSource(
     30         const SourceDetails& details,
     31         SQLiteIndex&& index,
     32         bool isInstalledSource,
     33         bool requireManifestHash) :
     34         m_details(details), m_isInstalled(isInstalledSource), m_index(std::move(index)), m_requireManifestHash(requireManifestHash)
     35     {
     36         std::vector<std::string> cacheSources;
     37         cacheSources.push_back(m_details.Arg);
     38         if (!m_details.AlternateArg.empty())
     39         {
     40             cacheSources.push_back(m_details.AlternateArg);
     41         }
     42 
     43         switch (m_index.GetVersion().MajorVersion)
     44         {
     45         case 1:
     46             m_manifestCache = std::make_shared<Caching::FileCache>(Caching::FileCache::Type::IndexV1_Manifest, m_details.Identifier, std::move(cacheSources));
     47             break;
     48         case 2:
     49             m_manifestCache = std::make_shared<Caching::FileCache>(Caching::FileCache::Type::IndexV2_Manifest, m_details.Identifier, cacheSources);
     50             m_packageVersionDataCache = std::make_shared<Caching::FileCache>(Caching::FileCache::Type::IndexV2_PackageVersionData, m_details.Identifier, std::move(cacheSources));
     51             break;
     52         default:
     53             THROW_WIN32(ERROR_NOT_SUPPORTED);
     54         }
     55     }
     56 
     57     const SourceDetails& SQLiteIndexSource::GetDetails() const
     58     {
     59         return m_details;
     60     }
     61 
     62     const std::string& SQLiteIndexSource::GetIdentifier() const
     63     {
     64         return m_details.Identifier;
     65     }
     66 
     67     SearchResult SQLiteIndexSource::Search(const SearchRequest& request) const
     68     {
     69         auto indexResults = m_index.Search(request);
     70 
     71         SearchResult result;
     72         std::shared_ptr<SQLiteIndexSource> sharedThis = NonConstSharedFromThis();
     73         uint32_t majorVersion = m_index.GetVersion().MajorVersion;
     74 
     75         for (auto& indexResult : indexResults.Matches)
     76         {
     77             std::shared_ptr<ICompositePackage> package;
     78 
     79             switch (majorVersion)
     80             {
     81             case 1:
     82                 package = std::make_shared<details::V1::SQLitePackage>(sharedThis, indexResult.first, m_manifestCache, m_isInstalled);
     83                 break;
     84             case 2:
     85                 package = std::make_shared<details::V2::SQLitePackage>(sharedThis, indexResult.first, m_manifestCache, m_packageVersionDataCache, m_isInstalled);
     86                 break;
     87             default:
     88                 THROW_WIN32(ERROR_NOT_SUPPORTED);
     89             }
     90 
     91             result.Matches.emplace_back(
     92                 std::move(package),
     93                 std::move(indexResult.second));
     94         }
     95 
     96         result.Truncated = indexResults.Truncated;
     97         return result;
     98     }
     99 
    100     void* SQLiteIndexSource::CastTo(ISourceType type)
    101     {
    102         if (type == SourceType)
    103         {
    104             return this;
    105         }
    106 
    107         return nullptr;
    108     }
    109 
    110     bool SQLiteIndexSource::IsSame(const SQLiteIndexSource* other) const
    111     {
    112         return (other && GetIdentifier() == other->GetIdentifier());
    113     }
    114 
    115     std::shared_ptr<SQLiteIndexSource> SQLiteIndexSource::NonConstSharedFromThis() const
    116     {
    117         return const_cast<SQLiteIndexSource*>(this)->shared_from_this();
    118     }
    119 
    120     SQLiteIndexWriteableSource::SQLiteIndexWriteableSource(const SourceDetails& details, SQLiteIndex&& index, bool isInstalledSource) :
    121         SQLiteIndexSource(details, std::move(index), isInstalledSource)
    122     {
    123     }
    124 
    125     void* SQLiteIndexWriteableSource::CastTo(ISourceType type)
    126     {
    127         if (type == ISourceType::IMutablePackageSource)
    128         {
    129             return static_cast<IMutablePackageSource*>(this);
    130         }
    131 
    132         return SQLiteIndexSource::CastTo(type);
    133     }
    134 
    135     void SQLiteIndexWriteableSource::AddPackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
    136     {
    137         m_index.AddManifest(manifest, relativePath);
    138     }
    139     
    140     void SQLiteIndexWriteableSource::RemovePackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
    141     {
    142         m_index.RemoveManifest(manifest, relativePath);
    143     }
    144 }