-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: DbPool for managing shared db files
- Loading branch information
Showing
4 changed files
with
59 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters