kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | c559bbce6d4f454854ebf15eb5ac497fc1e7d91b286dc2cf58d1ba48a7f6e58c |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-12-18 16:17 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-12-18 16:17 +01:00 |
| Parents | 929f9e585424 |
1 file changed
modified driver-core/src/main/java/com/datastax/driver/core/CodecRegistry.java +25 −36
| @@ -41,16 +41,14 @@ | ||
| 41 | 41 | * it will lookup in the registry for a suitable codec. The registry is initialized with default codecs that handle |
| 42 | 42 | * basic conversions (e.g. CQL {@code text} to {@code java.lang.String}), and users can add their own. Complex |
| 43 | 43 | * codecs can also be generated on-the-fly from simpler ones (more details below). |
| 44 | - * <p/> | |
| 45 | - * <h3>Creating a registry</h3> | |
| 46 | - * <p/> | |
| 47 | - * <p/> | |
| 44 | + * <h3> | |
| 45 | + * Creating a registry | |
| 46 | + * </h3> | |
| 48 | 47 | * By default, the driver uses {@link CodecRegistry#DEFAULT_INSTANCE}, a shareable, JVM-wide instance initialized with |
| 49 | 48 | * built-in codecs for all the base CQL types. |
| 50 | 49 | * The only reason to create your own instances is if you have multiple {@code Cluster} objects that use different |
| 51 | 50 | * sets of codecs. In that case, use {@link com.datastax.driver.core.Cluster.Builder#withCodecRegistry(CodecRegistry)} |
| 52 | 51 | * to associate the registry with the cluster: |
| 53 | - * <p/> | |
| 54 | 52 | * <pre> |
| 55 | 53 | * {@code |
| 56 | 54 | * CodecRegistry myCodecRegistry = new CodecRegistry(); |
| @@ -58,29 +56,25 @@ | ||
| 58 | 56 | * Cluster cluster = Cluster.builder().withCodecRegistry(myCodecRegistry).build(); |
| 59 | 57 | * |
| 60 | 58 | * // To retrieve the registry later: |
| 61 | - * CodecRegistry registry = cluster.getConfiguration().getCodecRegistry(); | |
| 62 | - * }</pre> | |
| 63 | - * | |
| 59 | + * CodecRegistry registry = cluster.getConfiguration().getCodecRegistry();} | |
| 60 | + * </pre> | |
| 64 | 61 | * {@code CodecRegistry} instances are thread-safe. |
| 65 | - * | |
| 66 | - * | |
| 62 | + * <p/> | |
| 67 | 63 | * It is possible to turn on log messages by setting the {@code com.datastax.driver.core.CodecRegistry} logger |
| 68 | 64 | * level to {@code TRACE}. Beware that the registry can be very verbose at this log level. |
| 69 | - * | |
| 70 | - * <h3>Registering and using custom codecs</h3> | |
| 71 | - * | |
| 65 | + * <h3> | |
| 66 | + * Registering and using custom codecs | |
| 67 | + * </h3> | |
| 72 | 68 | * To create a custom codec, write a class that extends {@link TypeCodec}, create an instance, and pass it to one of |
| 73 | 69 | * the {@link #register(TypeCodec) register} methods; for example, one could create a codec that maps CQL |
| 74 | 70 | * timestamps to JDK8's {@code java.time.LocalDate}: |
| 75 | - * | |
| 76 | 71 | * <pre> |
| 77 | 72 | * {@code |
| 78 | 73 | * class LocalDateCodec extends TypeCodec<java.time.LocalDate> { |
| 79 | 74 | * ... |
| 80 | 75 | * } |
| 81 | - * myCodecRegistry.register(new LocalDateCodec()); | |
| 82 | - * }</pre> | |
| 83 | - * | |
| 76 | + * myCodecRegistry.register(new LocalDateCodec());} | |
| 77 | + * </pre> | |
| 84 | 78 | * The conversion will be available to: |
| 85 | 79 | * <ul> |
| 86 | 80 | * <li>all driver types that implement {@link GettableByIndexData}, {@link GettableByNameData}, |
| @@ -89,21 +83,20 @@ | ||
| 89 | 83 | * <li>{@link SimpleStatement(String, Object...) simple statements};</li> |
| 90 | 84 | * <li>statements created with the {@link com.datastax.driver.core.querybuilder.QueryBuilder Query builder}.</li> |
| 91 | 85 | * </ul> |
| 92 | - * | |
| 86 | + * <p/> | |
| 93 | 87 | * Example: |
| 94 | - * <pre>{@code | |
| 88 | + * <pre> | |
| 89 | + * {@code | |
| 95 | 90 | * Row row = session.executeQuery("select date from some_table where pk = 1").one(); |
| 96 | - * java.time.LocalDate date = row.get(0, java.time.LocalDate.class); // uses LocalDateCodec registered above | |
| 97 | - * }</pre> | |
| 98 | - * | |
| 91 | + * java.time.LocalDate date = row.get(0, java.time.LocalDate.class); // uses LocalDateCodec registered above} | |
| 92 | + * </pre> | |
| 99 | 93 | * You can also bypass the codec registry by passing a standalone codec instance to methods such as |
| 100 | 94 | * {@link GettableByIndexData#get(int, TypeCodec)}. |
| 101 | - * | |
| 102 | - * <h3>Codec generation</h3> | |
| 103 | - * | |
| 95 | + * <h3> | |
| 96 | + * Codec generation | |
| 97 | + * </h3> | |
| 104 | 98 | * When a {@code CodecRegistry} cannot find a suitable codec among existing ones, it will attempt to create it on-the-fly. |
| 105 | 99 | * It can manage: |
| 106 | - * | |
| 107 | 100 | * <ul> |
| 108 | 101 | * <li>collections (lists, sets and maps) of known types. For example, |
| 109 | 102 | * if you registered a codec for JDK8's {@code java.time.LocalDate} like in the example above, you get |
| @@ -116,12 +109,11 @@ | ||
| 116 | 109 | * <li>{@link TupleType tuple types}, mapped to {@link TupleValue} (with the same rules for nested fields);</li> |
| 117 | 110 | * <li>{@link com.datastax.driver.core.DataType.CustomType custom types}, mapped to {@code ByteBuffer}.</li> |
| 118 | 111 | * </ul> |
| 119 | - * | |
| 120 | 112 | * If the codec registry encounters a mapping that it can't handle automatically, a {@link CodecNotFoundException} is thrown; |
| 121 | 113 | * you'll need to register a custom codec for it. |
| 122 | - * | |
| 123 | - * <h3>Performance and caching</h3> | |
| 124 | - * | |
| 114 | + * <h3> | |
| 115 | + * Performance and caching | |
| 116 | + * </h3> | |
| 125 | 117 | * Whenever possible, the registry will cache the result of a codec lookup for a specific type mapping, including any generated |
| 126 | 118 | * codec. For example, if you registered {@code LocalDateCodec} and ask the registry for a codec to convert a CQL |
| 127 | 119 | * {@code list<timestamp>} to a Java {@code List<LocalDate>}: |
| @@ -130,13 +122,11 @@ | ||
| 130 | 122 | * the cache;</li> |
| 131 | 123 | * <li>the second lookup will hit the cache directly, and reuse the previously generated instance.</li> |
| 132 | 124 | * </ol> |
| 133 | - * | |
| 134 | 125 | * The javadoc for each {@link #codecFor(DataType) codecFor} variant specifies whether the result can be cached or not. |
| 135 | - * | |
| 136 | - * <h3>Codec order</h3> | |
| 137 | - * | |
| 126 | + * <h3> | |
| 127 | + * Codec order | |
| 128 | + * </h3> | |
| 138 | 129 | * When the registry looks up a codec, the rules of precedence are: |
| 139 | - * | |
| 140 | 130 | * <ul> |
| 141 | 131 | * <li>if a result was previously cached for that mapping, it is returned;</li> |
| 142 | 132 | * <li>otherwise, the registry checks the list of "basic" codecs: the default ones, and the ones that were explicitly |
| @@ -144,7 +134,6 @@ | ||
| 144 | 134 | * it can handle the mapping, and if so returns it;</li> |
| 145 | 135 | * <li>otherwise, the registry tries to generate a codec, according to the rules outlined above.</li> |
| 146 | 136 | * </ul> |
| 147 | - * | |
| 148 | 137 | * It is currently impossible to override an existing codec. If you try to do so, {@link #register(TypeCodec)} will log a |
| 149 | 138 | * warning and ignore it. |
| 150 | 139 | */ |