RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
common/augmentation.cc
00001 
00009 #include "rtbkit/common/augmentation.h"
00010 #include "jml/arch/format.h"
00011 
00012 #include <iostream>
00013 #include <algorithm>
00014 
00015 using namespace std;
00016 
00017 namespace RTBKIT {
00018 
00019 
00020 /******************************************************************************/
00021 /* UTILITIES                                                                  */
00022 /******************************************************************************/
00023 
00024 namespace {
00025 
00026 void mergeAugmentationData(Json::Value& lhs, const Json::Value& rhs)
00027 {
00028     vector<string> members = rhs.getMemberNames();
00029     for (auto it = members.begin(), end = members.end(); it != end; ++it) {
00030         ExcCheck(!lhs.isMember(*it), "Duplicated augmentation data.");
00031         lhs[*it] = rhs[*it];
00032     }
00033 }
00034 
00035 } // namespace anonymous
00036 
00037 
00038 /******************************************************************************/
00039 /* AUMGMENTATION                                                              */
00040 /******************************************************************************/
00041 
00042 void
00043 Augmentation::
00044 mergeWith(const Augmentation& other)
00045 {
00046     tags.insert(other.tags.begin(), other.tags.end());
00047     mergeAugmentationData(data, other.data);
00048 }
00049 
00050 Json::Value
00051 Augmentation::
00052 toJson() const
00053 {
00054     Json::Value json(Json::objectValue);
00055 
00056     json["data"] = data;
00057 
00058     if (!tags.empty()) {
00059         Json::Value jsonTags(Json::arrayValue);
00060 
00061         for (auto it = tags.begin(), end = tags.end(); it != end; ++it)
00062             jsonTags.append(*it);
00063 
00064         json["tags"] = jsonTags;
00065     }
00066 
00067     return json;
00068 }
00069 
00070 Augmentation
00071 Augmentation::
00072 fromJson(const Json::Value& json)
00073 {
00074     Augmentation aug;
00075 
00076     aug.data = json["data"];
00077 
00078     const Json::Value& jsonTags = json["tags"];
00079     for (auto it = jsonTags.begin(), end = jsonTags.end(); it != end; ++it)
00080         aug.tags.insert(it->asString());
00081 
00082     return aug;
00083 }
00084 
00085 
00086 /******************************************************************************/
00087 /* AUGMENTATION LIST                                                          */
00088 /******************************************************************************/
00089 
00090 void
00091 AugmentationList::
00092 mergeWith(const AugmentationList& other)
00093 {
00094     for (auto it = other.begin(), end = other.end(); it != end; ++it)
00095         (*this)[it->first].mergeWith(it->second);
00096 }
00097 
00098 Augmentation
00099 AugmentationList::
00100 filterForAccount(AccountKey account) const
00101 {
00102     Augmentation result;
00103 
00104     while(true) {
00105         auto it = find(account);
00106         if (it != end()) result.mergeWith(it->second);
00107 
00108         if (account.empty()) break;
00109         account.pop_back();
00110     }
00111 
00112     return result;
00113 }
00114 
00115 vector<string>
00116 AugmentationList::
00117 tagsForAccount(AccountKey account) const
00118 {
00119     vector<string> tags;
00120 
00121     while(true) {
00122         auto it = find(account);
00123         if (it != end()) {
00124             const auto& aug = it->second;
00125             tags.insert(tags.end(), aug.tags.begin(), aug.tags.end());
00126         }
00127 
00128         if (account.empty()) break;
00129         account.pop_back();
00130     }
00131 
00132     sort(tags.begin(), tags.end());
00133 
00134     auto it = unique(tags.begin(), tags.end());
00135     tags.erase(it, tags.end());
00136 
00137     return tags;
00138 }
00139 
00140 
00141 Json::Value
00142 AugmentationList::
00143 toJson() const
00144 {
00145     Json::Value result(Json::arrayValue);
00146 
00147     for (auto it = begin(), last = end(); it != last; ++it) {
00148         Json::Value augJson (Json::objectValue);
00149         augJson["account"] = it->first.toJson();
00150         augJson["augmentation"] = it->second.toJson();
00151         result.append(augJson);
00152     }
00153 
00154     return result;
00155 }
00156 
00157 AugmentationList
00158 AugmentationList::
00159 fromJson(const Json::Value& json)
00160 {
00161     AugmentationList list;
00162 
00163     for (auto it = json.begin(), end = json.end(); it != end; ++it) {
00164         AccountKey acc = AccountKey::fromJson((*it)["account"]);
00165         Augmentation aug = Augmentation::fromJson((*it)["augmentation"]);
00166         list.insert(make_pair(acc, aug));
00167     }
00168 
00169     return list;
00170 }
00171 
00172 } // namespace RTBKIT
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator