Repository: apache/arrow
File: cpp/src/arrow/util/compression_zstd.cc (C++)

Code before:
                                   static_cast<size_t>(input_len), compression_level_);
    if (ZSTD_isError(ret)) {
      return ZSTDError(ret, "ZSTD compression failed: ");
    }
    return static_cast<int64_t>(ret);
  }

  Result<std::shared_ptr<Compressor>> MakeCompressor() override {
    ARROW_ASSIGN_OR_RAISE(auto cctx, createCCtx());
    return std::make_shared<ZSTDCompressor>(std::move(cctx));
  }

  Result<std::shared_ptr<Decompressor>> MakeDecompressor() override {
    ARROW_ASSIGN_OR_RAISE(auto dctx, createDCtx());
    return std::make_shared<ZSTDDecompressor>(std::move(dctx));
  }

  Compression::type compression_type() const override { return Compression::ZSTD; }
  int minimum_compression_level() const override { return ZSTD_minCLevel(); }
  int maximum_compression_level() const override { return ZSTD_maxCLevel(); }
  int default_compression_level() const override { return kZSTDDefaultCompressionLevel; }

  int compression_level() const override { return compression_level_; }

 private:
  Result<CCtxPtr> createCCtx() const {
    auto cctx = ZSTD_createCCtx();
    auto ret = ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compression_level_);
    if (ZSTD_isError(ret)) {
      return ZSTDError(ret, "ZSTD_CCtx create failed: ");
    }
    for (auto& [key, value] : cctx_params_) {
      ret = ZSTD_CCtx_setParameter(cctx, static_cast<ZSTD_cParameter>(key), value);
      if (ZSTD_isError(ret)) {
        return ZSTDError(ret, "ZSTD_CCtx create failed: ");
      }
    }
    return {{cctx, ZSTD_freeCCtx}};
  }

  Result<DCtxPtr> createDCtx() const {
    auto dctx = ZSTD_createDCtx();
    for (auto& [key, value] : dctx_params_) {
      auto ret = ZSTD_DCtx_setParameter(dctx, static_cast<ZSTD_dParameter>(key), value);
      if (ZSTD_isError(ret)) {
        return ZSTDError(ret, "ZSTD_DCtx create failed: ");
      }
    }
    return {{dctx, ZSTD_freeDCtx}};
  }


Review comment (suggestion):
Let's use CamelCase as the rest of the codebase.
```suggestion
  Result<CCtxPtr> CreateCCtx() const {
```

Code after:
    size_t ret =
        ZSTD_compress2(cctx.get(), output_buffer, static_cast<size_t>(output_buffer_len),
                       input, static_cast<size_t>(input_len));
    if (ZSTD_isError(ret)) {
      return ZSTDError(ret, "ZSTD compression failed: ");
    }
    return static_cast<int64_t>(ret);
  }

  Result<std::shared_ptr<Compressor>> MakeCompressor() override {
    ARROW_ASSIGN_OR_RAISE(auto cctx, CreateCCtx());
    return std::make_shared<ZSTDCompressor>(std::move(cctx));
  }

  Result<std::shared_ptr<Decompressor>> MakeDecompressor() override {
    ARROW_ASSIGN_OR_RAISE(auto dctx, CreateDCtx());
    return std::make_shared<ZSTDDecompressor>(std::move(dctx));
  }

  Compression::type compression_type() const override { return Compression::ZSTD; }
  int minimum_compression_level() const override { return ZSTD_minCLevel(); }
  int maximum_compression_level() const override { return ZSTD_maxCLevel(); }
  int default_compression_level() const override { return kZSTDDefaultCompressionLevel; }

  int compression_level() const override { return compression_level_; }

 private:
  Result<CCtxPtr> CreateCCtx() const {
    CCtxPtr cctx{ZSTD_createCCtx(), ZSTD_freeCCtx};
    if (cctx == nullptr) {
      return Status::OutOfMemory("ZSTD_CCtx create failed");
    }
    auto ret =
        ZSTD_CCtx_setParameter(cctx.get(), ZSTD_c_compressionLevel, compression_level_);
    if (ZSTD_isError(ret)) {
      return ZSTDError(ret, "ZSTD_CCtx create failed: ");
    }
    for (auto& [key, value] : compression_context_params_) {
      ret = ZSTD_CCtx_setParameter(cctx.get(), static_cast<ZSTD_cParameter>(key), value);
      if (ZSTD_isError(ret)) {
        return ZSTDError(ret, "ZSTD_CCtx create failed: ");
      }
    }
    return cctx;
  }

  Result<DCtxPtr> CreateDCtx() const {
    DCtxPtr dctx{ZSTD_createDCtx(), ZSTD_freeDCtx};
    if (dctx == nullptr) {
      return Status::OutOfMemory("ZSTD_DCtx create failed");
    }