Browse Source

filename parameter addd for ReloadMetadata

pull/3185/head
Denys Ponomarenko 2 years ago
parent
commit
8b88fed563
3 changed files with 67 additions and 21 deletions
  1. +58
    -18
      cpp/src/phonenumbers/shortnumberinfo.cc
  2. +3
    -2
      cpp/src/phonenumbers/shortnumberinfo.h
  3. +6
    -1
      cpp/test/phonenumbers/shortnumberinfo_test.cc

+ 58
- 18
cpp/src/phonenumbers/shortnumberinfo.cc View File

@ -19,9 +19,10 @@
#include <iterator>
#include <map>
#include <boost/filesystem.hpp>
#include "phonenumbers/default_logger.h"
#include "phonenumbers/matcher_api.h"
#include "phonenumbers/phonemetadata.pb.h"
#include "phonenumbers/phonenumberutil.h"
#include "phonenumbers/regex_based_matcher.h"
#include "phonenumbers/region_code.h"
@ -37,6 +38,9 @@ using std::string;
#ifdef ISTREAM_DATA_PROVIDER
const string METADATA_SHORT_FILE_NAME = "metadata_short.dat";
bool LoadMetadataFromFile(string fileName, PhoneMetadataCollection* metadata) {
std::fstream input(fileName.c_str(), std::ios::in | std::ios::binary);
if (!input) {
@ -54,9 +58,45 @@ void ShortNumberInfo::ClearMetadata(){
regions_where_emergency_numbers_must_be_exact_->clear();
}
void ShortNumberInfo::ReloadMetadata() {
bool UpdateShortMetadataFile(const string& filepath) {
boost::filesystem::path path_to = boost::filesystem::current_path();
path_to += "/";
path_to += METADATA_SHORT_FILE_NAME;
boost::filesystem::path path_bk = path_to;
path_bk += ".bak";
boost::system::error_code ec;
boost::filesystem::copy(path_to.string(), path_bk.string(), ec);
if (ec) {
LOG(DFATAL) << "Could not create backup copy of metadata file." << ec.message();
return false;
}
boost::filesystem::rename(filepath, path_to.string(), ec);
if(ec) {
LOG(DFATAL) << "Could not rename metadata file." << ec.message();
return false;
}
boost::filesystem::remove(path_bk, ec);
if(ec) {
LOG(DFATAL) << "Could not remove metadata file." << ec.message();
}
return true;
}
bool ShortNumberInfo::ReloadMetadata(const string& filename) {
ClearMetadata();
LoadMetadata();
PhoneMetadataCollection metadata_collection;
if (!LoadMetadataFromFile(filename, &metadata_collection)) {
LOG(DFATAL) << "Could not parse metadata from file.";
return false;
}
LoadMetadataFromCollection(metadata_collection);
if(!UpdateShortMetadataFile(filename)){
LOG(DFATAL) << "Could not update metadata file.";
return false;
}
return true;
}
#else
bool LoadCompiledInMetadata(PhoneMetadataCollection* metadata) {
@ -68,20 +108,7 @@ bool LoadCompiledInMetadata(PhoneMetadataCollection* metadata) {
}
#endif // ISTREAM_DATA_PROVIDER
void ShortNumberInfo::LoadMetadata() {
PhoneMetadataCollection metadata_collection;
#ifdef ISTREAM_DATA_PROVIDER
const string metadataInputFile = "metadata_short.dat";
if (!LoadMetadataFromFile(metadataInputFile, &metadata_collection)) {
LOG(DFATAL) << "Could not parse metadata from file.";
return;
}
#else
if (!LoadCompiledInMetadata(&metadata_collection)) {
LOG(DFATAL) << "Could not parse compiled-in metadata.";
return;
}
#endif // ISTREAM_DATA_PROVIDER
void ShortNumberInfo::LoadMetadataFromCollection(const PhoneMetadataCollection& metadata_collection) {
for (const auto& metadata : metadata_collection.metadata()) {
const string& region_code = metadata.id();
region_to_short_metadata_map_->insert(std::make_pair(region_code, metadata));
@ -96,7 +123,20 @@ ShortNumberInfo::ShortNumberInfo()
matcher_api_(new RegexBasedMatcher()),
region_to_short_metadata_map_(new absl::flat_hash_map<string, PhoneMetadata>()),
regions_where_emergency_numbers_must_be_exact_(new absl::flat_hash_set<string>()) {
LoadMetadata();
PhoneMetadataCollection metadata_collection;
#ifdef ISTREAM_DATA_PROVIDER
if (!LoadMetadataFromFile(METADATA_SHORT_FILE_NAME, &metadata_collection)) {
LOG(DFATAL) << "Could not parse metadata from file.";
return;
}
#else
if (!LoadCompiledInMetadata(&metadata_collection)) {
LOG(DFATAL) << "Could not parse compiled-in metadata.";
return;
}
#endif // ISTREAM_DATA_PROVIDER
LoadMetadataFromCollection(metadata_collection);
}
ShortNumberInfo::~ShortNumberInfo() {}


+ 3
- 2
cpp/src/phonenumbers/shortnumberinfo.h View File

@ -23,7 +23,7 @@
#include <map>
#include <set>
#include <string>
#include "phonenumbers/phonemetadata.pb.h"
#include "phonenumbers/base/basictypes.h"
#include "phonenumbers/base/memory/scoped_ptr.h"
#include "absl/container/flat_hash_set.h"
@ -43,6 +43,7 @@ class PhoneNumber;
class PhoneNumberUtil;
class ShortNumberInfo {
void LoadMetadataFromCollection(const PhoneMetadataCollection& metadata_collection);
public:
ShortNumberInfo();
~ShortNumberInfo();
@ -50,7 +51,7 @@ class ShortNumberInfo {
private:
void ClearMetadata();
public:
void ReloadMetadata();
bool ReloadMetadata(const string& filename);
#endif // ISTREAM_DATA_PROVIDER
// Cost categories of short numbers.
enum ShortNumberCost {


+ 6
- 1
cpp/test/phonenumbers/shortnumberinfo_test.cc View File

@ -47,7 +47,7 @@ class ShortNumberInfoTest : public testing::Test {
}
const PhoneNumberUtil phone_util_;
const ShortNumberInfo short_info_;
ShortNumberInfo short_info_;
private:
DISALLOW_COPY_AND_ASSIGN(ShortNumberInfoTest);
@ -96,6 +96,11 @@ TEST_F(ShortNumberInfoTest, IsValidShortNumber) {
EXPECT_TRUE(short_info_.IsValidShortNumber(shared_number));
}
TEST_F(ShortNumberInfoTest, ReloadMetadata) {
const string filename = "metadata_short.dat";
EXPECT_TRUE(short_info_.ReloadMetadata(filename));
}
TEST_F(ShortNumberInfoTest, IsCarrierSpecific) {
PhoneNumber carrier_specific_number;
carrier_specific_number.set_country_code(1);


Loading…
Cancel
Save