未验证 提交 8f9339e0 编写于 作者: J Jeff Wang 提交者: GitHub

Only write the modified tablets to file system. (#304)

* Only write the modified tablets to file system.
Move some functions definitions from headers to cc files.

* Persist the data in the destructor
Use unordered_set first.

* Use -1 for the unknown.
上级 77c328ec
...@@ -21,6 +21,7 @@ Storage::Storage() { ...@@ -21,6 +21,7 @@ Storage::Storage() {
data_ = std::make_shared<storage::Storage>(); data_ = std::make_shared<storage::Storage>();
tablets_ = std::make_shared<std::map<std::string, storage::Tablet>>(); tablets_ = std::make_shared<std::map<std::string, storage::Tablet>>();
modes_ = std::make_shared<std::set<std::string>>(); modes_ = std::make_shared<std::set<std::string>>();
modified_tablet_set_ = std::unordered_set<std::string>();
time_t t; time_t t;
time(&t); time(&t);
data_->set_timestamp(t); data_->set_timestamp(t);
...@@ -31,6 +32,10 @@ Storage::Storage(const Storage& other) ...@@ -31,6 +32,10 @@ Storage::Storage(const Storage& other)
dir_ = other.dir_; dir_ = other.dir_;
} }
Storage::~Storage() {
PersistToDisk();
}
void Storage::AddMode(const std::string& x) { void Storage::AddMode(const std::string& x) {
// avoid duplicate modes. // avoid duplicate modes.
if (modes_->count(x) != 0) return; if (modes_->count(x) != 0) return;
...@@ -43,11 +48,20 @@ Tablet Storage::AddTablet(const std::string& x) { ...@@ -43,11 +48,20 @@ Tablet Storage::AddTablet(const std::string& x) {
CHECK(tablets_->count(x) == 0) << "tablet [" << x << "] has existed"; CHECK(tablets_->count(x) == 0) << "tablet [" << x << "] has existed";
(*tablets_)[x] = storage::Tablet(); (*tablets_)[x] = storage::Tablet();
AddTag(x); AddTag(x);
MarkTabletModified(x);
// WRITE_GUARD // WRITE_GUARD
PersistToDisk(); PersistToDisk();
return Tablet(&(*tablets_)[x], this); return Tablet(&(*tablets_)[x], this);
} }
void Storage::SetDir(const std::string& dir) {
*dir_ = dir;
}
std::string Storage::dir() const {
return *dir_;
}
void Storage::PersistToDisk() { PersistToDisk(*dir_); } void Storage::PersistToDisk() { PersistToDisk(*dir_); }
void Storage::PersistToDisk(const std::string& dir) { void Storage::PersistToDisk(const std::string& dir) {
...@@ -56,12 +70,29 @@ void Storage::PersistToDisk(const std::string& dir) { ...@@ -56,12 +70,29 @@ void Storage::PersistToDisk(const std::string& dir) {
fs::SerializeToFile(*data_, meta_path(dir)); fs::SerializeToFile(*data_, meta_path(dir));
for (auto tag : data_->tags()) { for (auto tag : data_->tags()) {
auto it = tablets_->find(tag); if (modified_tablet_set_.count(tag) > 0){
CHECK(it != tablets_->end()) << "tag " << tag << " not exist."; auto it = tablets_->find(tag);
fs::SerializeToFile(it->second, tablet_path(dir, tag)); CHECK(it != tablets_->end()) << "tag " << tag << " not exist.";
fs::SerializeToFile(it->second, tablet_path(dir, tag));
}
} }
modified_tablet_set_.clear();
} }
Storage* Storage::parent() {
return this;
}
void Storage::MarkTabletModified(const std::string tag) {
modified_tablet_set_.insert(tag);
}
void Storage::AddTag(const std::string& x) {
*data_->add_tags() = x;
WRITE_GUARD
}
// StorageReader
std::vector<std::string> StorageReader::all_tags() { std::vector<std::string> StorageReader::all_tags() {
storage::Storage storage; storage::Storage storage;
Reload(storage); Reload(storage);
......
...@@ -58,7 +58,7 @@ struct Storage { ...@@ -58,7 +58,7 @@ struct Storage {
Storage(); Storage();
Storage(const Storage& other); Storage(const Storage& other);
~Storage();
// Add a mode. Mode is similar to TB's FileWriter, It can be "train" or "test" // Add a mode. Mode is similar to TB's FileWriter, It can be "train" or "test"
// or something else. // or something else.
void AddMode(const std::string& x); void AddMode(const std::string& x);
...@@ -67,8 +67,8 @@ struct Storage { ...@@ -67,8 +67,8 @@ struct Storage {
Tablet AddTablet(const std::string& x); Tablet AddTablet(const std::string& x);
// Set storage's directory. // Set storage's directory.
void SetDir(const std::string& dir) { *dir_ = dir; } void SetDir(const std::string& dir);
std::string dir() const { return *dir_; } std::string dir() const;
// Save content in memory to `dir_`. // Save content in memory to `dir_`.
void PersistToDisk(); void PersistToDisk();
...@@ -77,20 +77,20 @@ struct Storage { ...@@ -77,20 +77,20 @@ struct Storage {
void PersistToDisk(const std::string& dir); void PersistToDisk(const std::string& dir);
// A trick help to retrieve the storage's `SimpleSyncMeta`. // A trick help to retrieve the storage's `SimpleSyncMeta`.
Storage* parent() { return this; } Storage* parent();
void MarkTabletModified(const std::string tag);
protected: protected:
// Add a tag which content is `x`. // Add a tag which content is `x`.
void AddTag(const std::string& x) { void AddTag(const std::string& x);
*data_->add_tags() = x;
WRITE_GUARD
}
private: private:
std::shared_ptr<std::string> dir_; std::shared_ptr<std::string> dir_;
std::shared_ptr<std::map<std::string, storage::Tablet>> tablets_; std::shared_ptr<std::map<std::string, storage::Tablet>> tablets_;
std::shared_ptr<storage::Storage> data_; std::shared_ptr<storage::Storage> data_;
std::shared_ptr<std::set<std::string>> modes_; std::shared_ptr<std::set<std::string>> modes_;
std::unordered_set<std::string> modified_tablet_set_;
}; };
// Storage reader, each method will trigger a reading from disk. // Storage reader, each method will trigger a reading from disk.
......
...@@ -13,9 +13,25 @@ See the License for the specific language governing permissions and ...@@ -13,9 +13,25 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "visualdl/storage/tablet.h" #include "visualdl/storage/tablet.h"
#include "visualdl/storage/storage.h"
namespace visualdl { namespace visualdl {
void Tablet::SetTag(const std::string& mode, const std::string& tag) {
auto internal_tag = mode + "/" + tag;
string::TagEncode(internal_tag);
internal_encoded_tag_ = internal_tag;
data_->set_tag(internal_tag);
WRITE_GUARD
}
Record Tablet::AddRecord() {
parent()->MarkTabletModified(internal_encoded_tag_);
IncTotalRecords();
WRITE_GUARD
return Record(data_->add_records(), parent());
}
TabletReader Tablet::reader() { return TabletReader(*data_); } TabletReader Tablet::reader() { return TabletReader(*data_); }
} // namespace visualdl } // namespace visualdl
...@@ -29,11 +29,11 @@ struct TabletReader; ...@@ -29,11 +29,11 @@ struct TabletReader;
* Tablet is a helper for operations on storage::Tablet. * Tablet is a helper for operations on storage::Tablet.
*/ */
struct Tablet { struct Tablet {
enum Type { kScalar = 0, kHistogram = 1, kImage = 2 }; enum Type { kScalar = 0, kHistogram = 1, kImage = 2, kUnknown = -1};
DECL_GUARD(Tablet); DECL_GUARD(Tablet);
Tablet(storage::Tablet* x, Storage* parent) : data_(x), x_(parent) {} Tablet(storage::Tablet* x, Storage* parent) : data_(x), x_(parent), internal_encoded_tag_("") {}
static Type type(const std::string& name) { static Type type(const std::string& name) {
if (name == "scalar") { if (name == "scalar") {
...@@ -46,6 +46,7 @@ struct Tablet { ...@@ -46,6 +46,7 @@ struct Tablet {
return kImage; return kImage;
} }
LOG(ERROR) << "unknown component: " << name; LOG(ERROR) << "unknown component: " << name;
return kUnknown;
} }
// write operations. // write operations.
...@@ -59,18 +60,8 @@ struct Tablet { ...@@ -59,18 +60,8 @@ struct Tablet {
WRITE_GUARD WRITE_GUARD
} }
void SetTag(const std::string& mode, const std::string& tag) { void SetTag(const std::string& mode, const std::string& tag);
auto internal_tag = mode + "/" + tag; Record AddRecord();
string::TagEncode(internal_tag);
data_->set_tag(internal_tag);
WRITE_GUARD
}
Record AddRecord() {
IncTotalRecords();
WRITE_GUARD
return Record(data_->add_records(), parent());
}
template <typename T> template <typename T>
Entry MutableMeta() { Entry MutableMeta() {
...@@ -102,6 +93,7 @@ struct Tablet { ...@@ -102,6 +93,7 @@ struct Tablet {
private: private:
Storage* x_; Storage* x_;
storage::Tablet* data_{nullptr}; storage::Tablet* data_{nullptr};
std::string internal_encoded_tag_;
}; };
/* /*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册