Skip to content

Commit 35b42a1

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 4521 b: refs/heads/logging-alpha c: 65f4f80 h: refs/heads/master i: 4519: 7ba0474
1 parent f0526dd commit 35b42a1

5 files changed

Lines changed: 45 additions & 4 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ refs/heads/compute-alpha: 969cba2627f1d53d352cc4a5ffe0879dacf65e6c
1212
refs/heads/dns-alpha: 2f90e7e338349287ace33375896907af0f032ca1
1313
refs/heads/dns-alpha-batch: 17442b07867021b85d0452f5f3eda29a3413288f
1414
refs/heads/gcs-nio: 283aeaf15efdcf3621eb6859f05e55ad7764375d
15-
refs/heads/logging-alpha: 8050cca81afee5f6a952cc7c544e3ae5aa616633
15+
refs/heads/logging-alpha: 65f4f8063273b65b258aaf2be73e0609afb9583a
1616
refs/tags/v0.1.0: a615317f7424ed58621b1f65d5c4d8cbbe8a6ed8
1717
refs/tags/v0.1.1: 7a7f6985fe465e9dd6a075af55493f42b4933be0
1818
refs/tags/v0.1.2: 3eb3fe866ba22487686048f45d927b8c8638ea3f

branches/logging-alpha/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ public String kind() {
157157
return leaf().kind();
158158
}
159159

160+
abstract BaseKey parent();
161+
160162
@Override
161163
public int hashCode() {
162164
return Objects.hash(projectId(), namespace(), path());

branches/logging-alpha/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ static IncompleteKey fromPb(DatastoreV1.Key keyPb) {
8484
return new IncompleteKey(projectId, namespace, path);
8585
}
8686

87+
/**
88+
* Returns the key's parent.
89+
*/
90+
@Override
91+
public Key parent() {
92+
List<PathElement> ancestors = ancestors();
93+
if (!ancestors.isEmpty()) {
94+
PathElement parent = ancestors.get(ancestors.size() - 1);
95+
Key.Builder keyBuilder;
96+
if (parent.hasName()) {
97+
keyBuilder = Key.builder(projectId(), parent.kind(), parent.name());
98+
} else {
99+
keyBuilder = Key.builder(projectId(), parent.kind(), parent.id());
100+
}
101+
return keyBuilder.ancestors(ancestors.subList(0, ancestors.size() - 1)).build();
102+
}
103+
return null;
104+
}
105+
87106
public static Builder builder(String projectId, String kind) {
88107
return new Builder(projectId, kind);
89108
}

branches/logging-alpha/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ protected BaseKey build() {
5050
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
5151
return null;
5252
}
53+
54+
@Override
55+
protected BaseKey parent() {
56+
return null;
57+
}
5358
};
5459
}
5560
}

branches/logging-alpha/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/IncompleteKeyTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,30 @@
1717
package com.google.gcloud.datastore;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNull;
2021
import static org.junit.Assert.assertTrue;
2122

23+
import org.junit.Before;
2224
import org.junit.Test;
2325

2426
public class IncompleteKeyTest {
2527

28+
private static IncompleteKey pk1, pk2;
29+
private static Key parent;
30+
31+
@Before
32+
public void setUp() {
33+
pk1 = IncompleteKey.builder("ds", "kind1").build();
34+
parent = Key.builder("ds", "kind2", 10).build();
35+
pk2 = IncompleteKey.builder(parent, "kind3").build();
36+
}
37+
2638
@Test
2739
public void testBuilders() throws Exception {
28-
IncompleteKey pk1 = IncompleteKey.builder("ds", "kind1").build();
2940
assertEquals("ds", pk1.projectId());
3041
assertEquals("kind1", pk1.kind());
3142
assertTrue(pk1.ancestors().isEmpty());
3243

33-
Key parent = Key.builder("ds", "kind2", 10).build();
34-
IncompleteKey pk2 = IncompleteKey.builder(parent, "kind3").build();
3544
assertEquals("ds", pk2.projectId());
3645
assertEquals("kind3", pk2.kind());
3746
assertEquals(parent.path(), pk2.ancestors());
@@ -42,4 +51,10 @@ public void testBuilders() throws Exception {
4251
assertEquals("kind4", pk3.kind());
4352
assertEquals(parent.path(), pk3.ancestors());
4453
}
54+
55+
@Test
56+
public void testParent() {
57+
assertNull(pk1.parent());
58+
assertEquals(parent, pk2.parent());
59+
}
4560
}

0 commit comments

Comments
 (0)