Class SharedObjectFactory<T>

java.lang.Object
com.linkedin.venice.utils.SharedObjectFactory<T>
Type Parameters:
T - Class whose objects need to release resources cleanly.

public class SharedObjectFactory<T> extends Object
A factory class to create shared objects that need to release resources cleanly. This class uses reference counting to ensure that resources are released safely.
  • Constructor Details

    • SharedObjectFactory

      public SharedObjectFactory()
  • Method Details

    • get

      public T get(String identifier, Supplier<T> constructor, Consumer<T> destroyer)
      Get a shared object that has the specified . If an object with the doesn't exist, a new one is created using the . When this function is called, the reference count of the shared object is incremented by 1.
      Parameters:
      identifier - A string that uniquely identifies the object being shared
      constructor - A {@link Supplier> to construct a new instance of the object
      destroyer - A Consumer to clean-up any state when the object is no longer needed
      Returns:
      A shared object
    • release

      public boolean release(String identifier)
      A method to notify to the factory that the user of the object no longer needs it. This method decreases the reference count of the shared object by 1. If the reference count becomes 0, the destroyer is invoked and the object is removed from the shared objects.
      Parameters:
      identifier - A string that uniquely identifies the object being shared
      Returns:
      true if the shared object is no longer being used; false otherwise
    • size

      public int size()
      Returns:
      The number of shared objects that are being managed
    • getReferenceCount

      public int getReferenceCount(String identifier)
      Return the current reference count of the object identified by the . If the factory isn't managing an object with the identifier, returns 0
      Parameters:
      identifier - A string that uniquely identifies the object being shared
      Returns:
      the current reference count of the object identified by the . If the factory isn't managing an object with the identifier, returns 0