void ZSetCache::GetRange(const ZRangeSpec& range, bool with_scores, bool with_attrs, ValueVisitCallback* cb, void* cbdata) { uint64 start = get_current_epoch_millis(); CacheReadLockGuard guard(m_lock); ZSetCaheElement min_ele(range.min.NumberValue(), ""); ZSetCaheElement max_ele(range.max.NumberValue(), ""); ZSetCacheElementSet::iterator min_it = m_cache.lower_bound(min_ele); ZSetCacheElementSet::iterator max_it = m_cache.lower_bound(max_ele); int cursor = 0; if (min_it != m_cache.end()) { while (!range.contain_min && min_it != m_cache.end() && min_it->score == range.min.NumberValue()) { min_it++; } while (range.contain_max && max_it != m_cache.end() && max_it->score == range.max.NumberValue()) { max_it++; } while (min_it != max_it && min_it != m_cache.end()) { ValueData v; Buffer buf(const_cast<char*>(min_it->value.data()), 0, min_it->value.size()); v.Decode(buf); cb(v, cursor++, cbdata); if (with_scores) { ValueData score; score.SetDoubleValue(min_it->score); cb(score, cursor++, cbdata); } if (with_attrs) { ValueData attr_value; Buffer attr_buf(const_cast<char*>(min_it->attr.data()), 0, min_it->attr.size()); attr_value.Decode(attr_buf); cb(attr_value, cursor++, cbdata); } min_it++; } } uint64 end = get_current_epoch_millis(); if (end - start > 10) { DEBUG_LOG("Cost %llums to get %d elements in between range [%.2f, %.2f]", end - start, cursor, range.min.NumberValue(), range.max.NumberValue()); } }
int ZSetCache::GetByValue(const ValueData& value, ValueData& score, ValueData& attr) { Buffer buf1; value.Encode(buf1); ZSetCaheElement e; e.value.assign(buf1.GetRawReadBuffer(), buf1.ReadableBytes()); CacheReadLockGuard guard(m_lock); ZSetCacheScoreMap::iterator sit = m_cache_score_dict.find(e.value); if (sit != m_cache_score_dict.end()) { e.score = sit->second; ZSetCacheElementSet::iterator found = m_cache.find(e); if (found != m_cache.end()) { score.SetDoubleValue(e.score); Buffer attr_buf(const_cast<char*>(found->attr.data()), 0, found->attr.size()); attr.Decode(attr_buf); return 0; } } return -1; }
int Ardb::GeoSearch(const DBID& db, const Slice& key, const GeoSearchOptions& options, ValueDataDeque& results) { uint64 start_time = get_current_epoch_micros(); GeoHashBitsSet ress; double x = options.x, y = options.y; if (options.coord_type == GEO_WGS84_TYPE) { x = GeoHashHelper::GetMercatorX(options.x); y = GeoHashHelper::GetMercatorY(options.y); } if (options.by_member) { ValueData score, attr; int err = ZGetNodeValue(db, key, options.member, score, attr); if (0 != err) { return err; } Buffer attr_content(const_cast<char*>(attr.bytes_value.data()), 0, attr.bytes_value.size()); GeoPoint point; point.Decode(attr_content); x = point.x; y = point.y; } ZSetCacheElementSet subset; if (options.in_members) { StringSet::const_iterator sit = options.submembers.begin(); while (sit != options.submembers.end()) { ZSetCaheElement ele; ValueData score, attr; if (0 == ZGetNodeValue(db, key, *sit, score, attr)) { ele.score = score.NumberValue(); Buffer buf1, buf2; ValueData vv; vv.SetValue(*sit, true); vv.Encode(buf1); attr.Encode(buf2); ele.value.assign(buf1.GetRawReadBuffer(), buf1.ReadableBytes()); ele.attr.assign(buf2.GetRawReadBuffer(), buf2.ReadableBytes()); subset.insert(ele); } sit++; } } GeoHashHelper::GetAreasByRadius(GEO_MERCATOR_TYPE, y, x, options.radius, ress); /* * Merge areas if possible to avoid disk search */ std::vector<ZRangeSpec> range_array; GeoHashBitsSet::iterator rit = ress.begin(); GeoHashBitsSet::iterator next_it = ress.begin(); next_it++; while (rit != ress.end()) { GeoHashBits& hash = *rit; GeoHashBits next = hash; next.bits++; while (next_it != ress.end() && next.bits == next_it->bits) { next.bits++; next_it++; rit++; } ZRangeSpec range; range.contain_min = true; range.contain_max = false; range.min.SetIntValue(GeoHashHelper::Allign52Bits(hash)); range.max.SetIntValue(GeoHashHelper::Allign52Bits(next)); range_array.push_back(range); rit++; next_it++; } DEBUG_LOG("After areas merging, reduce searching area size from %u to %u", ress.size(), range_array.size()); GeoPointArray points; std::vector<ZRangeSpec>::iterator hit = range_array.begin(); Iterator* iter = NULL; while (hit != range_array.end()) { ZSetQueryOptions z_options; z_options.withscores = false; z_options.withattr = true; ValueDataArray values; if (options.in_members) { ZSetCache::GetRangeInZSetCache(subset, *hit, z_options.withscores, z_options.withattr, ZSetValueStoreCallback, &values); } else { ZRangeByScoreRange(db, key, *hit, iter, z_options, true, ZSetValueStoreCallback, &values); } ValueDataArray::iterator vit = values.begin(); while (vit != values.end()) { GeoPoint point; vit->ToString(point.value); //attributes vit++; Buffer content(const_cast<char*>(vit->bytes_value.data()), 0, vit->bytes_value.size()); if (point.Decode(content)) { if (GeoHashHelper::GetDistanceSquareIfInRadius(GEO_MERCATOR_TYPE, x, y, point.x, point.y, options.radius, point.distance)) { /* * filter by exclude/include */ if (!options.includes.empty() || !options.excludes.empty()) { ValueData subst; subst.SetValue(point.value, false); bool matched = options.includes.empty() ? true : false; if (!options.includes.empty()) { StringStringMap::const_iterator sit = options.includes.begin(); while (sit != options.includes.end()) { ValueData mv; if (0 != MatchValueByPattern(db, sit->first, sit->second, subst, mv)) { matched = false; break; } else { matched = true; } sit++; } } if (matched && !options.excludes.empty()) { StringStringMap::const_iterator sit = options.excludes.begin(); while (sit != options.excludes.end()) { ValueData mv; if (0 == MatchValueByPattern(db, sit->first, sit->second, subst, mv)) { matched = false; break; } else { matched = true; } sit++; } } if (matched) { points.push_back(point); } } else { points.push_back(point); } } else { //DEBUG_LOG("%s is not in radius:%.2fm", point.value.c_str(), options.radius); } } else { WARN_LOG("Failed to decode geo point."); } vit++; } hit++; } DELETE(iter); if (!options.nosort) { std::sort(points.begin(), points.end(), options.asc ? less_by_distance : great_by_distance); } if (options.offset > 0) { if ((uint32) options.offset > points.size()) { points.clear(); } else { GeoPointArray::iterator start = points.begin() + options.offset; points.erase(points.begin(), start); } } if (options.limit > 0) { if ((uint32) options.limit < points.size()) { GeoPointArray::iterator end = points.begin() + options.limit; points.erase(end, points.end()); } } GeoPointArray::iterator pit = points.begin(); while (pit != points.end()) { ValueData v; v.SetValue(pit->value, false); results.push_back(v); GeoGetOptionDeque::const_iterator ait = options.get_patterns.begin(); while (ait != options.get_patterns.end()) { ValueData attr; if (ait->get_distances) { attr.SetDoubleValue(sqrt(pit->distance)); results.push_back(attr); } else if (ait->get_coodinates) { if (options.coord_type == GEO_WGS84_TYPE) { pit->x = GeoHashHelper::GetWGS84X(pit->x); pit->y = GeoHashHelper::GetWGS84Y(pit->y); } attr.SetDoubleValue(pit->x); results.push_back(attr); attr.SetDoubleValue(pit->y); results.push_back(attr); } else if (ait->get_attr) { StringStringMap::iterator found = pit->attrs.find(ait->get_pattern); if (found != pit->attrs.end()) { attr.SetValue(found->second, false); } results.push_back(attr); } else { GetValueByPattern(db, ait->get_pattern, v, attr); results.push_back(attr); } ait++; } pit++; } uint64 end_time = get_current_epoch_micros(); DEBUG_LOG("Cost %llu microseconds to search.", end_time - start_time); return 0; }