int Ardb::ZClear(const DBID& db, const Slice& key) { KeyLockerGuard keyguard(m_key_locker, db, key); ZSetMetaValue meta; if (0 != GetZSetMetaValue(db, key, meta)) { return 0; } Slice empty; ZSetKeyObject sk(key, empty, meta.min_score, db); BatchWriteGuard guard(GetEngine()); struct ZClearWalk: public WalkHandler { Ardb* z_db; 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; } ZClearWalk(Ardb* db) : z_db(db) { } } walk(this); Walk(sk, false, &walk); KeyObject k(key, ZSET_META, db); DelValue(k); return 0; }
int Ardb::FlushScripts() { KeyObject start(Slice(), SCRIPT, ARDB_GLOBAL_DB); Iterator* iter = FindValue(start, false); BatchWriteGuard guard(GetEngine()); while (NULL != iter && iter->Valid()) { Slice tmpkey = iter->Key(); KeyObject* kk = decode_key(tmpkey, NULL); if (NULL != kk) { if (kk->type == SCRIPT) { DelValue(*kk); } else { break; } } DELETE(kk); iter->Next(); } DELETE(iter); return 0; }
int Ardb::ZRem(const DBID& db, const Slice& key, const Slice& value) { KeyLockerGuard keyguard(m_key_locker, db, key); ZSetScoreKeyObject zk(key, value, db); ValueObject zv; if (0 == GetValue(zk, &zv)) { BatchWriteGuard guard(GetEngine()); DelValue(zk); ZSetKeyObject zsk(key, value, zv.v.double_v, db); DelValue(zsk); ZSetMetaValue meta; if (0 == GetZSetMetaValue(db, key, meta)) { meta.size--; SetZSetMetaValue(db, key, meta); } return 0; } return ERR_NOT_EXIST; }
/* * returns -1:no change 0:no meta&zset size change * 1:meta change & no zset size change * 2:meta change & zset size change */ int Ardb::TryZAdd(const DBID& db, const Slice& key, ZSetMetaValue& meta, double score, const Slice& value) { bool metachange = false; if (score > meta.max_score) { meta.max_score = score; metachange = true; } if (score < meta.min_score) { meta.min_score = score; metachange = true; } ZSetScoreKeyObject zk(key, value, db); ValueObject zv; if (0 != GetValue(zk, &zv)) { meta.size++; zv.type = DOUBLE; zv.v.double_v = score; SetValue(zk, zv); ZSetKeyObject zsk(key, value, score, db); ValueObject zsv; zsv.type = EMPTY; SetValue(zsk, zsv); return 2; } else { if (zv.v.double_v != score) { ZSetKeyObject zsk(key, value, zv.v.double_v, db); DelValue(zsk); zsk.score = score; ValueObject zsv; zsv.type = EMPTY; SetValue(zsk, zsv); zv.type = DOUBLE; zv.v.double_v = score; SetValue(zk, zv); return metachange ? 1 : 0; } } return -1; }
int Ardb::ZIncrby(const DBID& db, const Slice& key, double increment, const Slice& value, double& score) { KeyLockerGuard keyguard(m_key_locker, db, key); ZSetScoreKeyObject zk(key, value, db); ValueObject zv; if (0 == GetValue(zk, &zv)) { BatchWriteGuard guard(GetEngine()); SetValue(zk, zv); ZSetKeyObject zsk(key, value, zv.v.double_v, db); DelValue(zsk); zsk.score += increment; ValueObject zsv; zsv.type = EMPTY; SetValue(zsk, zsv); score = zsk.score; return 0; } return ERR_NOT_EXIST; }