protected Session getSession() { if (session == null) { session = cluster.getSession(); defaultReadLevel = cluster.getDefaultReadConsistencyLevel(); defaultWriteLevel = cluster.getDefaultWriteConsistencyLevel(); CodecRegistry registry = session.getCluster().getConfiguration().getCodecRegistry(); registerCodecIfNotFound(registry, new JsonCodec()); registerCodecIfNotFound(registry, new DeviceCredentialsTypeCodec()); registerCodecIfNotFound(registry, new AuthorityCodec()); registerCodecIfNotFound(registry, new ComponentLifecycleStateCodec()); registerCodecIfNotFound(registry, new ComponentTypeCodec()); registerCodecIfNotFound(registry, new ComponentScopeCodec()); registerCodecIfNotFound(registry, new EntityTypeCodec()); } return session; }
public CassandraStorage(ReaperApplicationConfiguration config, Environment environment) { CassandraFactory cassandraFactory = config.getCassandraFactory(); overrideQueryOptions(cassandraFactory); overrideRetryPolicy(cassandraFactory); overridePoolingOptions(cassandraFactory); cassandra = cassandraFactory.build(environment); if (config.getActivateQueryLogger()) { cassandra.register(QueryLogger.builder().build()); } CodecRegistry codecRegistry = cassandra.getConfiguration().getCodecRegistry(); codecRegistry.register(new DateTimeCodec()); session = cassandra.connect(config.getCassandraFactory().getKeyspace()); initializeAndUpgradeSchema(cassandra, session, config.getCassandraFactory().getKeyspace()); prepareStatements(); }
private void registerCodecs() { complexTypeCodecs = getCodecsForUserDefinedTypes(); if (complexTypeCodecs != null) { CodecRegistry registry = cluster.getConfiguration().getCodecRegistry(); if (cluster.getConfiguration().getProtocolOptions().getProtocolVersion().toInt() < 4) { LOG.error("Custom codecs are not supported for protocol version < 4"); throw new RuntimeException("Custom codecs are not supported for protocol version < 4"); } for (String typeCodecStr : complexTypeCodecs.keySet()) { TypeCodec codec = complexTypeCodecs.get(typeCodecStr); registry.register(codec); userDefinedTypesClass.put(typeCodecStr, codec.getJavaType().getRawType()); } } else { complexTypeCodecs = new HashMap<>(); } }
@Override public Map<String, TypeCodec> getCodecsForUserDefinedTypes() { Map<String, TypeCodec> allCodecs = new HashMap<>(); CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); UserType addressType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName()) .getUserType("address"); TypeCodec<UDTValue> addressTypeCodec = codecRegistry.codecFor(addressType); AddressCodec addressCodec = new AddressCodec(addressTypeCodec, Address.class); allCodecs.put("currentaddress", addressCodec); UserType userFullNameType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName()) .getUserType("fullname"); TypeCodec<UDTValue> userFullNameTypeCodec = codecRegistry.codecFor(userFullNameType); FullNameCodec fullNameCodec = new FullNameCodec(userFullNameTypeCodec, FullName.class); allCodecs.put("username", fullNameCodec); return allCodecs; }
public static DatastaxCassandraService datastaxCassandraService() { return DatastaxCassandraService.builder() .withNodes(SEEDS) .withConnectionsPerHostLocal(8) .withConnectionsPerHostRemote(2) .withCodecRegistry(new CodecRegistry() .register(InstantCodec.instance) .register(LocalDateCodec.instance) .register(new JacksonJsonCodec<>( org.atlasapi.content.v2.model.Clip.Wrapper.class, MAPPER )) .register(new JacksonJsonCodec<>( org.atlasapi.content.v2.model.Encoding.Wrapper.class, MAPPER )) ) .build(); }
private Cluster doCreateCluster(CassandraProperties properties) { Cluster cluster = Cluster.builder() .withClusterName(properties.getCluster()) .withPort(properties.getPort()) .addContactPoints(properties.getContactPoints()) .withTimestampGenerator(getTimestampGenerator()) .withPoolingOptions( //TODO some default options - move to config new PoolingOptions() .setConnectionsPerHost(HostDistance.LOCAL, 4, 4) .setConnectionsPerHost(HostDistance.REMOTE, 2, 2) .setMaxRequestsPerConnection(HostDistance.LOCAL, 1024) .setMaxRequestsPerConnection(HostDistance.REMOTE, 256) ) .build(); //almost all queries are idempotent except counter updates, so it's easier to mark them as idempotent cluster.getConfiguration().getQueryOptions().setDefaultIdempotence(true); CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); TupleType tupleType = cluster.getMetadata() .newTupleType(DataType.timestamp(), DataType.varchar()); codecRegistry.register(new ZonedDateTimeCodec(tupleType)); QueryLogger queryLogger = QueryLogger.builder() .withConstantThreshold(100) .withMaxQueryStringLength(200) .build(); cluster.register(queryLogger); return cluster; }
private void registerCodecIfNotFound(CodecRegistry registry, TypeCodec<?> codec) { try { registry.codecFor(codec.getCqlType(), codec.getJavaType()); } catch (CodecNotFoundException e) { registry.register(codec); } }
public CqlDeltaIterator(Iterator<Row> iterator, final int blockIndex, final int changeIdIndex, final int contentIndex, boolean reversed, int prefixLength, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { super(iterator, reversed, prefixLength); _blockIndex = blockIndex; _changeIdIndex = changeIdIndex; _contentIndex = contentIndex; _protocolVersion = protocolVersion; _codecRegistry = codecRegistry; }
private Cluster getCluster() throws StageException { return Cluster.builder() .addContactPoints(contactPoints) // If authentication is disabled on the C* cluster, this method has no effect. .withAuthProvider(getAuthProvider()) .withProtocolVersion(conf.protocolVersion) .withPort(conf.port) .withCodecRegistry(new CodecRegistry().register(SDC_CODECS)) .build(); }
public CassandraMetricBatch(Metadata metadata, ProtocolOptions protocol, CodecRegistry codec, TokenAwarePolicy lbPolicy, int batchLimit) { this.protocol = protocol; this.codec = codec; this.metadata = metadata; this.policy = lbPolicy; metricQueries = new HashMap<>(); this.batchLimit = batchLimit; metricQueries = new HashMap<>(); dimensionQueries = new HashMap<>(); dimensionMetricQueries = new HashMap<>(); metricDimensionQueries = new HashMap<>(); measurementQueries = new HashMap<>(); }
/** * Serialize a field using the data type passed. * @param dataType * @param value * @return */ @SuppressWarnings("unchecked") public <T> ByteBuffer serialize(DataType dataType, Object value) { final CodecRegistry codecRegistry = getCodecRegistry(); final TypeCodec<T> typeCodec = codecRegistry.codecFor(dataType); return typeCodec.serialize((T)value, protocolVersion); }
@Override public CodecRegistry getCodecRegistry() { return preparedStatement.getCodecRegistry(); }
private static CodecRegistry codecRegistry() { CodecRegistry registry = new CodecRegistry(); registry.register(LocalDateCodec.instance); return registry; }
private static CodecRegistry codecRegistry() { final CodecRegistry registry = new CodecRegistry(); registry.register(LocalDateCodec.instance); return registry; }
public CodecRegistry getCodecRegistry() { return cluster.getConfiguration().getCodecRegistry(); }
/** * @param datatype the db datatype * @param udtValue the udt value * @param fieldtype1 the field 1 type * @param fieldtype2 the field 2 type * @param fieldname the fieldname * @return the mapped value or <code>null</code> */ public <T> Object fromUdtValue(DataType datatype, UDTValue udtValue, Class<?> fieldtype1, Class<?> fieldtype2, String fieldname) { final CodecRegistry codecRegistry = getCodecRegistry(); // build-in type if (isBuildInType(datatype)) { final TypeCodec<T> typeCodec = codecRegistry.codecFor(datatype); try { if (udtValue.isNull(fieldname)) return null; return typeCodec.deserialize(udtValue.getBytesUnsafe(fieldname), protocolVersion); } catch(IllegalArgumentException ex) { return null; } // udt collection } else if (datatype.isCollection()) { // set if (DataType.Name.SET == datatype.getName()) { return fromUdtValues(datatype.getTypeArguments().get(0), ImmutableSet.copyOf(udtValue.getSet(fieldname, UDTValue.class)), fieldtype2); // list } else if (DataType.Name.LIST == datatype.getName()) { return fromUdtValues(datatype.getTypeArguments().get(0), ImmutableList.copyOf(udtValue.getList(fieldname, UDTValue.class)), fieldtype2); // map } else { if (isBuildInType(datatype.getTypeArguments().get(0))) { return fromUdtValues(datatype.getTypeArguments().get(0), datatype.getTypeArguments().get(1), ImmutableMap.<Object, Object>copyOf(udtValue.getMap(fieldname, fieldtype1, UDTValue.class)), fieldtype1, fieldtype2); } else if (isBuildInType(datatype.getTypeArguments().get(1))) { return fromUdtValues(datatype.getTypeArguments().get(0), datatype.getTypeArguments().get(1), ImmutableMap.<Object, Object>copyOf(udtValue.getMap(fieldname, UDTValue.class, fieldtype2)), fieldtype1, fieldtype2); } else { return fromUdtValues(datatype.getTypeArguments().get(0), datatype.getTypeArguments().get(1), ImmutableMap.<Object, Object>copyOf(udtValue.getMap(fieldname, UDTValue.class, UDTValue.class)), fieldtype1, fieldtype2); } } // udt } else { return fromUdtValue(datatype, udtValue, fieldtype1); } }
/** * Serialize a field using the Codec for the value itself * @param value * @return */ public <T> ByteBuffer serialize(T value) { final CodecRegistry codecRegistry = getCodecRegistry(); final TypeCodec<T> typeCodec = codecRegistry.codecFor(value); return typeCodec.serialize((T)value, protocolVersion); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public void configure(final Env env, final Config conf, final Binder binder) { ConnectionString cstr = Try.apply(() -> ConnectionString.parse(db)) .orElseGet(() -> ConnectionString.parse(conf.getString(db))); ServiceKey serviceKey = env.serviceKey(); Throwing.Function3<Class, String, Object, Void> bind = (type, name, value) -> { serviceKey.generate(type, name, k -> { binder.bind(k).toInstance(value); }); return null; }; Cluster.Builder builder = this.builder.get() .addContactPoints(cstr.contactPoints()) .withPort(cstr.port()); // allow user configure cluster builder if (ccbuilder != null) { ccbuilder.accept(builder, conf); } log.debug("Starting {}", cstr); Cluster cluster = builder.build(); // allow user configure cluster if (cc != null) { cc.accept(cluster, conf); } /** codecs */ Configuration configuration = cluster.getConfiguration(); CodecRegistry codecRegistry = configuration.getCodecRegistry(); // java 8 codecs codecRegistry.register( InstantCodec.instance, LocalDateCodec.instance, LocalTimeCodec.instance); hierarchy(cluster.getClass(), type -> bind.apply(type, cstr.keyspace(), cluster)); /** Session + Mapper */ Session session = cluster.connect(cstr.keyspace()); hierarchy(session.getClass(), type -> bind.apply(type, cstr.keyspace(), session)); MappingManager manager = new MappingManager(session); bind.apply(MappingManager.class, cstr.keyspace(), manager); bind.apply(Datastore.class, cstr.keyspace(), new Datastore(manager)); /** accessors */ accesors.forEach(c -> { Object accessor = manager.createAccessor(c); binder.bind(c).toInstance(accessor); }); env.router() .map(new CassandraMapper()); env.onStop(() -> { log.debug("Stopping {}", cstr); Try.run(session::close) .onFailure(x -> log.error("session.close() resulted in exception", x)); cluster.close(); log.info("Stopped {}", cstr); }); }
@Bean public CassandraPersistenceModule persistenceModule() { Iterable<String> seeds = Splitter.on(",").split(cassandraSeeds); ConfiguredAstyanaxContext contextSupplier = new ConfiguredAstyanaxContext(cassandraCluster, cassandraKeyspace, seeds, Integer.parseInt(cassandraPort), Integer.parseInt(cassandraClientThreads), Integer.parseInt(cassandraConnectionTimeout), metricsModule.metrics() ); AstyanaxContext<Keyspace> context = contextSupplier.get(); context.start(); DatastaxCassandraService cassandraService = DatastaxCassandraService.builder() .withNodes(seeds) .withConnectionsPerHostLocal(cassandraConnectionsPerHostLocal) .withConnectionsPerHostRemote(cassandraConnectionsPerHostRemote) .withCodecRegistry(new CodecRegistry() .register(InstantCodec.instance) .register(LocalDateCodec.instance) .register(new JacksonJsonCodec<>( org.atlasapi.content.v2.model.Clip.Wrapper.class, MAPPER )) .register(new JacksonJsonCodec<>( org.atlasapi.content.v2.model.Encoding.Wrapper.class, MAPPER ))) .withConnectTimeoutMillis(cassandraDatastaxConnectionTimeout) .withReadTimeoutMillis(cassandraDatastaxReadTimeout) .build(); cassandraService.startAsync().awaitRunning(); return CassandraPersistenceModule.builder() .withMessageSenderFactory(messaging.messageSenderFactory()) .withAstyanaxContext(context) .withDatastaxCassandraService(cassandraService) .withKeyspace(cassandraKeyspace) .withIdGeneratorBuilder(idGeneratorBuilder()) .withContentHasher(ContentHashGenerator.create( HashGenerator.create(), UTIL_METRIC_PREFIX, metricsModule.metrics() )) .withEventHasher(eventV2 -> UUID.randomUUID().toString()) .withMetrics(metricsModule.metrics()) .build(); }
/** * Get the CodecRegistry this uses to serialize/deserialize * @return the codecRegistry */ public CodecRegistry getCodecRegistry() { return this.codecRegistry; }
/** * jwestra: 3.x API change * deserialize a single field in a UDTValue map * @param dataType * @param udtValue * @param fieldname * @return */ public <T> T deserialize(DataType dataType, UDTValue udtValue, String fieldname) { final CodecRegistry codecRegistry = getCodecRegistry(); final TypeCodec<T> typeCodec = codecRegistry.codecFor(dataType); return typeCodec.deserialize(udtValue.getBytesUnsafe(fieldname), protocolVersion); }
/** * Deserialize a whole ByteBuffer into an object * @param dataType * @param byteBuffer * @return */ public <T> T deserialize(DataType dataType, ByteBuffer byteBuffer) { final CodecRegistry codecRegistry = getCodecRegistry(); final TypeCodec<T> typeCodec = codecRegistry.codecFor(dataType); return typeCodec.deserialize(byteBuffer, protocolVersion); }
public CodecRegistry getCodecRegistry() { return codecRegistry; }