private MappingManager getMappingManager(Session session) { synchronized (mappingManagers) { MappingManager mappingManager = mappingManagers.get(session); if (mappingManager == null) { mappingManager = new MappingManager(session); mappingManagers.put(session, mappingManager); CodecRegistry codecRegistry = session.getCluster().getConfiguration().getCodecRegistry(); for (TypeCodec<?> codec : codecs) { codecRegistry.register(codec); } for (Class<?> udtClass : udtClasses) { mappingManager.udtCodec(udtClass); } } return mappingManager; } } }
.getConfiguration() .getCodecRegistry() .register(instance); } catch (Exception exception) { throw new IllegalArgumentException(String.format(
CodecRegistry codecRegistry = configuration.getCodecRegistry(); codecRegistry.register( InstantCodec.instance, LocalDateCodec.instance,
/** * Register the given codecs with this registry. * * @param codecs The codecs to add to the registry. * @return this CodecRegistry (for method chaining). * @see #register(TypeCodec) */ public CodecRegistry register(Iterable<? extends TypeCodec<?>> codecs) { for (TypeCodec<?> codec : codecs) register(codec); return this; }
/** * Register the given codecs with this registry. * * @param codecs The codecs to add to the registry. * @return this CodecRegistry (for method chaining). * @see #register(TypeCodec) */ public CodecRegistry register(TypeCodec<?>... codecs) { for (TypeCodec<?> codec : codecs) register(codec); return this; }
@BeforeClass(groups = "short") public void setUpRegistry() { for (TypeMapping<?> mapping : mappings) { registry.register(mapping.codec); } }
/** * Ensures that a statement can be printed with and without a required custom codec. The * expectation is that if the codec is not registered, then the query string should contain bind * markers for all variables; if however all codecs are properly registered, then the query string * should contain all variables inlined and formatted properly. * * @jira_ticket JAVA-1272 */ @Test(groups = "unit") public void should_inline_custom_codec() throws Exception { assertThat(insertInto("users").value("id", new Foo(42)).toString()) .isEqualTo("INSERT INTO users (id) VALUES (?);"); CodecRegistry.DEFAULT_INSTANCE.register(new FooCodec()); assertThat(insertInto("users").value("id", new Foo(42)).toString()) .isEqualTo("INSERT INTO users (id) VALUES (42);"); }
public Cluster.Builder createClusterBuilder() { return Cluster.builder() .withCodecRegistry( new CodecRegistry().register(new MyIntCodec()) // global User <-> varchar codec ); }
public Cluster.Builder createClusterBuilder() { return Cluster.builder() .withCodecRegistry(new CodecRegistry().register(new IntToStringCodec())); }
Session session = cluster.connect(keyspace); setUpTupleTypes(cluster); codecRegistry.register(new LocationCodec(TypeCodec.tuple(locationType))); session.execute(insertQuery, uuid, "John Doe", locationValue); ResultSet rows = session.execute(selectQuery, uuid);
Session session = cluster.connect(keyspace); setUpTupleTypes(cluster); codecRegistry.register(new LocationCodec(TypeCodec.tuple(locationType))); session.execute(insertQuery, uuid, "John Doe", partialLocationValueInserted); ResultSet rows = session.execute(selectQuery, uuid);
@Test(groups = "unit") public void should_ignore_codec_colliding_with_already_registered_codec() { MemoryAppender logs = startCapturingLogs(); CodecRegistry registry = new CodecRegistry(); TypeCodec<?> newCodec = mockCodec(cint(), of(Integer.class)); registry.register(newCodec); assertThat(logs.getNext()).contains("Ignoring codec MockCodec"); assertThat(registry.codecFor(cint(), Integer.class)).isNotSameAs(newCodec); stopCapturingLogs(logs); }
TypeCodec<UDTValue> phoneTypeCodec = TypeCodec.userType(phoneType); codecRegistry .register(new AddressCodec(addressTypeCodec, Address.class)) .register(new PhoneCodec(phoneTypeCodec, Phone.class)); session.execute(insertQuery, uuid, "John Doe", address); ResultSet rows = session.execute(selectQuery, uuid);
@Test(groups = "unit") public void should_find_newly_registered_codec_by_cql_type() { // given CodecRegistry registry = new CodecRegistry(); TypeCodec<?> expected = mockCodec(list(text()), listOf(String.class)); registry.register(expected); // when TypeCodec<?> actual = registry.codecFor(list(text())); // then assertThat(actual).isNotNull().isSameAs(expected); }
@Test(groups = "unit") public void should_find_default_codec_if_cql_type_already_registered() { // given CodecRegistry registry = new CodecRegistry(); TypeCodec<?> newCodec = mockCodec(text(), of(StringBuilder.class)); registry.register(newCodec); // when TypeCodec<?> actual = registry.codecFor(text()); // then assertThat(actual) .isNotNull() .isNotSameAs(newCodec) .accepts(text()) .accepts(String.class) .doesNotAccept(StringBuilder.class); }
@Test(groups = "unit") public void should_create_derived_codecs_for_java_type_handled_by_custom_codec() { TypeCodec<?> newCodec = mockCodec(varchar(), of(StringBuilder.class)); CodecRegistry registry = new CodecRegistry().register(newCodec); // lookup by CQL type only returns default codec assertThat(registry.codecFor(list(varchar()))).doesNotAccept(listOf(StringBuilder.class)); assertThat(registry.codecFor(list(varchar()), listOf(StringBuilder.class))).isNotNull(); }
@Test(groups = "unit") public void should_find_newly_registered_codec_by_cql_type_and_java_type() { // given CodecRegistry registry = new CodecRegistry(); TypeCodec<?> expected = mockCodec(list(text()), listOf(String.class)); registry.register(expected); // when TypeCodec<?> actual = registry.codecFor(list(text()), listOf(String.class)); // then assertThat(actual).isNotNull().isSameAs(expected); }
@Test(groups = "unit") public void should_ignore_codec_colliding_with_already_generated_codec() { MemoryAppender logs = startCapturingLogs(); CodecRegistry registry = new CodecRegistry(); // Force generation of a list token from the default token registry.codecFor(list(cint()), listOf(Integer.class)); TypeCodec<?> newCodec = mockCodec(list(cint()), listOf(Integer.class)); registry.register(newCodec); assertThat(logs.getNext()).contains("Ignoring codec MockCodec"); assertThat(registry.codecFor(list(cint()), listOf(Integer.class))).isNotSameAs(newCodec); stopCapturingLogs(logs); }
@Override public Cluster.Builder createClusterBuilder() { return Cluster.builder() .withCodecRegistry( new CodecRegistry() .register( new NumberBoxCodec<Integer>(TypeCodec.cint()), new NumberBoxCodec<Long>(TypeCodec.bigint()), new NumberBoxCodec<Float>(TypeCodec.cfloat()), new NumberBoxCodec<Double>(TypeCodec.cdouble()), new NumberBoxCodec<BigInteger>(TypeCodec.varint()), new NumberBoxCodec<BigDecimal>(TypeCodec.decimal()))); }