Java源码示例:org.apache.cassandra.db.marshal.UserType
示例1
@Override
public UserType convert(DataType dataType) {
com.datastax.driver.core.UserType userType = (com.datastax.driver.core.UserType) dataType;
String typeNameString = userType.getTypeName();
Collection<String> fieldNames = userType.getFieldNames();
List<AbstractType<?>> innerAbstractTypes = new ArrayList<>(fieldNames.size());
ByteBuffer typeNameBuffer = UTF8Type.instance.fromString(typeNameString);
List<FieldIdentifier> fieldIdentifiers = new ArrayList<>(fieldNames.size());
for (String fieldName : fieldNames) {
fieldIdentifiers.add(FieldIdentifier.forInternalString(fieldName));
innerAbstractTypes.add((CassandraTypeConverter.convert(userType.getFieldType(fieldName))));
}
return new UserType(userType.getKeyspace(),
typeNameBuffer,
fieldIdentifiers,
innerAbstractTypes,
!userType.isFrozen());
}
示例2
public Object deserialize(AbstractType<?> abstractType, ByteBuffer bb) {
ByteBuffer userTypeByteBuffer = (ByteBuffer) super.deserialize(abstractType, bb);
UserType userType = (UserType) abstractType;
UserTypes.Value value = UserTypes.Value.fromSerialized(userTypeByteBuffer, userType);
List<ByteBuffer> elements = value.getElements();
Struct struct = new Struct(getSchemaBuilder(abstractType).build());
for (int i = 0; i < userType.fieldNames().size(); i++) {
String fieldName = userType.fieldNameAsString(i);
AbstractType<?> fieldType = userType.type(i);
struct.put(fieldName, CassandraTypeDeserializer.deserialize(fieldType, elements.get(i)));
}
return struct;
}
示例3
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
if (!(receiver.type instanceof UserType))
throw new InvalidRequestException(String.format("Invalid user type literal for %s of type %s", receiver, receiver.type.asCQL3Type()));
UserType ut = (UserType)receiver.type;
for (int i = 0; i < ut.size(); i++)
{
ColumnIdentifier field = new ColumnIdentifier(ut.fieldName(i), UTF8Type.instance);
Term.Raw value = entries.get(field);
if (value == null)
continue;
ColumnSpecification fieldSpec = fieldSpecOf(receiver, i);
if (!value.isAssignableTo(keyspace, fieldSpec))
throw new InvalidRequestException(String.format("Invalid user type literal for %s: field %s is not of type %s", receiver, field, fieldSpec.type.asCQL3Type()));
}
}
示例4
@Override
public SchemaBuilder getSchemaBuilder(AbstractType<?> abstractType) {
UserType userType = (UserType) abstractType;
SchemaBuilder schemaBuilder = SchemaBuilder.struct().name(userType.keyspace + "." + userType.getNameAsString());
List<org.apache.cassandra.cql3.FieldIdentifier> fieldIdentifiers = userType.fieldNames();
List<AbstractType<?>> fieldTypes = userType.fieldTypes();
for (int i = 0; i < fieldIdentifiers.size(); i++) {
Schema fieldSchema = CassandraTypeDeserializer.getSchemaBuilder(fieldTypes.get(i)).build();
schemaBuilder.field(fieldIdentifiers.get(i).toString(), fieldSchema);
}
return schemaBuilder;
}
示例5
private static Types getTypes() {
if (knownTypes.isEmpty()) {
return Types.none();
} else {
return Types.of(knownTypes.values().toArray(new UserType[0]));
}
}
示例6
public static ColumnSpecification fieldSpecOf(ColumnSpecification column, int field)
{
UserType ut = (UserType)column.type;
return new ColumnSpecification(column.ksName,
column.cfName,
new ColumnIdentifier(column.name + "." + UTF8Type.instance.compose(ut.fieldName(field)), true),
ut.fieldType(field));
}
示例7
public static void checkForDuplicateNames(UserType type) throws InvalidRequestException
{
for (int i = 0; i < type.size() - 1; i++)
{
ByteBuffer fieldName = type.fieldName(i);
for (int j = i+1; j < type.size(); j++)
{
if (fieldName.equals(type.fieldName(j)))
throw new InvalidRequestException(String.format("Duplicate field name %s in type %s",
UTF8Type.instance.getString(fieldName),
UTF8Type.instance.getString(type.name)));
}
}
}
示例8
private UserType createType() throws InvalidRequestException
{
List<ByteBuffer> names = new ArrayList<>(columnNames.size());
for (ColumnIdentifier name : columnNames)
names.add(name.bytes);
List<AbstractType<?>> types = new ArrayList<>(columnTypes.size());
for (CQL3Type.Raw type : columnTypes)
types.add(type.prepare(keyspace()).getType());
return new UserType(name.getKeyspace(), name.getUserTypeName(), names, types);
}
示例9
private static void addType(UserType ut)
{
KSMetaData ksm = Schema.instance.getKSMetaData(ut.keyspace);
assert ksm != null;
logger.info("Loading {}", ut);
ksm.userTypes.addType(ut);
if (!StorageService.instance.isClientMode())
MigrationManager.instance.notifyCreateUserType(ut);
}
示例10
private static void updateType(UserType ut)
{
KSMetaData ksm = Schema.instance.getKSMetaData(ut.keyspace);
assert ksm != null;
logger.info("Updating {}", ut);
ksm.userTypes.addType(ut);
if (!StorageService.instance.isClientMode())
MigrationManager.instance.notifyUpdateUserType(ut);
}
示例11
private static void dropType(UserType ut)
{
KSMetaData ksm = Schema.instance.getKSMetaData(ut.keyspace);
assert ksm != null;
ksm.userTypes.removeType(ut);
if (!StorageService.instance.isClientMode())
MigrationManager.instance.notifyDropUserType(ut);
}
示例12
@Test
public void testUserType() {
// this is slightly complicated, so we're testing in two parts:
// first, explicitly test for schema correctness
ByteBuffer expectedTypeName = ByteBuffer.wrap("FooType".getBytes(Charset.defaultCharset()));
List<FieldIdentifier> expectedFieldIdentifiers = new ArrayList<>();
expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("asciiField".getBytes(Charset.defaultCharset()))));
expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("doubleField".getBytes(Charset.defaultCharset()))));
expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("durationField".getBytes(Charset.defaultCharset()))));
// testing duration to make sure that recursive deserialization works correctly
List<AbstractType<?>> expectedFieldTypes = new ArrayList<>();
expectedFieldTypes.add(AsciiType.instance);
expectedFieldTypes.add(DoubleType.instance);
expectedFieldTypes.add(DurationType.instance);
UserType userType = new UserType("barspace",
expectedTypeName,
expectedFieldIdentifiers,
expectedFieldTypes,
true);
Schema userSchema = CassandraTypeDeserializer.getSchemaBuilder(userType).build();
long expectedNanoDuration = (30 + 2) * ChronoUnit.DAYS.getDuration().toNanos() + 3;
Struct expectedUserTypeData = new Struct(userSchema)
.put("asciiField", "foobar")
.put("doubleField", 1.5d)
.put("durationField", expectedNanoDuration);
Map<String, Object> jsonObject = new HashMap<>(3);
jsonObject.put("\"asciiField\"", "foobar");
jsonObject.put("\"doubleField\"", 1.5d);
jsonObject.put("\"durationField\"", DurationType.instance.getSerializer().toString(Duration.newInstance(1, 2, 3)));
Term userTypeObject = userType.fromJSONObject(jsonObject);
ByteBuffer buffer = userTypeObject.bindAndGet(QueryOptions.DEFAULT);
ByteBuffer serializedUserTypeObject = userType.decompose(buffer);
Object deserializedUserTypeObject = CassandraTypeDeserializer.deserialize(userType, serializedUserTypeObject);
Assert.assertEquals(expectedUserTypeData, deserializedUserTypeObject);
}
示例13
public DelayedValue(UserType type, List<Term> values)
{
this.type = type;
this.values = values;
}
示例14
@Override
public ByteBuffer bindAndGet(QueryOptions options) throws InvalidRequestException
{
return UserType.buildValue(bindInternal(options));
}
示例15
public FieldSelector(UserType type, int field, Selector selected)
{
this.type = type;
this.field = field;
this.selected = selected;
}
示例16
public void notifyCreateUserType(UserType ut)
{
for (MigrationListener listener : listeners)
listener.onCreateUserType(ut.keyspace, ut.getNameAsString());
}
示例17
public void notifyUpdateUserType(UserType ut)
{
for (MigrationListener listener : listeners)
listener.onUpdateUserType(ut.keyspace, ut.getNameAsString());
}
示例18
public void notifyDropUserType(UserType ut)
{
for (MigrationListener listener : listeners)
listener.onDropUserType(ut.keyspace, ut.getNameAsString());
}
示例19
public static void announceNewType(UserType newType)
{
announceNewType(newType, false);
}
示例20
public static void announceNewType(UserType newType, boolean announceLocally)
{
announce(addSerializedKeyspace(UTMetaData.toSchema(newType, FBUtilities.timestampMicros()), newType.keyspace), announceLocally);
}
示例21
public static void announceTypeUpdate(UserType updatedType)
{
announceTypeUpdate(updatedType, false);
}
示例22
public static void announceTypeUpdate(UserType updatedType, boolean announceLocally)
{
announceNewType(updatedType, announceLocally);
}
示例23
public static void announceTypeDrop(UserType droppedType)
{
announceTypeDrop(droppedType, false);
}
示例24
public static void announceTypeDrop(UserType droppedType, boolean announceLocally)
{
announce(addSerializedKeyspace(UTMetaData.dropFromSchema(droppedType, FBUtilities.timestampMicros()), droppedType.keyspace), announceLocally);
}