Pluggable Typehandler Benefits
As the name suggests Pluggable Typehandler allows anybody to
write custom typehandlers, and thus control the way the class objects are
stored to the database and retrieved in a query. Why would you do this? There
can be various reasons:
-
You know a more performant way to convert
objects to byte array or to compare them.
-
You need to store only part of the object's
information and want to skip unneeded fields to keep the database smaller. You
can also do the same using Transient
marker, but this is only possible for classes with available code. Using custom
typehandler you can configure partial storage for any third-party class.
-
You need to keep information that will allow you
to restore fields that cannot be stored as is, for example: references to
environmental variables (like time zone), proxy objects or variables of
temporary state (like current memory usage). Previously, this job was done by
Translators, but certainly custom Typehandler gives you more control
and unifies the approach.
-
You want to customize the way Strings are stored
(special encodings).
-
You need to perform a complex refactoring
on-the-fly (use typehandler versioning)
-
You want to cipher each object before putting it
into the database
-
You want to implement cascade on delete through the
typehandler
Other not so common and more
difficult in realization behaviours that can be realized with the new
Typehandler:
-
Custom handling of platform-specific generic
collections
-
Fast collection handlers that operate on a lower
level and scale for large collections
-
Customary indexes
-
Versioning of typehandlers (can be used for
refactoring and db4o version upgrades)
Of course, writing typehandlers
is not totally simple, but once you understand how to do that - you will also
gain a much deeper understanding of db4o itself. You can start with a simple
example provided in this documentation and continue by looking into existing
db4o typehandler implementations: StringHandler, VariableLengthTypeHandler,
IndexableTypeHandler etc.