URI u = null;
try
{
u = new URI(s);
} catch (URISyntaxException e){
throw new DatabusRuntimeException("Unable to decode a URI from the string s = " + s + " subscription = " + toString());
}
if (null == u.getScheme())
{
// TODO: Have V2 style subscriptions have an explicit codec type. Make it return "legacy" codec
// here. Given a subscription string, we should be able to convert it to DatabusSubscription
// in an idempotent way. That is, converting back and forth should give the same value
// Subscription of type com.linkedin.databus.events.dbName.tableName
String[] parts = s.split("\\.");
int len = parts.length;
if (len == 0)
{
// Error case
String errMsg = "Unexpected format for subscription. sub = " + toString() + " string = " + s;
throw new DatabusRuntimeException(errMsg);
}
else if (len == 1)
{
// Unit-tests case: logicalSource is specified as "source1"
return parts[0];
}
else
{
// Expected case. com.linkedin.databus.events.dbName.tableName
String pn = parts[len-2] + "_" + parts[len-1];
return pn;
}
}
else if (u.getScheme().equals("espresso"))
{
// Given that this subscription conforms to EspressoSubscriptionUriCodec,
// logicalSourceName (DBName.TableName) and partitionNumber(1) are guaranteed to be non-null
String dbName = getPhysicalPartition().getName();
boolean isWildCardOnTables = getLogicalSource().isAllSourcesWildcard();
String name = getLogicalPartition().getSource().getName();
boolean isWildCardOnPartitions = getPhysicalPartition().isAnyPartitionWildcard();
String pId = getPhysicalPartition().getId().toString();
StringBuilder sb = new StringBuilder();
sb.append(dbName);
if (! isWildCardOnTables)
{
sb.append("_");
String[] parts = name.split("\\.");
assert(parts.length == 2);
sb.append(parts[1]);
}
if (!isWildCardOnPartitions)
{
sb.append("_");
sb.append(pId);
}
s = sb.toString();
}
else
{
String errMsg = "The subscription object described as " + toString() + " is not of null or espresso type codec";
throw new DatabusRuntimeException(errMsg);
}
return s;
}