-
Notifications
You must be signed in to change notification settings - Fork 106
API介绍与使用
flabby edited this page May 27, 2017
·
1 revision
- 一致性接口:
- Read
- Write
- Delete
- 本地接口:
- DirtyWrite
- DirtyRead
- 查询接口:
- GetLeader
- GetServerStatus
- 调试接口
- set_log_level
- 使用Floyd步骤
- 1 设置FloydOptions
- 2 Floyd::Open()
- 3 floyd->Start()
- 4 调用API
#include <iostream>
#include <string>
#include <unistd.h>
#include "floyd/include/floyd_options.h"
#include "floyd/include/floyd.h"
#include "slash/include/slash_status.h"
int main() {
// Step 0: set options;
floyd::Options options;
// A cluster of 3 servers;
options.SetMembers("127.0.0.1:8900;127.0.0.1:8901:127.0.0.1:8902");
// Server 0 should use this ip:port
options.local_ip = "127.0.0.1";
options.local_port = 8900;
options.data_path = "data/path0/";
options.log_path = "log/path0/";
// Server 1 should use this ip:port
//options.local_ip = "127.0.0.1"
//options.local_port = 8901;
//options.data_path = "data/path1/";
//options.log_path = "log/path1/";
// Server 2 should use this ip:port
//options.local_ip = "127.0.0.1"
//options.local_port = 8902;
//options.data_path = "data/path2/";
//options.log_path = "log/path2/";
options.Dump();
// Step 1: Open Floyd
floyd::Floyd *floyd;
floyd::Floyd::Open(options, &floyd);
// Step 2: Start Floyd
slash::Status s = floyd->Start();
if (!s.ok()) {
printf ("Open Floyd failed %s\n", s.ToString().c_str());
return -1;
}
// Wait for leader by GetLeader API
std::string leader_ip;
int leader_port;
while (!floyd->GetLeader(&leader_ip, &leader_port)) {
printf ("Wait leader ...\n");
// Wait leader election
sleep(1);
}
// Write
slash::Status result;
result = floyd->Write("test_key", "test_val");
if (!result.ok()) {
printf ("Write failed %s\n", result.ToString().c_str());
} else {
printf ("Write key ok!");
}
// Read
std::string value;
result = floyd->Read("test_key", value);
if (result.ok()) {
printf ("Read key ok, value is %s\n", value.c_str());
} else if (result.IsNotFound()) {
printf ("Read key notfound\n");
} else {
printf ("Read failed %s\n", result.ToString().c_str());
}
delete floyd;
return 0;
}
- 可以参考
example/server
中使用Floyd、Pink构建节点FloydServer的代码; - 对应的
example/sdk
为客户端代码;