修改掉其中有问题的地方

This commit is contained in:
asypost committed 2026-07-31 17:20:58 +08:00
1 parent c451a077b2
commit 45060f03b6
6 files changed
+146 -104

No files matched your search

+64 -42
View File
@@ -130,18 +130,51 @@ namespace seafile::fs
}
}
struct SeafileStore::Database
class SeafileStore::Database
{
using Storage = decltype(make_storage(""));
Storage storage;
std::recursive_mutex mutex;
class LockProxy final
{
Storage& storage;
std::lock_guard<std::recursive_mutex> lock;
public:
LockProxy(Storage& storage, std::recursive_mutex& mutex) : storage(storage), lock(mutex)
{
}
LockProxy(const LockProxy&) = delete;
LockProxy& operator=(const LockProxy&) = delete;
LockProxy(LockProxy&&) = delete;
LockProxy& operator=(LockProxy&&) = delete;
Storage* operator->() const
{
return &this->storage;
}
};
public:
explicit Database(const std::string& path)
: storage(make_storage(path))
{
storage.pragma.journal_mode(sqlite_orm::journal_mode::WAL);
storage.pragma.synchronous(1);
}
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
[[nodiscard]]
LockProxy operator->()
{
return LockProxy{storage, mutex};
}
};
tl::expected<void, api::SeafileException> SeafileStore::auth()
@@ -166,9 +199,8 @@ namespace seafile::fs
tl::expected<RepoEntry, api::SeafileException> SeafileStore::findRepoEntry(
const std::string& repo_id, const std::string& path)
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
const auto entries = storage.get_all<RepoEntry>(
auto& storage = *(this->database);
const auto entries = storage->get_all<RepoEntry>(
sqlite_orm::where(
sqlite_orm::and_(
sqlite_orm::c(&RepoEntry::repo_id) == repo_id,
@@ -185,9 +217,8 @@ namespace seafile::fs
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string& repo_id)
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
const auto repos = storage.get_all<RepoInfo>(
auto& storage = *(this->database);
const auto repos = storage->get_all<RepoInfo>(
sqlite_orm::where(
sqlite_orm::c(&RepoInfo::id) == repo_id
));
@@ -218,9 +249,8 @@ namespace seafile::fs
try
{
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
storage.remove_all<RepoEntry>(sqlite_orm::where(
auto& storage = *this->database;
storage->remove_all<RepoEntry>(sqlite_orm::where(
sqlite_orm::c(&RepoEntry::repo_id) == repo_id
)
);
@@ -231,8 +261,7 @@ namespace seafile::fs
return tl::make_unexpected(repo_entries.error());
}
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
auto& storage = *this->database;
std::vector<RepoEntry> entries;
entries.reserve(repo_entries->size());
std::transform(repo_entries->begin(), repo_entries->end(), std::back_inserter(entries),
@@ -240,9 +269,9 @@ namespace seafile::fs
{
return RepoEntry::from_api_model(repo_id, "/", entry);
});
storage.transaction([&entries,&storage]
storage->transaction([&entries,&storage]
{
storage.replace_range(entries.begin(), entries.end());
storage->replace_range(entries.begin(), entries.end());
return true;
});
}
@@ -259,8 +288,7 @@ namespace seafile::fs
{
try
{
std::lock_guard lock(database->mutex);
this->database->storage.sync_schema();
(*this->database)->sync_schema();
}
catch (std::exception& e)
{
@@ -280,8 +308,7 @@ namespace seafile::fs
{
return tl::make_unexpected(user_info.error());
}
std::lock_guard lock(database->mutex);
this->database->storage.replace(*user_info);
(*this->database)->replace(*user_info);
return {};
}
@@ -292,8 +319,7 @@ namespace seafile::fs
{
return tl::make_unexpected(repos.error());
}
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
auto& storage = *this->database;
std::vector<RepoInfo> result;
result.reserve(repos->size());
for (const auto& repo : *repos)
@@ -302,7 +328,7 @@ namespace seafile::fs
result.push_back(storage_repo);
try
{
const auto existings = storage.get_all<RepoInfo>(
const auto existings = storage->get_all<RepoInfo>(
sqlite_orm::where(sqlite_orm::c(&RepoInfo::id) == storage_repo.id));
std::optional<RepoInfo> existing = std::nullopt;
if (!existings.empty())
@@ -311,10 +337,10 @@ namespace seafile::fs
}
if (existing && existing->head_commit_id != storage_repo.head_commit_id)
{
storage.remove_all<RepoEntry>(sqlite_orm::where(
storage->remove_all<RepoEntry>(sqlite_orm::where(
sqlite_orm::c(&RepoEntry::repo_id) == storage_repo.id));
}
storage.replace(storage_repo);
storage->replace(storage_repo);
}
catch (std::exception& e)
{
@@ -330,9 +356,8 @@ namespace seafile::fs
{
std::vector<RepoInfo> repos;
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
repos = storage.get_all<RepoInfo>(
auto& storage = *this->database;
repos = storage->get_all<RepoInfo>(
sqlite_orm::where(sqlite_orm::c(&RepoInfo::name) == name));
}
if (!repos.empty())
@@ -379,17 +404,17 @@ namespace seafile::fs
{
return RepoEntry::from_api_model(repo_id, parent_path, entry);
});
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
auto& storage = *this->database;
constexpr std::size_t batch_size = 500;
storage.transaction([&]
storage->transaction([&]
{
for (auto it = repo_entries.begin(); it != repo_entries.end();)
{
auto end = it;
std::advance(end, std::min(batch_size,static_cast<std::size_t>(std::distance(it, repo_entries.end()))));
std::advance(
end, std::min(batch_size, static_cast<std::size_t>(std::distance(it, repo_entries.end()))));
storage.replace_range(it, end);
storage->replace_range(it, end);
it = end;
}
@@ -406,16 +431,15 @@ namespace seafile::fs
std::vector<RepoEntry> SeafileStore::queryDirectoryChildren(
const std::string& repo_id, const std::string& parent_path)
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
auto& storage = *this->database;
const std::string prefix = (parent_path == "/") ? "" : parent_path;
const auto all = storage.get_all<RepoEntry>(
const auto all = storage->get_all<RepoEntry>(
sqlite_orm::where(
sqlite_orm::and_(
sqlite_orm::c(&RepoEntry::repo_id) == repo_id,
sqlite_orm::like(&RepoEntry::path, prefix + "/%")
)
),sqlite_orm::order_by(&RepoEntry::name));
), sqlite_orm::order_by(&RepoEntry::name));
std::vector<RepoEntry> children;
for (auto& entry : all)
{
@@ -478,14 +502,13 @@ namespace seafile::fs
if (parts.empty())
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
auto repos = storage.get_all<RepoInfo>(sqlite_orm::order_by(&RepoInfo::id));
auto& storage = *this->database;
auto repos = storage->get_all<RepoInfo>(sqlite_orm::order_by(&RepoInfo::id));
if (repos.empty())
{
if (const auto synced = this->syncRepos(); synced)
{
repos = storage.get_all<RepoInfo>();
repos = storage->get_all<RepoInfo>();
}
else
{
@@ -527,9 +550,8 @@ namespace seafile::fs
tl::expected<api::AccountInfo, api::SeafileException> SeafileStore::accountInfo() const
{
std::lock_guard lock(database->mutex);
auto& storage = this->database->storage;
if (const auto infos = storage.get_all<AccountInfo>(); !infos.empty())
auto& storage = *this->database;
if (const auto infos = storage->get_all<AccountInfo>(); !infos.empty())
{
return infos.front();
}