Exemplo n.º 1
0
void test_strings_strlen(Ardb& db)
{
    DBID dbid = 0;
    db.Set(dbid, "skey", "abcabcabc");
    int len = db.Strlen(dbid, "skey");
    CHECK_FATAL(len != 9, "Strlen failed:%d", len);
}
Exemplo n.º 2
0
				void Run()
				{
					db->VisitAllDB(this);
					db->GetEngine()->CommitBatchWrite();
					db->GetEngine()->CompactRange(Slice(), Slice());
					delete this;
				}
Exemplo n.º 3
0
int main(int argc, char** argv)
{
    std::string dir;
    if (argc >= 2)
    {
        int j = 1; /* First option to parse in argv[] */
        char *dirfile = NULL;

        /* Handle special options --help and --version */
        if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0)
            version();
        if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)
            usage();

        /* First argument is the config file name? */
        if (argv[j][0] != '-' || argv[j][1] != '-')
        {
            dirfile = argv[j++];
            dir = dirfile;
        }
    }
    else
    {
        printf("Warning: no database dir specified to repair.\n");
        return -1;
    }

    Ardb db;
    return db.Repair(dir);
}
Exemplo n.º 4
0
				int OnKeyValue(KeyObject* k, ValueObject* v, uint32 cursor)
				{
					ZSetKeyObject* zsk = (ZSetKeyObject*) k;
					bool need_delete = false;
					need_delete =
							z_containmin ?
									zsk->score >= z_min_score :
									zsk->score > z_min_score;
					if (need_delete)
					{
						need_delete =
								z_containmax ?
										zsk->score <= z_max_score :
										zsk->score < z_max_score;
					}
					if (need_delete)
					{
						ZSetScoreKeyObject zk(zsk->key, zsk->value, zsk->db);
						z_db->DelValue(zk);
						z_db->DelValue(*zsk);
						z_meta.size--;
						z_count++;
					}
					if (zsk->score == z_max_score)
					{
						return -1;
					}
					return 0;
				}
Exemplo n.º 5
0
void test_strings_getrange(Ardb& db)
{
    DBID dbid = 0;
    std::string v;
    db.Set(dbid, "skey", "abcabc");
    db.GetRange(dbid, "skey", 4, -1, v);
    CHECK_FATAL(v != "bc", "GetRange failed");
}
Exemplo n.º 6
0
void test_strings_exists(Ardb& db)
{
    DBID dbid = 0;
    db.Del(dbid, "intkey1");
    CHECK_FATAL(db.Exists(dbid, "intkey1"), "Exists intkey1 failed");
    db.Set(dbid, "intkey1", "123");
    CHECK_FATAL(db.Exists(dbid, "intkey1") == false, "Exists intkey failed");
}
Exemplo n.º 7
0
void test_strings_setnx(Ardb& db)
{
    DBID dbid = 0;
    db.Set(dbid, "intkey1", "123");
    CHECK_FATAL(db.SetNX(dbid, "intkey1", "2345") == 0, "SetNX intkey failed");
    db.Del(dbid, "intkey1");
    CHECK_FATAL(db.SetNX(dbid, "intkey1", "2345") != 0, "SetNX intkey failed");
}
Exemplo n.º 8
0
				int OnKeyValue(KeyObject* k, ValueObject* value, uint32 cursor)
				{
					ZSetKeyObject* sek = (ZSetKeyObject*) k;
					ZSetScoreKeyObject tmp(sek->key, sek->value, sek->db);
					z_db->DelValue(*sek);
					z_db->DelValue(tmp);
					return 0;
				}
Exemplo n.º 9
0
void test_strings_getset(Ardb& db)
{
    DBID dbid = 0;
    std::string v;
    db.Set(dbid, "skey", "abcabc");
    db.GetSet(dbid, "skey", "edfgth", v);
    CHECK_FATAL(v != "abcabc", "GetSet failed:%s", v.c_str());
}
Exemplo n.º 10
0
void test_strings_setrange(Ardb& db)
{
    DBID dbid = 0;
    std::string v;
    db.Set(dbid, "skey", "abcabc");
    db.SetRange(dbid, "skey", 3, "12345");
    db.Get(dbid, "skey", v);
    CHECK_FATAL(v != "abc12345", "SetRange failed:%s", v.c_str());
}
Exemplo n.º 11
0
void test_strings_expire(Ardb& db)
{
    DBID dbid = 0;
    ValueData v;
    db.Set(dbid, "intkey1", "123");
    db.Expire(dbid, "intkey1", 1);
    CHECK_FATAL(db.Exists(dbid, "intkey1") == false, "Expire intkey1 failed");
    sleep(2);
    CHECK_FATAL(db.Exists(dbid, "intkey1") == true, "Expire intkey failed");
}
Exemplo n.º 12
0
void test_set_expire(Ardb& db)
{
	DBID dbid = 0;
	db.SClear(dbid, "myset");
	db.SAdd(dbid, "myset", "123");
	db.Expire(dbid, "myset", 1);
	CHECK_FATAL(db.Exists(dbid, "myset") == false, "Expire myset failed");
	sleep(2);
	CHECK_FATAL(db.Exists(dbid, "myset") == true, "Expire myset failed");
}
Exemplo n.º 13
0
void test_strings_decr(Ardb& db)
{
    DBID dbid = 0;
    db.Set(dbid, "intkey", "10");
    int64_t iv = 0;
    db.Decr(dbid, "intkey", iv);
    CHECK_FATAL(iv != 9, "Decr1 failed %"PRId64, iv);
    db.Decrby(dbid, "intkey", 2, iv);
    CHECK_FATAL(iv != 7, "Decrby failed: %"PRId64, iv);
}
Exemplo n.º 14
0
void test_zset_expire(Ardb& db)
{
    DBID dbid = 0;
    db.ZClear(dbid, "myzset");
    db.ZAdd(dbid, "myzset", ValueData((int64) 1), "one");
    db.Expire(dbid, "myzset", 1);
    CHECK_FATAL(db.Exists(dbid, "myzset") == false, "Expire myzset failed");
    sleep(2);
    CHECK_FATAL(db.Exists(dbid, "myzset") == true, "Expire myzset failed");
}
Exemplo n.º 15
0
void test_set_union(Ardb& db)
{
	DBID dbid = 0;
	db.SClear(dbid, "myset1");
	db.SClear(dbid, "myset2");
	db.SClear(dbid, "myset3");
	db.SAdd(dbid, "myset1", "a");
	db.SAdd(dbid, "myset1", "b");
	db.SAdd(dbid, "myset1", "c");
	db.SAdd(dbid, "myset1", "d");
	db.SAdd(dbid, "myset2", "c");
	db.SAdd(dbid, "myset3", "a");
	db.SAdd(dbid, "myset3", "c");
	db.SAdd(dbid, "myset3", "e");

	SliceArray keys;
	keys.push_back("myset1");
	keys.push_back("myset2");
	keys.push_back("myset3");
	ValueArray values;
	db.SUnion(dbid, keys, values);
	CHECK_FATAL(values.size() != 5, "SUnion failed:");
	std::string str;
	CHECK_FATAL( values.begin()->ToString(str) != "a", "SUnion store failed:");
	db.SUnionStore(dbid, "myset2", keys);
	CHECK_FATAL(db.SCard(dbid, "myset2") != 5, "SUnionStore myset2 failed:");
}
Exemplo n.º 16
0
void test_set_inter(Ardb& db)
{
	DBID dbid = 0;
	db.SClear(dbid, "myset1");
	db.SClear(dbid, "myset2");
	db.SClear(dbid, "myset3");
	db.SAdd(dbid, "myset1", "a");
	db.SAdd(dbid, "myset1", "b");
	db.SAdd(dbid, "myset1", "c");
	db.SAdd(dbid, "myset1", "d");
	db.SAdd(dbid, "myset2", "c");
	db.SAdd(dbid, "myset3", "a");
	db.SAdd(dbid, "myset3", "c");
	db.SAdd(dbid, "myset3", "e");

	SliceArray keys;
	keys.push_back("myset1");
	keys.push_back("myset2");
	keys.push_back("myset3");
	ValueSet values;
	db.SInter(dbid, keys, values);
	std::string str;
	CHECK_FATAL( values.size() != 1, "Sinter failed:");
	CHECK_FATAL(values.begin()->ToString(str) != "c", "Sinter store failed:");
	db.SInterStore(dbid, "myset2", keys);
	CHECK_FATAL( db.SCard(dbid, "myset2") != 1, "SInterStore myset2 failed:");
}
Exemplo n.º 17
0
void test_set_diff(Ardb& db)
{
	DBID dbid = 0;
	db.SClear(dbid, "myset1");
	db.SClear(dbid, "myset2");
	db.SClear(dbid, "myset3");
	db.SAdd(dbid, "myset1", "a");
	db.SAdd(dbid, "myset1", "b");
	db.SAdd(dbid, "myset1", "c");
	db.SAdd(dbid, "myset1", "d");
	db.SAdd(dbid, "myset2", "c");
	db.SAdd(dbid, "myset3", "a");
	db.SAdd(dbid, "myset3", "c");
	db.SAdd(dbid, "myset3", "e");

	std::string str;
	SliceArray keys;
	keys.push_back("myset1");
	keys.push_back("myset2");
	keys.push_back("myset3");
	ValueSet values;
	db.SDiff(dbid, keys, values);
	CHECK_FATAL(values.size() != 2, "Sdiff failed:");
	CHECK_FATAL(values.begin()->ToString(str) != "b", "Sdiff store failed:");
	//CHECK_FATAL(FATAL, values[1] != "d") << "Sdiff store failed:";
	int len = db.SDiffStore(dbid, "myset2", keys);
	CHECK_FATAL(len != 2, "SDiffStore myset2 failed:%d", len);
	len = db.SCard(dbid, "myset2");
	CHECK_FATAL(len != 2, "SDiffStore myset2 failed:%d", len);
}
Exemplo n.º 18
0
void test_strings_incr(Ardb& db)
{
    DBID dbid = 0;
    db.Set(dbid, "intkey", "12");
    int64_t iv = 0;
    db.Incrby(dbid, "intkey", 2, iv);
    CHECK_FATAL(iv != 14, "Incrby failed");
    double dv;
    db.IncrbyFloat(dbid, "intkey", 1.23, dv);
    CHECK_FATAL(dv != 15.23, "IncrbyFloat failed");
}
Exemplo n.º 19
0
void test_strings_append(Ardb& db)
{
    DBID dbid = 0;
    std::string v;
    //append
    db.Set(dbid, "skey", "abc");
    db.Append(dbid, "skey", "abc");
    int ret = db.Get(dbid, "skey", v);
    CHECK_FATAL(ret != 0, "Failed to get skey.");
    CHECK_FATAL(v != "abcabc", "Invalid str:%s", v.c_str());
}
Exemplo n.º 20
0
void test_zsets_zcount(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");
    int count = db.ZCount(dbid, "myzset", "-inf", "+inf");
    CHECK_FATAL(count != 3, "Fail:%d", count);
    count = db.ZCount(dbid, "myzset", "(1", "3");
    CHECK_FATAL(count != 2, "Fail:%d", count);
}
Exemplo n.º 21
0
				void Run()
				{
					adb->GetEngine()->BeginBatchWrite();
					adb->VisitDB(dbid, this);
					adb->GetEngine()->CommitBatchWrite();
					KeyObject start(Slice(), KV, dbid);
					KeyObject end(Slice(), KV, dbid + 1);
					Buffer sbuf, ebuf;
					encode_key(sbuf, start);
					encode_key(ebuf, end);
					adb->GetEngine()->CompactRange(sbuf.AsString(), ebuf.AsString());
					delete this;
				}
Exemplo n.º 22
0
				int OnKeyValue(KeyObject* k, ValueObject* value, uint32 cursor)
				{
					ZSetKeyObject* sek = (ZSetKeyObject*) k;
					ZSetScoreKeyObject tmp(sek->key, sek->value, sek->db);
					vs.push_back(sek->value);
					count--;
					z_db->DelValue(*sek);
					z_db->DelValue(tmp);
					if (count == 0)
					{
						return -1;
					}
					return 0;
				}
Exemplo n.º 23
0
				int OnRawKeyValue(const Slice& key, const Slice& value)
				{
					if (count % 100 == 0)
					{
						if (count > 0)
						{
							db->GetEngine()->CommitBatchWrite();
						}
						db->GetEngine()->BeginBatchWrite();
					}
					count++;
					db->RawDel(key);
					return 0;
				}
Exemplo n.º 24
0
void test_zsets_zrank(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");
    db.ZCount(dbid, "myzset", "-inf", "+inf");
    int rank = db.ZRank(dbid, "myzset", "three");
    CHECK_FATAL(rank != 2, "Fail:%d", rank);

    db.ZClear(dbid, "myzset");
    uint32 maxzsetsize = 100000;
    uint64 start = get_current_epoch_millis();
    for (uint32 i = 0; i < maxzsetsize; i++)
    {
        char value[16];
        sprintf(value, "value%u", i);
        db.ZAdd(dbid, "myzset", ValueData((int64) i), value);
    }
    uint64 end = get_current_epoch_millis();
    INFO_LOG("Cost %lldms to write %u zset elements", (end - start), maxzsetsize);
    start = get_current_epoch_millis();
    rank = db.ZRank(dbid, "myzset", "value50001");
    CHECK_FATAL(rank != 50001, "Fail:%d", rank);
    end = get_current_epoch_millis();
    INFO_LOG("Cost %lldms to rank.", (end - start));
}
Exemplo n.º 25
0
void test_set_member(Ardb& db)
{
	DBID dbid = 0;
	std::string str;
	db.SClear(dbid, "myset");
	db.SAdd(dbid, "myset", "v1");
	db.SAdd(dbid, "myset", "v2");
	db.SAdd(dbid, "myset", "v3");
	CHECK_FATAL(db.SIsMember(dbid, "myset", "v0") != false,
			"SIsMember myset failed:");
	ValueArray members;
	db.SMembers(dbid, "myset", members);
	CHECK_FATAL( members.size() != 3, "SMembers myset failed:");
	CHECK_FATAL( members[0].ToString(str) != "v1", "SMembers myset failed:");
}
Exemplo n.º 26
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());
}
Exemplo n.º 27
0
void test_lists_lrem(Context& ctx, Ardb& db)
{
    db.GetConfig().list_max_ziplist_entries = 16;
    RedisCommandFrame del;
    del.SetFullCommand("del mylist");
    db.Call(ctx, del, 0);
    //ziplist push
    for (int i = 0; i < 10; i++)
    {
        RedisCommandFrame lpush;
        lpush.SetFullCommand("lpush mylist valuexx");
        db.Call(ctx, lpush, 0);
    }
    RedisCommandFrame lrem;
    lrem.SetFullCommand("lrem mylist 2 valueyy");
    db.Call(ctx, lrem, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_INTEGER, "lrem mylist failed");
    CHECK_FATAL(ctx.reply.integer != 0, "lrem mylist failed");
    lrem.SetFullCommand("lrem mylist -2 valuexx");
    db.Call(ctx, lrem, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_INTEGER, "lrem mylist failed");
    CHECK_FATAL(ctx.reply.integer != 2, "lrem mylist failed");
    lrem.SetFullCommand("lrem mylist 0 valuexx");
    db.Call(ctx, lrem, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_INTEGER, "lrem mylist failed");
    CHECK_FATAL(ctx.reply.integer != 8, "lrem mylist failed");

    //non ziplist lpush
    for (int i = 0; i < 20; i++)
    {
        RedisCommandFrame lpush;
        lpush.SetFullCommand("lpush mylist valuexx");
        db.Call(ctx, lpush, 0);
    }
    lrem.SetFullCommand("lrem mylist -2 valueyy");
    db.Call(ctx, lrem, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_INTEGER, "lrem mylist failed");
    CHECK_FATAL(ctx.reply.integer != 0, "lrem mylist failed");
    lrem.SetFullCommand("lrem mylist -2 valuexx");
    db.Call(ctx, lrem, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_INTEGER, "lrem mylist failed");
    CHECK_FATAL(ctx.reply.integer != 2, "lrem mylist failed");
    lrem.SetFullCommand("lrem mylist 0 valuexx");
    db.Call(ctx, lrem, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_INTEGER, "lrem mylist failed");
    CHECK_FATAL(ctx.reply.integer != 18, "lrem mylist failed");
}
Exemplo n.º 28
0
void test_lists_rpoplpush(Context& ctx, Ardb& db)
{
    db.GetConfig().list_max_ziplist_entries = 16;
    RedisCommandFrame del, del1;
    del.SetFullCommand("del mylist");
    del1.SetFullCommand("del mylist1");
    db.Call(ctx, del, 0);
    db.Call(ctx, del1, 0);
    //ziplist push
    for (int i = 0; i < 10; i++)
    {
        RedisCommandFrame lpush;
        lpush.SetFullCommand("lpush mylist value%d", i);
        db.Call(ctx, lpush, 0);
    }
    RedisCommandFrame rpoplpush;
    rpoplpush.SetFullCommand("rpoplpush mylist mylist1");
    db.Call(ctx, rpoplpush, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_STRING, "rpoplpush mylist failed");
    CHECK_FATAL(ctx.reply.str != "value0", "rpoplpush mylist failed");

    //non ziplist lpush
    for (int i = 0; i < 20; i++)
    {
        RedisCommandFrame lpush;
        lpush.SetFullCommand("lpush mylist xvalue%d", i);
        db.Call(ctx, lpush, 0);
    }
    db.Call(ctx, rpoplpush, 0);
    CHECK_FATAL(ctx.reply.type != REDIS_REPLY_STRING, "rpoplpush mylist failed");
    CHECK_FATAL(ctx.reply.str != "value1", "rpoplpush mylist failed");
}
Exemplo n.º 29
0
void test_geo_common(Context& ctx, Ardb& db)
{
    db.GetConfig().zset_max_ziplist_entries = 16;
    RedisCommandFrame del;
    del.SetFullCommand("del mygeo");
    db.Call(ctx, del, 0);
    double x = 300.3;
    double y = 300.3;

    double p_x = 1000.0;
    double p_y = 1000.0;
    uint32 raius = 1000;
    uint32 total = 100000;
    GeoPointArray cmp;
    for (uint32 i = 0; i < total; i++)
    {
        char name[100];
        sprintf(name, "p%u", i);
        /*
         * min accuracy is 0.2meters
         */
        double xx = x + i * 0.3;
        double yy = y + i * 0.3;
        if (((xx - p_x) * (xx - p_x) + (yy - p_y) * (yy - p_y)) <= raius * raius)
        {
            GeoPoint p;
            p.x = xx;
            p.y = yy;
            cmp.push_back(p);
        }
        RedisCommandFrame geoadd;
        geoadd.SetFullCommand("geoadd mygeo MERCATOR %.2f %.2f %s", xx, yy, name);
        db.Call(ctx, geoadd, 0);
    }

    RedisCommandFrame zcard;
    zcard.SetFullCommand("zcard mygeo");
    db.Call(ctx, zcard, 0);
    CHECK_FATAL(ctx.reply.integer != total, "geoadd failed");
    RedisCommandFrame geosearch;
    geosearch.SetFullCommand("geosearch mygeo MERCATOR %.2f %.2f radius %d ASC WITHCOORDINATES WITHDISTANCES", p_x, p_y,
            raius);
    db.Call(ctx, geosearch, 0);
    CHECK_FATAL(ctx.reply.MemberSize() != cmp.size() * 4, "geosearch failed");
}
Exemplo n.º 30
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());
}