Example #1
0
// returns the number of newly added items
static int nset_one(SSDBImpl *ssdb, const Bytes &name, const Bytes &val, const Bytes &score, char log_type){
	if(name.empty() || val.empty()){
		log_error("empty name or val!");
		return 0;
		//return -1;
	}
	if(name.size() > SSDB_KEY_LEN_MAX ){
		log_error("name too long!");
		return -1;
	}

	std::string new_score = filter_score(score);
	int ret = 0;
	std::string dbval;
	if(ssdb->nget(name, new_score, &dbval) == 0){ // not found
		std::string nkey = encode_nscore_key(name, score);
		ssdb->binlogs->Put(nkey, slice(val));
		ssdb->binlogs->add_log(log_type, BinlogCommand::NSET, nkey);
		ret = 1;
	}else{
		if(dbval != val){
			std::string nkey = encode_nscore_key(name, score);
			ssdb->binlogs->Put(nkey, slice(val));
			ssdb->binlogs->add_log(log_type, BinlogCommand::NSET, nkey);
			ret = 1;
		}else {
			ret = 0;
		}

	}
	return ret;
}
Example #2
0
// returns the number of newly added items
static int zset_one(SSDB *ssdb, const Bytes &name, const Bytes &key, const Bytes &score, 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 new_score = filter_score(score);
	std::string old_score;
	int found = ssdb->zget(name, key, &old_score);
	if(found == 0 || old_score != new_score){
		std::string k0, k1, k2;

		if(found){
			// delete zscore key
			k1 = encode_zscore_key(name, key, old_score);
			ssdb->binlogs->Delete(k1);
		}

		// add zscore key
		k2 = encode_zscore_key(name, key, new_score);
		ssdb->binlogs->Put(k2, "");

		// update zset
		k0 = encode_zset_key(name, key);
		ssdb->binlogs->Put(k0, new_score);
		ssdb->binlogs->add(log_type, BinlogCommand::ZSET, k0);

		return found? 0 : 1;
	}
	return 0;
}