text:
#include "tensorflow/core/kernels/data/experimental/io_ops.h"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>

#include "tensorflow/core/data/captured_function.h"
#include "tensorflow/core/data/hash_utils.h"
#include "tensorflow/core/data/name_utils.h"
#include "tensorflow/core/data/root_dataset.h"
#include "tensorflow/core/data/snapshot_utils.h"
#include "tensorflow/core/framework/dataset.h"
#include "tensorflow/core/framework/model.h"
#include "tensorflow/core/framework/op_requires.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/platform/cpu_info.h"
#include "tensorflow/core/platform/errors.h"
#include "tensorflow/core/platform/stringprintf.h"
#include "tensorflow/core/protobuf/snapshot.pb.h"

namespace tensorflow {
namespace data {
namespace experimental {

/* static */ constexpr const int SaveDatasetOp::kFileFormatVersion;
/* static */ constexpr const char* const SaveDatasetV2Op::kInputDataset;
/* static */ constexpr const char* const SaveDatasetV2Op::kPath;
/* static */ constexpr const char* const SaveDatasetV2Op::kCompression;
/* static */ constexpr const char* const SaveDatasetV2Op::kDatasetType;
/* static */ constexpr const char* const SaveDatasetV2Op::kOutputTypes;
/* static */ constexpr const char* const SaveDatasetV2Op::kOutputShapes;
/* static */ constexpr const char* const SaveDatasetV2Op::kShardFunc;
/* static */ constexpr const char* const SaveDatasetV2Op::kShardFuncOtherArgs;
/* static */ constexpr const char* const SaveDatasetV2Op::kUseShardFunc;
/* static */ constexpr const char* const SaveDatasetV2Op::kShardFuncTarguments;
/* static */ constexpr const char* const LoadDatasetOp::kCompression;
/* static */ constexpr const char* const LoadDatasetOp::kReaderFunc;
/* static */ constexpr const char* const LoadDatasetOp::kReaderFuncTarguments;

SaveDatasetOp::SaveDatasetOp(OpKernelConstruction* ctx)
    : HybridAsyncOpKernel(ctx, "tf_data_save_dataset") {
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kCompression, &compression_));
  OP_REQUIRES_OK(ctx, FunctionMetadata::Create(ctx, kShardFunc, /*params=*/{},
                                               &func_metadata_));
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kUseShardFunc, &use_shard_func_));
}

Status SaveDatasetOp::DoCompute(OpKernelContext* ctx) {
  DatasetBase* dataset;
  TF_RETURN_IF_ERROR(GetDatasetFromVariantTensor(ctx->input(0), &dataset));

  tstring path;
  TF_RETURN_IF_ERROR(ParseScalarArgument(ctx, kPath, &path));

  // Create a run directory.
  auto run_id = random::New64();
  auto run_dir = snapshot_util::RunDirectory(path, run_id);
  TF_RETURN_IF_ERROR(ctx->env()->RecursivelyCreateDir(run_dir));
  TF_RETURN_IF_ERROR(
      WriteMetadataFile(ctx->env(), path, run_id, dataset->output_dtypes(),
                        /*num_elements=*/0, /*finalized=*/false));

  std::unique_ptr<CapturedFunction> captured_func;
  TF_RETURN_IF_ERROR(CapturedFunction::Create(
      ctx, func_metadata_, kShardFuncOtherArgs, &captured_func));

  uint64 num_elements = 0;
  TF_RETURN_IF_ERROR(WriteData(ctx, dataset, std::move(captured_func), run_dir,
                               &num_elements));
  TF_RETURN_IF_ERROR(WriteMetadataFile(ctx->env(), path, run_id,
                                       dataset->output_dtypes(), num_elements,
                                       /*finalized=*/true));
  return Status::OK();
}

Status SaveDatasetOp::WriteData(OpKernelContext* ctx, DatasetBase* dataset,
                                std::unique_ptr<CapturedFunction> captured_func,
                                const std::string& run_dir,
                                uint64* num_elements) {
  IteratorContext::Params params(ctx);
  auto function_handle_cache =
      absl::make_unique<FunctionHandleCache>(params.flr);
  params.function_handle_cache = function_handle_cache.get();
  ResourceMgr resource_mgr;
  params.resource_mgr = &resource_mgr;
  CancellationManager cancellation_manager(ctx->cancellation_manager());
  params.cancellation_manager = &cancellation_manager;

  IteratorContext iter_ctx(std::move(params));
  std::unique_ptr<InstantiatedCapturedFunction> instantiated_captured_func;
  TF_RETURN_IF_ERROR(
      captured_func->Instantiate(&iter_ctx, &instantiated_captured_func));

  DatasetBase* finalized_dataset;
  TF_RETURN_IF_ERROR(FinalizeDataset(ctx, dataset, &finalized_dataset));

  std::unique_ptr<IteratorBase> iterator;
  TF_RETURN_IF_ERROR(finalized_dataset->MakeIterator(
      &iter_ctx, /*parent=*/nullptr, "Save", &iterator));

  mutex mu;
  Status status;
  absl::flat_hash_map<int64, std::unique_ptr<snapshot_util::AsyncWriter>>
      writers;
  while (true) {
    if (ctx->cancellation_manager()->IsCancelled()) {
      return errors::Cancelled("Operation was cancelled");
    }
    std::vector<Tensor> element;
    bool end_of_input;
    TF_RETURN_IF_ERROR(iterator->GetNext(&iter_ctx, &element, &end_of_input));
    if (end_of_input) {
      break;
    }
    (*num_elements)++;

    // Run the shard function to compute the shard index.
    int64 shard_index = -1;
    TF_RETURN_IF_ERROR(GetShardIndex(
        &iter_ctx, instantiated_captured_func.get(), element, &shard_index));

    // If the index does not exist, we will start a new thread.
    if (writers.count(shard_index) == 0) {
      const auto snapshot_shard_directory =
          snapshot_util::ShardDirectory(run_dir, shard_index);
      auto writer_thread = std::make_unique<snapshot_util::AsyncWriter>(
          ctx->env(), shard_index, snapshot_shard_directory,
          /*checkpoint_id=*/0, compression_, kFileFormatVersion,
          finalized_dataset->output_dtypes(), [&mu, &status](Status s) {
            mutex_lock l(mu);
            status.Update(s);
          });
      writers.insert({shard_index, std::move(writer_thread)});
    }
    writers[shard_index]->Write(element);
  }

  // Push the end of sequence signal to each of the threads to close files.
  for (auto& writer : writers) {
    writer.second->SignalEOF();
  }
  // Wait for the writer threads to join.
  writers.clear();

  return status;
}

Status SaveDatasetOp::GetShardIndex(IteratorContext* ctx,
                                    InstantiatedCapturedFunction* function,
                                    const std::vector<Tensor>& element,
                                    int64* shard_index) {
  if (!use_shard_func_) {
    *shard_index = (*shard_index + 1) % port::NumSchedulableCPUs();
    return Status::OK();
  }
  std::vector<Tensor> output_tensors;
  TF_RETURN_IF_ERROR(function->RunWithBorrowedArgs(
      ctx, element, &output_tensors, /*node=*/nullptr));

  if (output_tensors.size() != 1 || output_tensors[0].dtype() != DT_INT64 ||
      output_tensors[0].NumElements() != 1) {
    return errors::InvalidArgument("`shard_func` must return a scalar int64.");
  }
  *shard_index = output_tensors[0].flat<int64>()(0);
  return Status::OK();
}

Status SaveDatasetOp::WriteMetadataFile(Env* env, const std::string& path,
                                        uint64 run_id,
                                        const DataTypeVector& output_dtypes,
                                        uint64 num_elements, bool finalized) {
  SnapshotMetadataRecord metadata;
  metadata.set_creation_timestamp(EnvTime::NowMicros());
  metadata.set_run_id(
      strings::Printf("%llu", static_cast<unsigned long long>(run_id)));
  metadata.set_version(kFileFormatVersion);
  for (const auto& output_dtype : output_dtypes) {
    metadata.add_dtype(output_dtype);
  }
  metadata.set_finalized(finalized);
  metadata.set_num_elements(num_elements);
  return snapshot_util::WriteMetadataFile(env, path, &metadata);
}

class SaveDatasetV2Op::Dataset : public DatasetBase {
 public:
  Dataset(OpKernelContext* ctx, const DatasetBase* input, const tstring& path,
          const std::string& compression,
          std::unique_ptr<CapturedFunction> shard_func, bool use_shard_func)
      : DatasetBase(DatasetContext(ctx)),
        input_(input),
        path_(path),
        compression_(compression),
        shard_func_(std::move(shard_func)),
        use_shard_func_(use_shard_func) {
    input_->Ref();
  }

  ~Dataset() override { input_->Unref(); }

  std::unique_ptr<IteratorBase> MakeIteratorInternal(
      const string& prefix) const override {
    return absl::make_unique<Iterator>(Iterator::Params{
        this, name_utils::IteratorPrefix(kDatasetType, prefix)});
  }

  const DataTypeVector& output_dtypes() const override {
    return input_->output_dtypes();
  }

  const std::vector<PartialTensorShape>& output_shapes() const override {
    return input_->output_shapes();
  }

  string DebugString() const override {
    return name_utils::DatasetDebugString(kDatasetType);
  }

  int64 Cardinality() const override { return input_->Cardinality(); }

  Status InputDatasets(std::vector<const DatasetBase*>* inputs) const override {
    inputs->push_back(input_);
    return Status::OK();
  }

  Status CheckExternalState() const override {
    return input_->CheckExternalState();
  }

 protected:
  Status AsGraphDefInternal(SerializationContext* ctx,
                            DatasetGraphDefBuilder* b,
                            Node** output) const override {
    Node* input_graph_node = nullptr;
    TF_RETURN_IF_ERROR(b->AddInputDataset(ctx, input_, &input_graph_node));

    Node* path_node = nullptr;
    TF_RETURN_IF_ERROR(b->AddScalar(path_, &path_node));

    std::vector<Node*> shard_func_other_args;
    DataTypeVector shard_func_other_args_types;
    TF_RETURN_IF_ERROR(shard_func_->AddToGraph(ctx, b, &shard_func_other_args,
                                               &shard_func_other_args_types));

    // Attr: compression
    AttrValue compression_attr;
    b->BuildAttrValue(compression_, &compression_attr);

    // Attr: shard_func
    AttrValue shard_func_attr;
    b->BuildAttrValue(shard_func_->func(), &shard_func_attr);

    // Attr: use_shard_func
    AttrValue use_shard_func_attr;
    b->BuildAttrValue(use_shard_func_, &use_shard_func_attr);

    // Attr: shard_func_arguments_types
    AttrValue shard_func_arguments_types_attr;
    b->BuildAttrValue(shard_func_other_args_types,
                      &shard_func_arguments_types_attr);

    TF_RETURN_IF_ERROR(b->AddDataset(
        this,
        /*inputs=*/
        {std::make_pair(0, input_graph_node), std::make_pair(1, path_node)},
        /*list_inputs=*/
        {std::make_pair(1, shard_func_other_args)},
        /*attrs=*/
        {std::make_pair(kCompression, compression_attr),
         std::make_pair(kShardFunc, shard_func_attr),
         std::make_pair(kUseShardFunc, use_shard_func_attr),
         std::make_pair(kShardFuncTarguments, shard_func_arguments_types_attr)},
        output));

    return Status::OK();
  }

 private:
  class Iterator : public DatasetIterator<Dataset> {
   public:
    static constexpr const char* const kIteratorName = "Writer";
    static constexpr const char* const kRunId = "run_id";
    static constexpr const char* const kCurrentCheckpointId =
        "current_checkpoint_id";

    explicit Iterator(const Params& params)
        : DatasetIterator<Dataset>(params),
          writers_closed_(false),
          run_id_(0),
          current_checkpoint_id_(0) {}

    ~Iterator() override {
      mutex_lock l(mu_);
      SignalEOF(true);
    }

    Status Initialize(IteratorContext* ctx) override {
      mutex_lock l(mu_);
      TF_RETURN_IF_ERROR(
          dataset()->shard_func_->Instantiate(ctx, &instantiated_shard_func_));

      // If we are restoring from a checkpointed iterator, we initialize
      // the run directory within the RestoreInternal method.
      if (!ctx->is_restoring()) {
        run_id_ = random::New64();
        run_dir_ = snapshot_util::RunDirectory(
            io::JoinPath(dataset()->writer_prefix_, dataset()->path_), run_id_);
        TF_RETURN_IF_ERROR(ctx->env()->RecursivelyCreateDir(run_dir_));
        TF_RETURN_IF_ERROR(WriteMetadataFile(
            ctx->env(), dataset()->path_, run_id_, dataset()->output_dtypes(),
            /*num_elements=*/0, /*finalized=*/false));
      }
      return dataset()->input_->MakeIterator(ctx, this, prefix(), &input_impl_);
    }

    Status GetNextInternal(IteratorContext* ctx,
                           std::vector<Tensor>* out_tensors,
                           bool* end_of_sequence) override {
      *end_of_sequence = false;
      snapshot_util::AsyncWriter* current_writer;

      {
        std::vector<Tensor> output_tensors;
        mutex_lock l(mu_);

        // Writers have either encountered an error or are closed.
        {
          mutex_lock wsl(writer_status_mu_);
          if (!writer_status_.ok() || writers_closed_) {
            *end_of_sequence = true;
            return writer_status_;
          }
        }

        TF_RETURN_IF_ERROR(
            input_impl_->GetNext(ctx, out_tensors, end_of_sequence));

        // Finalize metadata file when we are at the end of the iterator.
        if (*end_of_sequence) {
          SignalEOF(/*mark_closed=*/true);
          {
            mutex_lock wsl(writer_status_mu_);
            TF_RETURN_IF_ERROR(writer_status_);
          }
          return WriteMetadataFile(ctx->env(), dataset()->path_, run_id_,
                                   dataset()->output_dtypes(),
                                   /*num_elements=*/0, /*finalized=*/true);
        }
        (num_elements_)++;

        int64 shard_index = 0;
        TF_RETURN_IF_ERROR(
            GetShardIndex(ctx, instantiated_shard_func_.get(), *out_tensors,
                          dataset()->use_shard_func_, &shard_index));

        // If the index does not exist, we will start a new thread.
        if (writers_.count(shard_index) == 0) {
          auto snapshot_shard_directory =
              snapshot_util::ShardDirectory(run_dir_, shard_index);
          auto writer = std::make_unique<snapshot_util::AsyncWriter>(
              ctx->env(), shard_index, snapshot_shard_directory,
              current_checkpoint_id_, dataset()->compression_,
              kFileFormatVersion, dataset()->output_dtypes(), [this](Status s) {
                if (!s.ok()) {
                  mutex_lock l(writer_status_mu_);
                  writer_status_ = s;
                }
              });
          writers_.insert({shard_index, std::move(writer)});
        }
        current_writer = writers_[shard_index].get();
      }

      current_writer->Write(*out_tensors);
      return Status::OK();
    }

   protected:
    Status SaveInternal(SerializationContext* ctx,
                        IteratorStateWriter* writer) override {
      mutex_lock l(mu_);
      TF_RETURN_IF_ERROR(
          writer->WriteScalar(full_name(kRunId), static_cast<int64>(run_id_)));
      TF_RETURN_IF_ERROR(
          writer->WriteScalar(full_name(kCurrentCheckpointId),
                              static_cast<int64>(current_checkpoint_id_)));
      SignalEOF(/*mark_closed=*/false);
      writers_.clear();
      current_checkpoint_id_++;
      return SaveInput(ctx, writer, input_impl_);
    }

    Status RestoreInternal(IteratorContext* ctx,
                           IteratorStateReader* reader) override {
      mutex_lock l(mu_);
      int64 run_id_signed;
      int64 current_checkpoint_id;

      TF_RETURN_IF_ERROR(reader->ReadScalar(full_name(kRunId), &run_id_signed));
      TF_RETURN_IF_ERROR(reader->ReadScalar(full_name(kCurrentCheckpointId),
                                            &current_checkpoint_id));

      run_id_ = static_cast<uint64>(run_id_signed);
      run_dir_ = snapshot_util::RunDirectory(
          io::JoinPath(dataset()->writer_prefix_, dataset()->path_), run_id_);
      current_checkpoint_id_ = static_cast<uint64>(current_checkpoint_id);

      if (ctx->is_restoring()) {
        TF_RETURN_IF_ERROR(ctx->env()->RecursivelyCreateDir(run_dir_));
        TF_RETURN_IF_ERROR(WriteMetadataFile(
            ctx->env(), dataset()->path_, run_id_, dataset()->output_dtypes(),
            /*num_elements=*/0, /*finalized=*/false));
      }

      return RestoreInput(ctx, reader, input_impl_);
    }

   private:
    Status GetShardIndex(IteratorContext* ctx,
                         InstantiatedCapturedFunction* function,
                         const std::vector<Tensor>& element,
                         bool use_shard_func, int64* shard_index)
        TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
      if (!use_shard_func) {
        *shard_index = (*shard_index + 1) % port::NumSchedulableCPUs();
        return Status::OK();
      }
      std::vector<Tensor> output_tensors;
      TF_RETURN_IF_ERROR(function->RunWithBorrowedArgs(
          ctx, element, &output_tensors, /*node=*/nullptr));

      if (output_tensors.size() != 1 || output_tensors[0].dtype() != DT_INT64 ||
          output_tensors[0].NumElements() != 1) {
        return errors::InvalidArgument(
            "`shard_func` must return a scalar int64.");
      }
      *shard_index = output_tensors[0].flat<int64>()(0);
      return Status::OK();
    }

    Status WriteMetadataFile(Env* env, const std::string& path, uint64 run_id,
                             const DataTypeVector& output_dtypes,
                             uint64 num_elements, bool finalized)
        TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
      SnapshotMetadataRecord metadata;
      metadata.set_creation_timestamp(EnvTime::NowMicros());
      metadata.set_run_id(
          strings::Printf("%llu", static_cast<unsigned long long>(run_id)));
      metadata.set_version(kFileFormatVersion);
      for (const auto& output_dtype : output_dtypes) {
        metadata.add_dtype(output_dtype);
      }
      metadata.set_finalized(finalized);
      metadata.set_num_elements(num_elements);
      return snapshot_util::WriteMetadataFile(env, path, &metadata);
    }

    void SignalEOF(bool mark_closed) TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
      if (!writers_closed_) {
        for (auto& writer : writers_) {
          writer.second->SignalEOF();
        }

        writers_.clear();
        writers_closed_ = mark_closed;
      }
    }

    mutex mu_;
    mutex writer_status_mu_;
    std::unique_ptr<IteratorBase> input_impl_ TF_GUARDED_BY(mu_);
    int64 num_elements_;

    absl::flat_hash_map<int64, std::unique_ptr<snapshot_util::AsyncWriter>>
        writers_ TF_GUARDED_BY(mu_);
    Status writer_status_ TF_GUARDED_BY(writer_status_mu_);
    bool writers_closed_ TF_GUARDED_BY(mu_);

    uint64 run_id_ TF_GUARDED_BY(mu_);
    tstring run_dir_ TF_GUARDED_BY(mu_);

    uint64 current_checkpoint_id_ TF_GUARDED_BY(mu_);
    std::unique_ptr<InstantiatedCapturedFunction> instantiated_shard_func_
        TF_GUARDED_BY(mu_);
  };

  const DatasetBase* input_;
  const tstring path_;
  const std::string compression_;
  const std::unique_ptr<CapturedFunction> shard_func_;
  const bool use_shard_func_;
  const DataTypeVector output_types_;
  const std::vector<PartialTensorShape> output_shapes_;
  const std::shared_ptr<FunctionMetadata> func_metadata_;
  const std::string writer_prefix_;
};

SaveDatasetV2Op::SaveDatasetV2Op(OpKernelConstruction* ctx)
    : UnaryDatasetOpKernel(ctx) {
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kCompression, &compression_));
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kOutputTypes, &output_types_));
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kOutputShapes, &output_shapes_));
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kUseShardFunc, &use_shard_func_));
  OP_REQUIRES_OK(ctx, FunctionMetadata::Create(ctx, kShardFunc, /*params=*/{},
                                               &func_metadata_));
}

void SaveDatasetV2Op::MakeDataset(OpKernelContext* ctx, DatasetBase* input,
                                  DatasetBase** output) {
  DatasetBase* dataset;
  OP_REQUIRES_OK(ctx, GetDatasetFromVariantTensor(ctx->input(0), &dataset));

  tstring path;
  OP_REQUIRES_OK(ctx, ParseScalarArgument(ctx, kPath, &path));

  std::unique_ptr<CapturedFunction> shard_func;
  OP_REQUIRES_OK(
      ctx, CapturedFunction::Create(ctx, func_metadata_, kShardFuncOtherArgs,
                                    &shard_func));

  *output = new Dataset(ctx, dataset, path, compression_, std::move(shard_func),
                        use_shard_func_);
}

class LoadDatasetOp::Dataset : public DatasetBase {
 public:
  Dataset(OpKernelContext* ctx, const tstring& path,
          SnapshotMetadataRecord metadata, const std::string& compression,
          std::unique_ptr<CapturedFunction> captured_func,
          const DataTypeVector& output_types,
          const std::vector<PartialTensorShape>& output_shapes)
      : DatasetBase(DatasetContext(ctx)),
        captured_func_(std::move(captured_func)),
        compression_(compression),
        metadata_(std::move(metadata)),
        output_types_(output_types),
        output_shapes_(output_shapes),
        path_(path) {}

  std::unique_ptr<IteratorBase> MakeIteratorInternal(
      const string& prefix) const override {
    return absl::make_unique<Iterator>(Iterator::Params{
        this, name_utils::IteratorPrefix(kDatasetType, prefix)});
  }

  const DataTypeVector& output_dtypes() const override { return output_types_; }

  const std::vector<PartialTensorShape>& output_shapes() const override {
    return output_shapes_;
  }

  string DebugString() const override {
    return name_utils::DatasetDebugString(kDatasetType);
  }

  int64 Cardinality() const override { return metadata_.num_elements(); }

  Status CheckExternalState() const override {
    return captured_func_->CheckExternalState();
  }

  Status InputDatasets(std::vector<const DatasetBase*>* inputs) const override {
    inputs->clear();
    return Status::OK();
  }

 protected:
  Status AsGraphDefInternal(SerializationContext* ctx,
                            DatasetGraphDefBuilder* b,
                            Node** output) const override {
    Node* path_node = nullptr;
    TF_RETURN_IF_ERROR(b->AddScalar(path_, &path_node));

    std::vector<Node*> reader_func_other_args;
    DataTypeVector reader_func_other_args_types;
    TF_RETURN_IF_ERROR(captured_func_->AddToGraph(
        ctx, b, &reader_func_other_args, &reader_func_other_args_types));

    // Attr: compression
    AttrValue compression_attr;
    b->BuildAttrValue(compression_, &compression_attr);

    // Attr: reader_func
    AttrValue reader_func_attr;
    b->BuildAttrValue(captured_func_->func(), &reader_func_attr);

    AttrValue reader_func_arguments_types_attr;
    b->BuildAttrValue(reader_func_other_args_types,
                      &reader_func_arguments_types_attr);

    TF_RETURN_IF_ERROR(b->AddDataset(
        this, {std::make_pair(0, path_node)},         // Single tensor inputs.
        {std::make_pair(1, reader_func_other_args)},  // Tensor list inputs.
        {std::make_pair(kCompression, compression_attr),
         std::make_pair(kReaderFunc, reader_func_attr),
         std::make_pair(kReaderFuncTarguments,
                        reader_func_arguments_types_attr)},  // Attrs
        output));
    return Status::OK();
  }

 private:
  class Iterator : public DatasetIterator<Dataset> {
   public:
    explicit Iterator(const Params& params)
        : DatasetIterator<Dataset>(params) {}

    ~Iterator() override {
      if (input_) {
        input_->Unref();
      }
    }

    Status Initialize(IteratorContext* ctx) override {
      mutex_lock l(mu_);
      TF_RETURN_IF_ERROR(dataset()->captured_func_->Instantiate(
          ctx, &instantiated_captured_func_));
      TF_RETURN_IF_ERROR(InitializeInput(ctx));
      return input_->MakeIterator(ctx, this, prefix(), &input_impl_);
    }

    Status GetNextInternal(IteratorContext* ctx,
                           std::vector<Tensor>* out_tensors,
                           bool* end_of_sequence) override {
      mutex_lock l(mu_);
      return input_impl_->GetNext(ctx, out_tensors, end_of_sequence);
    }

   protected:
    std::shared_ptr<model::Node> CreateNode(
        IteratorContext* ctx, model::Node::Args args) const override {
      return model::MakeUnknownRatioNode(std::move(args));
    }

    Status SaveInternal(SerializationContext* ctx,
                        IteratorStateWriter* writer) override {
      mutex_lock l(mu_);
      return this->SaveInput(ctx, writer, input_impl_);
    }

    Status RestoreInternal(IteratorContext* ctx,
                           IteratorStateReader* reader) override {
      mutex_lock l(mu_);
      return this->RestoreInput(ctx, reader, input_impl_);
    }

   private:
    Status InitializeInput(IteratorContext* ctx)
        TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
      auto run_dir = snapshot_util::RunDirectory(dataset()->path_,
                                                 dataset()->metadata_.run_id());

      std::vector<std::string> snapshot_shard_dirs;
      TF_RETURN_IF_ERROR(ctx->env()->GetMatchingPaths(
          io::JoinPath(run_dir,
                       strings::Printf("%s%s", "*",
                                       snapshot_util::kShardDirectorySuffix)),
          &snapshot_shard_dirs));
      std::sort(snapshot_shard_dirs.begin(), snapshot_shard_dirs.end());

      DatasetBase* dataset_of_snapshot_files;
      TF_RETURN_IF_ERROR(snapshot_util::Reader::MakeNestedDataset(
          ctx->env(), snapshot_shard_dirs, dataset()->compression_,
          dataset()->metadata_.version(), dataset()->output_dtypes(),
          dataset()->output_shapes(), /*start_index=*/0,
          &dataset_of_snapshot_files));

      Tensor input_dataset_tensor(DT_VARIANT, TensorShape({}));
      TF_RETURN_IF_ERROR(StoreDatasetInVariantTensor(dataset_of_snapshot_files,
                                                     &input_dataset_tensor));

      std::vector<Tensor> reader_input;
      std::vector<Tensor> reader_output;
      reader_input.push_back(std::move(input_dataset_tensor));

      // NOTE: We intentionally ignore resource modeling outside GetNext().
      TF_RETURN_IF_ERROR(instantiated_captured_func_->Run(
          ctx, std::move(reader_input), &reader_output, /*node=*/nullptr));
      if (reader_output.size() != 1) {
        return errors::InvalidArgument(
            "reader_func returns more than one argument.");
      }
      TF_RETURN_IF_ERROR(
          GetDatasetFromVariantTensor(reader_output[0], &input_));
      // We need to take a reference here as we will use the input_ and
      // its iterator.
      input_->Ref();
      return Status::OK();
    }

    mutex mu_;
    DatasetBase* input_ TF_GUARDED_BY(mu_) = nullptr;
    std::unique_ptr<IteratorBase> input_impl_ TF_GUARDED_BY(mu_);
    std::unique_ptr<InstantiatedCapturedFunction> instantiated_captured_func_;
  };

  const std::unique_ptr<CapturedFunction> captured_func_;
  const std::string compression_;
  const SnapshotMetadataRecord metadata_;
  const DataTypeVector output_types_;
  const std::vector<PartialTensorShape> output_shapes_;
  const tstring path_;
};

LoadDatasetOp::LoadDatasetOp(OpKernelConstruction* ctx) : DatasetOpKernel(ctx) {
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kCompression, &compression_));
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kOutputTypes, &output_types_));
  OP_REQUIRES_OK(ctx, ctx->GetAttr(kOutputShapes, &output_shapes_));
  OP_REQUIRES_OK(ctx, FunctionMetadata::Create(ctx, kReaderFunc, /*params=*/{},
                                               &func_metadata_));
}

void LoadDatasetOp::MakeDataset(OpKernelContext* ctx, DatasetBase** output) {
  tstring path;
  OP_REQUIRES_OK(ctx, ParseScalarArgument(ctx, kPath, &path));

  std::unique_ptr<CapturedFunction> captured_func;
  OP_REQUIRES_OK(
      ctx, CapturedFunction::Create(ctx, func_metadata_, kReaderFuncOtherArgs,
                                    &captured_func));

  bool metadata_file_exists;
  experimental::SnapshotMetadataRecord metadata;
  OP_REQUIRES_OK(ctx, snapshot_util::ReadMetadataFile(
                          ctx->env(), path, &metadata, &metadata_file_exists));

  OP_REQUIRES(ctx, metadata_file_exists,
              errors::NotFound("Could not find metadata file."));

  *output =
      new Dataset(ctx, path, std::move(metadata), compression_,
                  std::move(captured_func), output_types_, output_shapes_);
}

namespace {
REGISTER_KERNEL_BUILDER(Name("SaveDataset").Device(DEVICE_CPU), SaveDatasetOp);
REGISTER_KERNEL_BUILDER(Name("SaveDatasetV2").Device(DEVICE_CPU),
                        SaveDatasetV2Op);
REGISTER_KERNEL_BUILDER(Name("LoadDataset").Device(DEVICE_CPU), LoadDatasetOp);
}  // namespace

}  // namespace experimental
}  // namespace data
}  // namespace tensorflow


text:
nubuilder
=========

Tools related to nuBuilder (https://www.nubuilder.net/).

nubuilder_dump_trap.py:
    Dump nuBuilder debug log (zzsys_trap).

redpajama_set_name: RedPajamaGithub


text:
[ File.dirname(__FILE__),
  File.join(File.dirname(__FILE__), "../lib")
].each { |path| $: << path unless $:.include?(path) }

require 'oflow'

class HelloWorld < ::OFlow::Actor
  def initialize(task, options)
    super
  end

  def perform(op, box)
    puts 'Hello World!'
  end

end # HelloWorld

def hello_flow(period)
    $oflow_env.flow('hello_world') { |f|
      f.task(:repeater, ::OFlow::Actors::Timer, repeat: 10, period: period) { |t|
        t.link(nil, :hello, nil)
      }
      f.task(:hello, HelloWorld)
    }
end

hello_flow(1.0)

if $0 == __FILE__
  $oflow_env.flush()
end


text:
from vmwarelib import inventory
from vmwarelib.actions import BaseAction

class GetVMMoid(BaseAction):

    def run(self, vm_names):
        results = {}

        vmlist = inventory.get_virtualmachines(self.si_content)

        for vm in vmlist.view:
            if vm_names:
                if vm.name in vm_names:
                    results[vm.name] = str(vm).split(':')[-1].replace("'", "")
            else:
                results[vm.name] = str(vm).split(':')[-1].replace("'", "")

        return results


text:
<?xml version='1.0' encoding='utf-8'?>
<resources>
    <string name="app_name">ionic-blankapp</string>
    <string name="launcher_name">@string/app_name</string>
    <string name="activity_name">@string/launcher_name</string>
</resources>


text:
#ifndef UIRequestEvent_h
#define UIRequestEvent_h

#include "EventTarget.h"
#include "UIEvent.h"

#if ENABLE(INDIE_UI)

namespace WebCore {

struct UIRequestEventInit : public UIEventInit {
    UIRequestEventInit();

    RefPtr<EventTarget> receiver;
};

class UIRequestEvent : public UIEvent {
public:
    static PassRefPtr<UIRequestEvent> create();
    static PassRefPtr<UIRequestEvent> create(const AtomicString& type, bool bubbles, bool cancelable, PassRefPtr<AbstractView>, int detail, PassRefPtr<EventTarget> receiver);

    static PassRefPtr<UIRequestEvent> create(const AtomicString& eventType, const UIRequestEventInit&);

    virtual ~UIRequestEvent();

    EventTarget* receiver() const { return m_receiver.get(); }

protected:
    UIRequestEvent(const AtomicString& type, bool bubbles, bool cancelable, PassRefPtr<AbstractView>, int detail, PassRefPtr<EventTarget> receiver);

    UIRequestEvent(const AtomicString& type, const UIRequestEventInit&);

    UIRequestEvent();

    const AtomicString& interfaceName() const OVERRIDE;

private:
    RefPtr<EventTarget> m_receiver;
};

class UIRequestEventDispatchMediator : public EventDispatchMediator {
public:
    static PassRefPtr<UIRequestEventDispatchMediator> create(PassRefPtr<UIRequestEvent> event)
    {
        return adoptRef(new UIRequestEventDispatchMediator(event));
    }

private:
    explicit UIRequestEventDispatchMediator(PassRefPtr<UIRequestEvent>);

    UIRequestEvent* event() const;

    virtual bool dispatchEvent(EventDispatcher*) const OVERRIDE;
};

} // namespace WebCore

#endif // ENABLE(INDIE_UI)

#endif // UIRequestEvent_h


text:
package nam.model.query;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.aries.util.NameUtil;

import nam.model.Project;
import nam.model.Query;
import nam.model.util.QueryUtil;
import nam.ui.design.AbstractDomainElementWizard;
import nam.ui.design.SelectionContext;

@SessionScoped
@Named("queryWizard")
@SuppressWarnings("serial")
public class QueryWizard extends AbstractDomainElementWizard<Query> implements Serializable {

	@Inject
	private QueryDataManager queryDataManager;

	@Inject
	private QueryPageManager queryPageManager;

	@Inject
	private QueryEventManager queryEventManager;

	@Inject
	private SelectionContext selectionContext;

	@Override
	public String getName() {
		return "Query";
	}

	@Override
	public String getUrlContext() {
		return queryPageManager.getQueryWizardPage();
	}

	@Override
	public void initialize(Query query) {
		setOrigin(getSelectionContext().getUrl());
		assignPages(queryPageManager.getSections());
		super.initialize(query);
	}

	@Override
	public boolean isBackEnabled() {
		return super.isBackEnabled();
	}

	@Override
	public boolean isNextEnabled() {
		return super.isNextEnabled();
	}

	@Override
	public boolean isFinishEnabled() {
		return super.isFinishEnabled();
	}

	@Override
	public String refresh() {
		String url = super.showPage();
		selectionContext.setUrl(url);
		queryPageManager.updateState();
		return url;
	}

	@Override
	public String first() {
		String url = super.first();
		queryPageManager.updateState();
		return url;
	}

	@Override
	public String back() {
		String url = super.back();
		queryPageManager.updateState();
		return url;
	}

	@Override
	public String next() {
		String url = super.next();
		queryPageManager.updateState();
		return url;
	}

	@Override
	public boolean isValid() {
		return super.isValid();
	}

	@Override
	public String finish() {
		Query query = getInstance();
		queryDataManager.saveQuery(query);
		queryEventManager.fireSavedEvent(query);
		String url = selectionContext.popOrigin();
		return url;
	}

	@Override
	public String cancel() {
		Query query = getInstance();
		//TODO take this out soon
		if (query == null)
			query = new Query();
		queryEventManager.fireCancelledEvent(query);
		String url = selectionContext.popOrigin();
		return url;
	}

	public String populateDefaultValues() {
		Query query = selectionContext.getSelection("query");
		String name = query.getName();
		if (StringUtils.isEmpty(name)) {
			display = getFromSession("display");
			display.setModule("queryWizard");
			display.error("Query name must be specified");
			return null;
		}

		Project project = selectionContext.getSelection("project");

		String nameCapped = NameUtil.capName(name);
		String nameUncapped = NameUtil.uncapName(name);
		return getUrl();
	}

}


text:
using BulletML;
using Microsoft.Xna.Framework;
using XmasHell.Physics.Collision;

namespace XmasHell.Entities.Bosses.DebugBoss
{
    internal class BossDebug : Boss
    {
        public BossDebug(XmasHell game, PositionDelegate playerPositionDelegate) :
            base(game, BossType.Debug, playerPositionDelegate)
        {
            // BulletML
            BulletPatternFiles.Add("BossDebug/pattern1");

            // Behaviours
            Behaviours.Add(new BossDebugBehaviour1(this));

            SpriterFilename = "Graphics/Sprites/Bosses/BossDebug/boss-debug";
        }

        protected override void LoadSpriterSprite()
        {
            base.LoadSpriterSprite();
        }

        protected override void InitializePhysics(bool setupPhysicsWorld = false)
        {
            base.InitializePhysics();

            Game.GameManager.CollisionWorld.AddBossHitBox(new SpriterCollisionCircle(this, "body.png", new Vector2(0f, 10f), 0.90f));
        }

        public override void Reset()
        {
            base.Reset();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        protected override void UpdateBehaviourIndex()
        {
            base.UpdateBehaviourIndex();
        }
    }
}


text:
// Copyright 2016 Twitter. All rights reserved.
//
// 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
//
//    http://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.

package org.apache.storm.topology;

import java.util.Map;

import org.apache.storm.task.TopologyContext;
import org.apache.storm.tuple.Tuple;

public interface IBasicBolt extends IComponent {
  @SuppressWarnings("rawtypes")
  void prepare(Map stormConf, TopologyContext context);

  /**
   * Process the input tuple and optionally emit new tuples based on the input tuple.
   * <p>
   * All acking is managed for you. Throw a FailedException if you want to fail the tuple.
   */
  void execute(Tuple input, BasicOutputCollector collector);

  void cleanup();
}


text:
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>
        <item name="android:windowLightStatusBar">true</item>
    </style>
    <!-- Button Style. -->
        <style name="ButtonStyle">
            <item name="android:layout_width">120dp</item>
            <item name="android:layout_height">120dp</item>

        </style>
    <!-- Question Style -->
        <style name="Question">
            <item name="android:layout_height">wrap_content</item>
            <item name="android:layout_width">wrap_content</item>
            <item name="android:textSize">18sp</item>
            <item name="android:textColor">@color/colorAccent</item>

        </style>
    <!-- Options -->
        <style name="option_mcq">

        </style>

</resources>


text:
elasticsearch-php
=================

[![Build Status](https://img.shields.io/travis/elastic/elasticsearch-php.svg?style=flat-square)](https://travis-ci.org/elastic/elasticsearch-php)

Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in PHP; because of this it tries to be opinion-free and very extendable.

To maintain consistency across all the low-level clients (Ruby, Python, etc.), clients accept simple associative arrays as parameters.  All parameters, from the URI to the document body, are defined in the associative array.

Features
--------

 - One-to-one mapping with REST API and other language clients
 - Configurable, automatic discovery of cluster nodes
 - Persistent, Keep-Alive connections (within the lifetime of the script)
 - Load balancing (with pluggable selection strategy) across all available nodes. Defaults to round-robin
 - Pluggable connection pools to offer different connection strategies
 - Generalized, pluggable architecture - most components can be replaced with your own custom class if specialized behavior is required
 - Option to use asynchronous future, which enables parallel execution of curl requests to multiple nodes

**Note:** If you want to use X-Pack API, you need to install an optional extension [elasticsearch/xpack](https://github.com/elastic/elasticsearch-x-pack-php).

Version Matrix
--------------

| Elasticsearch Version | Elasticsearch-PHP Branch |
| --------------------- | ------------------------ |
| >= 6.0                | 6.0                      |
| >= 5.0, < 6.0         | 5.0                      |
| >= 2.0, < 5.0         | 1.0 or 2.0               |
| >= 1.0, < 2.0         | 1.0 or 2.0               |
| <= 0.90.x             | 0.4                      |

 - If you are using Elasticsearch 6.0+ , use Elasticsearch-PHP 6.0 branch.
 - If you are using Elasticsearch 5.x , use Elasticsearch-PHP 5.0 branch.
 - If you are using Elasticsearch 1.x or 2.x, prefer using the Elasticsearch-PHP 2.0 branch.  The 1.0 branch is compatible however.
 - If you are using a version older than 1.0, you must install the `0.4` Elasticsearch-PHP branch. Since ES 0.90.x and below is now EOL, the corresponding `0.4` branch will not receive any more development or bugfixes.  Please upgrade.
 - You should never use Elasticsearch-PHP Master branch, as it tracks Elasticsearch master and may contain incomplete features or breaks in backwards compatibility. Only use ES-PHP master if you are developing against ES master for some reason.

Documentation
--------------
[Full documentation can be found here.](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/5.0/index.html)  Docs are stored within the repo under /docs/, so if you see a typo or problem, please submit a PR to fix it!

Installation via Composer
-------------------------
The recommended method to install _Elasticsearch-PHP_ is through [Composer](http://getcomposer.org).

1. Add `elasticsearch/elasticsearch` as a dependency in your project's `composer.json` file (change version to suit your version of Elasticsearch):

    ```json
        {
            "require": {
                "elasticsearch/elasticsearch": "~6.0"
            }
        }
    ```

2. Download and install Composer:

    ```bash
        curl -s http://getcomposer.org/installer | php
    ```

3. Install your dependencies:

    ```bash
        php composer.phar install
    ```

4. Require Composer's autoloader

    Composer also prepares an autoload file that's capable of autoloading all the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process:

    ```php
        <?php

        use Elasticsearch\ClientBuilder;

        require 'vendor/autoload.php';

        $client = ClientBuilder::create()->build();
    ```
You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at [getcomposer.org](http://getcomposer.org).

PHP Version Requirement
----
Version 6.0 of this library requires at least PHP version 7.0.0 to function.  In addition, it requires the native JSON
extension to be version 1.3.7 or higher.

| Elasticsearch-PHP Branch | PHP Version |
| ----------- | ------------------------ |
| 6.0         | >= 7.0.0                 |
| 5.0         | >= 5.6.6                 |
| 2.0         | >= 5.4.0                 |
| 0.4, 1.0    | >= 5.3.9                 |

Quickstart
----

### Index a document

In elasticsearch-php, almost everything is configured by associative arrays.  The REST endpoint, document and optional parameters - everything is an associative array.

To index a document, we need to specify four pieces of information: index, type, id and a document body. This is done by
constructing an associative array of key:value pairs.  The request body is itself an associative array with key:value pairs
corresponding to the data in your document:

```php
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];

$response = $client->index($params);
print_r($response);
```

The response that you get back indicates the document was created in the index that you specified.  The response is an
associative array containing a decoded version of the JSON that Elasticsearch returns:

```php
Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 1
    [created] => 1
)

```

### Get a document

Let's get the document that we just indexed.  This will simply return the document:

```php
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

$response = $client->get($params);
print_r($response);
```

The response contains some metadata (index, type, etc.) as well as a `_source` field...this is the original document
that you sent to Elasticsearch.

```php
Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 1
    [found] => 1
    [_source] => Array
        (
            [testField] => abc
        )

)
```

If you want to retrieve the `_source` field directly, there is the `getSource` method:

```php
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

$source = $client->getSource($params);
doSomething($source);
```

### Search for a document

Searching is a hallmark of Elasticsearch, so let's perform a search.  We are going to use the Match query as a demonstration:

```php
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);
```

The response is a little different from the previous responses.  We see some metadata (`took`, `timed_out`, etc.) and
an array named `hits`.  This represents your search results.  Inside of `hits` is another array named `hits`, which contains
individual search results:

```php
Array
(
    [took] => 1
    [timed_out] =>
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 0.30685282
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => my_index
                            [_type] => my_type
                            [_id] => my_id
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [testField] => abc
                                )
                        )
                )
        )
)
```

### Delete a document

Alright, let's go ahead and delete the document that we added previously:

```php
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

$response = $client->delete($params);
print_r($response);
```

You'll notice this is identical syntax to the `get` syntax.  The only difference is the operation: `delete` instead of
`get`.  The response will confirm the document was deleted:

```php
Array
(
    [found] => 1
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 2
)
```

### Delete an index

Due to the dynamic nature of Elasticsearch, the first document we added automatically built an index with some default settings.  Let's delete that index because we want to specify our own settings later:

```php
$deleteParams = [
    'index' => 'my_index'
];
$response = $client->indices()->delete($deleteParams);
print_r($response);
```

The response:

```php
Array
(
    [acknowledged] => 1
)
```

### Create an index

Now that we are starting fresh (no data or index), let's add a new index with some custom settings:

```php
$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 2,
            'number_of_replicas' => 0
        ]
    ]
];

$response = $client->indices()->create($params);
print_r($response);
```

Elasticsearch will now create that index with your chosen settings, and return an acknowledgement:

```php
Array
(
    [acknowledged] => 1
)
```

Unit Testing using Mock a Elastic Client
========================================
```php
use GuzzleHttp\Ring\Client\MockHandler;
use Elasticsearch\ClientBuilder;

// The connection class requires 'body' to be a file stream handle
// Depending on what kind of request you do, you may need to set more values here
$handler = new MockHandler([
  'status' => 200,
  'transfer_stats' => [
     'total_time' => 100
  ],
  'body' => fopen('somefile.json')
]);
$builder = ClientBuilder::create();
$builder->setHosts(['somehost']);
$builder->setHandler($handler);
$client = $builder->build();
// Do a request and you'll get back the 'body' response above
```

Wrap up
=======

That was just a crash-course overview of the client and its syntax.  If you are familiar with Elasticsearch, you'll notice that the methods are named just like REST endpoints.

You'll also notice that the client is configured in a manner that facilitates easy discovery via the IDE.  All core actions are available under the `$client` object (indexing, searching, getting, etc.).  Index and cluster management are located under the `$client->indices()` and `$client->cluster()` objects, respectively.

Check out the rest of the [Documentation](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/index.html) to see how the entire client works.

Available Licenses
-------

Starting with version 1.3.1, Elasticsearch-PHP is available under two licenses: Apache v2.0 and LGPL v2.1.  Versions
prior to 1.3.1 are still licensed with only Apache v2.0.

The user may choose which license they wish to use.  Since there is no discriminating executable or distribution bundle
to differentiate licensing, the user should document their license choice externally, in case the library is re-distributed.
If no explicit choice is made, assumption is that redistribution obeys rules of both licenses.

### Contributions
All contributions to the library are to be so that they can be licensed under both licenses.

Apache v2.0 License:
>Copyright 2013-2016 Elasticsearch
>
>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
>
>    http://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.

LGPL v2.1 Notice:
>Copyright (C) 2013-2016 Elasticsearch
>
>This library is free software; you can redistribute it and/or
>modify it under the terms of the GNU Lesser General Public
>License as published by the Free Software Foundation; either
>version 2.1 of the License, or (at your option) any later version.
>
>This library is distributed in the hope that it will be useful,
>but WITHOUT ANY WARRANTY; without even the implied warranty of
>MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>Lesser General Public License for more details.
>
>You should have received a copy of the GNU Lesser General Public
>License along with this library; if not, write to the Free Software
>Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


text:
package softlayer_product_upgrade_request

// DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED

import (
	slapi "github.com/sudorandom/softlayer-go/slapi"

	time "time"
)

// ApproveChanges - When a change is made to an upgrade by Sales, this method will approve the changes
// that were made. A customer must acknowledge the change and approve it so that the upgrade request
// can proceed.
func ApproveChanges(ID interface{}) slapi.Request {
	return slapi.Request{
		Service: "SoftLayer_Product_Upgrade_Request",
		Method:  "approveChanges",
		ID:      ID,
	}
}

// GetObject - getObject retrieves a SoftLayer_Product_Upgrade_Request object on your account whose ID
// corresponds to the ID of the init parameter passed to the SoftLayer_Product_Upgrade_Request service.
func GetObject(ID interface{}) slapi.Request {
	return slapi.Request{
		Service: "SoftLayer_Product_Upgrade_Request",
		Method:  "getObject",
		ID:      ID,
	}
}

// UpdateMaintenanceWindow - In case an upgrade cannot be performed, the maintenance window needs to be
// updated to a future date.
func UpdateMaintenanceWindow(ID interface{}, maintenanceStartTime time.Time, maintenanceWindowId int) slapi.Request {
	return slapi.Request{
		Service:    "SoftLayer_Product_Upgrade_Request",
		Method:     "updateMaintenanceWindow",
		ID:         ID,
		Parameters: []interface{}{maintenanceStartTime, maintenanceWindowId},
	}
}


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="60dp">

    <ImageButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:background="@drawable/btn_back"/>
    <TextView
        android:id="@+id/ab_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:text="Discovery Result"
        style="@style/myTitleText"/>
    <ImageButton
        android:id="@+id/device_reg_btn"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/btn_reg" />
</RelativeLayout>


text:
// TODO when we add the editor back, make sure this does what it's supposed to when the editor is open
// TODO what happens when the user selects another node without closing the panel?

import {
  concat,
  compact,
  forOwn,
  fromPairs,
  groupBy,
  isPlainObject,
  isBoolean,
  isString,
  isNumber,
  isArray,
  pick,
  remove,
  toPairs
} from "lodash";
import { BridgeDb } from "../../BridgeDb";
import * as React from "react";
import * as ReactDOM from "react-dom";
import * as Rx from "rxjs";
import {
  normalize,
  setupPage,
  fillParent,
  vertical,
  horizontal,
  flex,
  content,
  width
} from "csstips";
import { style } from "typestyle";
import * as styles from "./styles.style";

//import { Icon } from "react-icons-kit";
//import Icon from "react-icons-kit";
// Icon1 below is a weird kludge I'm forced to use, even though either of the two lines above should work.
// see https://github.com/wmira/react-icons-kit/blob/d61d7bc7337d8b68ad66b654bc4e02bf49d0bb26/index.d.ts#L13
import { Icon, IconProp } from "react-icons-kit";
const Icon1: React.Component<IconProp> & typeof Icon & any = Icon;

import { remove as iconRemove } from "react-icons-kit/fa/remove";
//import {ic_clear as iconRemove} from 'react-icons-kit/md/ic_clear';

let bridgeDb = new BridgeDb();

interface ListItemValueRaw {
  key: string;
  primary: boolean;
  text: string;
  uri?: string;
}

interface ListItemRaw {
  key: string;
  values: ListItemValueRaw[];
}

/**
 * @private
 * Used to create an HTML string for one or more xrefIdentifiers/links.
 * @typedef {Object} ListItemValue Set of xrefIdentifiers,
 *                      each with a linkout when available, such as for a specific Xref.
 * @property {String} text Displayed value, e.g., "WP1"
 * @property {String} [uri] (when available) Link to the main human-readable description/page
 *                          about that xrefIdentifier.
 *                          Sometimes called a "linkout.". See
 *                          {@link http://www.w3.org/2001/XMLSchema#anyURI|xsd:anyURI}.
 *                          Example: {@link http://wikipathways.org/index.php/Pathway:WP1}
 */

interface ListItemValue {
  text: string;
  uri?: string;
}

/**
 * @private
 * List element with one or more subelements, e.g., Xrefs for one dataset.
 * @typedef {Object} ListItem
 * @property {String} key Title of the list element, e.g., "WikiPathways"
 * @property {ListItemValue[]} values
 */

interface ListItem {
  key: string;
  values: ListItemValue[];
}

function getLinkout(entityReference) {
  const urlPattern = entityReference.isDataItemIn.hasPrimaryUriPattern;
  if (urlPattern) {
    return urlPattern.replace("$id", entityReference.xrefIdentifier);
  } else {
    return compact(concat([entityReference.id], entityReference.sameAs)).filter(
      function(uri) {
        return uri.indexOf("http") > -1;
      }
    )[0];
  }
}

function convertEntityReferenceToListItems(entityReference): [ListItem] {
  let listItemValue: ListItemValue = {
    text: entityReference.xrefIdentifier
  };

  const uri = getLinkout(entityReference);
  if (uri) {
    listItemValue.uri = uri;
  }

  return [
    {
      key: entityReference.isDataItemIn.conventionalName,
      values: [listItemValue]
    }
  ];
}

function addWikiPathwaysSearch(
  entityReference,
  listItems: ListItem[]
): ListItem[] {
  const displayName = entityReference.displayName;
  listItems.push({
    key: "Find other pathways containing",
    values: [
      {
        text: displayName,
        uri:
          "http://www.wikipathways.org/index.php?title=Special:SearchPathways&doSearch=1&query=" +
          displayName
      }
    ]
  });
  return listItems;
}

function isEqual(prev, current) {
  return (
    prev === current ||
    (isPlainObject(prev) &&
      isPlainObject(current) &&
      JSON.stringify(prev) !== JSON.stringify(current)) ||
    (isArray(prev) &&
      isArray(current) &&
      prev.reduce(function(acc, x, i) {
        return acc && isEqual(x, current[i]);
      }, true))
  );
}

export class XrefsAnnotationPanel extends React.Component<any, any> {
  xrefsRequest: Rx.Observable<any>;
  constructor(props) {
    super(props);
    this.state = {
      xrefs: [],
      ...props
    };
    normalize();
    // TODO doublecheck whether we should use setupPage for this
    //setupPage('body');
  }

  updateXrefs() {
    let that = this;
    let state = that.state;

    const primaryDataSource = state.dataSource;
    const primaryDbId = state.xrefIdentifier;
    let primaryEntityReference = {
      displayName: state.displayName,
      xrefIdentifier: primaryDbId,
      isDataItemIn: {
        conventionalName: primaryDataSource
      }
    };

    let xrefsRequest = (that.xrefsRequest = bridgeDb.xrefs(
      state.organism,
      state.dataSource,
      state.xrefIdentifier
    ));

    xrefsRequest
      .map(function(entityReference): ListItemValueRaw {
        const xrefIdentifier = entityReference.xrefIdentifier;
        let listItem: ListItemValueRaw = {
          key: entityReference.isDataItemIn.conventionalName,
          text: xrefIdentifier,
          primary: entityReference.isDataItemIn.primary
        };

        const uri = getLinkout(entityReference);
        if (uri) {
          listItem.uri = uri;
        }

        return listItem;
      })
      .toArray()
      .map(function(listItems): ListItem[] {
        // Here we handle case where BridgeDb webservice returns no reults or just one result.
        // Getting this number of results might mean the webservice is down.
        const listItemsCount = listItems.length;
        if (listItemsCount <= 1) {
          console.warn(
            "Received " +
              String(listItemsCount) +
              " results. Is webservice.bridgedb.org down?"
          );
          return convertEntityReferenceToListItems(primaryEntityReference);
        }

        // two-factor sort:
        //   1) by whether primary
        //   2) by key (e.g., the data source conventional name)
        listItems.sort(function(a, b) {
          // by primary
          if (a.primary === b.primary) {
            var x = a.key.toLowerCase();
            var y = b.key.toLowerCase();
            // by key
            return x < y ? -1 : x > y ? 1 : 0;
          } else if (b.primary) {
            return 1;
          } else {
            return -1;
          }
        });

        let sortedListItems: ListItemRaw[] = toPairs(
          groupBy(listItems, "key")
        ).reduce(function(acc, pair) {
          acc.push({
            key: pair[0],
            values: pair[1]
          });
          return acc;
        }, []);

        // Set the first item in the list to be the one with the dataSource/xrefIdentifier
        // that was specified for this data node by the pathway author.
        var primaryListItem = remove(sortedListItems, function(element) {
          return element.key === primaryDataSource;
        })[0];
        var primaryXRefId = remove(primaryListItem.values, function(element) {
          return element.text === primaryDbId;
        })[0];
        primaryListItem.values.unshift(primaryXRefId);
        sortedListItems.unshift(primaryListItem);

        return sortedListItems.map(function(formattedListItem) {
          return {
            key: formattedListItem.key,
            values: formattedListItem.values.map(
              v => pick(v, ["text", "uri"]) as ListItemValue
            )
          };
        });
      })
      .do(
        function(xrefs) {
          that.setState({
            xrefs: addWikiPathwaysSearch(primaryEntityReference, xrefs)
          });
        },
        function(err) {
          err.message = err.message || "";
          err.message +=
            " Error getting or formatting xrefs (is webservice.bridgedb.org down?)";
          console.error(err);

          // TODO use the info the user provided to show a minimal result
          that.setState({ xrefs: null });
        }
      )
      .subscribe(null, console.error);
  }

  componentDidMount() {
    let that = this;
    that.updateXrefs();
  }

  componentWillReceiveProps(nextProps) {
    let that = this;
    const state = that.state;
    const prevProps = that.props;

    /* TODO see note at componentDidUpdate
		const xrefsUpdateNeeded = nextProps.dataSource !== state.dataSource ||
			nextProps.xrefIdentifier !== state.xrefIdentifier;
		//*/

    /*
		const changedStatePairs = [];
		// TODO this is kludgy
		forOwn(nextProps, function(nextProp, key) {
			const prevPropValue = prevProps[key];
			if (isEqual(prevPropValue, nextProp)) {
				changedStatePairs.push([key, nextProp]);
			}
		});
	 	//*/
    const changedStatePairs = [
      "organism",
      "dataSource",
      "xrefIdentifier",
      "xrefs",
      "entityType",
      "displayName"
    ]
      .filter(k => prevProps[k] !== nextProps[k])
      .reduce(function(acc, k) {
        acc.push([k, nextProps[k]]);
        return acc;
      }, []);

    if (changedStatePairs.length > 0) {
      that.setState(fromPairs(changedStatePairs));
    }

    /* TODO see note at componentDidUpdate
		if (xrefsUpdateNeeded) {
			console.log('xrefs need to be updated');
			that.updateXrefs();
		}
	 	//*/
  }

  //*
  // TODO is this correct? Or should we use componentWillUpdate?
  // componentWillReceiveProps doesn't update when I click one node and then click another node without closing.
  componentDidUpdate(prevProps, prevState) {
    let that = this;
    let props = that.props;
    if (
      prevProps.dataSource !== props.dataSource ||
      prevProps.xrefIdentifier !== props.xrefIdentifier
    ) {
      that.updateXrefs();
    }
  }
  //*/

  componentWillUnmount() {
    let that = this;
    // TODO cancel any pending network requests, possibly something like this:
    //that.xrefsRequest.dispose();
  }

  render() {
    let that = this;
    let state = that.state;
    const { displayName, entityType, handleClose, xrefs } = state;
    return (
      <div className={`${styles.Annotation} ui-draggable`}>

          {/* TODO do we need so many divs here? */}

{displayName}

{entityType}

                <Icon1 className={styles.Close} icon={iconRemove} />

          {

              {xrefs.map(function(xref) {
                let values = xref.values;
                const valueCount = values.length;
                return (

                      {xref.key + ": "}

                    {values.map(function(v, i) {
                      const text = v.text;
                      const uri = v.uri;
                      const separator = i <= valueCount ? " " : "";

                      return (

                          {uri ? (

                              {text}

                          ) : (

                              {text}

                          )}
                          {separator}

                      );
                    })}

                );
              })}

          }

    );
  }
}


text:
// -----------------------------------------------------------------------
//  <copyright file="EntityMapperAssemblyFinder.cs" company="OSharp开源团队">
//      Copyright (c) 2014-2015 OSharp. All rights reserved.
//  </copyright>
//  <site>http://www.osharp.org</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2015-09-23 10:00</last-date>
// -----------------------------------------------------------------------

using System;
using System.Linq;
using System.Reflection;

using OSharp.Core.Reflection;

namespace OSharp.Data.Entity
{
    /// <summary>
    /// 实体映射程序集查找器
    /// </summary>
    public class EntityMapperAssemblyFinder : IEntityMapperAssemblyFinder
    {
        /// <summary>
        /// 获取或设置 所有程序集查找器
        /// </summary>
        public IAllAssemblyFinder AllAssemblyFinder { get; set; }

        /// <summary>
        /// 查找指定条件的项
        /// </summary>
        /// <param name="predicate">筛选条件</param>
        /// <returns></returns>
        public Assembly[] Find(Func<Assembly, bool> predicate)
        {
            return FindAll().Where(predicate).ToArray();
        }

        /// <summary>
        /// 查找所有项
        /// </summary>
        /// <returns></returns>
        public Assembly[] FindAll()
        {
            Type baseType = typeof(IEntityMapper);
            Assembly[] assemblies = AllAssemblyFinder.Find(assembly =>
                assembly.GetTypes().Any(type => baseType.IsAssignableFrom(type) && !type.IsAbstract));
            return assemblies;
        }
    }
}


text:
class AddConfidentialToDoorkeeperApplication < ActiveRecord::Migration[5.1]
  def change
    add_column(
      :oauth_applications,
      :confidential,
      :boolean,
      null: false,
      default: true # maintaining backwards compatibility: require secrets
    )
  end
end


text:
<!DOCTYPE html>

	<title>

            Gold Stock Corp -

    Complete profile - Canadian Company Capabilities - Industries and Business - Industry Canada
  </title>

<meta charset="utf-8" />
<meta name="dcterms.language" title="ISO639-2" content="eng" />
<meta name="dcterms.title" content="" />
<meta name="description" content="" />
<meta name="dcterms.description" content="" />
<meta name="dcterms.type" content="report, data set" />
<meta name="dcterms.subject" content="businesses, industry" />
<meta name="dcterms.subject" content="businesses, industry" />
<meta name="dcterms.issued" title="W3CDTF" content="" />
<meta name="dcterms.modified" title="W3CDTF" content="" />
<meta name="keywords" content="" />

<meta name="dcterms.creator" content="" />
<meta name="author" content="" />
<meta name="dcterms.created" title="W3CDTF" content="" />
<meta name="dcterms.publisher" content="" />
<meta name="dcterms.audience" title="icaudience" content="" />
<meta name="dcterms.spatial" title="ISO3166-1" content="" />
<meta name="dcterms.spatial" title="gcgeonames" content="" />
<meta name="dcterms.format" content="HTML" />
<meta name="dcterms.identifier" title="ICsiteProduct" content="536" />

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta content="width=device-width,initial-scale=1" name="viewport">

<meta name = "format-detection" content = "telephone=no">

<meta name="robots" content="noarchive">

<link href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/assets/favicon.ico" rel="icon" type="image/x-icon">
<link rel="stylesheet" href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/css/wet-boew.min.css">

<link rel="stylesheet" href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/css/theme.min.css">

<link rel="stylesheet" href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/css/noscript.min.css" />

<link rel="stylesheet" href="/eic/home.nsf/css/add_WET_4-0_Canada_Apps.css">

  	<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
  	<link href="/app/ccc/srch/css/print.css" media="print" rel="stylesheet" type="text/css" />

<meta property="legalName" content="Department_of_Industry">

Skip to main content

Skip to "About this site"

Language selection

Français

<object type="image/svg+xml" tabindex="-1" data="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/assets/sig-blk-en.svg"></object> Government of Canada

Search and menus

Search and menus

Topics menu

Jobs

Immigration

Travel

Business

Benefits

Health

Taxes

More services

You are here:

Home

Industries and Business

Canadian Company Capabilities

<input id="showMore" type="hidden" value='more'/>
<input id="showLess" type="hidden" value='less'/>

 Company profile - Canadian Company Capabilities

New Search |

<form name="searchForm" method="post" action="/app/ccc/srch/bscSrch.do">

          <input type="hidden" name="lang" value="eng" />
          <input type="hidden" name="profileId" value="" />
          <input type="hidden" name="prtl" value="1" />
          <input type="hidden" name="searchPage" value="%2Fapp%2Fccc%2Fsrch%2FcccBscSrch.do%3Flang%3Deng%26amp%3Bprtl%3D1%26amp%3Btagid%3D"  />

          <input type="hidden" name="V_SEARCH.scopeCategory" value="CCC.Root" />

          <input type="hidden" name="V_SEARCH.depth" value="1" />
          <input type="hidden" name="V_SEARCH.showStricts" value="false" />
          <input id="repeatSearchBtn" class="btn btn-link" type="submit" value="Return to search results" />

  </form>

| Previous Company

| Next Company

      Third-Party Information Liability Disclaimer

Some of the information on this Web page has been provided by external sources. The Government of Canada is not responsible for the accuracy, reliability or currency of the information supplied by external sources. Users wishing to rely upon this information should consult directly with the source of the information. Content provided by external sources is not subject to official languages, privacy and accessibility requirements.

      Gold Stock Corp

Legal/Operating Name:

Gold Stock Corp

https://goldstocklive.com/

info@goldstocklive.com

Mailing Address:

              	<address class="mrgn-bttm-md">

                   302-21 Dundas Square

                    TORONTO,

                    Ontario

                  M5B 1B7

                </address>

Location Address:

              	         <address class="mrgn-bttm-md">

                   302-21 Dundas Square

                    TORONTO,

                    Ontario

                  M5B 1B7

                         </address>

Tel.:
		      (416) 214-4526

Tel.:
			      (888) 944-6539

Fax:

Company Profile

            With over 100 years of combined experience, GOLDSTOCK remains a market leader in both Canada and throughout the world.Distributing to corporations, various businesses and private clients, GOLDSTOCK offers an affordable, safe and secure investment alternative to volatile cash, stock and other equity markets. Gold has long been considered the ultimate investment, from ancient pharaohs to modern leading global economists, it's your safe investment. As a market leader GOLD STOCK actually deals in all precious metals. We offer our customers every imaginable purchasing and selling option available in today's market. Be it platinum, gold or silver, bullion, coins, jewelry, even fillings, it's our intention to be a leader in the industry. From large corporations, to the average consumer, we treat everyone like gold.

 Technology

"We understand the importance of precision in such a number-oriented industry"  - Gold Stock

To ensure our process is of the highest standard, we use advanced technologies, to both refine and examine any gold-made content. From the smelting of scrap jewelry to the examination of the purity level in said content, our technology is what sets us apart. It is what allows us to pay our clients at the highest market standard on the gold brought to us.

Corporate Policy

Our Values Leadership: At Gold Stock, you receive the kind of quality and service you expect from a leader. Our company is always evolving as the needs or our customers change and as new opportunities are created in the market. You can rest assured that, working with Gold Stock, you will have the latest services, technology and developments in the industry.

Teamwork

We make it our responsibility to know you and your business. We work closely with you to ensure that the solutions we provide are tailored to meet your unique needs and challenges. We are committed to your success.

Customer Relations: At Gold Stock, our highest priority is satisfied customers. You are important to us and you can expect us to go the extra mile for your business. Superior customer service is the hallmark of Gold Sock, We are proud to serve you and work hard to earn your business Call today for your quote. Be it a large bulk order or a small consumer investment, we are more than happy to satisfy any order, securely.

GOLD STOCK, For Centuries, For Your Future.

				Full profile

				Full profile

				Contact information

                Amir
                Shingray

                  Title:

                Manager

                  Telephone:

                (905) 212-9211

                    Email:

                  amir@goldstocklive.com

  top of page

				Company description

            Exporting:

              No

                Primary Industry (NAICS):

				418920 - Mineral, Ore and Precious Metal Wholesaler-Distributors

            Primary Business Activity:

            Trading House / Wholesaler / Agent and Distributor

				Product / Service / Licensing

                              Product Name:

                             Gold, Silver and Platinum Bullion and Coins Wholesalers

                            Gold Stock Corp is re-seller and licensed distributor to some of the world most trusted mints such as The Royal Canadian Mint and Australian Perth Mint. With over 100 years of combined experience, GOLDSTOCK remains a market leader in both Canada and throughout the world. Distributing to corporations, various businesses and private clients, GOLDSTOCK offers an affordable, safe and secure investment alternative to volatile cash, stock and other equity markets. Gold has long been considered the ultimate investment, from ancient pharaohs to modern leading global economists, it's your safe investment.

  top of page

					Third-Party Information Liability Disclaimer

					Some of the information on this Web page has been provided by external sources. The Government of Canada is not responsible for the accuracy, reliability or currency of the information supplied by external sources. Users wishing to rely upon this information should consult directly with the source of the information. Content provided by external sources is not subject to official languages, privacy and accessibility requirements.

				Contacts

				Contact information

                Amir
                Shingray

                  Title:

                Manager

                  Telephone:

                (905) 212-9211

                    Email:

                  amir@goldstocklive.com

				Description

				Company description

            Exporting:

              No

                Primary Industry (NAICS):

				418920 - Mineral, Ore and Precious Metal Wholesaler-Distributors

            Primary Business Activity:

            Trading House / Wholesaler / Agent and Distributor

				Products, services and licensing

				Product / Service / Licensing

                              Product Name:

                             Gold, Silver and Platinum Bullion and Coins Wholesalers

                            Gold Stock Corp is re-seller and licensed distributor to some of the world most trusted mints such as The Royal Canadian Mint and Australian Perth Mint. With over 100 years of combined experience, GOLDSTOCK remains a market leader in both Canada and throughout the world. Distributing to corporations, various businesses and private clients, GOLDSTOCK offers an affordable, safe and secure investment alternative to volatile cash, stock and other equity markets. Gold has long been considered the ultimate investment, from ancient pharaohs to modern leading global economists, it's your safe investment.

    Last Update Date 2017-01-13

Date Modified:

      <time>2017-03-02</time>

About government

Contact us

Departments and agencies

Public service and military

News

Treaties, laws and regulations

Government-wide reporting

Prime Minister

How government works

Open government

About this site

Social media

Mobile applications

About Canada.ca

Terms and conditions

Privacy

Top of Page

<object type="image/svg+xml" tabindex="-1" role="img" data="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/assets/wmms-blk.svg" aria-label="Symbol of the Government of Canada"></object>


text:
DealerApp
=========

Web service to publish discounted stuff


text:
package org.silverbullit.auditlog;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToOne;

import org.silverbullit.auditlog.annotation.Auditable;

@Entity(name = "Person")
public class Person extends BaseEntity {

	private static final long serialVersionUID = 1L;

	@Auditable
	@Column
	private String firstname;

	@Column
	@Auditable
	private String lastname;

	@Auditable
	@OneToOne(cascade = { CascadeType.ALL })
	private Address address;

	public Address getAddress() {
		return this.address;
	}

	public String getFirstname() {
		return this.firstname;
	}

	public long getId() {
		return this.id;
	}

	public String getLastname() {
		return this.lastname;
	}

	public void setAddress(final Address address) {
		this.address = address;
	}

	public void setFirstname(final String firstname) {
		this.firstname = firstname;
	}

	public void setLastname(final String lastname) {
		this.lastname = lastname;
	}
}


text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cosmos</groupId>
    <artifactId>cosmos-project</artifactId>
    <version>0.3.0-SNAPSHOT</version>
  </parent>
  <artifactId>cosmos-sql-console</artifactId>
  <name>SQL Console</name>
  <!-- Some "reasonable" defaults" -->
  <properties>
	<zookeepers>localhost:2181</zookeepers>
	<instanceName>accumulo</instanceName>
	<username>root</username>
	<password>password</password>
  </properties>
  <dependencies>
    <dependency>
      <groupId>cosmos</groupId>
      <artifactId>cosmos-core</artifactId>
    </dependency>
    <dependency>
      <groupId>cosmos</groupId>
      <artifactId>cosmos-test</artifactId>
    </dependency>
    <dependency>
      <groupId>cosmos</groupId>
      <artifactId>cosmos-sql</artifactId>
    </dependency>
    <dependency>
      <groupId>cosmos</groupId>
      <artifactId>cosmos-test-data</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.accumulo</groupId>
      <artifactId>accumulo-minicluster</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
    </dependency>
    <dependency>
      <groupId>net.hydromatic</groupId>
      <artifactId>optiq</artifactId>
    </dependency>
    <dependency>
      <groupId>net.hydromatic</groupId>
      <artifactId>linq4j</artifactId>
    </dependency>
    <dependency>
      <groupId>eigenbase</groupId>
      <artifactId>eigenbase-properties</artifactId>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
      <groupId>jline</groupId>
      <artifactId>jline</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
    </dependency>
    <dependency>
      <groupId>com.beust</groupId>
      <artifactId>jcommander</artifactId>
    </dependency>
  </dependencies>
  <profiles>
    <profile>
      <id>mac-shell</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
              <executable>java</executable>
              <arguments>
                <argument>-classpath</argument>
                <classpath></classpath>
                <argument>cosmos.sql.console.CosmosConsole</argument>
				<argument>-m</argument>
				<argument>-l</argument>
              </arguments>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>shell</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
              <executable>java</executable>
              <arguments>
                <argument>-classpath</argument>
                <classpath></classpath>
                <argument>cosmos.sql.console.CosmosConsole</argument>
                <argument>-z</argument>
                <argument>${zookeepers}</argument>
                <argument>-i</argument>
                <argument>${instanceName}</argument>
                <argument>-u</argument>
                <argument>${username}</argument>
                <argument>-p</argument>
                <argument>${password}</argument>
                <argument>-r</argument>
              </arguments>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>shell-load-data</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
              <executable>java</executable>
              <arguments>
                <argument>-classpath</argument>
                <classpath></classpath>
                <argument>cosmos.sql.console.CosmosConsole</argument>
                <argument>-z</argument>
                <argument>${zookeepers}</argument>
                <argument>-i</argument>
                <argument>${instanceName}</argument>
                <argument>-u</argument>
                <argument>${username}</argument>
                <argument>-p</argument>
                <argument>${password}</argument>
                <argument>-r</argument>
                <argument>-l</argument>
              </arguments>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>


text:
// Prefab.cpp

/*
Implements the cPrefab class, representing a cPiece descendant for the cPieceGenerator that
uses a prefabricate in a cBlockArea for drawing itself.
*/

#include "Globals.h"
#include "Prefab.h"
#include "../WorldStorage/SchematicFileSerializer.h"
#include "ChunkDesc.h"

cPrefab::cPrefab(const cPrefab::sDef & a_Def) :
	m_Size(a_Def.m_SizeX, a_Def.m_SizeY, a_Def.m_SizeZ),
	m_HitBox(
		{a_Def.m_HitboxMinX, a_Def.m_HitboxMinY, a_Def.m_HitboxMinZ},
		{a_Def.m_HitboxMaxX, a_Def.m_HitboxMaxY, a_Def.m_HitboxMaxZ}
	),
	m_AllowedRotations(a_Def.m_AllowedRotations),
	m_MergeStrategy(a_Def.m_MergeStrategy),
	m_ExtendFloorStrategy(a_Def.m_ExtendFloorStrategy),
	m_DefaultWeight(a_Def.m_DefaultWeight),
	m_AddWeightIfSame(a_Def.m_AddWeightIfSame),
	m_MoveToGround(a_Def.m_MoveToGround)
{
	m_BlockArea[0].Create(m_Size);
	CharMap cm;
	ParseCharMap(cm, a_Def.m_CharMap);
	ParseBlockImage(cm, a_Def.m_Image);
	ParseConnectors(a_Def.m_Connectors);
	ParseDepthWeight(a_Def.m_DepthWeight);

	AddRotatedBlockAreas();
}

cPrefab::cPrefab(const cBlockArea & a_Image, int a_AllowedRotations) :
	m_Size(a_Image.GetSize()),
	m_AllowedRotations(a_AllowedRotations),
	m_MergeStrategy(cBlockArea::msOverwrite),
	m_ExtendFloorStrategy(efsNone),
	m_DefaultWeight(1),
	m_AddWeightIfSame(0),
	m_MoveToGround(false)
{
	m_HitBox.p1.Set(0, 0, 0);
	m_HitBox.p2.Set(m_Size.x - 1, m_Size.y - 1, m_Size.z - 1);
	m_BlockArea[0].CopyFrom(a_Image);
	AddRotatedBlockAreas();
}

cPrefab::cPrefab(const cBlockArea & a_Image) :
	m_Size(a_Image.GetSize()),
	m_AllowedRotations(0),
	m_MergeStrategy(cBlockArea::msOverwrite),
	m_ExtendFloorStrategy(efsNone),
	m_DefaultWeight(1),
	m_AddWeightIfSame(0),
	m_MoveToGround(false)
{
	m_HitBox.p1.Set(0, 0, 0);
	m_HitBox.p2.Set(m_Size.x - 1, m_Size.y - 1, m_Size.z - 1);
	m_BlockArea[0].CopyFrom(a_Image);
}

cPrefab::cPrefab(const AString & a_BlockDefinitions, const AString & a_BlockData, int a_SizeX, int a_SizeY, int a_SizeZ) :
	m_Size(a_SizeX, a_SizeY, a_SizeZ),
	m_AllowedRotations(0),
	m_MergeStrategy(cBlockArea::msOverwrite),
	m_ExtendFloorStrategy(efsNone),
	m_DefaultWeight(1),
	m_AddWeightIfSame(0),
	m_MoveToGround(false)
{
	m_HitBox.p1.Set(0, 0, 0);
	m_HitBox.p2.Set(m_Size.x - 1, m_Size.y - 1, m_Size.z - 1);
	m_BlockArea[0].Create(m_Size);
	CharMap cm;
	ParseCharMap(cm, a_BlockDefinitions.c_str());
	ParseBlockImage(cm, a_BlockData.c_str());
}

void cPrefab::AddRotatedBlockAreas(void)
{
	// 1 CCW rotation:
	if ((m_AllowedRotations & 0x01) != 0)
	{
		m_BlockArea[1].CopyFrom(m_BlockArea[0]);
		m_BlockArea[1].RotateCCW();
	}

	// 2 rotations are the same as mirroring twice; mirroring is faster because it has no reallocations
	if ((m_AllowedRotations & 0x02) != 0)
	{
		m_BlockArea[2].CopyFrom(m_BlockArea[0]);
		m_BlockArea[2].MirrorXY();
		m_BlockArea[2].MirrorYZ();
	}

	// 3 CCW rotations = 1 CW rotation:
	if ((m_AllowedRotations & 0x04) != 0)
	{
		m_BlockArea[3].CopyFrom(m_BlockArea[0]);
		m_BlockArea[3].RotateCW();
	}
}

void cPrefab::Draw(cChunkDesc & a_Dest, const cPlacedPiece * a_Placement) const
{
	Draw(a_Dest, a_Placement->GetCoords(), a_Placement->GetNumCCWRotations());
}

void cPrefab::Draw(cChunkDesc & a_Dest, const Vector3i & a_Placement, int a_NumRotations) const
{
	// Draw the basic image:
	Vector3i Placement(a_Placement);
	int ChunkStartX = a_Dest.GetChunkX() * cChunkDef::Width;
	int ChunkStartZ = a_Dest.GetChunkZ() * cChunkDef::Width;
	Placement.Move(-ChunkStartX, 0, -ChunkStartZ);
	const cBlockArea & Image = m_BlockArea[a_NumRotations];

	// If the placement is outside this chunk, bail out:
	if (
		(Placement.x > cChunkDef::Width) || (Placement.x + Image.GetSizeX() < 0) ||
		(Placement.z > cChunkDef::Width) || (Placement.z + Image.GetSizeZ() < 0)
	)
	{
		return;
	}

	// Write the image:
	a_Dest.WriteBlockArea(Image, Placement.x, Placement.y, Placement.z, m_MergeStrategy);

	// If requested, draw the floor (from the bottom of the prefab down to the nearest non-air)
	switch (m_ExtendFloorStrategy)
	{
		case efsNone: break;  // Nothing needed
		case efsRepeatBottomTillNonAir:
		{
			int MaxX = Image.GetSizeX();
			int MaxZ = Image.GetSizeZ();
			for (int z = 0; z < MaxZ; z++)
			{
				int RelZ = Placement.z + z;
				if ((RelZ < 0) || (RelZ >= cChunkDef::Width))
				{
					// Z coord outside the chunk
					continue;
				}
				for (int x = 0; x < MaxX; x++)
				{
					int RelX = Placement.x + x;
					if ((RelX < 0) || (RelX >= cChunkDef::Width))
					{
						// X coord outside the chunk
						continue;
					}
					BLOCKTYPE BlockType;
					NIBBLETYPE BlockMeta;
					Image.GetRelBlockTypeMeta(x, 0, z, BlockType, BlockMeta);
					if ((BlockType == E_BLOCK_AIR) || (BlockType == E_BLOCK_SPONGE))
					{
						// Do not expand air nor sponge blocks
						continue;
					}
					for (int y = Placement.y - 1; y >= 0; y--)
					{
						BLOCKTYPE ExistingBlock = a_Dest.GetBlockType(RelX, y, RelZ);
						if (ExistingBlock != E_BLOCK_AIR)
						{
							// End the expansion for this column, reached the end
							break;
						}
						a_Dest.SetBlockTypeMeta(RelX, y, RelZ, BlockType, BlockMeta);
					}  // for y
				}  // for x
			}  // for z
			break;
		}  // efsRepeatBottomTillNonAir

		case efsRepeatBottomTillSolid:
		{
			int MaxX = Image.GetSizeX();
			int MaxZ = Image.GetSizeZ();
			for (int z = 0; z < MaxZ; z++)
			{
				int RelZ = Placement.z + z;
				if ((RelZ < 0) || (RelZ >= cChunkDef::Width))
				{
					// Z coord outside the chunk
					continue;
				}
				for (int x = 0; x < MaxX; x++)
				{
					int RelX = Placement.x + x;
					if ((RelX < 0) || (RelX >= cChunkDef::Width))
					{
						// X coord outside the chunk
						continue;
					}
					BLOCKTYPE BlockType;
					NIBBLETYPE BlockMeta;
					Image.GetRelBlockTypeMeta(x, 0, z, BlockType, BlockMeta);
					if ((BlockType == E_BLOCK_AIR) || (BlockType == E_BLOCK_SPONGE))
					{
						// Do not expand air nor sponge blocks
						continue;
					}
					for (int y = Placement.y - 1; y >= 0; y--)
					{
						BLOCKTYPE ExistingBlock = a_Dest.GetBlockType(RelX, y, RelZ);
						if (cBlockInfo::IsSolid(ExistingBlock))
						{
							// End the expansion for this column, reached the end
							break;
						}
						a_Dest.SetBlockTypeMeta(RelX, y, RelZ, BlockType, BlockMeta);
					}  // for y
				}  // for x
			}  // for z
			break;
		}  // efsRepeatBottomTillSolid
	}
}

bool cPrefab::HasConnectorType(int a_ConnectorType) const
{
	for (cConnectors::const_iterator itr = m_Connectors.begin(), end = m_Connectors.end(); itr != end; ++itr)
	{
		if (itr->m_Type == a_ConnectorType)
		{
			return true;
		}
	}  // for itr - m_Connectors[]
	return false;
}

int cPrefab::GetPieceWeight(const cPlacedPiece & a_PlacedPiece, const cPiece::cConnector & a_ExistingConnector) const
{
	// Use the default or per-depth weight:
	cDepthWeight::const_iterator itr = m_DepthWeight.find(a_PlacedPiece.GetDepth() + 1);
	int res = (itr == m_DepthWeight.end()) ? m_DefaultWeight : itr->second;

	// If the piece is the same as the parent, apply the m_AddWeightIfSame modifier:
	const cPiece * ParentPiece = &a_PlacedPiece.GetPiece();
	const cPiece * ThisPiece = this;
	if (ThisPiece == ParentPiece)
	{
		res += m_AddWeightIfSame;
	}
	return res;
}

void cPrefab::SetDefaultWeight(int a_DefaultWeight)
{
	m_DefaultWeight = a_DefaultWeight;
}

void cPrefab::AddConnector(int a_RelX, int a_RelY, int a_RelZ, cPiece::cConnector::eDirection a_Direction, int a_Type)
{
	m_Connectors.push_back(cConnector(a_RelX, a_RelY, a_RelZ, a_Type, a_Direction));
}

void cPrefab::SetAllowedRotations(int a_AllowedRotations)
{
	m_AllowedRotations = a_AllowedRotations;
	AddRotatedBlockAreas();
}

void cPrefab::ParseCharMap(CharMap & a_CharMapOut, const char * a_CharMapDef)
{
	ASSERT(a_CharMapDef != nullptr);

	// Initialize the charmap to all-invalid values:
	for (size_t i = 0; i < ARRAYCOUNT(a_CharMapOut); i++)
	{
		a_CharMapOut[i].m_BlockType = 0;
		a_CharMapOut[i].m_BlockMeta = 16;  // Mark unassigned entries with a meta that is impossible otherwise
	}

	// Process the lines in the definition:
	AStringVector Lines = StringSplitAndTrim(a_CharMapDef, "\n");
	for (AStringVector::const_iterator itr = Lines.begin(), end = Lines.end(); itr != end; ++itr)
	{
		AStringVector CharDef = StringSplitAndTrim(*itr, ":");
		size_t NumElements = CharDef.size();
		if ((NumElements < 2) || CharDef[0].empty() || CharDef[1].empty())
		{
			LOGWARNING("Bad prefab CharMap definition line: \"%s\", skipping.", itr->c_str());
			continue;
		}
		unsigned char Src = static_cast<unsigned char>(CharDef[0][0]);
		ASSERT(a_CharMapOut[Src].m_BlockMeta == 16);  // This letter has not been assigned yet?
		a_CharMapOut[Src].m_BlockType = static_cast<BLOCKTYPE>(atoi(CharDef[1].c_str()));
		NIBBLETYPE BlockMeta = 0;
		if ((NumElements >= 3) && !CharDef[2].empty())
		{
			BlockMeta = static_cast<NIBBLETYPE>(atoi(CharDef[2].c_str()));
			ASSERT((BlockMeta <= 15));
		}
		a_CharMapOut[Src].m_BlockMeta = BlockMeta;
	}  // for itr - Lines[]
}

void cPrefab::ParseBlockImage(const CharMap & a_CharMap, const char * a_BlockImage)
{
	// Map each letter in the a_BlockImage (from the in-source definition) to real blocktype / blockmeta:
	for (int y = 0; y < m_Size.y; y++)
	{
		for (int z = 0; z < m_Size.z; z++)
		{
			const unsigned char * BlockImage = reinterpret_cast<const unsigned char *>(a_BlockImage + y * m_Size.x * m_Size.z + z * m_Size.x);
			for (int x = 0; x < m_Size.x; x++)
			{
				const sBlockTypeDef & MappedValue = a_CharMap[BlockImage[x]];
				ASSERT(MappedValue.m_BlockMeta != 16);  // Using a letter not defined in the CharMap?
				m_BlockArea[0].SetRelBlockTypeMeta(x, y, z, MappedValue.m_BlockType, MappedValue.m_BlockMeta);
			}
		}
	}
}

void cPrefab::ParseConnectors(const char * a_ConnectorsDef)
{
	ASSERT(a_ConnectorsDef != nullptr);

	AStringVector Lines = StringSplitAndTrim(a_ConnectorsDef, "\n");
	for (AStringVector::const_iterator itr = Lines.begin(), end = Lines.end(); itr != end; ++itr)
	{
		if (itr->empty())
		{
			continue;
		}
		// Split into components: "Type: X, Y, Z: Direction":
		AStringVector Defs = StringSplitAndTrim(*itr, ":");
		if (Defs.size() != 3)
		{
			LOGWARNING("Bad prefab Connector definition line: \"%s\", skipping.", itr->c_str());
			continue;
		}
		AStringVector Coords = StringSplitAndTrim(Defs[1], ",");
		if (Coords.size() != 3)
		{
			LOGWARNING("Bad prefab Connector coords definition: \"%s\", skipping.", Defs[1].c_str());
			continue;
		}

		// Check that the Direction is valid:
		cPiece::cConnector::eDirection Direction;
		if (!cPiece::cConnector::StringToDirection(Defs[2], Direction))
		{
			LOGWARNING("Bad prefab Connector direction: \"%s\", skipping.", Defs[2].c_str());
			continue;
		}

		// Add the connector:
		m_Connectors.push_back(cPiece::cConnector(
			atoi(Coords[0].c_str()), atoi(Coords[1].c_str()), atoi(Coords[2].c_str()),  // Connector pos
			atoi(Defs[0].c_str()),  // Connector type
			Direction
		));
	}  // for itr - Lines[]
}

void cPrefab::ParseDepthWeight(const char * a_DepthWeightDef)
{
	// The member needn't be defined at all, if so, skip:
	if (a_DepthWeightDef == nullptr)
	{
		return;
	}

	// Split into individual records: "Record | Record | Record"
	AStringVector Defs = StringSplitAndTrim(a_DepthWeightDef, "|");

	// Add each record's contents:
	for (AStringVector::const_iterator itr = Defs.begin(), end = Defs.end(); itr != end; ++itr)
	{
		// Split into components: "Depth : Weight"
		AStringVector Components = StringSplitAndTrim(*itr, ":");
		if (Components.size() != 2)
		{
			LOGWARNING("Bad prefab DepthWeight record: \"%s\", skipping.", itr->c_str());
			continue;
		}

		// Parse depth:
		int Depth = atoi(Components[0].c_str());
		if ((Depth == 0) && (Components[0] != "0"))
		{
			LOGWARNING("Bad prefab DepthWeight record, cannot parse depth \"%s\", skipping.", Components[0].c_str());
			continue;
		}

		// Parse weight:
		int Weight = atoi(Components[1].c_str());
		if ((Weight == 0) && (Components[1] != "0"))
		{
			LOGWARNING("Bad prefab DepthWeight record, cannot parse weight \"%s\", skipping.", Components[1].c_str());
			continue;
		}

		// Save to map:
		ASSERT(m_DepthWeight.find(Depth) == m_DepthWeight.end());  // Not a duplicate
		m_DepthWeight[Depth] = Weight;
	}  // for itr - Defs[]
}

cPiece::cConnectors cPrefab::GetConnectors(void) const
{
	return m_Connectors;
}

Vector3i cPrefab::GetSize(void) const
{
	return m_Size;
}

cCuboid cPrefab::GetHitBox(void) const
{
	return m_HitBox;
}

bool cPrefab::CanRotateCCW(int a_NumRotations) const
{
	// Either zero rotations
	// Or the proper bit in m_AllowedRotations is set
	return (a_NumRotations == 0) || ((m_AllowedRotations & (1 << ((a_NumRotations + 3) % 4))) != 0);
}