<commit_before>require 'base64'
require 'uuidtools'

module Serf
module Util

  ##
  # Helper module to for various UUID tasks.
  #
  # 1. Primarily to create and parse 'coded' UUIDs, which are just
  #   base64 encoded UUIDs without trailing '='.
  #
  module Uuidable

    ##
    # Creates a Timestamp UUID, base64 encoded.
    #
    # NOTE: UUIDTools TimeStamp code creates a UTC based timestamp UUID.
    #
    def self.create_coded_uuid
      # All raw UUIDs are 16 bytes long. Base64 lengthens the string to
      # 24 bytes long. We chomp off the last two equal signs '==' to
      # trim the string length to 22 bytes. This gives us an overhead
      # of an extra 6 bytes over raw UUID, but with the readability
      # benefit. And saves us 14 bytes of size from the 'standard'
      # string hex representation of UUIDs.
      Base64.urlsafe_encode64(UUIDTools::UUID.timestamp_create.raw).chomp('==')
    end

    ##
    # @param coded_uuid a coded uuid to parse.
    #
    def self.parse_coded_uuid(coded_uuid)
      UUIDTools::UUID.parse_raw Base64.urlsafe_decode64("#{coded_uuid}==")
    end

    ##
    # Create a new set of uuids.
    #
    def self.create_uuids(parent={})
      {
        uuid: create_coded_uuid,
        parent_uuid: parent['uuid'],
        origin_uuid: (
          parent[:origin_uuid] ||
          parent[:parent_uuid] ||
          parent[:uuid])
      }
    end

    ##
    # Set a message's UUIDs with new UUIDs based on the parent's UUIDs.
    #
    def self.annotate_with_uuids!(message, parent={})
      uuids = self.create_uuids parent<commit_msg>Uuidable now has instance methods to front its logic.<commit_after>require 'base64'
require 'uuidtools'

module Serf
module Util

  ##
  # Helper module to for various UUID tasks.
  #
  # 1. Primarily to create and parse 'coded' UUIDs, which are just
  #   base64 encoded UUIDs without trailing '='.
  #
  module Uuidable

    ##
    # @see self.create_coded_uuid
    def create_coded_uuid
      Uuidable.create_coded_uuid
    end

    ##
    # @see self.parse_coded_uuid
    def parse_coded_uuid(coded_uuid)
      Uuidable.parse_coded_uuid coded_uuid
    end

    ##
    # @see self.create_uuids
    def create_uuids(parent={})
      Uuidable.create_uuids parent
    end

    ##
    # @see self.annotate_with_uuids!
    def annotate_with_uuids!(message, parent={})
      Uuidable.annotate_with_uuids! message, parent
    end

    ##
    # Creates a Timestamp UUID, base64 encoded.
    #
    # NOTE: UUIDTools TimeStamp code creates a UTC based timestamp UUID.
    #
    def self.create_coded_uuid
      # All raw UUIDs are 16 bytes long. Base64 lengthens the string to
      # 24 bytes long. We chomp off the last two equal signs '==' to
      # trim the string length to 22 bytes. This gives us an overhead
      # of an extra 6 bytes over raw UUID, but with the readability
      # benefit. And saves us 14 bytes of size from the 'standard'
      # string hex representation of UUIDs.
      Base64.urlsafe_encode64(UUIDTools::UUID.timestamp_create.raw).chomp('==')
    end

    ##
    # @param coded_uuid a coded uuid to parse.
    #
    def self.parse_coded_uuid(coded_uuid)
      UUIDTools::UUID.parse_raw Base64.urlsafe_decode64("#{coded_uuid}==")
    end

    ##
    # Create a new set of uuids.
    #
    def self.create_uuids(parent={})
      {
        uuid: create_coded_uuid,
        parent_uuid: parent[:uuid],
        origin_uuid: (
          parent[:origin_uuid] ||
          parent[:parent_uuid] ||
          parent[:uuid])
      }
    end

    ##
    # Set a message's UUIDs with new UUIDs based on the parent's UUIDs.
    #
    def self.annotate_with_uuids!(message, parent={})
      uuids = self.create_uuids parent
{'fraction_non_alphanumeric': 0.08729519204942252, 'fraction_numerical': 0.013967230727907601, 'mean_word_length': 2.5773294908741593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5154154', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1179, 'n_words': 364}