int SSDBImpl::hlist(const Bytes &name_s, const Bytes &name_e, uint64_t limit, std::vector<std::string> *list){ std::string start; std::string end; start = encode_hsize_key(name_s); if(!name_e.empty()){ end = encode_hsize_key(name_e); } Iterator *it = this->iterator(start, end, limit); get_hnames(it, list); delete it; return 0; }
int SSDB::hdel(const Bytes &name, const Bytes &key) const{ std::string hkey = encode_hash_key(name, key); std::string dbval; leveldb::Status s; leveldb::WriteBatch batch; if(this->hget(name, key, &dbval) == 0){ return 0; } int64_t size = this->hsize(name); if(size == -1){ return -1; } size --; std::string size_key = encode_hsize_key(name); if(size == 0){ batch.Delete(size_key); }else{ batch.Put(size_key, leveldb::Slice((char *)&size, sizeof(int64_t))); } batch.Delete(hkey); s = db->Write(write_options, &batch); if(!s.ok()){ return -1; } return 1; }
int SSDB::hset(const Bytes &name, const Bytes &key, const Bytes &val) const{ std::string hkey = encode_hash_key(name, key); std::string dbval; leveldb::Status s; leveldb::WriteBatch batch; if(this->hget(name, key, &dbval) == 0){ int64_t size = this->hsize(name); if(size == -1){ return -1; } size ++; std::string size_key = encode_hsize_key(name); batch.Put(size_key, leveldb::Slice((char *)&size, sizeof(int64_t))); } if(dbval != val){ batch.Put(hkey, val.Slice()); } s = db->Write(write_options, &batch); if(!s.ok()){ return -1; } return 1; }
static int incr_hsize(SSDB *ssdb, const Bytes &name, int64_t incr){ int64_t size = ssdb->hsize(name); size += incr; std::string size_key = encode_hsize_key(name); if(size == 0){ ssdb->binlogs->Delete(size_key); }else{ ssdb->binlogs->Put(size_key, leveldb::Slice((char *)&size, sizeof(int64_t))); } return 0; }
int SSDB::hlist(const Bytes &name_s, const Bytes &name_e, int limit, std::vector<std::string> *list) const{ std::string start; std::string end; start = encode_hsize_key(name_s); if(!end.empty()){ end = encode_hsize_key(name_e); } Iterator *it = this->iterator(start, end, limit); while(it->next()){ Bytes ks = it->key(); if(ks.data()[0] != DataType::HSIZE){ break; } std::string n; if(decode_hsize_key(ks, &n) == -1){ continue; } list->push_back(n); } delete it; return 0; }
int64_t SSDB::hsize(const Bytes &name) const{ std::string size_key = encode_hsize_key(name); std::string val; leveldb::Status s; s = db->Get(leveldb::ReadOptions(), size_key, &val); if(s.IsNotFound()){ return 0; }else if(!s.ok()){ return -1; }else{ if(val.size() != sizeof(uint64_t)){ return 0; } int64_t ret = *(int64_t *)val.data(); return ret < 0? 0 : ret; } }
int SSDBImpl::key_range(std::vector<std::string> *keys){ int ret = 0; std::string kstart, kend; std::string hstart, hend; std::string zstart, zend; std::string qstart, qend; Iterator *it; it = this->iterator(encode_kv_key(""), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::KV){ std::string n; if(decode_kv_key(ks, &n) == -1){ ret = -1; }else{ kstart = n; } } } delete it; it = this->rev_iterator(encode_kv_key("\xff"), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::KV){ std::string n; if(decode_kv_key(ks, &n) == -1){ ret = -1; }else{ kend = n; } } } delete it; it = this->iterator(encode_hsize_key(""), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::HSIZE){ std::string n; if(decode_hsize_key(ks, &n) == -1){ ret = -1; }else{ hstart = n; } } } delete it; it = this->rev_iterator(encode_hsize_key("\xff"), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::HSIZE){ std::string n; if(decode_hsize_key(ks, &n) == -1){ ret = -1; }else{ hend = n; } } } delete it; it = this->iterator(encode_zsize_key(""), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::ZSIZE){ std::string n; if(decode_hsize_key(ks, &n) == -1){ ret = -1; }else{ zstart = n; } } } delete it; it = this->rev_iterator(encode_zsize_key("\xff"), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::ZSIZE){ std::string n; if(decode_hsize_key(ks, &n) == -1){ ret = -1; }else{ zend = n; } } } delete it; it = this->iterator(encode_qsize_key(""), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::QSIZE){ std::string n; if(decode_qsize_key(ks, &n) == -1){ ret = -1; }else{ qstart = n; } } } delete it; it = this->rev_iterator(encode_qsize_key("\xff"), "", 1); if(it->next()){ Bytes ks = it->key(); if(ks.data()[0] == DataType::QSIZE){ std::string n; if(decode_qsize_key(ks, &n) == -1){ ret = -1; }else{ qend = n; } } } delete it; keys->push_back(kstart); keys->push_back(kend); keys->push_back(hstart); keys->push_back(hend); keys->push_back(zstart); keys->push_back(zend); keys->push_back(qstart); keys->push_back(qend); return ret; }