1 // 2 // Copyright RIME Developers 3 // Distributed under the BSD License 4 // 5 // 2013-04-18 GONG Chen <chen.sst@gmail.com> 6 // 7 #ifndef RIME_DB_UTILS_H_ 8 #define RIME_DB_UTILS_H_ 9 10 #include <rime/common.h> 11 12 namespace rime { 13 14 class Sink { 15 public: 16 virtual ~Sink() = default; 17 virtual bool MetaPut(const string& key, const string& value) = 0; 18 virtual bool Put(const string& key, const string& value) = 0; 19 20 template <class SourceType> 21 int operator<< (SourceType& source); 22 }; 23 24 class Source { 25 public: 26 virtual ~Source() = default; 27 virtual bool MetaGet(string* key, string* value) = 0; 28 virtual bool Get(string* key, string* value) = 0; 29 30 template <class SinkType> 31 int operator>> (SinkType& sink); 32 33 int Dump(Sink* sink); 34 }; 35 36 template <class SourceType> 37 int Sink::operator<< (SourceType& source) { 38 return source.Dump(this); 39 } 40 41 template <class SinkType> 42 int Source::operator>> (SinkType& sink) { 43 return Dump(&sink); 44 } 45 46 class Db; 47 class DbAccessor; 48 49 class DbSink : public Sink { 50 public: 51 explicit DbSink(Db* db); 52 53 virtual bool MetaPut(const string& key, const string& value); 54 virtual bool Put(const string& key, const string& value); 55 56 protected: 57 Db* db_; 58 }; 59 60 class DbSource : public Source { 61 public: 62 explicit DbSource(Db* db); 63 64 virtual bool MetaGet(string* key, string* value); 65 virtual bool Get(string* key, string* value); 66 67 protected: 68 Db* db_; 69 an<DbAccessor> metadata_; 70 an<DbAccessor> data_; 71 }; 72 73 } // namespace rime 74 75 #endif // RIME_DB_UTILS_H_ 76