Class SwitchPointCache

java.lang.Object
org.postgresql.pljava.internal.SwitchPointCache

public class SwitchPointCache extends Object
Tool for implementing objects or families of objects with methods that lazily compute various values and then return the same values until invalidated, after which new values will be lazily computed when next requested.

Synchronization

Items that have been cached are returned directly, until invalidated by the action of SwitchPoint.

When an item has not been cached or the cached value has been invalidated, its lazy recomputation at next use takes place within doInPG, that is, "on the PG thread". (An extended discussion of what that really means can be found at DualState.) The PG thread must be the only thread where the SwitchPoints will be invalidated, and an old SwitchPoint must be replaced in its field by a newly-constructed one before the old one is invalidated.

  • Method Details

    • doNotCache

      public static void doNotCache()
      Called from a computation method to prevent caching of the value being returned.

      This can be useful in cases where a not-yet-determined value should not 'stick'. Whatever the computation method returns will be returned to the caller, but the computation method will be reinvoked the next time a caller wants the value.

      This state is reset on entry and after return of any computation method. Therefore, if there are actions within a computation method that could involve calling other SwitchPointCache-based methods, this method must be called after all of those to have reliable effect. By convention, it should be called immediately before returning.

    • fix

      public static MethodHandle fix(MethodHandle m)
      Transform a MethodHandle into one with a reference to itself.
      Parameters:
      m - MethodHandle with methodType(r,MethodHandle,p0,...,pk) where the expected first parameter is a MethodHandle h of methodType(r,p0,...,pk) that invokes m with inserted first argument h.
      Returns:
      h
    • setConstant

      public static void setConstant(MethodHandle[] slots, int index, Object v)
      Replace slots[index] with a constant returning v forever, immune to invalidation.

      The slot must already be populated, as by the initializer created by a Builder; this method adapts the supplied constant to the slot's existing methodType.