diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2014-05-31 16:14:33 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2014-05-31 16:19:57 +0900 |
| commit | 631f37433a8fda03eb057e9f01e2811111c60d5e (patch) | |
| tree | 5423c539ab253a6249b0bce5e42ba6fe69b0c45d /src/SessionSerializer.cc | |
| parent | 998fba3264c1e896a249f140cb6060cc35c7d6bc (diff) | |
Save session only when there is change since the last serialization
This is a slight optimization not to cause useless disk access. This
only applies to saving session automatically (see
--save-session-interval). aria2.saveSession and serialization at the
end of the session are always performed as before.
When serialization, we first check that whether there is any change
since the last serialization. To do this, we first calculate hash
value of serialized content without writing into file. Then compare
this value to the value of last serialization. If they do not match,
perform serialization.
Diffstat (limited to 'src/SessionSerializer.cc')
| -rw-r--r-- | src/SessionSerializer.cc | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/SessionSerializer.cc b/src/SessionSerializer.cc index cf02548b..9e149f0a 100644 --- a/src/SessionSerializer.cc +++ b/src/SessionSerializer.cc @@ -35,6 +35,7 @@ #include "SessionSerializer.h" #include <cstdio> +#include <cassert> #include <iterator> #include <set> @@ -52,6 +53,7 @@ #include "BufferedFile.h" #include "OptionParser.h" #include "OptionHandler.h" +#include "MessageDigest.h" #if HAVE_ZLIB #include "GZipFile.h" @@ -325,4 +327,88 @@ bool SessionSerializer::save(IOFile& fp) const return true; } +namespace { +// Class to calculate SHA1 hash value for data written into this +// object. No file I/O is done in this class. +class SHA1IOFile : public IOFile { +public: + SHA1IOFile() + : sha1_(MessageDigest::sha1()) + {} + + std::string digest() + { + return sha1_->digest(); + } +protected: + virtual size_t onRead(void* ptr, size_t count) CXX11_OVERRIDE + { + assert(0); + } + + virtual size_t onWrite(const void* ptr, size_t count) CXX11_OVERRIDE + { + sha1_->update(ptr, count); + + return count; + } + + virtual char* onGets(char* s, int size) CXX11_OVERRIDE + { + assert(0); + } + + virtual int onVprintf(const char* format, va_list va) CXX11_OVERRIDE + { + assert(0); + } + + virtual int onFlush() CXX11_OVERRIDE + { + return 0; + } + + virtual int onClose() CXX11_OVERRIDE + { + return 0; + } + + virtual bool onSupportsColor() CXX11_OVERRIDE + { + return false; + } + + virtual bool isError() const CXX11_OVERRIDE + { + return false; + } + + virtual bool isEOF() const CXX11_OVERRIDE + { + return false; + } + + virtual bool isOpen() const CXX11_OVERRIDE + { + return true; + } + +private: + std::unique_ptr<MessageDigest> sha1_; +}; +} // namespace + +std::string SessionSerializer::calculateHash() const +{ + SHA1IOFile sha1io; + + auto rv = save(sha1io); + + if(!rv) { + return ""; + } + + return sha1io.digest(); +} + } // namespace aria2 |
