// returns the number of newly added items static int hset_one(const SSDB *ssdb, const Bytes &name, const Bytes &key, const Bytes &val, char log_type){ if(name.size() > SSDB_KEY_LEN_MAX ){ log_error("name too long!"); return -1; } if(key.size() > SSDB_KEY_LEN_MAX){ log_error("key too long!"); return -1; } int ret = 0; std::string dbval; if(ssdb->hget(name, key, &dbval) == 0){ // not found std::string hkey = encode_hash_key(name, key); ssdb->binlogs->Put(hkey, val.Slice()); ssdb->binlogs->add(log_type, BinlogCommand::HSET, hkey); ret = 1; }else{ if(dbval != val){ std::string hkey = encode_hash_key(name, key); ssdb->binlogs->Put(hkey, val.Slice()); ssdb->binlogs->add(log_type, BinlogCommand::HSET, hkey); } ret = 0; } return ret; }
// returns the number of newly added items static int hset_one(SSDBImpl *ssdb, const Bytes &name, const Bytes &key, const Bytes &val, char log_type){ if(name.empty() || key.empty()){ log_error("empty name or key!"); return -1; } if(name.size() > SSDB_KEY_LEN_MAX ){ log_error("name too long! %s", hexmem(name.data(), name.size()).c_str()); return -1; } if(key.size() > SSDB_KEY_LEN_MAX){ log_error("key too long! %s", hexmem(key.data(), key.size()).c_str()); return -1; } int ret = 0; std::string dbval; if(ssdb->hget(name, key, &dbval) == 0){ // not found std::string hkey = encode_hash_key(name, key); ssdb->binlogs->Put(hkey, slice(val)); ssdb->binlogs->add_log(log_type, BinlogCommand::HSET, hkey); ret = 1; }else{ if(dbval != val){ std::string hkey = encode_hash_key(name, key); ssdb->binlogs->Put(hkey, slice(val)); ssdb->binlogs->add_log(log_type, BinlogCommand::HSET, hkey); } ret = 0; } return ret; }
HIterator* SSDB::hscan(const Bytes &name, const Bytes &start, const Bytes &end, int limit) const{ std::string key_start, key_end; key_start = encode_hash_key(name, start); if(!end.empty()){ key_end = encode_hash_key(name, end); } //dump(key_start.data(), key_start.size(), "scan.start"); //dump(key_end.data(), key_end.size(), "scan.end"); return new HIterator(this->iterator(key_start, key_end, limit), name); }
HIterator* SSDBImpl::hrscan(const Bytes &name, const Bytes &start, const Bytes &end, uint64_t limit){ std::string key_start, key_end; key_start = encode_hash_key(name, start); if(start.empty()){ key_start.append(1, 255); } if(!end.empty()){ key_end = encode_hash_key(name, end); } //dump(key_start.data(), key_start.size(), "scan.start"); //dump(key_end.data(), key_end.size(), "scan.end"); return new HIterator(this->rev_iterator(key_start, key_end, limit), name); }
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; }
int SSDB::hset(const Bytes &name, const Bytes &key, const Bytes &val) const{ std::string buf = encode_hash_key(name, key); leveldb::Status s = db->Put(write_options, buf, val.Slice()); if(!s.ok()){ return -1; } return 1; }
int SSDB::hdel(const Bytes &name, const Bytes &key) const{ std::string buf = encode_hash_key(name, key); leveldb::Status s = db->Delete(write_options, buf); if(!s.ok()){ return -1; } return 1; }
int SSDB::hget(const Bytes &name, const Bytes &key, std::string *val) const{ std::string dbkey = encode_hash_key(name, key); leveldb::Status s = db->Get(leveldb::ReadOptions(), dbkey, val); if(s.IsNotFound()){ return 0; } if(!s.ok()){ return -1; } return 1; }
int SSDBImpl::hget(const Bytes &name, const Bytes &key, std::string *val){ std::string dbkey = encode_hash_key(name, key); leveldb::Status s = ldb->Get(leveldb::ReadOptions(), dbkey, val); if(s.IsNotFound()){ return 0; } if(!s.ok()){ log_error("%s", s.ToString().c_str()); return -1; } return 1; }
static int hdel_one(const SSDB *ssdb, const Bytes &name, const Bytes &key, char log_type){ if(name.size() > SSDB_KEY_LEN_MAX ){ log_error("name too long!"); return -1; } if(key.size() > SSDB_KEY_LEN_MAX){ log_error("key too long!"); return -1; } std::string dbval; if(ssdb->hget(name, key, &dbval) == 0){ return 0; } std::string hkey = encode_hash_key(name, key); ssdb->binlogs->Delete(hkey); ssdb->binlogs->add(log_type, BinlogCommand::HDEL, hkey); return 1; }
leveldb::Status Server::hset(leveldb::Slice user_key, leveldb::Slice field, leveldb::Slice value) { std::string hash_key; encode_hash_key(user_key, field, &hash_key); return db->Put(default_write_options(), hash_key, value); }
inline leveldb::Status Server::hget(leveldb::Slice user_key, leveldb::Slice field, std::string* value) { std::string hash_key; encode_hash_key(user_key, field, &hash_key); return db->Get(default_read_options(), hash_key, value); }