/** * Check whether the topic exists. */ public boolean exists() { return getGeneration() > 0; }
/** * Check whether the topic exists. */ public boolean exists() { return getGeneration() > 0; }
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; }
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); }
@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 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 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 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 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 rollback(TopicMetadata metadata, RollbackDetail rollbackDetail) throws IOException { //long startTimestamp, short startSequenceId, //long endTimestamp, short endSequenceId 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, rollbackDetail.getStartTimestamp()); Bytes.putShort(startRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getStartSequenceId()); byte[] stopRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT]; Bytes.putBytes(stopRow, 0, topic, 0, topic.length); Bytes.putLong(stopRow, topic.length, rollbackDetail.getEndTimestamp()); Bytes.putShort(stopRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getEndSequenceId()); rollback(startRow, Bytes.stopKeyForPrefix(stopRow), Bytes.toBytes(-1 * rollbackDetail.getTransactionWritePointer())); }
@Override public void rollback(TopicMetadata metadata, RollbackDetail rollbackDetail) throws IOException { //long startTimestamp, short startSequenceId, //long endTimestamp, short endSequenceId 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, rollbackDetail.getStartTimestamp()); Bytes.putShort(startRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getStartSequenceId()); byte[] stopRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT]; Bytes.putBytes(stopRow, 0, topic, 0, topic.length); Bytes.putLong(stopRow, topic.length, rollbackDetail.getEndTimestamp()); Bytes.putShort(stopRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getEndSequenceId()); rollback(startRow, Bytes.stopKeyForPrefix(stopRow), Bytes.toBytes(-1 * rollbackDetail.getTransactionWritePointer())); }
@Test public void testGeMetadata() throws Exception { TopicId topicId = new NamespaceId("ns2").topic("d"); TopicMetadata metadata = new TopicMetadata(topicId, "ttl", "100"); for (int i = 1; i <= 5; i++) { client.createTopic(metadata); TopicMetadata topicMetadata = client.getTopic(topicId); Assert.assertEquals(100, topicMetadata.getTTL()); Assert.assertEquals(i, topicMetadata.getGeneration()); client.deleteTopic(topicId); } }
@Test public void testGenerations() throws Exception { try (MetadataTable table = createMetadataTable()) { TopicId topicId = NamespaceId.DEFAULT.topic("gtopic"); for (int i = 1; i <= 50; i++) { table.createTopic(new TopicMetadata(topicId, "ttl", 1)); TopicMetadata metadata = table.getMetadata(topicId); Assert.assertEquals(i, metadata.getGeneration()); Assert.assertEquals(1, metadata.getTTL()); table.deleteTopic(topicId); try { table.getMetadata(topicId); Assert.fail("Expected TopicNotFoundException"); } catch (TopicNotFoundException ex) { // Expected } } } }