<commit_before># Copyright (c) 2010 VMware, Inc.  All Rights Reserved.
require 'set'
require 'monitor'

module RbVmomi

class TypeStore
  def initialize fn
    File.open(fn, 'r') do |io|
      @db = Marshal.load io
    end
  end

  def [](k)
    @db[k]
  end
end

class TypeLoader
  attr_reader :typenames

  def initialize target, fn
    @target = target
    @db = TypeStore.new fn
    @lock = Monitor.new
  end

  def init
    @typenames = Set.new(@db['_typenames'] + BasicTypes::BUILTIN)
    @target.constants.select { |x| has_type? x.to_s }.each { |x| load_type x.to_s }
    BasicTypes::BUILTIN.each do |x|
      @target.const_set x, BasicTypes.const_get(x)
      load_extension x
    end
    Object.constants.map(&:to_s).select { |x| has_type? x }.each do |x|
      load_type x
    end
  end

  def has_type? name
    fail unless name.is_a? String
    @typenames.member? name
  end

  def load_type name
    fail unless name.is_a? String
    @lock.synchronize do
      @target.const_set name, make_type(name)
      load_extension name
    end
    nil
  end

  private

  def load_extension name
    dirs = @target.extension_dirs
    dirs.map { |x| File.join(x, "#{name}.rb") }.
         select { |x| File.exists? x }.
         each { |x| load x }
  end

  def make_type name
    name = name.to_s
    fail if BasicTypes::BUILTIN.member? name
    desc = @db[name] or fail "unknown VMODL type #{name}"
    case desc['kind']
    when 'data' then make_data_type name, desc<commit_msg>simplify TypeLoader<commit_after># Copyright (c) 2010 VMware, Inc.  All Rights Reserved.
require 'set'
require 'monitor'

module RbVmomi

class TypeLoader
  def initialize target, fn
    @target = target
    @lock = Monitor.new
    @db = {}
    @id2wsdl = {}
    add_types Hash[BasicTypes::BUILTIN.map { |k| [k,nil] }]
    vmodl_database = File.open(fn, 'r') { |io| Marshal.load io }
    vmodl_database.reject! { |k,v| k =~ /^_/ }
    add_types vmodl_database
  end

  def init
    @target.constants.select { |x| has_type? x.to_s }.each { |x| load_type x.to_s }
    BasicTypes::BUILTIN.each do |x|
      @target.const_set x, BasicTypes.const_get(x)
      load_extension x
    end
    Object.constants.map(&:to_s).select { |x| has_type? x }.each do |x|
      load_type x
    end
  end

  def has_type? name
    fail unless name.is_a? String
    @db.member? name
  end

  def load_type name
    fail unless name.is_a? String
    @lock.synchronize do
      @target.const_set name, make_type(name)
      load_extension name
    end
    nil
  end

  def add_types types
    @lock.synchronize do
      @db.merge! types
    end
  end

  def typenames
    @db.keys
  end

  private

  def load_extension name
    dirs = @target.extension_dirs
    dirs.map { |x| File.join(x, "#{name}.rb") }.
         select { |x| File.exists? x }.
         each { |x| load x }
  end

  def make_type name
    name = name.to_s
    fail if BasicTypes::BUILTIN.member? name
    desc = @db[name] or fail "unknown VMODL type #{name}"
    case desc['kind']
    when 'data' then make_data_type name, desc
{'fraction_non_alphanumeric': 0.11122015047432122, 'fraction_numerical': 0.002944062806673209, 'mean_word_length': 2.6318289786223277, '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': '7527033', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1119, 'n_words': 311}