1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_INDEXING_SQL_STORAGE_H_
#define CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_INDEXING_SQL_STORAGE_H_
#include <optional>
#include <set>
#include <string>
#include "base/component_export.h"
#include "base/files/file_path.h"
#include "base/sequence_checker.h"
#include "chromeos/ash/components/file_manager/indexing/file_info.h"
#include "chromeos/ash/components/file_manager/indexing/file_info_table.h"
#include "chromeos/ash/components/file_manager/indexing/index_storage.h"
#include "chromeos/ash/components/file_manager/indexing/posting_list_table.h"
#include "chromeos/ash/components/file_manager/indexing/term.h"
#include "chromeos/ash/components/file_manager/indexing/term_table.h"
#include "chromeos/ash/components/file_manager/indexing/token_table.h"
#include "chromeos/ash/components/file_manager/indexing/url_table.h"
#include "sql/database.h"
#include "url/gurl.h"
namespace sql {
class Statement;
} // namespace sql
namespace ash::file_manager {
// Represents an inverted index storage implemented on top of SQL database.
// Use this in production environments. Typical use it to create an instance
// of the FileIndexService class via its factory. If you need to create it
// manually, you would need to run:
//
// base::FilePath db_path("path/to/where/db/is/stored/dbname.db");
// SqlStorage storage(db_path, "uma_unique_db_tag");
// CHECK(storage.Init());
//
// Once successfully initialized, the storage is ready to use. Use it to
// store associations between terms and files, using public method of this
// class.
class COMPONENT_EXPORT(FILE_MANAGER) SqlStorage : public IndexStorage {
public:
SqlStorage(base::FilePath db_path, sql::Database::Tag uma_tag);
~SqlStorage() override;
SqlStorage(const SqlStorage&) = delete;
SqlStorage& operator=(const SqlStorage&) = delete;
// SQL implementation of IndexStorage methods.
// Lifecycle methods.
[[nodiscard]] bool Init() override;
bool Close() override;
// Inverted index and plain index functions.
const std::set<int64_t> GetUrlIdsForTermId(int64_t term_id) const override;
const std::set<int64_t> GetTermIdsForUrl(int64_t url_id) const override;
// Posting list support.
size_t AddToPostingList(int64_t term_id, int64_t url_id) override;
size_t DeleteFromPostingList(int64_t term_id, int64_t url_id) override;
// Term ID management.
int64_t GetTermId(const Term& term) const override;
int64_t GetOrCreateTermId(const Term& term) override;
// Token ID management.
int64_t GetTokenId(const std::string& token_bytes) const override;
int64_t GetOrCreateTokenId(const std::string& token0_bytes) override;
// URL ID management.
int64_t GetUrlId(const GURL& url) const override;
int64_t GetOrCreateUrlId(const GURL& url) override;
int64_t MoveUrl(const GURL& from, const GURL& to) override;
int64_t DeleteUrl(const GURL& url) override;
// FileInfo management.
int64_t PutFileInfo(const FileInfo& file_info) override;
std::optional<FileInfo> GetFileInfo(int64_t url_id) const override;
int64_t DeleteFileInfo(int64_t url_id) override;
// Miscellaneous.
size_t AddTermIdsForUrl(const std::set<int64_t>& term_ids,
int64_t url_id) override;
size_t DeleteTermIdsForUrl(const std::set<int64_t>& term_ids,
int64_t url_id) override;
// Access to the actual SQL database. Tests only.
sql::Database* GetDbForTests() { return &db_; }
private:
// Error callback set on the database.
void OnErrorCallback(int error, sql::Statement* stmt);
// Executes the code that initializes all tables owned by this storage.
bool InitTables();
// Resets the database to empty state. Only call after catastrophic error.s
void Restart();
// The full path to the database (folder and name).
base::FilePath db_path_;
// The actual SQL Lite database.
sql::Database db_;
// The table that holds a mapping from tokens to token IDs.
TokenTable token_table_;
// The table that holds a mapping from terms to their IDs.
TermTable term_table_;
// The table that holds a mapping from URLs to URL IDs.
UrlTable url_table_;
// The table that holds a mapping from URL IDs to FileInfo objects.
FileInfoTable file_info_table_;
// The table that holds associations between term IDs and
// URL IDs. It also maintains indexes that allow fast retrieval of all
// URL IDs associated with the given term ID and all term IDs present
// in a file with the given URL ID.
PostingListTable posting_list_table_;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace ash::file_manager
#endif // CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_INDEXING_SQL_STORAGE_H_
|