winget-cli

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

SQLiteIndexSource.h (3567B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include "Microsoft/SQLiteIndex.h"
      5 #include "ISource.h"
      6 #include <winget/FileCache.h>
      7 #include <memory>
      8 
      9 
     10 namespace AppInstaller::Repository::Microsoft
     11 {
     12     // A source that holds a SQLiteIndex and lock.
     13     struct SQLiteIndexSource : public std::enable_shared_from_this<SQLiteIndexSource>, public ISource
     14     {
     15         static constexpr ISourceType SourceType = ISourceType::SQLiteIndexSource;
     16 
     17         SQLiteIndexSource(
     18             const SourceDetails& details,
     19             SQLiteIndex&& index,
     20             bool isInstalledSource = false, 
     21             bool requireManifestHash = false);
     22 
     23         SQLiteIndexSource(const SQLiteIndexSource&) = delete;
     24         SQLiteIndexSource& operator=(const SQLiteIndexSource&) = delete;
     25 
     26         SQLiteIndexSource(SQLiteIndexSource&&) = default;
     27         SQLiteIndexSource& operator=(SQLiteIndexSource&&) = default;
     28 
     29         ~SQLiteIndexSource() = default;
     30 
     31         // Get the source's details.
     32         const SourceDetails& GetDetails() const override;
     33 
     34         // Gets the source's identifier; a unique identifier independent of the name
     35         // that will not change between a remove/add or between additional adds.
     36         // Must be suitable for filesystem names.
     37         const std::string& GetIdentifier() const override;
     38 
     39         // Execute a search on the source.
     40         SearchResult Search(const SearchRequest& request) const override;
     41 
     42         // Casts to the requested type.
     43         void* CastTo(ISourceType type) override;
     44 
     45         // Gets the index.
     46         SQLiteIndex& GetIndex() { return m_index; }
     47         const SQLiteIndex& GetIndex() const { return m_index; }
     48 
     49         // Determines if the other source refers to the same as this.
     50         bool IsSame(const SQLiteIndexSource* other) const;
     51 
     52         bool RequireManifestHash() const { return m_requireManifestHash; }
     53 
     54     private:
     55         std::shared_ptr<SQLiteIndexSource> NonConstSharedFromThis() const;
     56 
     57         SourceDetails m_details;
     58         bool m_requireManifestHash;
     59         bool m_isInstalled;
     60         std::shared_ptr<Caching::FileCache> m_manifestCache;
     61         std::shared_ptr<Caching::FileCache> m_packageVersionDataCache;
     62 
     63     protected:
     64         SQLiteIndex m_index;
     65     };
     66 
     67     // A source that holds a SQLiteIndex and lock.
     68     struct SQLiteIndexWriteableSource : public SQLiteIndexSource, public IMutablePackageSource
     69     {
     70         SQLiteIndexWriteableSource(
     71             const SourceDetails& details,
     72             SQLiteIndex&& index,
     73             bool isInstalledSource = false);
     74 
     75         // Casts to the requested type.
     76         void* CastTo(ISourceType type) override;
     77 
     78         // Adds a package version to the source.
     79         void AddPackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath);
     80 
     81         // Removes a package version from the source.
     82         void RemovePackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath);
     83     };
     84 
     85     namespace details
     86     {
     87         // For the IPackage(Version) implementations that need to hold a weak reference to a SQLiteIndexSource.
     88         struct SourceReference
     89         {
     90             SourceReference(const std::shared_ptr<SQLiteIndexSource>& source);
     91 
     92         protected:
     93             std::shared_ptr<SQLiteIndexSource> GetReferenceSource() const;
     94 
     95         private:
     96             std::weak_ptr<SQLiteIndexSource> m_source;
     97         };
     98     }
     99 }