Welcome to mirror list, hosted at ThFree Co, Russian Federation.

json_size_estimator.rb « utils « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f8ea3e61f96f08e59c35d67c4087add991e6605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

module Gitlab
  module Utils
    # This class estimates the JSON blob byte size of a ruby object using as
    # little allocations as possible.
    # The estimation should be quite accurate when using simple objects.
    #
    # Example:
    #
    # Gitlab::Utils::JsonSizeEstimator.estimate(["a", { b: 12, c: nil }])
    class JsonSizeEstimator
      ARRAY_BRACKETS_SIZE = 2 # []
      OBJECT_BRACKETS_SIZE = 2 # {}
      DOUBLEQUOTE_SIZE = 2 # ""
      COLON_SIZE = 1 # : character size from {"a": 1}
      MINUS_SIGN_SIZE = 1 # - character size from -1
      NULL_SIZE = 4 # null

      class << self
        # Returns: integer (number of bytes)
        def estimate(object)
          case object
          when Hash
            estimate_hash(object)
          when Array
            estimate_array(object)
          when String
            estimate_string(object)
          when Integer
            estimate_integer(object)
          when Float
            estimate_float(object)
          when DateTime, Time
            estimate_time(object)
          when NilClass
            NULL_SIZE
          else
            # might be incorrect, but #to_s is safe, #to_json might be disabled for some objects: User
            estimate_string(object.to_s)
          end
        end

        private

        def estimate_hash(hash)
          size = 0
          item_count = 0

          hash.each do |key, value|
            item_count += 1

            size += estimate(key.to_s) + COLON_SIZE + estimate(value)
          end

          size + OBJECT_BRACKETS_SIZE + comma_count(item_count)
        end

        def estimate_array(array)
          size = 0
          item_count = 0

          array.each do |item|
            item_count += 1

            size += estimate(item)
          end

          size + ARRAY_BRACKETS_SIZE + comma_count(item_count)
        end

        def estimate_string(string)
          string.bytesize + DOUBLEQUOTE_SIZE
        end

        def estimate_float(float)
          float.to_s.bytesize
        end

        def estimate_integer(integer)
          if integer > 0
            integer_string_size(integer)
          elsif integer < 0
            integer_string_size(integer.abs) + MINUS_SIGN_SIZE
          else # 0
            1
          end
        end

        def estimate_time(time)
          time.to_json.size
        end

        def integer_string_size(integer)
          Math.log10(integer).floor + 1
        end

        def comma_count(item_count)
          item_count == 0 ? 0 : item_count - 1
        end
      end
    end
  end
end