use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class LevelDBMetadataTable method getMetadata.
@Override
public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException {
try {
byte[] value = levelDB.get(MessagingUtils.toMetadataRowKey(topicId));
if (value == null) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
TopicMetadata topicMetadata = new TopicMetadata(topicId, properties);
if (!topicMetadata.exists()) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
return topicMetadata;
} catch (DBException e) {
// DBException is a RuntimeException. Turn it to IOException so that it forces caller to handle it.
throw new IOException(e);
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class LevelDBMetadataTable method updateTopic.
@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
try {
TopicId topicId = topicMetadata.getTopicId();
byte[] key = MessagingUtils.toMetadataRowKey(topicId);
synchronized (this) {
byte[] tableValue = levelDB.get(key);
if (tableValue == null) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
TopicMetadata oldMetadata = new TopicMetadata(topicId, oldProperties);
if (!oldMetadata.exists()) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
TreeMap<String, String> newProperties = new TreeMap<>(topicMetadata.getProperties());
newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration()));
levelDB.put(key, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)), WRITE_OPTIONS);
}
} catch (DBException e) {
throw new IOException(e);
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class HBaseMetadataTable method createTopic.
@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
TopicId topicId = topicMetadata.getTopicId();
byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId);
PutBuilder putBuilder = tableUtil.buildPut(rowKey);
Get get = tableUtil.buildGet(rowKey).addFamily(columnFamily).build();
try {
boolean completed = false;
while (!completed) {
Result result = hTable.get(get);
byte[] value = result.getValue(columnFamily, COL);
if (value == null) {
TreeMap<String, String> properties = new TreeMap<>(topicMetadata.getProperties());
properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION);
putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(properties, MAP_TYPE)));
completed = hTable.checkAndPut(rowKey, columnFamily, COL, null, putBuilder.build());
} else {
Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
TopicMetadata metadata = new TopicMetadata(topicId, properties);
if (metadata.exists()) {
throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
}
int newGenerationId = (metadata.getGeneration() * -1) + 1;
TreeMap<String, String> newProperties = new TreeMap<>(properties);
newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(newGenerationId));
putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)));
completed = hTable.checkAndPut(rowKey, columnFamily, COL, value, putBuilder.build());
}
}
} catch (IOException e) {
throw exceptionHandler.handle(e);
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class HBaseMetadataTable method deleteTopic.
@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException {
byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId);
boolean completed = false;
try {
// Keep trying to delete
while (!completed) {
TopicMetadata oldMetadata = getMetadata(topicId);
TreeMap<String, String> newProperties = new TreeMap<>(oldMetadata.getProperties());
newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration() * -1));
Put put = tableUtil.buildPut(rowKey).add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))).build();
byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE));
completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put);
}
} catch (IOException e) {
throw exceptionHandler.handle(e);
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class HBaseMetadataTable method updateTopic.
@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
byte[] rowKey = MessagingUtils.toMetadataRowKey(topicMetadata.getTopicId());
boolean completed = false;
try {
// Keep trying to update
while (!completed) {
TopicMetadata oldMetadata = getMetadata(topicMetadata.getTopicId());
TreeMap<String, String> newProperties = new TreeMap<>(topicMetadata.getProperties());
newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration()));
Put put = tableUtil.buildPut(rowKey).add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))).build();
byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE));
completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put);
}
} catch (IOException e) {
throw exceptionHandler.handle(e);
}
}
Aggregations