use of org.iq80.leveldb.DBException in project cdap by caskdata.
the class LevelDBPayloadTable method persist.
@Override
public void persist(Iterator<RawPayloadTableEntry> entries) throws IOException {
try (WriteBatch writeBatch = levelDB.createWriteBatch()) {
while (entries.hasNext()) {
RawPayloadTableEntry entry = entries.next();
byte[] key = entry.getKey();
byte[] value = entry.getValue();
// LevelDB doesn't make copies, and since we reuse RawPayloadTableEntry object, we need to create copies.
writeBatch.put(Arrays.copyOf(key, key.length), Arrays.copyOf(value, value.length));
}
levelDB.write(writeBatch, WRITE_OPTIONS);
} catch (DBException ex) {
throw new IOException(ex);
}
}
use of org.iq80.leveldb.DBException in project cdap by caskdata.
the class LevelDBMetadataTable method createTopic.
@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
try {
TopicId topicId = topicMetadata.getTopicId();
byte[] key = MessagingUtils.toMetadataRowKey(topicId);
TreeMap<String, String> properties = new TreeMap<>(topicMetadata.getProperties());
properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION);
synchronized (this) {
byte[] tableValue = levelDB.get(key);
if (tableValue != null) {
Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
TopicMetadata metadata = new TopicMetadata(topicId, oldProperties);
if (metadata.exists()) {
throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
}
int newGenerationId = (metadata.getGeneration() * -1) + 1;
properties.put(TopicMetadata.GENERATION_KEY, Integer.toString(newGenerationId));
}
byte[] value = Bytes.toBytes(GSON.toJson(properties, MAP_TYPE));
levelDB.put(key, value, WRITE_OPTIONS);
}
} catch (DBException e) {
throw new IOException(e);
}
}
use of org.iq80.leveldb.DBException 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 org.iq80.leveldb.DBException 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 org.iq80.leveldb.DBException in project tez by apache.
the class ShuffleHandler method startStore.
private void startStore(Path recoveryRoot) throws IOException {
Options options = new Options();
options.createIfMissing(false);
options.logger(new LevelDBLogger());
Path dbPath = new Path(recoveryRoot, STATE_DB_NAME);
LOG.info("Using state database at " + dbPath + " for recovery");
File dbfile = new File(dbPath.toString());
try {
stateDb = JniDBFactory.factory.open(dbfile, options);
} catch (NativeDB.DBException e) {
if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
LOG.info("Creating state database at " + dbfile);
options.createIfMissing(true);
try {
stateDb = JniDBFactory.factory.open(dbfile, options);
storeVersion();
} catch (DBException dbExc) {
throw new IOException("Unable to create state store", dbExc);
}
} else {
throw e;
}
}
checkVersion();
}
Aggregations