Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create DB 1.1 schema for system reference strings #564

Merged
merged 6 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
377 changes: 369 additions & 8 deletions src/AppInstallerCLITests/SQLiteIndex.cpp

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/AppInstallerCLITests/Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "TestCommon.h"
#include <AppInstallerStrings.h>

using namespace std::string_view_literals;
using namespace AppInstaller::Utility;


Expand Down Expand Up @@ -93,3 +94,11 @@ TEST_CASE("CaseInsensitiveStartsWith", "[strings]")
REQUIRE(!CaseInsensitiveStartsWith("withstarts", "starts"));
REQUIRE(!CaseInsensitiveStartsWith(" starts", "starts"));
}

TEST_CASE("FoldCase", "[strings]")
{
REQUIRE(FoldCase(""sv) == FoldCase(""sv));
REQUIRE(FoldCase("foldcase"sv) == FoldCase("FOLDCASE"sv));
REQUIRE(FoldCase(u8"f\xF6ldcase"sv) == FoldCase(u8"F\xD6LDCASE"sv));
REQUIRE(FoldCase(u8"foldc\x430se"sv) == FoldCase(u8"FOLDC\x410SE"sv));
}
49 changes: 49 additions & 0 deletions src/AppInstallerCommonCore/AppInstallerStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,55 @@ namespace AppInstaller::Utility
return result;
}

std::string FoldCase(std::string_view input)
{
if (input.empty())
{
return {};
}

wil::unique_any<UCaseMap*, decltype(ucasemap_close), &ucasemap_close> caseMap;
UErrorCode errorCode = UErrorCode::U_ZERO_ERROR;
caseMap.reset(ucasemap_open(nullptr, U_FOLD_CASE_DEFAULT, &errorCode));

if (U_FAILURE(errorCode))
{
AICLI_LOG(Core, Error, << "ucasemap_open returned " << errorCode);
THROW_HR(E_UNEXPECTED);
}

int32_t cch = ucasemap_utf8FoldCase(caseMap.get(), nullptr, 0, input.data(), static_cast<int32_t>(input.size()), &errorCode);
if (errorCode != U_BUFFER_OVERFLOW_ERROR)
{
AICLI_LOG(Core, Error, << "ucasemap_utf8FoldCase returned " << errorCode);
THROW_HR(E_UNEXPECTED);
}

errorCode = UErrorCode::U_ZERO_ERROR;

std::string result(cch, '\0');
cch = ucasemap_utf8FoldCase(caseMap.get(), &result[0], cch, input.data(), static_cast<int32_t>(input.size()), &errorCode);
if (U_FAILURE(errorCode))
{
AICLI_LOG(Core, Error, << "ucasemap_utf8FoldCase returned " << errorCode);
THROW_HR(E_UNEXPECTED);
}

while (result.back() == '\0')
{
result.pop_back();
}

return result;
}

NormalizedString FoldCase(const NormalizedString& input)
{
NormalizedString result;
result.assign(FoldCase(static_cast<std::string_view>(input)));
return result;
}

bool IsEmptyOrWhitespace(std::wstring_view str)
{
if (str.empty())
Expand Down
11 changes: 8 additions & 3 deletions src/AppInstallerCommonCore/Public/AppInstallerStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ namespace AppInstaller::Utility
// Get the lower case version of the given std::wstring
std::wstring ToLower(std::wstring_view in);

// Folds the case of the given std::string
// See https://unicode-org.github.io/icu/userguide/transforms/casemappings.html#case-folding
std::string FoldCase(std::string_view input);

// Folds the case of the given NormalizedString, returning it as also Normalized
// See https://unicode-org.github.io/icu/userguide/transforms/casemappings.html#case-folding
NormalizedString FoldCase(const NormalizedString& input);

// Checks if the input string is empty or whitespace
bool IsEmptyOrWhitespace(std::wstring_view str);

Expand All @@ -99,9 +107,6 @@ namespace AppInstaller::Utility
// Removes whitespace from the beginning and end of the string.
std::string& Trim(std::string& str);

// Gets the number of bytes remaining in the stream.
std::streamsize GetRemainingByteCount(std::istream& stream);

// Reads the entire stream into a string.
std::string ReadEntireStream(std::istream& stream);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ namespace AppInstaller::Manifest
// Store Product Id
string_t ProductId;

// Package family name for MSIX type only.
Copy link
Contributor

@yao-msft yao-msft Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSIX [](start = 35, length = 5)

or Store type? #Resolved

string_t PackageFamilyName;

// Product code for non-MSIX only.
string_t ProductCode;

// If present, has more precedence than root
InstallerTypeEnum InstallerType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@
<ClInclude Include="Microsoft\Schema\1_0\SearchResultsTable.h" />
<ClInclude Include="Microsoft\Schema\1_0\TagsTable.h" />
<ClInclude Include="Microsoft\Schema\1_0\VersionTable.h" />
<ClInclude Include="Microsoft\Schema\1_1\Interface.h" />
<ClInclude Include="Microsoft\Schema\1_1\PackageFamilyNameTable.h" />
<ClInclude Include="Microsoft\Schema\1_1\ProductCodeTable.h" />
<ClInclude Include="Microsoft\Schema\1_1\SearchResultsTable.h" />
<ClInclude Include="Microsoft\Schema\ISQLiteIndex.h" />
<ClInclude Include="Microsoft\Schema\MetadataTable.h" />
<ClInclude Include="Microsoft\Schema\Version.h" />
Expand All @@ -214,12 +218,14 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Microsoft\PreIndexedPackageSourceFactory.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\Interface.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\Interface_1_0.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\ManifestTable.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\OneToManyTable.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\OneToOneTable.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\PathPartTable.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\SearchResultsTable.cpp" />
<ClCompile Include="Microsoft\Schema\1_0\SearchResultsTable_1_0.cpp" />
<ClCompile Include="Microsoft\Schema\1_1\Interface_1_1.cpp" />
<ClCompile Include="Microsoft\Schema\1_1\SearchResultsTable_1_1.cpp" />
<ClCompile Include="Microsoft\Schema\MetadataTable.cpp" />
<ClCompile Include="Microsoft\Schema\Version.cpp" />
<ClCompile Include="Microsoft\SQLiteIndex.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<Filter Include="ICU">
<UniqueIdentifier>{dac1a359-45ac-4456-8a83-f7df10058197}</UniqueIdentifier>
</Filter>
<Filter Include="Microsoft\Schema\1_1">
<UniqueIdentifier>{aef8989c-44fd-4848-ae2c-c81d3f2e2c72}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
Expand Down Expand Up @@ -114,6 +117,18 @@
<ClInclude Include="AggregatedSource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Microsoft\Schema\1_1\Interface.h">
<Filter>Microsoft\Schema\1_1</Filter>
</ClInclude>
<ClInclude Include="Microsoft\Schema\1_1\PackageFamilyNameTable.h">
<Filter>Microsoft\Schema\1_1</Filter>
</ClInclude>
<ClInclude Include="Microsoft\Schema\1_1\ProductCodeTable.h">
<Filter>Microsoft\Schema\1_1</Filter>
</ClInclude>
<ClInclude Include="Microsoft\Schema\1_1\SearchResultsTable.h">
<Filter>Microsoft\Schema\1_1</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand All @@ -131,9 +146,6 @@
<ClCompile Include="Microsoft\Schema\Version.cpp">
<Filter>Microsoft\Schema</Filter>
</ClCompile>
<ClCompile Include="Microsoft\Schema\1_0\Interface.cpp">
<Filter>Microsoft\Schema\1_0</Filter>
</ClCompile>
<ClCompile Include="Microsoft\Schema\1_0\OneToOneTable.cpp">
<Filter>Microsoft\Schema\1_0</Filter>
</ClCompile>
Expand Down Expand Up @@ -161,15 +173,24 @@
<ClCompile Include="SQLiteTempTable.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Microsoft\Schema\1_0\SearchResultsTable.cpp">
<Filter>Microsoft\Schema\1_0</Filter>
</ClCompile>
<ClCompile Include="ICU\SQLiteICU.c">
<Filter>ICU</Filter>
</ClCompile>
<ClCompile Include="AggregatedSource.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Microsoft\Schema\1_0\Interface_1_0.cpp">
<Filter>Microsoft\Schema\1_0</Filter>
</ClCompile>
<ClCompile Include="Microsoft\Schema\1_1\Interface_1_1.cpp">
<Filter>Microsoft\Schema\1_1</Filter>
</ClCompile>
<ClCompile Include="Microsoft\Schema\1_0\SearchResultsTable_1_0.cpp">
<Filter>Microsoft\Schema\1_0</Filter>
</ClCompile>
<ClCompile Include="Microsoft\Schema\1_1\SearchResultsTable_1_1.cpp">
<Filter>Microsoft\Schema\1_1</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
Expand Down
7 changes: 6 additions & 1 deletion src/AppInstallerRepositoryCore/Microsoft/SQLiteIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ namespace AppInstaller::Repository::Microsoft
m_version = m_interface->GetVersion();
}

void SQLiteIndex::ForceVersion(const Schema::Version& version)
{
m_interface = version.CreateISQLiteIndex();
}

void SQLiteIndex::AddManifest(const std::filesystem::path& manifestPath, const std::filesystem::path& relativePath)
{
AICLI_LOG(Repo, Info, << "Adding manifest from file [" << manifestPath << "]");
Expand Down Expand Up @@ -159,7 +164,7 @@ namespace AppInstaller::Repository::Microsoft

SQLite::Savepoint savepoint = SQLite::Savepoint::Create(m_dbconn, "sqliteindex_updatemanifest");

bool result = m_interface->UpdateManifest(m_dbconn, manifest, relativePath);
bool result = m_interface->UpdateManifest(m_dbconn, manifest, relativePath).first;

if (result)
{
Expand Down
4 changes: 4 additions & 0 deletions src/AppInstallerRepositoryCore/Microsoft/SQLiteIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ namespace AppInstaller::Repository::Microsoft
// Gets the schema version of the index.
Schema::Version GetVersion() const { return m_version; }

// Changes the version of the interface being used to operate on the database.
// Should only be used for testing.
void ForceVersion(const Schema::Version& version);
Copy link
Contributor

@yao-msft yao-msft Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void ForceVersion(const Schema::Version& version); [](start = 8, length = 50)

should we put it inside existing #def DISABLE***TEST_HOOK since it's test only? #Resolved


// Gets the last write time for the index.
std::chrono::system_clock::time_point GetLastWriteTime();

Expand Down
22 changes: 18 additions & 4 deletions src/AppInstallerRepositoryCore/Microsoft/Schema/1_0/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@
// Licensed under the MIT License.
#pragma once
#include "Microsoft/Schema/ISQLiteIndex.h"
#include "Microsoft/Schema/1_0/SearchResultsTable.h"

#include <memory>
#include <vector>


namespace AppInstaller::Repository::Microsoft::Schema::V1_0
{
// Interface to this schema version exposed through ISQLiteIndex.
struct Interface : public SQLiteIndexBase
struct Interface : public ISQLiteIndex
{
// Version 1.0
Schema::Version GetVersion() const override;
void CreateTables(SQLite::Connection& connection) override;
void AddManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) override;
bool UpdateManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) override;
void RemoveManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) override;
SQLite::rowid_t AddManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) override;
std::pair<bool, SQLite::rowid_t> UpdateManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) override;
SQLite::rowid_t RemoveManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) override;
void PrepareForPackaging(SQLite::Connection& connection) override;
SearchResult Search(SQLite::Connection& connection, const SearchRequest& request) override;
std::optional<std::string> GetIdStringById(SQLite::Connection& connection, SQLite::rowid_t id) override;
std::optional<std::string> GetNameStringById(SQLite::Connection& connection, SQLite::rowid_t id) override;
std::optional<std::string> GetPathStringByKey(SQLite::Connection& connection, SQLite::rowid_t id, std::string_view version, std::string_view channel) override;
std::vector<Utility::VersionAndChannel> GetVersionsById(SQLite::Connection& connection, SQLite::rowid_t id) override;

protected:
// Creates the search results table.
virtual std::unique_ptr<SearchResultsTable> CreateSearchResultsTable(SQLite::Connection& connection) const;

// Gets the ordering of matches to execute, with more specific matches coming first.
virtual std::vector<MatchType> GetMatchTypeOrder(MatchType type) const;

// Executes all relevant searchs for the query.
virtual void PerformQuerySearch(SearchResultsTable& resultsTable, const RequestMatch& query) const;
};
}
Loading