cache.cpp 3.4 KB
Newer Older
T
TeslaZhao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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

T
TeslaZhao 已提交
15
#include "core/predictor/framework/cache.h"
T
TeslaZhao 已提交
16
#include <dirent.h>
T
TeslaZhao 已提交
17
#include <sys/stat.h>
T
TeslaZhao 已提交
18 19
#include <fstream>
#include <string>
T
TeslaZhao 已提交
20
#include <utility>
T
TeslaZhao 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
#include "core/cube/cube-builder/include/cube-builder/seqfile_reader.h"

namespace baidu {
namespace paddle_serving {
namespace predictor {

int CubeCache::clear() {
  for (auto it = _map_cache.begin(); it != _map_cache.end(); ++it) {
    if (it->second) {
      delete (it->second);
      it->second = nullptr;
    }
  }
T
TeslaZhao 已提交
34 35
  _map_cache.clear();

T
TeslaZhao 已提交
36 37 38 39 40 41 42 43 44 45 46 47
  return 0;
}

rec::mcube::CubeValue* CubeCache::get_data(uint64_t key) {
  auto it = _map_cache.find(key);
  if (it != _map_cache.end()) {
    return it->second;
  }
  return nullptr;
}

int CubeCache::reload_data(const std::string& cache_path) {
T
TeslaZhao 已提交
48
  LOG(INFO) << "cube cache is loading data, path: " << cache_path;
T
TeslaZhao 已提交
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
  DIR* dp = nullptr;
  struct dirent* dirp = nullptr;
  struct stat st;

  // clear cache data
  clear();

  // loading data from cache files
  if (stat(cache_path.c_str(), &st) < 0 || !S_ISDIR(st.st_mode)) {
    LOG(ERROR) << "invalid cache path " << cache_path;
    return -1;
  }
  if ((dp = opendir(cache_path.c_str())) == nullptr) {
    LOG(ERROR) << "opendir " << cache_path << " fail.";
    return -1;
  }
  while ((dirp = readdir(dp)) != nullptr) {
    // filtering by file type.
    if (dirp->d_type != DT_REG) {
      continue;
    }
    // Filter upper-level directories and hidden files
    if ((!strncmp(dirp->d_name, ".", 1)) || (!strncmp(dirp->d_name, "..", 2))) {
      continue;
    }
    // Match the file whose name prefix is ​​'part-'
    if (std::string(dirp->d_name).find("part-") != std::string::npos) {
T
TeslaZhao 已提交
76
      SequenceFileRecordReader reader(cache_path + "/" + dirp->d_name);
T
TeslaZhao 已提交
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

      if (reader.open() != 0) {
        LOG(ERROR) << "open file failed! " << dirp->d_name;
        continue;
      }
      if (reader.read_header() != 0) {
        LOG(ERROR) << "read header error! " << dirp->d_name;
        reader.close();
        continue;
      }

      Record record(reader.get_header());
      while (reader.next(&record) == 0) {
        uint64_t key =
            *reinterpret_cast<uint64_t*>(const_cast<char*>(record.key.data()));

        auto it_find = _map_cache.find(key);
        if (it_find != _map_cache.end()) {
          // load dumplicate key
          LOG(WARNING) << "Load dumplicate key:" << key
                       << " from file:" << dirp->d_name;
          continue;
        }
        rec::mcube::CubeValue* new_value = new rec::mcube::CubeValue();
        new_value->error = 0;
T
TeslaZhao 已提交
102
        new_value->buff.swap(record.value);
T
TeslaZhao 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
        _map_cache.insert(std::make_pair(key, new_value));
      }

      LOG(WARNING) << "Load cube cache file " << dirp->d_name << " done.";
    }
    LOG(WARNING) << "Load all cube cache files done";
  }
  return 0;
}

}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu