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

refactor: DbPool for managing shared db files #779

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions src/rime/dict/db_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef RIME_DB_POOL_H_
#define RIME_DB_POOL_H_

#include <rime/common.h>
#include <rime/resource.h>

namespace rime {

class ResourceResolver;

template <class T>
class DbPool {
public:
explicit DbPool(the<ResourceResolver> resource_resolver);

an<T> GetDb(const string& db_name);

protected:
the<ResourceResolver> resource_resolver_;
map<string, weak<T>> db_pool_;
};

} // namespace rime

#endif // RIME_DB_POOL_H_
26 changes: 26 additions & 0 deletions src/rime/dict/db_pool_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef RIME_DB_POOL_IMPL_H_
#define RIME_DB_POOL_IMPL_H_

#include "db_pool.h"
#include <rime/resource.h>

namespace rime {

template <class T>
DbPool<T>::DbPool(the<ResourceResolver> resource_resolver)
: resource_resolver_(std::move(resource_resolver)) {}

template <class T>
an<T> DbPool<T>::GetDb(const string& db_name) {
auto db = db_pool_[db_name].lock();
if (!db) {
auto file_path = resource_resolver_->ResolvePath(db_name).string();
db = New<T>(file_path);
db_pool_[db_name] = db;
}
return db;
};

} // namespace rime

#endif // RIME_DB_POOL_IMPL_H_
13 changes: 5 additions & 8 deletions src/rime/dict/reverse_lookup_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <rime/schema.h>
#include <rime/service.h>
#include <rime/ticket.h>
#include <rime/dict/db_pool_impl.h>
#include <rime/dict/dict_settings.h>
#include <rime/dict/reverse_lookup_dictionary.h>

Expand Down Expand Up @@ -235,17 +236,13 @@ static const ResourceType kReverseDbResourceType = {"reverse_db", "",
".reverse.bin"};

ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent()
: resource_resolver_(Service::instance().CreateDeployedResourceResolver(
kReverseDbResourceType)) {}
: DbPool(the<ResourceResolver>(
Service::instance().CreateDeployedResourceResolver(
kReverseDbResourceType))) {}

ReverseLookupDictionary* ReverseLookupDictionaryComponent::Create(
const string& dict_name) {
auto db = db_pool_[dict_name].lock();
if (!db) {
auto file_path = resource_resolver_->ResolvePath(dict_name).string();
db = New<ReverseDb>(file_path);
db_pool_[dict_name] = db;
}
auto db = GetDb(dict_name);
return new ReverseLookupDictionary(db);
};

Expand Down
8 changes: 3 additions & 5 deletions src/rime/dict/reverse_lookup_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdint.h>
#include <rime/common.h>
#include <rime/component.h>
#include <rime/dict/db_pool.h>
#include <rime/dict/mapped_file.h>
#include <rime/dict/string_table.h>
#include <rime/dict/vocabulary.h>
Expand Down Expand Up @@ -75,15 +76,12 @@ class ReverseLookupDictionary
class ResourceResolver;

class ReverseLookupDictionaryComponent
: public ReverseLookupDictionary::Component {
: public ReverseLookupDictionary::Component,
protected DbPool<ReverseDb> {
public:
ReverseLookupDictionaryComponent();
ReverseLookupDictionary* Create(const Ticket& ticket);
ReverseLookupDictionary* Create(const string& dict_name);

private:
map<string, weak<ReverseDb>> db_pool_;
the<ResourceResolver> resource_resolver_;
};

} // namespace rime
Expand Down