std::vector BuildKeysPrefixNameSlices()

in tensorflow_recommenders_addons/dynamic_embedding/core/kernels/redis_impl/redis_table_op_util.hpp [403:467]


std::vector<std::string> BuildKeysPrefixNameSlices(
    const std::vector<std::pair<unsigned, unsigned>> cluster_slots,
    const unsigned storage_slice, std::vector<std::string> redis_hash_tags,
    const std::string &keys_prefix_name) {
  std::vector<std::string> keys_prefix_name_slices;
  keys_prefix_name_slices.reserve(storage_slice);
  if (redis_hash_tags.size() == storage_slice) {
    LOG(INFO) << "Using the prefix redis_hash_tags for every bucket.";
    for (auto redis_hash_tag : redis_hash_tags) {
      if (redis_hash_tag.back() != '}') {
        redis_hash_tag.push_back('}');
      }
      if (redis_hash_tag.front() != '{') {
        redis_hash_tag.insert(redis_hash_tag.begin(), '{');
      }
      keys_prefix_name_slices.emplace_back(keys_prefix_name + redis_hash_tag);
    }
  } else {
    LOG(INFO)
        << "Number of prefix redis_hash_tags is not equal to the prefix "
           "storage_slice. Now using the hash tags generated sequentially.";
    if (cluster_slots.size() == 0) {
      const unsigned slot_num_in_redis = 16384 / storage_slice;
      const unsigned slot_in_redis_rem = 16384 % storage_slice;
      for (unsigned i = 0; i < storage_slice; ++i) {
        keys_prefix_name_slices.emplace_back(
            keys_prefix_name + '{' +
            std::to_string(
                redis_slots_tab[slot_num_in_redis * i + slot_in_redis_rem]) +
            '}');
      }
    } else {
      if (cluster_slots.size() < storage_slice) {
        for (unsigned i = 0; i < storage_slice;) {
          for (auto cluster_slot : cluster_slots) {
            keys_prefix_name_slices.emplace_back(
                keys_prefix_name + '{' +
                std::to_string(redis_slots_tab[cluster_slot.first +
                                               i / cluster_slots.size() *
                                                   ((cluster_slot.second -
                                                     cluster_slot.first) *
                                                    cluster_slots.size() /
                                                    storage_slice)]) +
                '}');
            ++i;
            if (i == storage_slice) {
              break;
            }
          }
        }
      } else {
        if (cluster_slots.size() > storage_slice) {
          LOG(WARNING) << "Nodes in Redis service is bigger than storage_slice "
                          "set by user, it may cause data skew.";
        }
        for (unsigned i = 0; i < storage_slice; ++i) {
          keys_prefix_name_slices.emplace_back(
              keys_prefix_name + '{' +
              std::to_string(redis_slots_tab[cluster_slots.at(i).first]) + '}');
        }
      }
    }
  }
  return keys_prefix_name_slices;
}