Sha256: 2c19db159111d51ed8cb97f7f11b7715487e83345b66d72e48265b1527f76930

Contents?: true

Size: 1.81 KB

Versions: 19

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

#  Copyright 2023. Couchbase, Inc.
#
#  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.

module Couchbase
  # @api private
  class TranscoderFlags
    FORMAT_MAP = {
      reserved: 0,
      private: 1,
      json: 2,
      binary: 3,
      string: 4,
    }.freeze
    INV_FORMAT_MAP = FORMAT_MAP.invert.freeze

    COMPRESSION_MAP = {none: 0}.freeze
    INV_COMPRESSION_MAP = COMPRESSION_MAP.invert

    attr_reader :format
    attr_reader :compression
    attr_reader :lower_bits

    def initialize(format:, compression: :none, lower_bits: 0)
      @format = format
      @compression = compression
      @lower_bits = lower_bits
    end

    def self.decode(flags)
      return TranscoderFlags.new(format: flags) if flags.is_a?(Symbol)

      common_flags = flags >> 24
      lower_bits = flags & 0x00ffff

      return TranscoderFlags.new(format: nil, lower_bits: lower_bits) if common_flags.zero?

      compression_bits = common_flags >> 5
      format_bits = common_flags & 0x0f
      TranscoderFlags.new(
        format: INV_FORMAT_MAP[format_bits],
        compression: INV_COMPRESSION_MAP[compression_bits],
        lower_bits: lower_bits
      )
    end

    def encode
      common_flags = (COMPRESSION_MAP[@compression] << 5) | FORMAT_MAP[@format]
      (common_flags << 24) | @lower_bits
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
couchbase-3.5.5 lib/couchbase/transcoder_flags.rb
couchbase-3.5.5-arm64-darwin lib/couchbase/transcoder_flags.rb
couchbase-3.5.5-x86_64-darwin lib/couchbase/transcoder_flags.rb
couchbase-3.5.5-x86_64-linux lib/couchbase/transcoder_flags.rb
couchbase-3.5.5-x86_64-linux-musl lib/couchbase/transcoder_flags.rb
couchbase-3.5.4 lib/couchbase/transcoder_flags.rb
couchbase-3.5.4-arm64-darwin lib/couchbase/transcoder_flags.rb
couchbase-3.5.4-x86_64-darwin lib/couchbase/transcoder_flags.rb
couchbase-3.5.4-x86_64-linux lib/couchbase/transcoder_flags.rb
couchbase-3.5.4-x86_64-linux-musl lib/couchbase/transcoder_flags.rb
couchbase-3.5.3-x86_64-linux-musl lib/couchbase/transcoder_flags.rb
couchbase-3.5.3 lib/couchbase/transcoder_flags.rb
couchbase-3.5.3-arm64-darwin lib/couchbase/transcoder_flags.rb
couchbase-3.5.3-x86_64-darwin lib/couchbase/transcoder_flags.rb
couchbase-3.5.3-x86_64-linux lib/couchbase/transcoder_flags.rb
couchbase-3.5.2 lib/couchbase/transcoder_flags.rb
couchbase-3.5.2-x86_64-linux lib/couchbase/transcoder_flags.rb
couchbase-3.5.2-x86_64-darwin lib/couchbase/transcoder_flags.rb
couchbase-3.5.2-x86_64-linux-musl lib/couchbase/transcoder_flags.rb