MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cpp-samples / IndexGcsPrefix

Function IndexGcsPrefix

getting-started/index_gcs_prefix.cc:76–168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

74} // namespace
75
76gcf::HttpResponse IndexGcsPrefix(gcf::HttpRequest request) { // NOLINT
77 // This example assumes the push subscription is set for 10 minute deadline.
78 // We allow ourselves up to 5 minutes processing this request.
79 auto const deadline =
80 std::chrono::steady_clock::now() + std::chrono::minutes(5);
81
82 auto const ct = request.headers().find("content-type");
83 if (ct == request.headers().end() || ct->second != "application/json") {
84 return LogError("expected application/json data");
85 }
86 auto const payload = nlohmann::json::parse(request.payload());
87 if (!payload.contains("message")) {
88 return LogError("missing embedded Pub/Sub message");
89 }
90 auto const& message = payload["message"];
91 if (!message.contains("attributes")) {
92 return LogError("missing Pub/Sub attributes");
93 }
94 auto const& attributes = message["attributes"];
95 if (!attributes.contains("bucket")) {
96 return LogError("missing 'bucket' attribute in Pub/Sub message");
97 }
98 auto const bucket = attributes.value("bucket", "");
99 auto const prefix = [&attributes]() {
100 if (!attributes.contains("prefix")) return gcs::Prefix();
101 return gcs::Prefix(attributes.value("prefix", ""));
102 }();
103 auto const start = [&attributes]() {
104 if (!attributes.contains("start")) return gcs::StartOffset();
105 return gcs::StartOffset(attributes.value("start", ""));
106 }();
107
108 auto client = gcs::Client();
109 auto publisher = GetPublisher();
110
111 int mutation_count = 0;
112 std::vector<google::cloud::future<google::cloud::Status>> pending;
113 for (auto const& entry : client.ListObjectsAndPrefixes(bucket, prefix, start,
114 gcs::Delimiter("/"))) {
115 ThrowIfNotOkay("listing bucket " + bucket, entry.status());
116 if (std::chrono::steady_clock::now() >= deadline) {
117 struct SplitPoint {
118 std::string operator()(std::string const& s) { return s; }
119 std::string operator()(gcs::ObjectMetadata const& o) {
120 return o.name();
121 }
122 };
123 std::string start = absl::visit(SplitPoint{}, *entry);
124 auto builder = pubsub::MessageBuilder{}
125 .InsertAttribute("bucket", bucket)
126 .InsertAttribute("start", std::move(start));
127 if (prefix.has_value()) {
128 builder.InsertAttribute("prefix", prefix.value());
129 }
130 pending.push_back(
131 publisher.Publish(std::move(builder).Build()).then([](auto f) {
132 return f.get().status();
133 }));

Callers

nothing calls this directly

Calls 6

GetPublisherFunction · 0.85
FlushMethod · 0.80
LogErrorFunction · 0.70
ThrowIfNotOkayFunction · 0.70
UpdateObjectMetadataFunction · 0.70
GetSpannerClientFunction · 0.70

Tested by

no test coverage detected