public TopicIdMetaKey(TopicId topicId) { this.topicId = topicId; this.key = MessagingUtils.toMetadataRowKey(topicId); }
public TopicIdMetaKey(TopicId topicId) { this.topicId = topicId; this.key = MessagingUtils.toMetadataRowKey(topicId); }
/** * Convert {@link TopicId} and generation id to byte array to be used for data tables (message and payload) as * row key prefix. * * @param topicId {@link TopicId} * @param generation generation id of the topic * @return byte array representation to be used as row key prefix for data tables */ public static byte[] toDataKeyPrefix(TopicId topicId, int generation) { byte[] metadataRowKey = toMetadataRowKey(topicId); byte[] keyPrefix = new byte[metadataRowKey.length + Bytes.SIZEOF_INT]; Bytes.putBytes(keyPrefix, 0, metadataRowKey, 0, metadataRowKey.length); Bytes.putInt(keyPrefix, metadataRowKey.length, generation); return keyPrefix; }
/** * Convert {@link TopicId} and generation id to byte array to be used for data tables (message and payload) as * row key prefix. * * @param topicId {@link TopicId} * @param generation generation id of the topic * @return byte array representation to be used as row key prefix for data tables */ public static byte[] toDataKeyPrefix(TopicId topicId, int generation) { byte[] metadataRowKey = toMetadataRowKey(topicId); byte[] keyPrefix = new byte[metadataRowKey.length + Bytes.SIZEOF_INT]; Bytes.putBytes(keyPrefix, 0, metadataRowKey, 0, metadataRowKey.length); Bytes.putInt(keyPrefix, metadataRowKey.length, generation); return keyPrefix; }
@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); } }
@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); } }
@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 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); } }
@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); } }
@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); } }
TopicId topicId = topicMetadata.getTopicId(); byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId); PutBuilder putBuilder = tableUtil.buildPut(rowKey);
@Test public void testTopicConversion() throws Exception { TopicId id = new TopicId("n1", "t1"); byte[] topicBytes = MessagingUtils.toMetadataRowKey(id); TopicId topicId = MessagingUtils.toTopicId(topicBytes); Assert.assertEquals(id, topicId); }
@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); } }