Skip to content

Commit

Permalink
Making GCS Client Singleton
Browse files Browse the repository at this point in the history
Summary: Making the GCS Client a singleton

Differential Revision: D37853909

fbshipit-source-id: 37d1cc0da1d24ab467e5f9f99ebfab3105a2caad
  • Loading branch information
achyutFB authored and facebook-github-bot committed Jul 14, 2022
1 parent 14b0e6b commit 5098e6c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
15 changes: 12 additions & 3 deletions fbpcf/io/cloud_util/CloudFileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

#include "fbpcf/io/cloud_util/CloudFileUtil.h"
#include <aws/s3/S3Client.h>
#include <google/cloud/storage/client.h>
#include <re2/re2.h>

#include "fbpcf/aws/S3Util.h"
#include "fbpcf/exception/PcfException.h"
#include "fbpcf/gcp/GCSUtil.h"
#include "fbpcf/io/cloud_util/CloudFileUtil.h"
#include "fbpcf/io/cloud_util/GCSClient.h"
#include "fbpcf/io/cloud_util/GCSFileReader.h"
#include "fbpcf/io/cloud_util/GCSFileUploader.h"
#include "fbpcf/io/cloud_util/S3Client.h"
Expand Down Expand Up @@ -65,7 +68,10 @@ std::unique_ptr<IFileReader> getCloudFileReader(const std::string& filePath) {
fbpcf::aws::S3ClientOption{.region = ref.region})
.getS3Client());
} else if (fileType == CloudFileType::GCS) {
return std::make_unique<GCSFileReader>(fbpcf::gcp::createGCSClient());
const auto& ref = fbpcf::gcp::uriToObjectReference(filePath);
return std::make_unique<GCSFileReader>(
fbpcf::cloudio::GCSClient::getInstance(fbpcf::gcp::GCSClientOption{})
.getGCSClient());
} else {
throw fbpcf::PcfException("Not supported yet.");
}
Expand All @@ -82,8 +88,11 @@ std::unique_ptr<IFileUploader> getCloudFileUploader(
.getS3Client(),
filePath);
} else if (fileType == CloudFileType::GCS) {
const auto& ref = fbpcf::gcp::uriToObjectReference(filePath);
return std::make_unique<GCSFileUploader>(
fbpcf::gcp::createGCSClient(), filePath);
fbpcf::cloudio::GCSClient::getInstance(fbpcf::gcp::GCSClientOption{})
.getGCSClient(),
filePath);
} else {
throw fbpcf::PcfException("Not supported yet.");
}
Expand Down
17 changes: 17 additions & 0 deletions fbpcf/io/cloud_util/GCSClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <google/cloud/storage/client.h>

#include "fbpcf/io/cloud_util/GCSClient.h"

namespace fbpcf::cloudio {
GCSClient& GCSClient::getInstance(const fbpcf::gcp::GCSClientOption& option) {
static GCSClient GCSClient(option);
return GCSClient;
}
} // namespace fbpcf::cloudio
34 changes: 34 additions & 0 deletions fbpcf/io/cloud_util/GCSClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <memory>

#include <google/cloud/storage/client.h>
#include "fbpcf/gcp/GCSUtil.h"

namespace fbpcf::cloudio {

class GCSClient {
private:
explicit GCSClient(const fbpcf::gcp::GCSClientOption& option) {
GCSClient_ = fbpcf::gcp::createGCSClient();
}

public:
static GCSClient& getInstance(const fbpcf::gcp::GCSClientOption& option);

std::shared_ptr<google::cloud::storage::Client> getGCSClient() {
return GCSClient_;
}

private:
std::shared_ptr<google::cloud::storage::Client> GCSClient_;
};

} // namespace fbpcf::cloudio

0 comments on commit 5098e6c

Please sign in to comment.