cube_api.h 3.0 KB
Newer Older
Y
yangrui07 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <stdint.h>
#include <string>
#include <vector>

W
wangguibao 已提交
21 22 23
#ifdef BCLOUD
#include "baidu/rpc/server.h"
#else
Y
yangrui07 已提交
24
#include "brpc/server.h"
W
wangguibao 已提交
25 26
#endif

Y
yangrui07 已提交
27 28
#include "cube/cube-api/cube.pb.h"
#include "cube/cube-api/include/meta.h"
Y
yangrui07 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

namespace rec {
namespace mcube {

struct CubeValue {
  int error;
  std::string buff;
};

class CubeAPI {
 public:
  static CubeAPI* instance();

  static void cleanup();

 public:
  CubeAPI();
  ~CubeAPI();

  /**
   * @brief: init api, not thread safe, should be called before seek.
   * @param [in] conf_file: filepath of config file.
   * @return: error code. 0 for succ.
   */
  int init(const char* conf_file);

  /**
   * @brief: destroy api, should be called before program exit.
   * @return: error code. 0 for succ.
   */
  int destroy();

  /** brief: get data from cube server, thread safe.
   * @param [in] dict_name: dict name.
   * @param [in] key: key to seek.
   * @param [out] val: value of key.
   * @return: error code. 0 for succ.
   */
  int seek(const std::string& dict_name, const uint64_t& key, CubeValue* val);

  /**
   * @brief: get data from cube server, thread safe.
   * @param [in] dict_name: dict name.
   * @param [in] keys: keys to seek.
   * @param [out] vals: value of keys.
   * @return: TODO
   */
  int seek(const std::string& dict_name,
           const std::vector<uint64_t>& keys,
           std::vector<CubeValue>* vals);

  int opt_seek(const std::string& dict_name,
               const std::vector<uint64_t>& keys,
               std::function<void(DictValue*, size_t)> parse);

  /**
   * @brief: get data from cube server, thread safe.
   * @param [in] dict_name: dict name.
   * @param [in] keys: keys to seek.
   * @param [out] vals: value of keys.
   * @param [out] version: data version.
   * @return: TODO
   */
  int seek(const std::string& dict_name,
           const std::vector<uint64_t>& keys,
           std::vector<CubeValue>* vals,
           std::string* version);

  int opt_seek(const std::string& dict_name,
               const std::vector<uint64_t>& keys,
               std::function<void(DictValue*, size_t)> parse,
               std::string* version);

 public:
  static const char* error_msg(int error_code);

 private:
  CubeAPI(const CubeAPI&) {}

 private:
  Meta* _meta;
  bthread_key_t _tls_key;
  // void split(const std::vector<uint64_t>& keys);
};

}  // namespace mcube
}  // namespace rec