RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* include_exclude.cc 00002 Jeremy Barnes, 8 March 2012 00003 Copyright (c) 2012 Datacratic. All rights reserved. 00004 00005 */ 00006 00007 #include "include_exclude.h" 00008 #include "rtbkit/common/segments.h" 00009 00010 namespace RTBKIT { 00011 00012 std::string jsonToString(const Json::Value & value) 00013 { 00014 return value.asString(); 00015 } 00016 00017 void jsonParse(const Json::Value & value, boost::u32regex & rex) 00018 { 00019 rex = boost::make_u32regex(value.asString()); 00020 } 00021 00022 void jsonParse(const Json::Value & value, boost::regex & rex) 00023 { 00024 rex = value.asString(); 00025 } 00026 00027 void jsonParse(const Json::Value & value, std::string & str) 00028 { 00029 str = value.asString(); 00030 } 00031 00032 void jsonParse(const Json::Value & value, int & i) 00033 { 00034 i = value.asInt(); 00035 } 00036 00037 bool matchesAnyAny(const std::vector<int> & values, const SegmentList & vals, 00038 bool matchIfEmpty) 00039 { 00040 if (values.empty()) return matchIfEmpty; 00041 return vals.match(values); 00042 } 00043 00044 template class IncludeExclude<std::string>; 00045 template class IncludeExclude<boost::regex>; 00046 template class IncludeExclude<int>; 00047 template class IncludeExclude<boost::u32regex>; 00048 00049 #if 0 00050 00051 // Structure to match a value against an include and exclude list of 00052 // regular expressions, caching the result of the computations. 00053 struct RegexMatcher { 00054 RegexMatcher() 00055 { 00056 } 00057 00058 bool isIncluded(const std::string & val, 00059 const std::vector<boost::regex> & include, 00060 const std::vector<boost::regex> & exclude) 00061 { 00062 bool included = include.empty(); 00063 for (unsigned i = 0; !included && i < include.size(); ++i) { 00064 const boost::regex & rex = include[i]; 00065 string rexStr = rex.str(); 00066 bool regexResult; 00067 if (regexCache.count(rexStr)) 00068 regexResult = regexCache[rexStr]; 00069 else { 00070 regexResult = regexCache[rexStr] 00071 = boost::regex_search(val, rex); 00072 } 00073 00074 included = regexResult; 00075 } 00076 00077 if (!included) return false; 00078 00079 bool excluded = false; 00080 for (unsigned i = 0; !excluded && i < exclude.size(); ++i) { 00081 const boost::regex & rex = exclude[i]; 00082 string rexStr = rex.str(); 00083 bool regexResult; 00084 if (regexCache.count(rexStr)) 00085 regexResult = regexCache[rexStr]; 00086 else { 00087 regexResult = regexCache[rexStr] 00088 = boost::regex_search(val, rex); 00089 } 00090 00091 excluded = regexResult; 00092 } 00093 00094 if (excluded) return false; 00095 00096 return true; 00097 } 00098 00099 std::map<std::string, bool> regexCache; 00100 }; 00101 00102 RegexMatcher urlMatcher, locationMatcher, languageMatcher; 00103 00104 #endif 00105 00106 } // namespace RTBKIT