package org.apache.cassandra.schema;
import java.nio.ByteBuffer;
import java.util.*;
import javax.annotation.Nullable;
import com.google.common.collect.*;
import org.apache.cassandra.cql3.FieldIdentifier;
import org.apache.cassandra.cql3.CQL3Type;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.UserType;
import org.apache.cassandra.exceptions.ConfigurationException;
import static java.lang.String.format;
import static com.google.common.collect.Iterables.filter;
import static java.util.stream.Collectors.toList;
import static org.apache.cassandra.utils.ByteBufferUtil.bytes;
public final class Types implements Iterable<UserType>
{
private static final Types NONE = new Types(ImmutableMap.of());
private final Map<ByteBuffer, UserType> types;
private Types(Builder builder)
{
types = builder.types.build();
}
private Types(Map<ByteBuffer, UserType> types)
{
this.types = types;
}
public static Builder builder()
{
return new Builder();
}
public static RawBuilder rawBuilder(String keyspace)
{
return new RawBuilder(keyspace);
}
public static Types none()
{
return NONE;
}
public static Types of(UserType... types)
{
return builder().add(types).build();
}
public Iterator<UserType> iterator()
{
return types.values().iterator();
}
public Optional<UserType> get(ByteBuffer name)
{
return Optional.ofNullable(types.get(name));
}
@Nullable
public UserType getNullable(ByteBuffer name)
{
return types.get(name);
}
public Types with(UserType type)
{
if (get(type.name).isPresent())
throw new IllegalStateException(format("Type %s already exists", type.name));
return builder().add(this).add(type).build();
}
public Types without(ByteBuffer name)
{
UserType type =
get(name).orElseThrow(() -> new IllegalStateException(format("Type %s doesn't exists", name)));
return builder().add(filter(this, t -> t != type)).build();
}
MapDifference<ByteBuffer, UserType> diff(Types other)
{
return Maps.difference(types, other.types);
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof Types))
return false;
Types other = (Types) o;
if (types.size() != other.types.size())
return false;
Iterator<Map.Entry<ByteBuffer, UserType>> thisIter = this.types.entrySet().iterator();
Iterator<Map.Entry<ByteBuffer, UserType>> otherIter = other.types.entrySet().iterator();
while (thisIter.hasNext())
{
Map.Entry<ByteBuffer, UserType> thisNext = thisIter.next();
Map.Entry<ByteBuffer, UserType> otherNext = otherIter.next();
if (!thisNext.getKey().equals(otherNext.getKey()))
return false;
if (!thisNext.getValue().equals(otherNext.getValue(), true))
return false;
}
return true;
}
@Override
public int hashCode()
{
return types.hashCode();
}
@Override
public String toString()
{
return types.values().toString();
}
public static final class Builder
{
final ImmutableSortedMap.Builder<ByteBuffer, UserType> types = ImmutableSortedMap.naturalOrder();
private Builder()
{
}
public Types build()
{
return new Types(this);
}
public Builder add(UserType type)
{
assert type.isMultiCell();
types.put(type.name, type);
return this;
}
public Builder add(UserType... types)
{
for (UserType type : types)
add(type);
return this;
}
public Builder add(Iterable<UserType> types)
{
types.forEach(this::add);
return this;
}
}
public static final class RawBuilder
{
final String keyspace;
final List<RawUDT> definitions;
private RawBuilder(String keyspace)
{
this.keyspace = keyspace;
this.definitions = new ArrayList<>();
}
public Types build()
{
if (definitions.isEmpty())
return Types.none();
Map<RawUDT, Integer> vertices = new HashMap<>();
for (RawUDT udt : definitions)
vertices.put(udt, 0);
Multimap<RawUDT, RawUDT> adjacencyList = HashMultimap.create();
for (RawUDT udt1 : definitions)
for (RawUDT udt2 : definitions)
if (udt1 != udt2 && udt1.referencesUserType(udt2))
adjacencyList.put(udt2, udt1);
adjacencyList.values().forEach(vertex -> vertices.put(vertex, vertices.get(vertex) + 1));
Queue<RawUDT> resolvableTypes = new LinkedList<>();
for (Map.Entry<RawUDT, Integer> entry : vertices.entrySet())
if (entry.getValue() == 0)
resolvableTypes.add(entry.getKey());
Types types = new Types(new HashMap<>());
while (!resolvableTypes.isEmpty())
{
RawUDT vertex = resolvableTypes.remove();
for (RawUDT dependentType : adjacencyList.get(vertex))
if (vertices.replace(dependentType, vertices.get(dependentType) - 1) == 1)
resolvableTypes.add(dependentType);
UserType udt = vertex.prepare(keyspace, types);
types.types.put(udt.name, udt);
}
if (types.types.size() != definitions.size())
throw new ConfigurationException(format("Cannot resolve UDTs for keyspace %s: some types are missing", keyspace));
return Types.builder().add(types).build();
}
public void add(String name, List<String> fieldNames, List<String> fieldTypes)
{
List<CQL3Type.Raw> rawFieldTypes =
fieldTypes.stream()
.map(CQLTypeParser::parseRaw)
.collect(toList());
definitions.add(new RawUDT(name, fieldNames, rawFieldTypes));
}
private static final class RawUDT
{
final String name;
final List<String> fieldNames;
final List<CQL3Type.Raw> fieldTypes;
RawUDT(String name, List<String> fieldNames, List<CQL3Type.Raw> fieldTypes)
{
this.name = name;
this.fieldNames = fieldNames;
this.fieldTypes = fieldTypes;
}
boolean referencesUserType(RawUDT other)
{
return fieldTypes.stream().anyMatch(t -> t.referencesUserType(other.name));
}
UserType prepare(String keyspace, Types types)
{
List<FieldIdentifier> preparedFieldNames =
fieldNames.stream()
.map(t -> FieldIdentifier.forInternalString(t))
.collect(toList());
List<AbstractType<?>> preparedFieldTypes =
fieldTypes.stream()
.map(t -> t.prepareInternal(keyspace, types).getType())
.collect(toList());
return new UserType(keyspace, bytes(name), preparedFieldNames, preparedFieldTypes, true);
}
@Override
public int hashCode()
{
return name.hashCode();
}
@Override
public boolean equals(Object other)
{
return name.equals(((RawUDT) other).name);
}
}
}
}