Skip to content

Commit

Permalink
Merge pull request #967 from xupeilin/split-tera-h
Browse files Browse the repository at this point in the history
split tera.h to several roles #964
  • Loading branch information
taocp authored Aug 17, 2016
2 parents 89e2902 + 3738911 commit 55ea4ce
Show file tree
Hide file tree
Showing 9 changed files with 954 additions and 763 deletions.
777 changes: 14 additions & 763 deletions include/tera.h

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions include/tera/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TERA_CLIENT_H_
#define TERA_CLIENT_H_

#include <stdint.h>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>

#include "error_code.h"
#include "table.h"
#include "table_descriptor.h"

#pragma GCC visibility push(default)
namespace tera {

class Client {
public:
/// 使用glog的用户必须调用此接口,避免glog被重复初始化
static void SetGlogIsInitialized();

static Client* NewClient(const std::string& confpath,
const std::string& log_prefix,
ErrorCode* err = NULL);

static Client* NewClient(const std::string& confpath,
ErrorCode* err = NULL);

static Client* NewClient();

/// 创建表格
virtual bool CreateTable(const TableDescriptor& desc, ErrorCode* err) = 0;
virtual bool CreateTable(const TableDescriptor& desc,
const std::vector<std::string>& tablet_delim,
ErrorCode* err) = 0;
/// 更新表格Schema
virtual bool UpdateTable(const TableDescriptor& desc, ErrorCode* err) = 0;
virtual bool UpdateCheck(const std::string& table_name, bool* done, ErrorCode* err) = 0;
/// 删除表格
virtual bool DeleteTable(const std::string& name, ErrorCode* err) = 0;
/// 停止表格服务
virtual bool DisableTable(const std::string& name, ErrorCode* err) = 0;
/// 恢复表格服务
virtual bool EnableTable(const std::string& name, ErrorCode* err) = 0;

/// acl
virtual bool CreateUser(const std::string& user,
const std::string& password, ErrorCode* err) = 0;
virtual bool DeleteUser(const std::string& user, ErrorCode* err) = 0;
virtual bool ChangePwd(const std::string& user,
const std::string& password, ErrorCode* err) = 0;
virtual bool ShowUser(const std::string& user, std::vector<std::string>& user_groups,
ErrorCode* err) = 0;
virtual bool AddUserToGroup(const std::string& user,
const std::string& group, ErrorCode* err) = 0;
virtual bool DeleteUserFromGroup(const std::string& user,
const std::string& group, ErrorCode* err) = 0;
/// 打开表格, 失败返回NULL
virtual Table* OpenTable(const std::string& table_name, ErrorCode* err) = 0;
/// 获取表格分布信息
virtual bool GetTabletLocation(const std::string& table_name,
std::vector<TabletInfo>* tablets,
ErrorCode* err) = 0;
/// 获取表格Schema
virtual TableDescriptor* GetTableDescriptor(const std::string& table_name,
ErrorCode* err) = 0;

virtual bool List(std::vector<TableInfo>* table_list,
ErrorCode* err) = 0;

virtual bool List(const std::string& table_name,
TableInfo* table_info,
std::vector<TabletInfo>* tablet_list,
ErrorCode* err) = 0;

virtual bool IsTableExist(const std::string& table_name, ErrorCode* err) = 0;

virtual bool IsTableEnabled(const std::string& table_name, ErrorCode* err) = 0;

virtual bool IsTableEmpty(const std::string& table_name, ErrorCode* err) = 0;

virtual bool GetSnapshot(const std::string& name, uint64_t* snapshot, ErrorCode* err) = 0;
virtual bool DelSnapshot(const std::string& name, uint64_t snapshot,ErrorCode* err) = 0;
virtual bool Rollback(const std::string& name, uint64_t snapshot,
const std::string& rollback_name, ErrorCode* err) = 0;

virtual bool CmdCtrl(const std::string& command,
const std::vector<std::string>& arg_list,
bool* bool_result,
std::string* str_result,
ErrorCode* err) = 0;
virtual bool Rename(const std::string& old_table_name,
const std::string& new_table_name,
ErrorCode* err) = 0 ;
Client() {}
virtual ~Client() {}

private:
Client(const Client&);
void operator=(const Client&);
};

} // namespace tera
#pragma GCC visibility pop

#endif // TERA_CLIENT_H_
50 changes: 50 additions & 0 deletions include/tera/error_code.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TERA_ERROR_CODE_
#define TERA_ERROR_CODE_

#include <stdint.h>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>

#pragma GCC visibility push(default)
namespace tera {

class ErrorCode {
public:
enum ErrorCodeType {
kOK = 0,
kNotFound,
kBadParam,
kSystem,
kTimeout,
kBusy,
kNoQuota,
kNoAuth,
kUnknown,
kNotImpl,
kTxnFail
};
ErrorCode();
std::string ToString() const;

std::string GetReason() const;
ErrorCodeType GetType() const;
void SetFailed(ErrorCodeType err, const std::string& reason = "");

private:
ErrorCodeType _err;
std::string _reason;
};

const char* strerr(ErrorCode error_code);

} // namespace tera
#pragma GCC visibility pop

#endif // TERA_ERROR_CODE_
164 changes: 164 additions & 0 deletions include/tera/mutation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TERA_MUTATION_H_
#define TERA_MUTATION_H_

#include <stdint.h>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>

#pragma GCC visibility push(default)
namespace tera {

class RowLock {
};

class Transaction;
class Table;
/// 修改操作
class RowMutation {
/// rowkey的限制:大小 [0, 64KB),任意二进制串
public:
enum Type {
kPut,
kDeleteColumn,
kDeleteColumns,
kDeleteFamily,
kDeleteRow,
kAdd,
kPutIfAbsent,
kAppend,
kAddInt64
};
struct Mutation {
Type type;

/// 大小 [0, 64KB),cf名本身是可打印字符串,不限制数量
std::string family;

/// 大小 [0, 64KB),任意二进制串
std::string qualifier;

/// 大小 [0, 32MB)
std::string value;

int64_t timestamp;
int32_t ttl;
};

RowMutation();
virtual ~RowMutation();

virtual const std::string& RowKey() = 0;

virtual void Reset(const std::string& row_key) = 0;

/// 修改指定列
virtual void Put(const std::string& family, const std::string& qualifier,
const int64_t value) = 0;
/// 修改指定列
virtual void Put(const std::string& family, const std::string& qualifier,
const std::string& value) = 0;
/// 带TTL的修改一个列
virtual void Put(const std::string& family, const std::string& qualifier,
const std::string& value, int32_t ttl) = 0;
// 原子加一个Cell
virtual void Add(const std::string& family, const std::string& qualifier,
const int64_t delta) = 0;
// 原子加一个Cell
virtual void AddInt64(const std::string& family, const std::string& qualifier,
const int64_t delta) = 0;

// 原子操作:如果不存在才能Put成功
virtual void PutIfAbsent(const std::string& family,
const std::string& qualifier,
const std::string& value) = 0;

/// 原子操作:追加内容到一个Cell
virtual void Append(const std::string& family, const std::string& qualifier,
const std::string& value) = 0;

/// 修改一个列的特定版本
virtual void Put(const std::string& family, const std::string& qualifier,
int64_t timestamp, const std::string& value) = 0;
/// 带TTL的修改一个列的特定版本
virtual void Put(const std::string& family, const std::string& qualifier,
int64_t timestamp, const std::string& value, int32_t ttl) = 0;
/// 修改默认列
virtual void Put(const std::string& value) = 0;
/// 修改默认列
virtual void Put(const int64_t value) = 0;

/// 带TTL的修改默认列
virtual void Put(const std::string& value, int32_t ttl) = 0;

/// 修改默认列的特定版本
virtual void Put(int64_t timestamp, const std::string& value) = 0;

/// 删除一个列的最新版本
virtual void DeleteColumn(const std::string& family,
const std::string& qualifier) = 0;
/// 删除一个列的指定版本
virtual void DeleteColumn(const std::string& family,
const std::string& qualifier,
int64_t timestamp) = 0;
/// 删除一个列的全部版本
virtual void DeleteColumns(const std::string& family,
const std::string& qualifier) = 0;
/// 删除一个列的指定范围版本
virtual void DeleteColumns(const std::string& family,
const std::string& qualifier,
int64_t timestamp) = 0;
/// 删除一个列族的所有列的全部版本
virtual void DeleteFamily(const std::string& family) = 0;
/// 删除一个列族的所有列的指定范围版本
virtual void DeleteFamily(const std::string& family, int64_t timestamp) = 0;
/// 删除整行的全部数据
virtual void DeleteRow() = 0;
/// 删除整行的指定范围版本
virtual void DeleteRow(int64_t timestamp) = 0;

/// 修改锁住的行, 必须提供行锁
virtual void SetLock(RowLock* rowlock) = 0;
/// 设置超时时间(只影响当前操作,不影响Table::SetWriteTimeout设置的默认写超时)
virtual void SetTimeOut(int64_t timeout_ms) = 0;
/// 返回超时时间
virtual int64_t TimeOut() = 0;
/// 设置异步回调, 操作会异步返回
typedef void (*Callback)(RowMutation* param);
virtual void SetCallBack(Callback callback) = 0;
// 返回异步回调函数
virtual Callback GetCallBack() = 0;
/// 设置用户上下文,可在回调函数中获取
virtual void SetContext(void* context) = 0;
/// 获得用户上下文
virtual void* GetContext() = 0;
/// 获得结果错误码
virtual const ErrorCode& GetError() = 0;
/// 是否异步操作
virtual bool IsAsync() = 0;
/// 异步操作是否完成
/// !!! Not implemented
virtual bool IsFinished() const = 0;
/// mutation数量
virtual uint32_t MutationNum() = 0;
virtual uint32_t Size() = 0;
virtual uint32_t RetryTimes() = 0;
virtual const RowMutation::Mutation& GetMutation(uint32_t index) = 0;
/// 返回所属事务
virtual Transaction* GetTransaction() = 0;

private:
RowMutation(const RowMutation&);
void operator=(const RowMutation&);
};

} // namespace tera
#pragma GCC visibility pop

#endif // TERA_MUTATION_H_
Loading

0 comments on commit 55ea4ce

Please sign in to comment.