Fork me on GitHub
  • API

    Show / Hide Table of Contents

    Class DefaultCodecFactory

    Implements the default functionality of ICodecFactory.

    To replace the DefaultCodecFactory instance, call SetCodecFactory(ICodecFactory) at application start up. DefaultCodecFactory can be subclassed or passed additional parameters to register additional codecs, inject dependencies, or change caching behavior, as shown in the following examples. Alternatively, ICodecFactory can be implemented to provide complete control over codec creation and lifetimes.

    Register Additional Codecs

    Additional codecs can be added by initializing the instance of DefaultCodecFactory and passing an array of Codec-derived types.
    // Register the factory at application start up.
    Codec.SetCodecFactory(new DefaultCodecFactory {
        CustomCodecTypes = new Type[] { typeof(MyCodec), typeof(AnotherCodec) }
    });

    Only Use Explicitly Defined Codecs

    PutCodecType(Type) can be used to explicitly add codec types. In this example, the call to base.Initialize() is excluded to skip the built-in codec registration. Since AnotherCodec doesn't have a default constructor, the NewCodec(Type) method is overridden to supply the required parameters.
    public class ExplicitCodecFactory : DefaultCodecFactory
    {
        protected override void Initialize()
        {
            // Load specific codecs in a specific order.
            PutCodecType(typeof(MyCodec));
            PutCodecType(typeof(AnotherCodec));
        }
    
    protected override Codec NewCodec(Type type)
    {
        // Special case: AnotherCodec has a required dependency
        if (typeof(AnotherCodec).Equals(type))
            return new AnotherCodec(new SomeDependency());
    
        return base.NewCodec(type);
    }
    

    }

    // Register the factory at application start up. Codec.SetCodecFactory(new ExplicitCodecFactory());

    See the Lucene.Net.Codecs namespace documentation for more examples of how to inject dependencies into Codec subclasses.

    Use Reflection to Scan an Assembly for Codecs

    ScanForCodecs(Assembly) or ScanForCodecs(IEnumerable<Assembly>) can be used to scan assemblies using .NET Reflection for codec types and add all subclasses that are found automatically. This example calls base.Initialize() to load the default codecs prior to scanning for additional codecs.
    public class ScanningCodecFactory : DefaultCodecFactory
    {
        protected override void Initialize()
        {
            // Load all default codecs
            base.Initialize();
    
        // Load all of the codecs inside of the same assembly that MyCodec is defined in
        ScanForCodecs(typeof(MyCodec).Assembly);
    }
    

    }

    // Register the factory at application start up. Codec.SetCodecFactory(new ScanningCodecFactory());

    Codecs in the target assemblie(s) can be excluded from the scan by decorating them with the ExcludeCodecFromScanAttribute.
    Inheritance
    object
    NamedServiceFactory<Codec>
    DefaultCodecFactory
    Implements
    ICodecFactory
    IServiceListable
    Inherited Members
    NamedServiceFactory<Codec>.m_initializationLock
    NamedServiceFactory<Codec>.EnsureInitialized()
    NamedServiceFactory<Codec>.CodecsAssembly
    NamedServiceFactory<Codec>.IsServiceType(Type)
    NamedServiceFactory<Codec>.GetServiceName(Type)
    NamedServiceFactory<Codec>.GetCanonicalName(Type)
    NamedServiceFactory<Codec>.IsFullyTrusted
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Lucene.Net.Codecs
    Assembly: Lucene.Net.dll
    Syntax
    public class DefaultCodecFactory : NamedServiceFactory<Codec>, ICodecFactory, IServiceListable

    Constructors

    DefaultCodecFactory()

    Creates a new instance of DefaultCodecFactory.

    Declaration
    public DefaultCodecFactory()
    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    Properties

    AvailableServices

    Gets a list of the available Codecs (by name).

    Declaration
    public virtual ICollection<string> AvailableServices { get; }
    Property Value
    Type Description
    ICollection<string>

    A ICollection{string} of Codec names.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    CustomCodecTypes

    An array of custom Codec-derived types to be registered. This property can be initialized during construction of DefaultCodecFactory to make your custom codecs known to Lucene.

    These types will be registered after the default Lucene types, so if a custom type has the same name as a Lucene Codec (via CodecNameAttribute) the custom type will replace the Lucene type with the same name.
    Declaration
    public IEnumerable<Type> CustomCodecTypes { get; set; }
    Property Value
    Type Description
    IEnumerable<Type>
    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    Methods

    GetCodec(string)

    Gets the Codec instance from the provided name.

    Declaration
    public virtual Codec GetCodec(string name)
    Parameters
    Type Name Description
    string name

    The name of the Codec instance to retrieve.

    Returns
    Type Description
    Codec

    The Codec instance.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    GetCodec(Type)

    Gets the Codec instance from the provided type.

    Declaration
    protected virtual Codec GetCodec(Type type)
    Parameters
    Type Name Description
    Type type

    The Type of Codec to retrieve.

    Returns
    Type Description
    Codec

    The Codec instance.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    GetCodecType(string)

    Gets the CodecType from the provided name.

    Declaration
    protected virtual Type GetCodecType(string name)
    Parameters
    Type Name Description
    string name

    The name of the CodecType to retrieve.

    Returns
    Type Description
    Type

    The CodecType.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    Initialize()

    Initializes the codec type cache with the known Codec types. Override this method (and optionally call base.Initialize()) to add your own Codec types by calling PutCodecType(Type) or ScanForCodecs(Assembly).

    If two types have the same name by using the CodecNameAttribute, the last one registered wins.
    Declaration
    protected override void Initialize()
    Overrides
    NamedServiceFactory<Codec>.Initialize()
    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    NewCodec(Type)

    Instantiates a Codec based on the provided type.

    Declaration
    protected virtual Codec NewCodec(Type type)
    Parameters
    Type Name Description
    Type type

    The Type of Codec to instantiate.

    Returns
    Type Description
    Codec

    The new instance.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    PutCodecType(Type)

    Adds a Codec type to the codecNameToTypeMap, using the name provided in the CodecNameAttribute, if present, or the name of the codec class minus the "Codec" suffix as the name by default.

    Note that if a Codec with the same name already exists in the map, calling this method will update it to the new type.
    Declaration
    protected virtual void PutCodecType(Type codec)
    Parameters
    Type Name Description
    Type codec

    A type that subclasses Codec.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    ScanForCodecs(IEnumerable<Assembly>)

    Scans the given assemblies for subclasses of Codec and adds their names to the codecNameToTypeMap. Note that names will be automatically overridden if the Codec name appears multiple times - the last match wins.

    Declaration
    protected virtual void ScanForCodecs(IEnumerable<Assembly> assemblies)
    Parameters
    Type Name Description
    IEnumerable<Assembly> assemblies

    A list of assemblies to scan. The assemblies will be scanned from first to last, and the last match for each Codec name wins.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    ScanForCodecs(Assembly)

    Scans the given assembly for subclasses of Codec and adds their names to the codecNameToTypeMap. Note that names will be automatically overridden if the Codec name appears multiple times - the last match wins.

    Declaration
    protected virtual void ScanForCodecs(Assembly assembly)
    Parameters
    Type Name Description
    Assembly assembly

    The assembly to scan.

    See Also
    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute

    Implements

    ICodecFactory
    IServiceListable

    See Also

    ICodecFactory
    IServiceListable
    ExcludeCodecFromScanAttribute
    Back to top Copyright © 2024 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.