leveldb-expand接口設計

leveldb-expand接口設計

接口設計

  • 前綴匹配:
virtual Status GetPrefix( const Slice& prefix,
                          std::map<std::string, std::string>&entry_list );

接口名:GetPrefix
接口功能:根據prefix指定的前綴,獲取匹配該前綴的所有k-v pair,結果存儲在entry_list當中。
參數說明

  • prefix:傳入參數,指定要匹配的前綴
  • entry_list:傳出參數,存儲匹配該前綴的所有k-v pair.

返回值:返回Status對象


  • 前綴匹配(指定數量):
virtual Status GetNPrefix( const Slice& prefix, 
                           int n, 
                           std::map<std::string, std::string>&entry_list );

接口名:GetPrefix
接口功能:根據prefix指定的前綴,以及獲取記錄條數n, 獲取匹配該前綴的所有k-v pair,並將結果存儲在entry_list當中。
參數說明:

  • prefix:傳入參數,指定要匹配的前綴
  • n:傳入參數,指定匹配的記錄條數。
  • entry_list:傳出參數,存儲匹配指定數量前綴的所有k-v pair.

返回值:返回Status對象

  • 多feature匹配:
virtual Status GetFeature( std::vector<std::string>& feature_list, 
                           std::vector<int>& offset_list,
                           std::map<std::string, std::string>&entry_list );

接口名:GetFeature();
接口功能:根據feature_list指定的feature,以及offset_list當中指定的feature偏移,獲取匹配指定偏移feature的所有k-v pair,將結果存儲在entry_list當中。
參數說明

  • feature_list:傳入參數,指定要匹配的feature
  • offset:傳入參數,指定匹配feature的偏移
  • entry_list:傳出參數,存儲匹配feature的所有k-v pair.

返回值:返回Status對象

  • 多feature匹配(指定數量):
virtual Status GetNFeature( std::vector<std::string>& feature_list, 
                            std::vector<int>& offset_list,
                            int n,
                            std::map<std::string, std::string>&entry_list );

接口名:GetNFeature();
接口功能:根據feature_list指定的feature,以及offset_list當中指定的feature偏移,以及要匹配的記錄數量,獲取匹配指定偏移feature的所有k-v pair,將結果存儲在entry_list當中。
參數說明

  • feature_list:傳入參數,指定要匹配的feature
  • offset:傳入參數,指定匹配feature的偏移
  • n:傳入參數,指定匹配的記錄數量
  • entry_list:傳出參數,存儲匹配feature的所有k-v pair.

返回值:返回Status對象

測試程序


  • 前綴匹配

使用場景:當DB中key由多個feature構成時,eg: key = uid__::__timestamp,按照前綴進行匹配,獲取存儲記錄

key value
5205266179__::__20170304 李易峯
5205266179__::__20171204 楊冪
5205266179__::__20170804 杜蘭特
#include <cassert>
#include "leveldb/db.h"

leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());

leveldb::Slice prefix = "5205266179";
std::map< std::string, std::string > entry_list;
status = level::DB::GetPrefix( prefix, entry_list );

// assess the entry_list
// code remain to finish


  • 前綴匹配(指定數量):

使用場景:當DB中key由多個feature構成時,eg: key = uid__::__timestamp,按照前綴進行匹配,指定要獲取的記錄數量n,獲取指定數量的存儲記錄。DB樣例如上。
#include <cassert>
#include "leveldb/db.h"

leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());

leveldb::Slice prefix = "5205266179";
int n = 8;
std::map< std::string, std::string > entry_list;
status = level::DB::GetPrefix( prefix, n, entry_list );

// assess the entry_list
// code remain to finish


  • 多feature匹配:

使用場景:當DB中key由多個feature構成時,eg: key = uid__::__timestamp__query,按照指定位置的feature進行匹配,獲取歷史記錄,DB樣例如下。比如,可以按照第二個和第三個feature進行檢索,檢索在20170804查詢詞爲杜蘭特的記錄

key value
5205266179__::20170304::__李易峯 李易峯
5205266179__::20171204::__楊冪 楊冪
5205266179__::20170804::__杜蘭特 杜蘭特
3656728192__::20170804::__李易峯 李易峯
2938271928__::20170804::__杜蘭特 李易峯
#include <cassert>
#include "leveldb/db.h"

leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());

std::string timestamp = "20170804";
int offset_timestamp = 1;
std::string query = "杜蘭特";
int offset_query = "2";
std::vector<std::string> feature_list;
feature_list.push_back( timestamp );
feature_list.push_back(query);
std::vector<int> offset_list;
off_set.push_back( offset_timestamp );
off_set.push_back( offset_query );
std::map< std::string, std::string > entry_list;
status = level::DB::GetFeature( feature_list, offset_list, entry_list );

// assess the entry_list
// code remain to finish


  • 多feature匹配(指定數量):

使用場景:當DB中key由多個feature構成時,eg: key = uid__::__timestamp__query,按照指定位置的feature進行匹配,獲取指定數量的歷史記錄,DB樣例如下。比如,可以按照第二個和第三個feature進行檢索,檢索在20170804查詢詞爲杜蘭特的記錄

key value
5205266179__::20170304::__李易峯 李易峯
5205266179__::20171204::__楊冪 楊冪
5205266179__::20170804::__杜蘭特 杜蘭特
3656728192__::20170804::__李易峯 李易峯
2938271928__::20170804::__杜蘭特 李易峯
#include <cassert>
#include "leveldb/db.h"

leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());

std::string timestamp = "20170804";
int offset_timestamp = 1;
std::string query = "杜蘭特";
int offset_query = "2";
std::vector<std::string> feature_list;
feature_list.push_back( timestamp );
feature_list.push_back(query);
std::vector<int> offset_list;
off_set.push_back( offset_timestamp );
off_set.push_back( offset_query );
int n = 2; // 獲取兩條
std::map< std::string, std::string > entry_list;
status = level::DB::GetFeature( feature_list, offset_list, n, entry_list );

// assess the entry_list
// code remain to finish
發佈了356 篇原創文章 · 獲贊 43 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章