您的位置:首页 > Web前端 > Node.js

node-haystack Episode 8: Simple Recovery And Verification

2016-09-10 14:43 721 查看
Any crafts made by mankind have defects. Some extra work must be done to save our ass.

Recovery

Recovery happens at volume level. To simplify the problem, we just use file copy as recovery. The demonstration code will show up in next episode.

Verification

The verification just compares two volumes byte by byte. It assumes the source is complete health. If another one, the destination, has just one byte difference from the source, the verification do fail. Sorry for the callback hell, I know it difficulty for reading. Probably it’s the time to buy yourself a new wider monitor.

/*!
\brief Verify two volumes byte by byte.
\return "Return of the function"
*/
void Verify(const std::string& src, const std::string& dst, const cb_verify_t& cb) {
AsyncFile afs_src;
AsyncFile afs_dst;
u64 off = 0;

afs_src.Stat(src, [&, cb](int err, const AsyncFile::file_stat_t& src_fstat, void*) mutable {
if (err) {
LOG("Failed to stat source");
return cb(err, "existence.source", 0);
}

afs_dst.Stat(dst, [&, cb](int err, const AsyncFile::file_stat_t& dst_fstat, void*) mutable {
if (err) {
LOG("Failed to stat destination");
return cb(err, "existence.destination", 0);
}

if (src_fstat.size != dst_fstat.size) {
return cb(_ERR(VOL_NONIDENTICAL), "validate.size", 0);
}

afs_src.Open(src, [&, cb](int err, void*) mutable {
if (err) {
LOG("Failed to open source");
return cb(err, "open.source", 0);
}

afs_dst.Open(dst, [&, cb](int err, void*) mutable {
if (err) {
LOG("Failed to open destination");
return cb(err, "open.destination", 0);
}

bst::function<void()> do_verify;

do_verify = [&]() {
afs_src.Read(off, READ_BUFFER_SIZE, [&, cb](int err, shared_t<vec_t<char>>& src_buf, void*) mutable {
if (err) {
return cb(err, "read.source", 0);
}

afs_dst.Read(off, READ_BUFFER_SIZE, [&](int err, shared_t<vec_t<char>>& dst_buf, void*) mutable {
if (err) {
return cb(err, "read.destination", 0);
}

if (src_buf->size() != dst_buf->size()) {
return cb(_ERR(VOL_NONIDENTICAL), "validate.result", 0);
}

vec_t<char>& src_vec = *src_buf.get();
vec_t<char>& dst_vec = *dst_buf.get();

if (0 != std::memcmp(&src_vec[0], &dst_vec[0], src_vec.size())) {
return cb(_ERR(VOL_NONIDENTICAL), "validate.data", 0);
}

// Notify progress
cb(_ERR(NOERROR), "progress", (off + dst_buf->size() * 100) / src_fstat.size);

if (src_buf->size() < READ_BUFFER_SIZE) {
return cb(_ERR(NOERROR), "done", 0);    // Succeeded
}

do_verify();
}); // dst read
}); // src read
}; // do_verify

do_verify();
}); // dst open
}); // src open
}); // dst stat
}); // src stat
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: