Beispiel #1
0
void test_zsets_union(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset1");
    db.ZClear(dbid, "myzset2");
    db.ZAdd(dbid, "myzset1", ValueData((int64) 1), "one");
    db.ZAdd(dbid, "myzset1", ValueData((int64) 2), "two");
    db.ZAdd(dbid, "myzset2", ValueData((int64) 1), "one");
    db.ZAdd(dbid, "myzset2", ValueData((int64) 2), "two");
    db.ZAdd(dbid, "myzset2", ValueData((int64) 3), "three");
    SliceArray keys;
    keys.push_back("myzset1");
    keys.push_back("myzset2");
    WeightArray ws;
    ws.push_back(10);
    ws.push_back(4);
    db.ZUnionStore(dbid, "myzset3", keys, ws);

    CHECK_FATAL(db.ZCard(dbid, "myzset3") != 3, "Fail:");
    ZSetQueryOptions options;
    options.withscores = true;
    ValueDataArray values;
    db.ZRange(dbid, "myzset3", 0, -1, values, options);
    std::string str;
    CHECK_FATAL(values.size() != 6, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "three", "Fail:%s", values[0].ToString(str).c_str());
    CHECK_FATAL(values[2].ToString(str) != "one", "Fail:%s", values[2].ToString(str).c_str());
    CHECK_FATAL(values[4].ToString(str) != "two", "Fail:%s", values[4].ToString(str).c_str());
}
Beispiel #2
0
void test_zsets_inter(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset1");
    db.ZClear(dbid, "myzset2");
    db.ZAdd(dbid, "myzset1", ValueData((int64) 1), "one");
    db.ZAdd(dbid, "myzset1", ValueData((int64) 2), "two");
    db.ZAdd(dbid, "myzset2", ValueData((int64) 1), "one");
    db.ZAdd(dbid, "myzset2", ValueData((int64) 2), "two");
    db.ZAdd(dbid, "myzset2", ValueData((int64) 3), "three");
    SliceArray keys;
    keys.push_back("myzset1");
    keys.push_back("myzset2");
    WeightArray ws;
    ws.push_back(20);
    ws.push_back(4);
    db.ZInterStore(dbid, "myzset3", keys, ws);

    int zcard = db.ZCard(dbid, "myzset3");
    CHECK_FATAL(zcard != 2, "Fail:%d", zcard);
    ZSetQueryOptions options;
    options.withscores = true;
    ValueDataArray values;
    db.ZRange(dbid, "myzset3", 0, -1, values, options);
    std::string str;
    CHECK_FATAL(values.size() != 4, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "one", "Fail:");
}
Beispiel #3
0
void test_zsets_addrem(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset");

    db.ZAdd(dbid, "myzset", ValueData((int64) 3), "v0");
    db.ZAdd(dbid, "myzset", ValueData((int64) 2), "v1");
    db.ZAdd(dbid, "myzset", ValueData((int64) 1), "v2");
    CHECK_FATAL(db.ZCard(dbid, "myzset") != 3, "zadd myzset failed:%d", db.ZCard(dbid, "myzset"));
    ValueData score((int64) 0);
    db.ZScore(dbid, "myzset", "v1", score);
    CHECK_FATAL(score.NumberValue() != 2, "zscore myzset failed:%f", score.NumberValue());

    db.ZAdd(dbid, "myzset", ValueData((int64) 0), "v2");
    CHECK_FATAL(db.ZCard(dbid, "myzset") != 3, "zadd myzset failed:%d", db.ZCard(dbid, "myzset"));
    db.ZRem(dbid, "myzset", "v2");
    CHECK_FATAL(db.ZCard(dbid, "myzset") != 2, "zrem myzset failed:%d", db.ZCard(dbid, "myzset"));
    ValueDataArray values;
    ZSetQueryOptions options;
    db.ZRange(dbid, "myzset", 0, -1, values, options);
    std::string str;
    CHECK_FATAL(values.size() != 2, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "v1", "Fail:%s", values[0].ToString(str).c_str());
    CHECK_FATAL(values[1].ToString(str) != "v0", "Fail:%s", values[1].ToString(str).c_str());
}
Beispiel #4
0
void test_zsets_incr(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset");
    db.ZAdd(dbid, "myzset", ValueData((int64) 1), "one");
    db.ZAdd(dbid, "myzset", ValueData((int64) 2), "two");
    ValueData score;
    db.ZIncrby(dbid, "myzset", ValueData((int64) 2), "one", score);
    ZSetQueryOptions options;
    options.withscores = true;
    ValueDataArray values;
    db.ZRange(dbid, "myzset", 0, -1, values, options);
    std::string str;
    CHECK_FATAL(values.size() != 4, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "two", "Fail:%s", values[0].ToString(str).c_str());
}
Beispiel #5
0
void test_sort_list(Ardb& db)
{
    DBID dbid = 0;
    db.LClear(dbid, "mylist");
    db.RPush(dbid, "mylist", "100");
    db.RPush(dbid, "mylist", "10");
    db.RPush(dbid, "mylist", "9");
    db.RPush(dbid, "mylist", "1000");

    StringArray args;
    ValueDataArray vs;
    db.Sort(dbid, "mylist", args, vs);
    CHECK_FATAL(vs.size() != 4, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].integer_value != 9, "sort result[0]:%"PRId64, vs[0].integer_value);
    CHECK_FATAL(vs[1].integer_value != 10, "sort result[0]:%"PRId64, vs[1].integer_value);
    CHECK_FATAL(vs[2].integer_value != 100, "sort result[0]:%"PRId64, vs[2].integer_value);
    CHECK_FATAL(vs[3].integer_value != 1000, "sort result[0]:%"PRId64, vs[3].integer_value);

    vs.clear();

    args.clear();
    string_to_string_array("limit 1 2", args);
    db.Sort(dbid, "mylist", args, vs);
    CHECK_FATAL(vs.size() != 2, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].integer_value != 10, "sort result[0]:%"PRId64, vs[0].integer_value);
    CHECK_FATAL(vs[1].integer_value != 100, "sort result[0]:%"PRId64, vs[1].integer_value);

    vs.clear();
    args.clear();
    string_to_string_array("by weight_*", args);
    db.Set(dbid, "weight_100", "1000");
    db.Set(dbid, "weight_10", "900");
    db.Set(dbid, "weight_9", "800");
    db.Set(dbid, "weight_1000", "700");
    db.Sort(dbid, "mylist", args, vs);
    CHECK_FATAL(vs.size() != 4, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].integer_value != 1000, "sort result[0]:%"PRId64, vs[0].integer_value);
    CHECK_FATAL(vs[1].integer_value != 9, "sort result[0]:%"PRId64, vs[1].integer_value);
    CHECK_FATAL(vs[2].integer_value != 10, "sort result[0]:%"PRId64, vs[2].integer_value);
    CHECK_FATAL(vs[3].integer_value != 100, "sort result[0]:%"PRId64, vs[3].integer_value);

    db.HSet(dbid, "myhash", "field_100", "hash100");
    db.HSet(dbid, "myhash", "field_10", "hash10");
    db.HSet(dbid, "myhash", "field_9", "hash9");
    db.HSet(dbid, "myhash", "field_1000", "hash1000");

    args.clear();
    string_to_string_array("by weight_* get myhash->field_* get #", args);
    vs.clear();
    db.Sort(dbid, "mylist", args, vs);
    std::string str;
    CHECK_FATAL(vs.size() != 8, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "hash1000", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[2].ToString(str) != "hash9", "sort result[2]:%s", str.c_str());
    CHECK_FATAL(vs[4].ToString(str) != "hash10", "sort result[4]:%s", str.c_str());
    CHECK_FATAL(vs[6].ToString(str) != "hash100", "sort result[6]:%s", str.c_str());
    CHECK_FATAL(vs[1].integer_value != 1000, "sort result[1]:%"PRId64, vs[1].integer_value);
    CHECK_FATAL(vs[3].integer_value != 9, "sort result[3]:%"PRId64, vs[3].integer_value);
    CHECK_FATAL(vs[5].integer_value != 10, "sort result[5]:%"PRId64, vs[5].integer_value);
    CHECK_FATAL(vs[7].integer_value != 100, "sort result[7]:%"PRId64, vs[7].integer_value);
}
Beispiel #6
0
void test_sort_zset(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset");
    db.ZAdd(dbid, "myzset", ValueData((int64) 0), "v0");
    db.ZAdd(dbid, "myzset", ValueData((int64) 10), "v10");
    db.ZAdd(dbid, "myzset", ValueData((int64) 3), "v3");
    db.ZAdd(dbid, "myzset", ValueData((int64) 5), "v5");

    StringArray args;
    ValueDataArray vs;
    db.Sort(dbid, "myzset", args, vs);
    std::string str;
    CHECK_FATAL(vs.size() != 4, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "v0", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[1].ToString(str) != "v3", "sort result[1]:%s", str.c_str());
    CHECK_FATAL(vs[2].ToString(str) != "v5", "sort result[2]:%s", str.c_str());
    CHECK_FATAL(vs[3].ToString(str) != "v10", "sort result[3]:%s", str.c_str());

    vs.clear();
    string_to_string_array("limit 1 2", args);
    db.Sort(dbid, "myzset", args, vs);
    CHECK_FATAL(vs.size() != 2, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "v3", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[1].ToString(str) != "v5", "sort result[1]:%s", str.c_str());

    vs.clear();
    args.clear();
    string_to_string_array("by weight_*", args);
    db.Set(dbid, "weight_v0", "1000");
    db.Set(dbid, "weight_v3", "900");
    db.Set(dbid, "weight_v5", "800");
    db.Set(dbid, "weight_v10", "700");
    db.Sort(dbid, "myzset", args, vs);
    CHECK_FATAL(vs.size() != 4, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "v10", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[1].ToString(str) != "v5", "sort result[1]:%s", str.c_str());
    CHECK_FATAL(vs[2].ToString(str) != "v3", "sort result[2]:%s", str.c_str());
    CHECK_FATAL(vs[3].ToString(str) != "v0", "sort result[3]:%s", str.c_str());

    db.HSet(dbid, "myhash_v0", "field", "100");
    db.HSet(dbid, "myhash_v3", "field", "10");
    db.HSet(dbid, "myhash_v5", "field", "9");
    db.HSet(dbid, "myhash_v10", "field", "1000");

    string_to_string_array("by weight_* get myhash_*->field aggregate sum", args);
    db.Sort(dbid, "myzset", args, vs);
    CHECK_FATAL(vs.size() != 1, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "1119", "sort result[0]:%s", str.c_str());

    string_to_string_array("by weight_* get myhash_*->field aggregate min", args);
    db.Sort(dbid, "myzset", args, vs);
    CHECK_FATAL(vs.size() != 1, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "9", "sort result[0]:%s", str.c_str());

    string_to_string_array("by weight_* get myhash_*->field aggregate max", args);
    db.Sort(dbid, "myzset", args, vs);
    CHECK_FATAL(vs.size() != 1, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "1000", "sort result[0]:%s", str.c_str());

    string_to_string_array("by weight_* get myhash_*->field aggregate avg", args);
    db.Sort(dbid, "myzset", args, vs);
    CHECK_FATAL(vs.size() != 1, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "279.75", "sort result[0]:%s", str.c_str());

    string_to_string_array("by weight_* get myhash_*->field aggregate count", args);
    db.Sort(dbid, "myzset", args, vs);
    CHECK_FATAL(vs.size() != 1, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "4", "sort result[0]:%s", str.c_str());
}
Beispiel #7
0
void test_sort_set(Ardb& db)
{
    DBID dbid = 0;
    db.SClear(dbid, "myset");
    db.SAdd(dbid, "myset", "ab3");
    db.SAdd(dbid, "myset", "ab2");
    db.SAdd(dbid, "myset", "ab1");
    db.SAdd(dbid, "myset", "ab4");

    StringArray args;
    ValueDataArray vs;
    db.Sort(dbid, "myset", args, vs);
    std::string str;
    CHECK_FATAL(vs.size() != 4, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "ab1", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[1].ToString(str) != "ab2", "sort result[1]:%s", str.c_str());
    CHECK_FATAL(vs[2].ToString(str) != "ab3", "sort result[2]:%s", str.c_str());
    CHECK_FATAL(vs[3].ToString(str) != "ab4", "sort result[3]:%s", str.c_str());

    vs.clear();
    string_to_string_array("limit 1 2", args);
    db.Sort(dbid, "myset", args, vs);
    CHECK_FATAL(vs.size() != 2, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "ab2", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[1].ToString(str) != "ab3", "sort result[1]:%s", str.c_str());

    vs.clear();
    args.clear();
    string_to_string_array("by weight_*", args);
    db.Set(dbid, "weight_ab1", "1000");
    db.Set(dbid, "weight_ab2", "900");
    db.Set(dbid, "weight_ab3", "800");
    db.Set(dbid, "weight_ab4", "700");
    db.Sort(dbid, "myset", args, vs);
    CHECK_FATAL(vs.size() != 4, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "ab4", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[1].ToString(str) != "ab3", "sort result[1]:%s", str.c_str());
    CHECK_FATAL(vs[2].ToString(str) != "ab2", "sort result[2]:%s", str.c_str());
    CHECK_FATAL(vs[3].ToString(str) != "ab1", "sort result[3]:%s", str.c_str());

    db.HSet(dbid, "myhash_ab1", "field", "hash100");
    db.HSet(dbid, "myhash_ab2", "field", "hash10");
    db.HSet(dbid, "myhash_ab3", "field", "hash9");
    db.HSet(dbid, "myhash_ab4", "field", "hash1000");
    args.clear();
    string_to_string_array("by weight_* get myhash_*->field get #", args);

    vs.clear();
    db.Sort(dbid, "myset", args, vs);
    CHECK_FATAL(vs.size() != 8, "sort result size error:%zu", vs.size());
    CHECK_FATAL(vs[0].ToString(str) != "hash1000", "sort result[0]:%s", str.c_str());
    CHECK_FATAL(vs[2].ToString(str) != "hash9", "sort result[2]:%s", str.c_str());
    CHECK_FATAL(vs[4].ToString(str) != "hash10", "sort result[4]:%s", str.c_str());
    CHECK_FATAL(vs[6].ToString(str) != "hash100", "sort result[6]:%s", str.c_str());
    CHECK_FATAL(vs[1].ToString(str) != "ab4", "sort result[1]:%s", str.c_str());
    CHECK_FATAL(vs[3].ToString(str) != "ab3", "sort result[3]:%s", str.c_str());
    CHECK_FATAL(vs[5].ToString(str) != "ab2", "sort result[5]:%s", str.c_str());
    CHECK_FATAL(vs[7].ToString(str) != "ab1", "sort result[7]:%s", str.c_str());
}
Beispiel #8
0
    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;
    }
Beispiel #9
0
 static int ZSetValueStoreCallback(const ValueData& value, int cursor, void* cb)
 {
     ValueDataArray* s = (ValueDataArray*) cb;
     s->push_back(value);
     return 0;
 }
Beispiel #10
0
void test_zsets_zrange(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset");
    db.ZAdd(dbid, "myzset", ValueData((int64) 1), "one");
    db.ZAdd(dbid, "myzset", ValueData((int64) 1), "uno");
    db.ZAdd(dbid, "myzset", ValueData((int64) 2), "two");
    db.ZAdd(dbid, "myzset", ValueData((int64) 3), "two");
    ZSetQueryOptions options;
    options.withscores = true;
    ValueDataArray values;
    db.ZRange(dbid, "myzset", 0, -1, values, options);
    std::string str;
    CHECK_FATAL(values.size() != 6, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "one", "Fail:%s", values[0].ToString(str).c_str());
    CHECK_FATAL(values[2].ToString(str) != "uno", "Fail:%s", values[1].ToString(str).c_str());
    values.clear();
    db.ZRangeByScore(dbid, "myzset", "-inf", "+inf", values, options);
    CHECK_FATAL(values.size() != 6, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "one", "Fail:%s", values[0].ToString(str).c_str());
    CHECK_FATAL(values[2].ToString(str) != "uno", "Fail:%s", values[1].ToString(str).c_str());
    values.clear();
    db.ZRangeByScore(dbid, "myzset", "(1", "3", values, options);
    CHECK_FATAL(values.size() != 2, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "two", "Fail:%s", values[0].ToString(str).c_str());

    db.ZClear(dbid, "myzset");
    uint32 maxzsetsize = 1000;
    for (uint32 i = 0; i < maxzsetsize; i++)
    {
        char value[16];
        sprintf(value, "value%u", i);
        db.ZAdd(dbid, "myzset", ValueData((int64) i), value);
    }
    options.withscores = false;
    values.clear();
    db.ZRange(dbid, "myzset", 1, 10, values, options);
    CHECK_FATAL(values.size() != 10, "Fail:%zu", values.size());
}
Beispiel #11
0
void test_zsets_zrev(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset");
    db.ZAdd(dbid, "myzset", ValueData((int64) 1), "one");
    db.ZAdd(dbid, "myzset", ValueData((int64) 2), "two");
    db.ZAdd(dbid, "myzset", ValueData((int64) 3), "three");
    ZSetQueryOptions options;
    options.withscores = true;
    ValueDataArray values;
    std::string str;
    db.ZRevRange(dbid, "myzset", 0, -1, values, options);
    CHECK_FATAL(values.size() != 6, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "three", "Fail:%s", values[0].ToString(str).c_str());
    CHECK_FATAL(values[2].ToString(str) != "two", "Fail:%s", values[2].ToString(str).c_str());
    values.clear();
    db.ZRevRangeByScore(dbid, "myzset", "+inf", "-inf", values, options);
    CHECK_FATAL(values.size() != 6, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "three", "Fail:%s", values[0].ToString(str).c_str());
    CHECK_FATAL(values[2].ToString(str) != "two", "Fail:%s", values[2].ToString(str).c_str());
    options.withscores = false;
    values.clear();
    db.ZRevRangeByScore(dbid, "myzset", "2", "1", values, options);
    CHECK_FATAL(values.size() != 2, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "two", "Fail:%s", values[0].ToString(str).c_str());
    CHECK_FATAL(values[1].ToString(str) != "one", "Fail:%s", values[1].ToString(str).c_str());
    values.clear();
    db.ZRevRangeByScore(dbid, "myzset", "2", "(1", values, options);
    CHECK_FATAL(values.size() != 1, "Fail:%zu", values.size());
    CHECK_FATAL(values[0].ToString(str) != "two", "Fail:%s", values[0].ToString(str).c_str());
}