Top Description Inners Fields Constructors Methods
org.postgresql.core

pack-priv final Class AsciiStringInterner

extends Object
Class Inheritance
Static Imports
org.postgresql.util.internal.Nullness.castNonNull

Provides the canonicalization/interning of String instances which contain only ascii characters, keyed by the byte[] representation (in ascii).

The values are stored in SoftReferences, allowing them to be garbage collected if not in use and there is memory pressure.

Note

Instances are safe for concurrent use.

Author
Brett Okken

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract static class
private static class
AsciiStringInterner.Key

Instance used for inserting values into the cache.

private class
AsciiStringInterner.StringReference

Custom SoftReference implementation which maintains a reference to the key in the cache, which allows aggressive cleaning when garbage collector collects the String instance.

private static class
AsciiStringInterner.TempKey

Only used for lookups, never to actually store entries.

Field Summary

Modifier and TypeField and Description
pack-priv final ConcurrentMap<AsciiStringInterner.BaseKey, SoftReference<String>>
cache

Contains the canonicalized values, keyed by the ascii byte[].

pack-priv final ReferenceQueue<String>
refQueue

Used for Reference as values in cache.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv static boolean
arrayEquals(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength)

Performs equality check between a and b (with corresponding offset/length values).

private void
cleanQueue()

Process any entries in refQueue to purge from the cache.

public String

Returns:

Decoded String from bytes.
getString
(byte[]
The bytes of the String. Must not be null.
bytes
,
int
Offset into bytes to start.
offset
,
int
The number of bytes in bytes which are relevant.
length
,
Encoding
To use if non-ascii bytes seen.
encoding
)

Produces a String instance for the given bytes.

public String

Returns:

Decoded String from bytes.
getStringIfPresent
(byte[]
The bytes of the String. Must not be null.
bytes
,
int
Offset into bytes to start.
offset
,
int
The number of bytes in bytes which are relevant.
length
,
Encoding
To use if non-ascii bytes seen.
encoding
)

Produces a String instance for the given bytes.

private static int

Returns:

hash code for relevant bytes, or 0 if non-ascii bytes present.
hashKey
(byte[] bytes, int offset, int length)

Generates a hash value for the relevant entries in bytes as long as all values are ascii (>= 0).

public boolean

Returns:

Indication if val is an ascii String and placed into cache.
putString
(String
The value to intern. Must not be null.
val
)

Preemptively populates a value into the cache.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cacheback to summary
pack-priv final ConcurrentMap<AsciiStringInterner.BaseKey, SoftReference<String>> cache

Contains the canonicalized values, keyed by the ascii byte[].

refQueueback to summary
pack-priv final ReferenceQueue<String> refQueue

Used for Reference as values in cache.

Constructor Detail

AsciiStringInternerback to summary
pack-priv AsciiStringInterner()

Method Detail

arrayEqualsback to summary
pack-priv static boolean arrayEquals(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength)

Performs equality check between a and b (with corresponding offset/length values).

The static boolean equals(byte[].class, int, int, byte[], int, int method in java.util.Arrays is optimized for longer byte[] instances than is expected to be seen here.

cleanQueueback to summary
private void cleanQueue()

Process any entries in refQueue to purge from the cache.

See Also
StringReference#dispose()
getStringback to summary
public String getString(byte[] bytes, int offset, int length, Encoding encoding) throws IOException

Produces a String instance for the given bytes. If all are valid ascii (i.e. >= 0) either an existing value will be returned, or the newly created String will be stored before being returned.

If non-ascii bytes are discovered, the encoding will be used to decode and that value will be returned (but not stored).

Parameters
bytes:byte[]

The bytes of the String. Must not be null.

offset:int

Offset into bytes to start.

length:int

The number of bytes in bytes which are relevant.

encoding:Encoding

To use if non-ascii bytes seen.

Returns:String

Decoded String from bytes.

Exceptions
IOException:
If error decoding from Encoding.
getStringIfPresentback to summary
public String getStringIfPresent(byte[] bytes, int offset, int length, Encoding encoding) throws IOException

Produces a String instance for the given bytes.

If all are valid ascii (i.e. >= 0) and a corresponding String value exists, it will be returned. If no value exists, a String will be created, but not stored.

If non-ascii bytes are discovered, the encoding will be used to decode and that value will be returned (but not stored).

Parameters
bytes:byte[]

The bytes of the String. Must not be null.

offset:int

Offset into bytes to start.

length:int

The number of bytes in bytes which are relevant.

encoding:Encoding

To use if non-ascii bytes seen.

Returns:String

Decoded String from bytes.

Exceptions
IOException:
If error decoding from Encoding.
hashKeyback to summary
private static int hashKey(byte[] bytes, int offset, int length)

Generates a hash value for the relevant entries in bytes as long as all values are ascii (>= 0).

Returns:int

hash code for relevant bytes, or 0 if non-ascii bytes present.

putStringback to summary
public boolean putString(String val)

Preemptively populates a value into the cache. This is intended to be used with String constants which are frequently used. While this can work with other String values, if val is ever garbage collected, it will not be actively removed from this instance.

Parameters
val:String

The value to intern. Must not be null.

Returns:boolean

Indication if val is an ascii String and placed into cache.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.postgresql.core back to summary

private abstract Class AsciiStringInterner.BaseKey

extends Object
Class Inheritance
Known Direct Subclasses
org.postgresql.core.AsciiStringInterner.TempKey, org.postgresql.core.AsciiStringInterner.Key

Field Summary

Modifier and TypeField and Description
private final int

Constructor Summary

AccessConstructor and Description
pack-priv
BaseKey(int hash)

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract void
public final boolean
equals(@Nullable Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

pack-priv abstract boolean
equals(byte[] other, int offset, int length)

pack-priv abstract boolean
public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

hashback to summary
private final int hash

Constructor Detail

BaseKeyback to summary
pack-priv BaseKey(int hash)

Method Detail

appendStringback to summary
pack-priv abstract void appendString(StringBuilder sb)
equalsback to summary
public final boolean equals(@Nullable Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:@Nullable Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
equalsback to summary
pack-priv abstract boolean equals(byte[] other, int offset, int length)
equalsBytesback to summary
pack-priv abstract boolean equalsBytes(AsciiStringInterner.BaseKey other)
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
org.postgresql.core back to summary

private final Class AsciiStringInterner.Key

extends BaseKey
Class Inheritance

Instance used for inserting values into the cache. The byte[] must be a copy that will never be mutated.

Field Summary

Modifier and TypeField and Description
pack-priv final byte[]

Constructor Summary

AccessConstructor and Description
pack-priv
Key(byte[] key, int hash)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public boolean
equals(byte[] other, int offset, int length)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.equals.

pack-priv boolean
Inherited from org.postgresql.core.AsciiStringInterner.BaseKey:
equalshashCode

Field Detail

keyback to summary
pack-priv final byte[] key

Constructor Detail

Keyback to summary
pack-priv Key(byte[] key, int hash)

Method Detail

appendStringback to summary
pack-priv void appendString(StringBuilder sb)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.appendString.

Annotations
@Override
equalsback to summary
public boolean equals(byte[] other, int offset, int length)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.equals.

Annotations
@Override
equalsBytesback to summary
pack-priv boolean equalsBytes(AsciiStringInterner.BaseKey other)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.equalsBytes.

Annotations
@Override
org.postgresql.core back to summary

private final Class AsciiStringInterner.StringReference

extends SoftReference<String>
Class Inheritance

Custom SoftReference implementation which maintains a reference to the key in the cache, which allows aggressive cleaning when garbage collector collects the String instance.

Field Summary

Modifier and TypeField and Description
private final AsciiStringInterner.BaseKey

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
Inherited from java.lang.ref.SoftReference:
get

Field Detail

keyback to summary
private final AsciiStringInterner.BaseKey key

Constructor Detail

StringReferenceback to summary
pack-priv StringReference(AsciiStringInterner.BaseKey key, String referent)

Method Detail

disposeback to summary
pack-priv void dispose()
org.postgresql.core back to summary

private Class AsciiStringInterner.TempKey

extends BaseKey
Class Inheritance

Only used for lookups, never to actually store entries.

Field Summary

Modifier and TypeField and Description
pack-priv final byte[]
pack-priv final int
pack-priv final int

Constructor Summary

AccessConstructor and Description
pack-priv
TempKey(int hash, byte[] bytes, int offset, int length)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public boolean
equals(byte[] other, int offset, int length)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.equals.

pack-priv boolean
Inherited from org.postgresql.core.AsciiStringInterner.BaseKey:
equalshashCode

Field Detail

bytesback to summary
pack-priv final byte[] bytes
lengthback to summary
pack-priv final int length
offsetback to summary
pack-priv final int offset

Constructor Detail

TempKeyback to summary
pack-priv TempKey(int hash, byte[] bytes, int offset, int length)

Method Detail

appendStringback to summary
pack-priv void appendString(StringBuilder sb)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.appendString.

Annotations
@Override
equalsback to summary
public boolean equals(byte[] other, int offset, int length)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.equals.

Annotations
@Override
equalsBytesback to summary
pack-priv boolean equalsBytes(AsciiStringInterner.BaseKey other)

Implements abstract org.postgresql.core.AsciiStringInterner.BaseKey.equalsBytes.

Annotations
@Override