CacheMessageTableEntry(TopicMetadata topicMetadata, long publishTimestamp, short sequenceId) { this.lookupOnly = true; this.topicId = topicMetadata.getTopicId(); this.generation = topicMetadata.getGeneration(); this.transactional = false; this.payload = null; this.publishTimestamp = publishTimestamp; this.sequenceId = sequenceId; }
private void checkTopic(TopicId topicId, int generation) { Preconditions.checkArgument(this.topicMetadata.getTopicId().equals(topicId), "Not allowed to use table with a " + "different topic id. Table's topic Id: {}. Specified topic id: {}", this.topicMetadata.getTopicId(), topicId); Preconditions.checkArgument(this.topicMetadata.getGeneration() == generation, "Not allowed to use table with " + "a different generation id. Table's generation: {}. Specified generation: {}", this.topicMetadata.getGeneration(), generation); }
private void checkTopic(TopicId topicId, int generation) { Preconditions.checkArgument(this.topicMetadata.getTopicId().equals(topicId), "Not allowed to use table with a " + "different topic id. Table's topic Id: {}. Specified topic id: {}", this.topicMetadata.getTopicId(), topicId); Preconditions.checkArgument(this.topicMetadata.getGeneration() == generation, "Not allowed to use table with " + "a different generation id. Table's generation: {}. Specified generation: {}", this.topicMetadata.getGeneration(), generation); }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.updateTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); topicCache.invalidate(topicMetadata.getTopicId()); } }
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; }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.updateTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); topicCache.invalidate(topicMetadata.getTopicId()); } }
@ParametersAreNonnullByDefault @Override public LevelDBMessageTable load(TopicMetadata key) throws Exception { File dbPath = getDataDBPath(messageTableName, key.getTopicId(), key.getGeneration()); LevelDBMessageTable messageTable = new LevelDBMessageTable(LEVEL_DB_FACTORY.open(dbPath, dbOptions), key); LOG.debug("Messaging message table created at {}", dbPath); return messageTable; } });
@ParametersAreNonnullByDefault @Override public LevelDBPayloadTable load(TopicMetadata key) throws Exception { File dbPath = getDataDBPath(payloadTableName, key.getTopicId(), key.getGeneration()); LevelDBPayloadTable payloadTable = new LevelDBPayloadTable(LEVEL_DB_FACTORY.open(dbPath, dbOptions), key); LOG.debug("Messaging payload table created at {}", dbPath); return payloadTable; } });
@Override TestEntry getEntry(TopicMetadata metadata, boolean transactional, long transactionWritePointer, long writeTimestamp, short sequenceId, @Nullable byte[] payload) { return new TestEntry(metadata.getTopicId(), transactional, transactionWritePointer, writeTimestamp, sequenceId, payload); }
@Override public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.createTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); } }
@Override public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.createTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); } }
@Override PayloadTable.Entry getEntry(TopicMetadata metadata, boolean transactional, long transactionWritePointer, long writeTimestamp, short sequenceId, @Nullable byte[] payload) { return entry .setTopicId(metadata.getTopicId()) .setGeneration(metadata.getGeneration()) .setTransactionWritePointer(transactionWritePointer) .setPayloadWriteTimestamp(writeTimestamp) .setPayloadSequenceId(sequenceId) .setPayload(payload); }
@Override PayloadTable.Entry getEntry(TopicMetadata metadata, boolean transactional, long transactionWritePointer, long writeTimestamp, short sequenceId, @Nullable byte[] payload) { return entry .setTopicId(metadata.getTopicId()) .setGeneration(metadata.getGeneration()) .setTransactionWritePointer(transactionWritePointer) .setPayloadWriteTimestamp(writeTimestamp) .setPayloadSequenceId(sequenceId) .setPayload(payload); }
@Override public CloseableIterator<Entry> fetch(TopicMetadata metadata, long startTime, int limit, @Nullable Transaction transaction) throws IOException { byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration()); byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG]; Bytes.putBytes(startRow, 0, topic, 0, topic.length); Bytes.putLong(startRow, topic.length, startTime); byte[] stopRow = Bytes.stopKeyForPrefix(topic); final CloseableIterator<RawMessageTableEntry> scanner = read(startRow, stopRow); return new FetchIterator(scanner, limit, null, transaction); }
@Override MessageTable.Entry getEntry(TopicMetadata metadata, boolean transactional, long transactionWritePointer, long writeTimestamp, short sequenceId, @Nullable byte[] payload) { return entry .setTopicId(metadata.getTopicId()) .setGeneration(metadata.getGeneration()) .setTransactional(transactional) .setTransactionWritePointer(transactionWritePointer) .setPublishTimestamp(writeTimestamp) .setSequenceId(sequenceId) .setPayload(payload); }
@Override public CloseableIterator<Entry> fetch(TopicMetadata metadata, MessageId messageId, boolean inclusive, final int limit, @Nullable final Transaction transaction) throws IOException { byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration()); byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT]; Bytes.putBytes(startRow, 0, topic, 0, topic.length); Bytes.putLong(startRow, topic.length, messageId.getPublishTimestamp()); Bytes.putShort(startRow, topic.length + Bytes.SIZEOF_LONG, messageId.getSequenceId()); byte[] stopRow = Bytes.stopKeyForPrefix(topic); final CloseableIterator<RawMessageTableEntry> scanner = read(startRow, stopRow); return new FetchIterator(scanner, limit, inclusive ? null : startRow, transaction); }
@Override public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException { TopicId topicId = topicMetadata.getTopicId(); HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId)) .withBody(GSON.toJson(topicMetadata.getProperties())) .build(); HttpResponse response = remoteClient.execute(request); if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) { throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic()); } handleError(response, "Failed to create topic " + topicId); }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { TopicId topicId = topicMetadata.getTopicId(); HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId) + "/properties") .withBody(GSON.toJson(topicMetadata.getProperties())) .build(); HttpResponse response = remoteClient.execute(request); if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } handleError(response, "Failed to update topic " + topicId); }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { TopicId topicId = topicMetadata.getTopicId(); HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId) + "/properties") .withBody(GSON.toJson(topicMetadata.getProperties())) .build(); HttpResponse response = remoteClient.execute(request); if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } handleError(response, "Failed to update topic " + topicId); }