<commit_before>// Copyright 2020 Google LLC
//
// 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
//
//      https://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.

#include "generator/generator.h"
#include "google/cloud/status_or.h"
#include "generator/internal/codegen_utils.h"
#include "generator/internal/descriptor_utils.h"
#include "generator/internal/generator_interface.h"
#include <google/api/client.pb.h>
#include <string>
#include <vector>

namespace google {
namespace cloud {
namespace generator {

bool Generator::Generate(google::protobuf::FileDescriptor const* file,
                         std::string const& parameters,
                         google::protobuf::compiler::GeneratorContext* context,
                         std::string* error) const {
  using ServiceGenerator =
      std::vector<std::unique_ptr<generator_internal::GeneratorInterface>>;

  if (file->options().cc_generic_services()) {
    *error =
        "cpp codegen proto compiler plugin does not work with generic "
        "services. To generate cpp codegen APIs, please set \""
        "cc_generic_service = false\".";
    return false;
  }

  StatusOr<std::vector<std::pair<std::string, std::string>>> command_line_args =
      generator_internal::ProcessCommandLineArgs(parameters);
  if (!command_line_args.ok()) {
    *error = command_line_args.status().message();
    return false;
  }

  std::vector<ServiceGenerator> services;
  services.reserve(file->service_count());
  for (int i = 0; i < file->service_count(); ++i) {
    services.push_back(generator_internal::MakeGenerators(
        file->service(i), context, *command_line_args));
  }

  for (auto const& class_generators : services) {
    for (auto const& c : class_generators) {
      auto result = c->Generate();
      if (!result.ok()) {
        *error = result.message();
        return false;
      }
    }
  }
  return true;
}

}  // namespace generator
}  // namespace cloud
}  // namespace google
<commit_msg>feature(generator): parallelize code generation (#5466)<commit_after>// Copyright 2020 Google LLC
//
// 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
//
//      https://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.

#include "generator/generator.h"
#include "google/cloud/internal/absl_str_cat_quiet.h"
#include "google/cloud/status_or.h"
#include "generator/internal/codegen_utils.h"
#include "generator/internal/descriptor_utils.h"
#include "generator/internal/generator_interface.h"
#include <google/api/client.pb.h>
#include <future>
#include <string>
#include <vector>

namespace google {
namespace cloud {
namespace generator {

bool Generator::Generate(google::protobuf::FileDescriptor const* file,
                         std::string const& parameters,
                         google::protobuf::compiler::GeneratorContext* context,
                         std::string* error) const {
  using ServiceGenerator =
      std::vector<std::unique_ptr<generator_internal::GeneratorInterface>>;

  if (file->options().cc_generic_services()) {
    *error =
        "cpp codegen proto compiler plugin does not work with generic "
        "services. To generate cpp codegen APIs, please set \""
        "cc_generic_service = false\".";
    return false;
  }

  StatusOr<std::vector<std::pair<std::string, std::string>>> command_line_args =
      generator_internal::ProcessCommandLineArgs(parameters);
  if (!command_line_args.ok()) {
    *error = command_line_args.status().message();
    return false;
  }

  std::vector<ServiceGenerator> services;
  services.reserve(file->service_count());
  for (int i = 0; i < file->service_count(); ++i) {
    services.push_back(generator_internal::MakeGenerators(
        file->service(i), context, *command_line_args));
  }

  std::vector<std::future<Status>> tasks;
  for (auto const& code_generators : services) {
    for (auto const& c : code_generators) {
      tasks.push_back(
          std::async(std::launch::async, [&c]() { return c->Generate(); }));
    }
  }

  std::string error_message;
  for (auto& t : tasks) {
    auto result = t.get();
    if (!result.ok()) {
      absl::StrAppend(&error_message, result.message(), "\n");
    }
  }

  if (!error_message.empty()) {
    *error = error_message;
    return false;
  }
  return true;
}

}  // namespace generator
}  // namespace cloud
}  // namespace google

{'fraction_non_alphanumeric': 0.12578977599080987, 'fraction_numerical': 0.00421213861765269, 'mean_word_length': 3.519031141868512, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6163335', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1448, 'n_words': 418}