<commit_before>#!/usr/bin/ruby -w
#
# This file contains the MUES::ObjectStore class, which is used to provide
# object persistance for MUES objects.
#
# The ObjectStore is a factory/wrapper class that provides a front end for
# combining a Strategy for object persistance (a MUES::ObjectStore::Backend)
# and, optionally, a one for swapping disused objects (mostly) out of memory
# temporarily via a MUES::ObjectStore::MemoryManager.
#   
# == Caveats/Requirements
#
# * All objects stored must inherit from the MUES::StorableObject class.
#
# * Multiple ObjectStores alive in the same ruby process must have distinct
#   names.
#
# == Specifying a Backend or MemoryManager
#
# <em>Not yet done.</em>
#
# == Synopsis
#
#   require 'mues/objectstore'
#   require 'mues/storableobject'
#
#   $store = MUES::ObjectStore::create(
#		:backend	=> "BerkeleyDB",
#		:memmgr			=> "PMOS",
#		:name		=> "mystore"
#	)
#
#   objs = []
#   ids = []
#   40.times do
#	    newObj = MUES::StorableObject.new
#       objs << newObj
#       ids << newObj.objectStoreId
#   end
#
#   $store.store( *objs )
#   $store.retrieve_readonly {|obj| puts obj.attr}
#
#	object = $store.retrieve( "objid" )
#   object.attribute_A = 3.14159262546
#   $store.store( object )
#
#   $store.close
#
# == Version
#
#  $Id: objectstore.rb,v 1.40 2003/10/13 04:02:16 deveiant Exp $
# 
# == Authors
#
# * <NAME> <<EMAIL>>
# * <NAME> <<EMAIL>>
#
#:include: COPYRIGHT
#
#---
#
# Please see the file COPYRIGHT for licensing details.
#


require 'forwardable'
require 'sync'

require 'mues/object'
require 'mues/exceptions'
require 'mues/storableobject'
require 'mues/objectspacevisitor'

module MUES

	# The ObjectStore provides a mechanism for object persistance and,
	# optionally, a way of swapping disused objects (mostly) out of memory
	# temporarily via a MemoryManager.
    class ObjectStore < MUES::Object

		include MUES::TypeCheckFunctions

		### Class constants
		Version = /([\d\.]+)/.match( %q{$Revision: 1.40 $} )[1]
		Rcsid = %q$Id: objectstore.rb,v 1.40 2003/10/13 04:02:16 deveiant Exp $


		# The default MemoryManager class
		DefaultMemMgr = "Null"

		# The default Backend class
		DefaultBackend = "Flatfile"

		# The default MemoryManager Visitor class
		DefaultVisitor = MUES::ObjectSpaceVisitor

		# The name of the subdirectory to load default backends and
		# memorymanager classes from (relative to $LOAD_PATH)
		BackendDir = 'mues/os-extensions'


		### Class globals

		# Instances of objectstores, keyed by name
		@@instances = {}


		#########
		# Class #
		#########

		### Disallow new() to prevent multiple instances of the same-named store
		private_class_method :new

		### Factory method: Combine the specified backend, memory manager,
		### and visitor objects together in a MUES::ObjectStore object. The
		### <tt>args</tt> are a Hash with the following keys:
		###
        ### [:name]
		###   A name (String) that acts as the store's unique
		###   identifier. Calling #create twice with the same name will return the
		###   previous one instantiated. <strong>Required</strong>.
		### [:backend]
		###   The name of the MUES::ObjectStore::Backend derivative to use, the
		###   class object itself, or an instance of a backend.  See the
		###   "Specifying a Backend or MemoryManager" section of
		###   lib/mues/ObjectStore.rb for more information.
		### [:memmgr]
		###   The name of the MUES::ObjectStore::MemoryManager derivative to
		###   use, the class object itself, or an instance of a memory
		###   manager. See the "Specifying a Backend or MemoryManager" section
		###   of lib/mues/ObjectStore.rb for more information.
		###
		### The following arguments are optional:
        ### [:indexes]
		###   An Array of symbols/strings for index methods. Index methods are
		###   methods which are to be used as index values to facilitate fast
		###   retrieval of objects from the store by lookups on values instead
		###   of by an id. See #lookup for more about how this works.
		### [:visitor]
		###   The MUES::ObjectSpaceVisitor derivative instance or class to use
		###   for traversing the objectspace contained in the store.
		### [:config]
		###   A configuration hash that contains one or more of the following
		###   keys. The value is used to configure the various parts of the
		###   objectstore at instantiation. The configuration values will be
		###   ignored if the corresponding part is specified by passing an
		###   instance instead of a Class or a name.
		###   [:memmgr]
		###      If specified, the value will be passed as the last argument to
		###      the MemoryManager on instantiation.
		###   [:backend]
		###      If specified, the value will be passed as the last argument to
		###      the Backend on instantiation.
		###   [:visitor]
		###      If specified, the value will be passed as the last argument to
		###      the Visitor on instantiation.
		def self.create( args )
			TypeCheckFunctions::checkType( args, Hash )

			config = args[:config] || {}

			# Check the name argument
			name = args[:name].to_s
			raise ArgumentError, "No name given." if name.empty?
			return @@instances[ name ] if @@instances.has_key?( name )

			# Check the indexes
			indexes = args[:indexes] || []
			TypeCheckFunctions::checkType( indexes, Array )
			TypeCheckFunctions::checkEachType( indexes, String, Symbol )

			# If they didn't supply a visitor, just use the default one
			args[:visitor] ||= DefaultVisitor
			if args[:visitor].is_a?( Class )
				visitor = args[:visitor].new( config[:visitor] )
			else
				TypeCheckFunctions::checkType( args[:visitor], MUES::ObjectSpaceVisitor )
				visitor = args[:visitor]
			end

			# Build the list of objects we need for the store
			backend	= 
				ObjectStore::Backend::create( args[:backend] || DefaultBackend,
											  name, indexes, config[:backend] )
			memmgr = 
				ObjectStore::MemoryManager::create( args[:memmgr] || DefaultMemMgr,
												    backend, config[:memmgr] )

			# Return the new store after adding it to the instance list
			return @@instances[ name ] = new( name, memmgr, backend, visitor )
		end


		### Initializes a new ObjectStore
		###
        ### [name]
		###   The store identifier
		### [memmgr]
		###   A MUES::ObjectStore::MemoryManager object.<commit_msg>- Converted from old config style to new.<commit_after>#!/usr/bin/ruby -w
#
# This file contains the MUES::ObjectStore class, which is used to provide
# object persistance for MUES objects.
#
# The ObjectStore is a factory/wrapper class that provides a front end for
# combining a Strategy for object persistance (a MUES::ObjectStore::Backend)
# and, optionally, a one for swapping disused objects (mostly) out of memory
# temporarily via a MUES::ObjectStore::MemoryManager.
#   
# == Caveats/Requirements
#
# * All objects stored must inherit from the MUES::StorableObject class.
#
# * Multiple ObjectStores alive in the same ruby process must have distinct
#   names.
#
# == Specifying a Backend or MemoryManager
#
# <em>Not yet done.</em>
#
# == Synopsis
#
#   require 'mues/objectstore'
#   require 'mues/storableobject'
#
#   $store = MUES::ObjectStore::create(
#		:backend	=> "BerkeleyDB",
#		:memmgr			=> "PMOS",
#		:name		=> "mystore"
#	)
#
#   objs = []
#   ids = []
#   40.times do
#	    newObj = MUES::StorableObject.new
#       objs << newObj
#       ids << newObj.objectStoreId
#   end
#
#   $store.store( *objs )
#   $store.retrieve_readonly {|obj| puts obj.attr}
#
#	object = $store.retrieve( "objid" )
#   object.attribute_A = 3.14159262546
#   $store.store( object )
#
#   $store.close
#
# == Version
#
#  $Id: objectstore.rb,v 1.41 2004/02/29 18:34:18 deveiant Exp $
# 
# == Authors
#
# * <NAME> <<EMAIL>>
# * <NAME> <<EMAIL>>
#
#:include: COPYRIGHT
#
#---
#
# Please see the file COPYRIGHT for licensing details.
#


require 'forwardable'
require 'sync'

require 'mues/object'
require 'mues/exceptions'
require 'mues/storableobject'
require 'mues/objectspacevisitor'

module MUES

	# The ObjectStore provides a mechanism for object persistance and,
	# optionally, a way of swapping disused objects (mostly) out of memory
	# temporarily via a MemoryManager.
    class ObjectStore < MUES::Object

		include MUES::TypeCheckFunctions

		### Class constants
		Version = /([\d\.]+)/.match( %q{$Revision: 1.41 $} )[1]
		Rcsid = %q$Id: objectstore.rb,v 1.41 2004/02/29 18:34:18 deveiant Exp $


		# The default MemoryManager class
		DefaultMemMgr = "Null"

		# The default Backend class
		DefaultBackend = "Flatfile"

		# The default MemoryManager Visitor class
		DefaultVisitor = MUES::ObjectSpaceVisitor

		# The name of the subdirectory to load default backends and
		# memorymanager classes from (relative to $LOAD_PATH)
		BackendDir = 'mues/os-extensions'


		### Class globals

		# Instances of objectstores, keyed by name
		@@instances = {}


		#########
		# Class #
		#########

		### Disallow new() to prevent multiple instances of the same-named store
		private_class_method :new

		### Factory method: Combine the specified backend, memory manager,
		### and visitor objects together in a MUES::ObjectStore object. The
		### <tt>args</tt> are a Hash with the following keys:
		###
        ### [:name]
		###   A name (String) that acts as the store's unique
		###   identifier. Calling #create twice with the same name will return the
		###   previous one instantiated. <strong>Required</strong>.
		### [:backend]
		###   The name of the MUES::ObjectStore::Backend derivative to use, the
		###   class object itself, or an instance of a backend.  See the
		###   "Specifying a Backend or MemoryManager" section of
		###   lib/mues/ObjectStore.rb for more information.
		### [:memmgr]
		###   The name of the MUES::ObjectStore::MemoryManager derivative to
		###   use, the class object itself, or an instance of a memory
		###   manager. See the "Specifying a Backend or MemoryManager" section
		###   of lib/mues/ObjectStore.rb for more information.
		###
		### The following arguments are optional:
        ### [:indexes]
		###   An Array of symbols/strings for index methods. Index methods are
		###   methods which are to be used as index values to facilitate fast
		###   retrieval of objects from the store by lookups on values instead
		###   of by an id. See #lookup for more about how this works.
		### [:visitor]
		###   The MUES::ObjectSpaceVisitor derivative instance or class to use
		###   for traversing the objectspace contained in the store.
		### [:config]
		###   A configuration hash that contains one or more of the following
		###   keys. The value is used to configure the various parts of the
		###   objectstore at instantiation. The configuration values will be
		###   ignored if the corresponding part is specified by passing an
		###   instance instead of a Class or a name.
		###   [:memmgr]
		###      If specified, the value will be passed as the last argument to
		###      the MemoryManager on instantiation.
		###   [:backend]
		###      If specified, the value will be passed as the last argument to
		###      the Backend on instantiation.
		###   [:visitor]
		###      If specified, the value will be passed as the last argument to
		###      the Visitor on instantiation.
		def self.create( args )
			TypeCheckFunctions::checkType( args, Hash )

			config = args[:config] || {}

			# Check the name argument
			name = args[:name].to_s
			raise ArgumentError, "No name given." if name.empty?
			return @@instances[ name ] if @@instances.has_key?( name )

			# Check the indexes
			indexes = args[:indexes] || []
			TypeCheckFunctions::checkType( indexes, Array )
			TypeCheckFunctions::checkEachType( indexes, String, Symbol )

			# If they didn't supply a visitor, just use the default one
			args[:visitor] ||= DefaultVisitor
			if args[:visitor].is_a?( Class )
				visitor = args[:visitor].new( config.visitor )
			else
				TypeCheckFunctions::checkType( args[:visitor], MUES::ObjectSpaceVisitor )
				visitor = args[:visitor]
			end

			# Build the list of objects we need for the store
			backend	= 
				ObjectStore::Backend::create( args[:backend] || DefaultBackend,
											  name, indexes, config.backend )
			memmgr = 
				ObjectStore::MemoryManager::create( args[:memmgr] || DefaultMemMgr,
												    backend, config.memorymanager )

			# Return the new store after adding it to the instance list
			return @@instances[ name ] = new( name, memmgr, backend, visitor )
		end


		### Initializes a new ObjectStore
		###
        ### [name]
		###   The store identifier
		### [memmgr]
		###   A MUES::ObjectStore::MemoryManager object.
{'fraction_non_alphanumeric': 0.11761900977509337, 'fraction_numerical': 0.008265119605817373, 'mean_word_length': 3.4894755618979665, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, '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': '5594938', 'n_tokens_mistral': 4205, 'n_tokens_neox': 3778, 'n_words': 1338}