Exemplo n.º 1
0
int BinlogQueue::find_last(Binlog *log) const{
	uint64_t ret = 0;
	std::string key_str = encode_seq_key(UINT64_MAX);
	leveldb::ReadOptions iterate_options;
	leveldb::Iterator *it = db->NewIterator(iterate_options);
	it->Seek(key_str);
	if(!it->Valid()){
		// Iterator::prev requires Valid, so we seek to last
		it->SeekToLast();
	}else{
		// UINT64_MAX is not used 
		it->Prev();
	}
	if(it->Valid()){
		leveldb::Slice key = it->key();
		if(decode_seq_key(key) != 0){
			leveldb::Slice val = it->value();
			if(log->load(val) == -1){
				ret = -1;
			}else{
				ret = 1;
			}
		}
	}
	delete it;
	return ret;
}
Exemplo n.º 2
0
int BinlogQueue::del(uint64_t seq){
	leveldb::Status s = db->Delete(leveldb::WriteOptions(), encode_seq_key(seq));
	if(!s.ok()){
		return -1;
	}
	return 0;
}
Exemplo n.º 3
0
// 因为老版本可能产生了断续的binlog
// 例如, binlog-1 存在, 但后面的被删除了, 然后到 binlog-100000 时又开始存在.
void BinlogQueue::clean_obsolete_binlogs(){
	std::string key_str = encode_seq_key(this->min_seq);
	leveldb::ReadOptions iterate_options;
	leveldb::Iterator *it = db->NewIterator(iterate_options);
	it->Seek(key_str);
	if(it->Valid()){
		it->Prev();
	}
	uint64_t count = 0;
	while(it->Valid()){
		leveldb::Slice key = it->key();
		uint64_t seq = decode_seq_key(key);
		if(seq == 0){
			break;
		}
		this->del(seq);
		
		it->Prev();
		count ++;
	}
	delete it;
	if(count > 0){
		log_info("clean_obsolete_binlogs: %" PRIu64, count);
	}
}
Exemplo n.º 4
0
int BinlogQueue::find_next(uint64_t next_seq, Binlog *log) const{
    if (!binlog_db) {
        return -1;
    }
	if(this->get(next_seq, log) == 1){
		return 1;
	}
	uint64_t ret = 0;
	std::string key_str = encode_seq_key(next_seq);
	leveldb::ReadOptions iterate_options;
	leveldb::Iterator *it = binlog_db->NewIterator(iterate_options);
	it->Seek(key_str);
	if(it->Valid()){
		leveldb::Slice key = it->key();
		if(decode_seq_key(key) != 0){
			leveldb::Slice val = it->value();
			if(log->load_log_data(val) == -1){
				ret = -1;
			}else{
				ret = 1;
			}
		}
	}
	delete it;
	return ret;
}
Exemplo n.º 5
0
int BinlogQueue::update(uint64_t seq, char type, char cmd, const std::string &key){
	Binlog log(seq, type, cmd, key);
	leveldb::Status s = db->Put(leveldb::WriteOptions(), encode_seq_key(seq), log.repr());
	if(s.ok()){
		return 0;
	}
	return -1;
}
Exemplo n.º 6
0
void BinlogQueue::add_log(char type, char cmd, const leveldb::Slice &key){
	if(!enabled){
		return;
	}
	tran_seq ++;
	Binlog log(tran_seq, type, cmd, key);
	batch.Put(encode_seq_key(tran_seq), log.repr());
}
Exemplo n.º 7
0
int BinlogQueue::get(uint64_t seq, Binlog *log) const{
	std::string val;
	leveldb::Status s = db->Get(leveldb::ReadOptions(), encode_seq_key(seq), &val);
	if(s.ok()){
		if(log->load(val) != -1){
			return 1;
		}
	}
	return 0;
}
Exemplo n.º 8
0
int BinlogQueue::update(uint64_t seq, char type, char cmd, const std::string &key, const std::string &val){
    if (!binlog_db) {
        return -1;
    }
	Binlog log(seq, type, cmd, key, val);
	leveldb::Status s = binlog_db->Put(leveldb::WriteOptions(), encode_seq_key(seq), log.log_data());
	if(s.ok()){
		return 0;
	}
	return -1;
}
Exemplo n.º 9
0
int BinlogQueue::del_range(uint64_t start, uint64_t end){
	while(start <= end){
		leveldb::WriteBatch batch;
		for(int count = 0; start <= end && count < 1000; start++, count++){
			batch.Delete(encode_seq_key(start));
		}
		leveldb::Status s = db->Write(leveldb::WriteOptions(), &batch);
		if(!s.ok()){
			return -1;
		}
	}
	return 0;
}
Exemplo n.º 10
0
void BinlogQueue::add(char type, char cmd, const leveldb::Slice &key){
	tran_seq ++;
	Binlog log(tran_seq, type, cmd, key);
	batch.Put(encode_seq_key(tran_seq), log.repr());
}