summaryrefslogtreecommitdiff
path: root/lib/gitlab/utils
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-04 12:07:52 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-04 12:07:52 +0000
commitc6c7437861bff9572747674095c4dfbdfbea4988 (patch)
tree237d1ed922193f19ae326923457344c082003788 /lib/gitlab/utils
parentd80f3cd75e700b6e62910865bfd36734644ffa89 (diff)
downloadgitlab-ce-c6c7437861bff9572747674095c4dfbdfbea4988.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/utils')
-rw-r--r--lib/gitlab/utils/json_size_estimator.rb104
-rw-r--r--lib/gitlab/utils/log_limited_array.rb2
2 files changed, 105 insertions, 1 deletions
diff --git a/lib/gitlab/utils/json_size_estimator.rb b/lib/gitlab/utils/json_size_estimator.rb
new file mode 100644
index 00000000000..9f8ea3e61f9
--- /dev/null
+++ b/lib/gitlab/utils/json_size_estimator.rb
@@ -0,0 +1,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
diff --git a/lib/gitlab/utils/log_limited_array.rb b/lib/gitlab/utils/log_limited_array.rb
index 9c207758580..e0589c3df4c 100644
--- a/lib/gitlab/utils/log_limited_array.rb
+++ b/lib/gitlab/utils/log_limited_array.rb
@@ -13,7 +13,7 @@ module Gitlab
total_length = 0
limited_array = array.take_while do |arg|
- total_length += arg.to_json.length
+ total_length += JsonSizeEstimator.estimate(arg)
total_length <= MAXIMUM_ARRAY_LENGTH
end