com.persistit.encoding.CoderManager Maven / Gradle / Ivy
Show all versions of akiban-persistit Show documentation
/** * Copyright © 2005-2012 Akiban Technologies, Inc. All rights reserved. * * This program and the accompanying materials are made available * under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * This program may also be available under different license terms. * For more information, see www.akiban.com or contact [email protected]. * * Contributors: * Akiban Technologies, Inc. */ package com.persistit.encoding; import java.util.Map; import com.persistit.DefaultCoderManager; import com.persistit.Exchange; import com.persistit.Key; /** *
KeyCoder. * * This leaves the question of how keys that may contain encoded objects of * different classes are ordered. For example, consider the code fragment shown * here:* Manages the collections of {@link KeyCoder}s and {@link ValueCoder}s that * encode and decode object values in Persistit™. An application should * register all
*KeyCoder
andValueCoder
* implementations before storing or accessing objects of the associated classes * in Persistit. ** When Persistit is initialized, a instance of {@link DefaultCoderManager} is * installed as the current
CoderManager
. Applications can refer to * it through the method {@link com.persistit.Persistit#getCoderManager}. * Applications should registerKeyCoder
and *ValueCoder
instances as follows:* *Subsequently, any time the application appends an instance of a ** ValueCoder vc1 = new MyClassValueCoder(); * KeyCoder kc1 = new MyClassKeyCoder(); * Persistit.getInstance().getCoderManager().registerValueCoder(MyClass.class, kc1); * Persistit.getInstance().getCoderManager().registerKeyCoder(MyClass.class, kc1); ** *MyClass
to aKey
, theMyClassKeyCoder
* will be used to convert the interior state of theMyClass
into a * representation within theKey
. Likewise, whenever the * application puts an object of classMyClass
into a *Value
, the registeredMyClassValueCoder
will be * used to encode it. Persistit uses the {@link #lookupKeyCoder} and * {@link #lookupValueCoder} methods to acquire the appropriate coder. * ** An application may replace the default implementation provided by *
*DefaultCoderManager
. Such a custom implementation might, for * example, handle the relationship between certain classes and their associated * coders in a special way. ** An application can create and install a customized
CoderManager
* as follows:* *Note that the* CoderManager myCoderManager = new MyCoderManager(Persistit.getInstance().getCoderManager()); * Persistit.getInstance().setCoderManager(myCoderManager); ** *MyCoderManager
instance receives the * previously installedCoderManager
as an argument of its * constructor. The newCoderManager
should return the previously * installed one as the value of its {@link #getParent} method, and the * {@link #lookupKeyCoder} and {@link #lookupValueCoder} should delegate any * lookups not handled by the customizedCoderManager
back to the * parent. * *Key Ordering
** The encoding of key values determines the ordering of traversal for * {@link Exchange#traverse} and associated methods. See {@link Key} for * definition of the ordering among the types for which built-in encoding * exists. The ordering of key values based on objects that are encoded by * custom
KeyCoder
s is determined by the implementation of each ** *This code will insert two records incorporating the values ** Exchange exchange = new Exchange(...); * Object a = new MyClass1(); // where MyClass1 and MyClass2 * Object b = new MyClass2(); // have registered KeyCoders * exchange.to(a).store(); // store something with a's value as key * exchange.to(b).store(); // store something with b's value as key * exchange.to(Key.BEFORE) * while (exchange.next()) * { * System.out.println(exchange.indexTo(-1).getTypeName()); * } ** *a
andb
into Persistit, and will then traverse the * resulting tree and print the names of the classes of the key values. The * question is whether "MyClass2" will be printed before or after "MyClass2". * ** The answer depends on the order in which MyClass1 and MyClass2 were first * registered within Persistit. At the time a new class is first registered, * Persistit assigns a permanent
* * @version 1.0 */ public interface CoderManager { /** * Create aint
-valued class handle to the * class. Whenever an instance of that class is encoded within either a *Key
or aValue
, that handle value, rather than the * class name, is used. This reduces disk, memory and CPU consumption whenever * Persistit stores or retrieves objects. Handles are assigned in increasing * numeric order as classes are registered. Thus ifMyClass1
was * registered beforeMyClass2
, it will precede *MyClass2
in traversal order. Otherwise it will follow *MyClass2
. *Map
ofClass
es to registered *ValueCoder
s. The map is an unmodifiable, is sorted by class * name, and does not change with subsequent registrations or * unregistrations ofValueCoder
s. * * @returnMap
ofClass
es to *ValueCoder
s. */ public Map, ? extends ValueCoder> getRegisteredValueCoders(); /** * Create a Map
ofClass
es to registered *KeyCoder
s. The map is an unmodifiable, and is sorted by * class name, and does not change with subsequent registrations or * unregistrations ofKeyCoder
s. * * @returnMap
ofClass
es toKeyCoder
* s. */ public Map, ? extends KeyCoder> getRegisteredKeyCoders(); /** * Register the provided KeyCoder
to encode and decode * instances of supplied class. * * @param clazz * The Class for which thisKeyCoder
will provide * encoding and decoding services * @param coder * The KeyCoder to register * @return TheKeyCoder
formerly registered for this *Class
, ornull
if there was none. */ public KeyCoder registerKeyCoder(Class> clazz, KeyCoder coder); /** * Remove the registeredKeyCoder
for the supplied class. * * @param clazz * TheClass
from which to remove a *KeyCoder
, if present * @return TheKeyCoder
that was removed, ornull
* if there was none. */ public KeyCoder unregisterKeyCoder(Class> clazz); /** * Remove the suppliedKeyCoder
from all classes to which it * was previously registered. * * @param coder * TheKeyCoder
to remove. * @return The count of classes thisKeyCoder
was registered * for. */ public int unregisterKeyCoder(KeyCoder coder); /** * Returns theKeyCoder
registered for the supplied *Class
, ornull
if noKeyCoder
is * registered. * * @param clazz * TheClass
* @return TheKeyCoder
ornull
if there is none. */ public KeyCoder lookupKeyCoder(Class> clazz); /** * Register the providedValueCoder
to encode and decode * instances of supplied class. * * @param clazz * The Class for which thisValueCoder
will provide * encoding and decoding services * @param coder * The ValueCoder to register * @return TheValueCoder
that was removed, or *null
if there was none. */ public ValueCoder registerValueCoder(Class> clazz, ValueCoder coder); /** * Remove the registeredValueCoder
for the supplied class. * * @param clazz * TheClass
from which to remove a *ValueCoder
, if present * @return TheValueCoder
that was removed, or *null
if there was none. */ public ValueCoder unregisterValueCoder(Class> clazz); /** * Remove the suppliedValueCoder
from all classes to which it * was previously registered. * * @param coder * TheValueCoder
to remove. * @return The count of classes thisValueCoder
was registered * for. */ public int unregisterValueCoder(ValueCoder coder); /** * Returns theValueCoder
registered for the supplied *Class
, ornull
if noValueCoder
is * registered. * * @param clazz * TheClass
* @return TheValueCoder
ornull
if there is * none. */ public ValueCoder lookupValueCoder(Class> clazz); /** * Return aValueCoder
for the suppliedClass
. If * there is none registered, implicitly create one and register it. * * @param clazz * @return The ValueCoder */ public ValueCoder getValueCoder(Class> clazz); /** * Return the parentCoderManager
implementation, or *null
if there is none. * * @return The parentCoderManager
*/ public CoderManager getParent(); }