private List<TopicId> listTopics(@Nullable byte[] startKey, @Nullable byte[] stopKey) throws IOException { List<TopicId> topicList = new ArrayList<>(); try (CloseableIterator<TopicMetadata> iterator = scanTopics(startKey, stopKey)) { while (iterator.hasNext()) { TopicMetadata metadata = iterator.next(); if (metadata.exists()) { topicList.add(metadata.getTopicId()); } } } return topicList; }
private List<TopicId> listTopics(@Nullable byte[] startKey, @Nullable byte[] stopKey) throws IOException { List<TopicId> topicList = new ArrayList<>(); try (CloseableIterator<TopicMetadata> iterator = scanTopics(startKey, stopKey)) { while (iterator.hasNext()) { TopicMetadata metadata = iterator.next(); if (metadata.exists()) { topicList.add(metadata.getTopicId()); } } } return topicList; }
/** * Scans the HBase table to get a list of {@link TopicId}. */ private List<TopicId> scanTopics(ScanBuilder scanBuilder) throws IOException { Scan scan = scanBuilder.setFilter(new FirstKeyOnlyFilter()).setCaching(scanCacheRows).build(); try { List<TopicId> topicIds = new ArrayList<>(); try (ResultScanner resultScanner = hTable.getScanner(scan)) { for (Result result : resultScanner) { TopicId topicId = MessagingUtils.toTopicId(result.getRow()); byte[] value = result.getValue(columnFamily, COL); Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE); TopicMetadata metadata = new TopicMetadata(topicId, properties); if (metadata.exists()) { topicIds.add(topicId); } } } return topicIds; } catch (IOException e) { throw exceptionHandler.handle(e); } }
/** * Scans the HBase table to get a list of {@link TopicId}. */ private List<TopicId> scanTopics(ScanBuilder scanBuilder) throws IOException { Scan scan = scanBuilder.setFilter(new FirstKeyOnlyFilter()).setCaching(scanCacheRows).build(); try { List<TopicId> topicIds = new ArrayList<>(); try (ResultScanner resultScanner = hTable.getScanner(scan)) { for (Result result : resultScanner) { TopicId topicId = MessagingUtils.toTopicId(result.getRow()); byte[] value = result.getValue(columnFamily, COL); Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE); TopicMetadata metadata = new TopicMetadata(topicId, properties); if (metadata.exists()) { topicIds.add(topicId); } } } return topicIds; } catch (IOException e) { throw exceptionHandler.handle(e); } }
@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); } }
@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); } }
@Override public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException { byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId); try { synchronized (this) { byte[] tableValue = levelDB.get(rowKey); if (tableValue == null) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE); TopicMetadata metadata = new TopicMetadata(topicId, oldProperties); if (!metadata.exists()) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } // Mark the topic as deleted TreeMap<String, String> newProperties = new TreeMap<>(metadata.getProperties()); newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(-1 * metadata.getGeneration())); levelDB.put(rowKey, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)), WRITE_OPTIONS); } } catch (DBException e) { throw new IOException(e); } }
@Override public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException { byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId); try { synchronized (this) { byte[] tableValue = levelDB.get(rowKey); if (tableValue == null) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE); TopicMetadata metadata = new TopicMetadata(topicId, oldProperties); if (!metadata.exists()) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } // Mark the topic as deleted TreeMap<String, String> newProperties = new TreeMap<>(metadata.getProperties()); newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(-1 * metadata.getGeneration())); levelDB.put(rowKey, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)), WRITE_OPTIONS); } } catch (DBException e) { throw new IOException(e); } }
@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); } }
@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); } }
@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); } }
@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); } }
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());
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());
@Override public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException { Get get = tableUtil.buildGet(MessagingUtils.toMetadataRowKey(topicId)) .addFamily(columnFamily) .build(); try { Result result = hTable.get(get); byte[] value = result.getValue(columnFamily, COL); 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 (IOException e) { throw exceptionHandler.handle(e); } }
@Override public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException { Get get = tableUtil.buildGet(MessagingUtils.toMetadataRowKey(topicId)) .addFamily(columnFamily) .build(); try { Result result = hTable.get(get); byte[] value = result.getValue(columnFamily, COL); 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 (IOException e) { throw exceptionHandler.handle(e); } }