]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/r/R/compression.R
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / r / R / compression.R
CommitLineData
1d09f67e
TL
1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18#' @include enums.R
19#' @include arrow-package.R
20#' @include io.R
21
22#' @title Compression Codec class
23#' @usage NULL
24#' @format NULL
25#' @docType class
26#' @description Codecs allow you to create [compressed input and output
27#' streams][compression].
28#' @section Factory:
29#' The `Codec$create()` factory method takes the following arguments:
30#' * `type`: string name of the compression method. Possible values are
31#' "uncompressed", "snappy", "gzip", "brotli", "zstd", "lz4", "lzo", or
32#' "bz2". `type` may be upper- or lower-cased. Not all methods may be
33#' available; support depends on build-time flags for the C++ library.
34#' See [codec_is_available()]. Most builds support at least "snappy" and
35#' "gzip". All support "uncompressed".
36#' * `compression_level`: compression level, the default value (`NA`) uses the
37#' default compression level for the selected compression `type`.
38#' @rdname Codec
39#' @name Codec
40#' @export
41Codec <- R6Class("Codec",
42 inherit = ArrowObject,
43 active = list(
44 name = function() util___Codec__name(self),
45 level = function() abort("Codec$level() not yet implemented")
46 )
47)
48Codec$create <- function(type = "gzip", compression_level = NA) {
49 if (is.string(type)) {
50 type <- util___Codec__Create(
51 compression_from_name(type), compression_level
52 )
53 }
54 assert_is(type, "Codec")
55 type
56}
57
58#' Check whether a compression codec is available
59#'
60#' Support for compression libraries depends on the build-time settings of
61#' the Arrow C++ library. This function lets you know which are available for
62#' use.
63#' @param type A string, one of "uncompressed", "snappy", "gzip", "brotli",
64#' "zstd", "lz4", "lzo", or "bz2", case insensitive.
65#' @return Logical: is `type` available?
66#' @export
67#' @examplesIf arrow_available()
68#' codec_is_available("gzip")
69codec_is_available <- function(type) {
70 util___Codec__IsAvailable(compression_from_name(type))
71}
72
73compression_from_name <- function(name) {
74 map_int(name, ~ CompressionType[[match.arg(toupper(.x), names(CompressionType))]])
75}
76
77#' @title Compressed stream classes
78#' @rdname compression
79#' @name compression
80#' @aliases CompressedInputStream CompressedOutputStream
81#' @docType class
82#' @usage NULL
83#' @format NULL
84#' @description `CompressedInputStream` and `CompressedOutputStream`
85#' allow you to apply a compression [Codec] to an
86#' input or output stream.
87#'
88#' @section Factory:
89#'
90#' The `CompressedInputStream$create()` and `CompressedOutputStream$create()`
91#' factory methods instantiate the object and take the following arguments:
92#'
93#' - `stream` An [InputStream] or [OutputStream], respectively
94#' - `codec` A `Codec`, either a [Codec][Codec] instance or a string
95#' - `compression_level` compression level for when the `codec` argument is given as a string
96#'
97#' @section Methods:
98#'
99#' Methods are inherited from [InputStream] and [OutputStream], respectively
100#' @export
101#' @include arrow-package.R
102CompressedOutputStream <- R6Class("CompressedOutputStream", inherit = OutputStream)
103CompressedOutputStream$create <- function(stream, codec = "gzip", compression_level = NA) {
104 codec <- Codec$create(codec, compression_level = compression_level)
105 if (is.string(stream)) {
106 stream <- FileOutputStream$create(stream)
107 }
108 assert_is(stream, "OutputStream")
109 io___CompressedOutputStream__Make(codec, stream)
110}
111
112#' @rdname compression
113#' @usage NULL
114#' @format NULL
115#' @export
116CompressedInputStream <- R6Class("CompressedInputStream", inherit = InputStream)
117CompressedInputStream$create <- function(stream, codec = "gzip", compression_level = NA) {
118 codec <- Codec$create(codec, compression_level = compression_level)
119 if (is.string(stream)) {
120 stream <- ReadableFile$create(stream)
121 }
122 assert_is(stream, "InputStream")
123 io___CompressedInputStream__Make(codec, stream)
124}