Skip to content

Commit 67be29c

Browse files
committed
Add identity for deleted topics
1 parent b27eb5c commit 67be29c

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/SubscriptionInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ public SubscriptionInfo build() {
182182
/**
183183
* Returns the identity of the topic this subscription refers to. If {@link TopicId#project()} is
184184
* {@code null} the topic is assumed to reside in the {@link PubSubOptions#projectId()} project.
185+
* After a topic is deleted, existing subscriptions to that topic are not deleted, but their topic
186+
* field is set to {@link TopicId#deletedTopic()}.
185187
*/
186188
public TopicId topic() {
187189
return topic;

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/TopicId.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@
3333
public final class TopicId implements Serializable {
3434

3535
private static final long serialVersionUID = -4913169763174877777L;
36+
private static final String DELETED_TOPIC_NAME = "_deleted_topic_";
37+
private static final TopicId DELETED_TOPIC = new TopicId(null, DELETED_TOPIC_NAME, true);
3638

3739
private final String project;
3840
private final String topic;
41+
private final boolean isDeleted;
3942

40-
private TopicId(String project, String topic) {
43+
private TopicId(String project, String topic, boolean isDeleted) {
4144
this.project = project;
4245
this.topic = checkNotNull(topic);
46+
this.isDeleted = isDeleted;
47+
}
48+
49+
private TopicId(String project, String topic) {
50+
this(project, topic, false);
4351
}
4452

4553
/**
@@ -57,14 +65,27 @@ public String topic() {
5765
return topic;
5866
}
5967

68+
/**
69+
* Returns {@code true} if this object is the identity of a deleted topic, {@code false}
70+
* otherwhise. If {@code isDeleted()} is {@code true}, {@link #topic()} returns
71+
* "{@code _deleted_topic_}" and {@link #project()} returns {@code null}.
72+
*/
73+
public boolean isDeleted() {
74+
return isDeleted;
75+
}
76+
6077
@Override
6178
public String toString() {
62-
return MoreObjects.toStringHelper(this).add("project", project).add("topic", topic).toString();
79+
return MoreObjects.toStringHelper(this)
80+
.add("project", project)
81+
.add("topic", topic)
82+
.add("isDeleted", isDeleted)
83+
.toString();
6384
}
6485

6586
@Override
6687
public int hashCode() {
67-
return Objects.hash(project, topic);
88+
return Objects.hash(project, topic, isDeleted);
6889
}
6990

7091
@Override
@@ -76,13 +97,24 @@ public boolean equals(Object obj) {
7697
return false;
7798
}
7899
TopicId other = (TopicId) obj;
79-
return Objects.equals(project, other.project) && Objects.equals(topic, other.topic);
100+
return Objects.equals(project, other.project)
101+
&& Objects.equals(topic, other.topic)
102+
&& Objects.equals(isDeleted, other.isDeleted);
80103
}
81104

82105
String toPb(String projectId) {
83106
return formatTopicName(project != null ? project : projectId, topic);
84107
}
85108

109+
/**
110+
* Returns the identity of a deleted topic. The deleted topic is such that {@link #isDeleted()}
111+
* returns {@code true}, {@link #topic()} returns "{@code _is_deleted_}" and {@link #project()}
112+
* returns {@code null}.
113+
*/
114+
public static TopicId deletedTopic() {
115+
return DELETED_TOPIC;
116+
}
117+
86118
/**
87119
* Returns a topic identity given the topic name.
88120
*/
@@ -98,6 +130,9 @@ public static TopicId of(String project, String topic) {
98130
}
99131

100132
static TopicId fromPb(String pb) {
133+
if (Objects.equals(pb, DELETED_TOPIC_NAME)) {
134+
return DELETED_TOPIC;
135+
}
101136
return TopicId.of(parseProjectFromTopicName(pb), parseTopicFromTopicName(pb));
102137
}
103138
}

gcloud-java-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionIdTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public void testToAndFromPb() {
4343

4444
private void compareSubscriptionId(SubscriptionId expected, SubscriptionId value) {
4545
assertEquals(expected, value);
46-
assertEquals(expected.project(), expected.project());
47-
assertEquals(expected.subscription(), expected.subscription());
48-
assertEquals(expected.hashCode(), expected.hashCode());
46+
assertEquals(expected.project(), value.project());
47+
assertEquals(expected.subscription(), value.subscription());
48+
assertEquals(expected.hashCode(), value.hashCode());
4949
}
5050
}

gcloud-java-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionInfoTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public void testToAndFromPb() {
111111
subscriptionInfo = SubscriptionInfo.of("topic", NAME, ENDPOINT);
112112
compareSubscriptionInfo(SubscriptionInfo.of(TOPIC, NAME, ENDPOINT),
113113
SubscriptionInfo.fromPb(subscriptionInfo.toPb("project")));
114+
com.google.pubsub.v1.Subscription subscription = SUBSCRIPTION_INFO.toPb("project");
115+
subscriptionInfo =
116+
SubscriptionInfo.fromPb(subscription.toBuilder().setTopic("_deleted_topic_").build());
117+
assertEquals(TopicId.deletedTopic(), subscriptionInfo.topic());
118+
assertEquals(NAME, subscriptionInfo.name());
119+
assertEquals(PUSH_CONFIG, subscriptionInfo.pushConfig());
120+
assertEquals(ACK_DEADLINE, subscriptionInfo.ackDeadlineSeconds());
114121
}
115122

116123
private void compareSubscriptionInfo(SubscriptionInfo expected, SubscriptionInfo value) {

gcloud-java-pubsub/src/test/java/com/google/cloud/pubsub/TopicIdTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package com.google.cloud.pubsub;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
22+
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
2124

2225
import org.junit.Test;
2326

@@ -35,6 +38,16 @@ public void testOf() {
3538
topicId = TopicId.of(NAME);
3639
assertNull(topicId.project());
3740
assertEquals(NAME, topicId.topic());
41+
assertFalse(topicId.isDeleted());
42+
}
43+
44+
@Test
45+
public void testDeletedTopic() {
46+
TopicId deletedTopic = TopicId.deletedTopic();
47+
assertNull(deletedTopic.project());
48+
assertEquals("_deleted_topic_", deletedTopic.topic());
49+
assertTrue(deletedTopic.isDeleted());
50+
assertSame(deletedTopic, TopicId.deletedTopic());
3851
}
3952

4053
@Test
@@ -51,9 +64,10 @@ public void testToAndFromPb() {
5164

5265
private void compareTopicId(TopicId expected, TopicId value) {
5366
assertEquals(expected, value);
54-
assertEquals(expected.project(), expected.project());
55-
assertEquals(expected.topic(), expected.topic());
56-
assertEquals(expected.toPb("project"), expected.toPb("project"));
57-
assertEquals(expected.hashCode(), expected.hashCode());
67+
assertEquals(expected.project(), value.project());
68+
assertEquals(expected.topic(), value.topic());
69+
assertEquals(expected.isDeleted(), value.isDeleted());
70+
assertEquals(expected.toPb("project"), value.toPb("project"));
71+
assertEquals(expected.hashCode(), value.hashCode());
5872
}
5973
}

0 commit comments

Comments
 (0)