@Override public Key build() { ImmutableList.Builder<PathElement> pathBuilder = ImmutableList.<PathElement>builder().addAll(ancestors); if (id == null) { pathBuilder.add(PathElement.of(kind, name)); } else { pathBuilder.add(PathElement.of(kind, id)); } return new Key(projectId, namespace, pathBuilder.build()); } }
public Key newKey(String name) { ImmutableList<PathElement> path = ImmutableList.<PathElement>builder() .addAll(ancestors) .add(PathElement.of(kind, name)) .build(); return new Key(projectId, namespace, path); }
@Override public IncompleteKey build() { ImmutableList<PathElement> path = ImmutableList.<PathElement>builder().addAll(ancestors).add(PathElement.of(kind)).build(); return new IncompleteKey(projectId, namespace, path); } }
public Key newKey(long id) { ImmutableList<PathElement> path = ImmutableList.<PathElement>builder() .addAll(ancestors) .add(PathElement.of(kind, id)) .build(); return new Key(projectId, namespace, path); }
public IncompleteKey newKey() { ImmutableList<PathElement> path = ImmutableList.<PathElement>builder().addAll(ancestors).add(PathElement.of(kind)).build(); return new IncompleteKey(projectId, namespace, path); }
@Override protected BaseKey build() { ImmutableList.Builder<PathElement> path = ImmutableList.builder(); path.addAll(ancestors); path.add(PathElement.of(kind)); return new BaseKey(projectId, namespace, path.build()) { @Override protected BaseKey getParent() { return null; } }; } }
private static void addParentToBuilder(Key parent, Builder builder) { builder.setNamespace(parent.getNamespace()); builder.addAncestors(parent.getAncestors()); if (parent.hasId()) { builder.addAncestors(PathElement.of(parent.getKind(), parent.getId())); } else { builder.addAncestors(PathElement.of(parent.getKind(), parent.getName())); } } }
@Test public void testNewIncompleteKey() throws Exception { IncompleteKey key = keyFactory.newKey(); verifyIncompleteKey(key, ""); PathElement p1 = PathElement.of("k1", "n"); PathElement p2 = PathElement.of("k2", 10); key = keyFactory.setNamespace("ns").addAncestors(p1, p2).newKey(); verifyIncompleteKey(key, "ns", p1, p2); }
@Test public void testAncestors() throws Exception { Builder builder = new Builder("ds", "k"); BaseKey key = builder.build(); assertTrue(key.getAncestors().isEmpty()); List<PathElement> path = new ArrayList<>(); path.add(PathElement.of("p1", "v1")); key = builder.addAncestor(path.get(0)).build(); assertEquals(path, key.getAncestors()); path.add(PathElement.of("p2", "v2")); key = builder.addAncestor(path.get(1)).build(); assertEquals(path, key.getAncestors()); } }
@Test public void testNewKey() throws Exception { Key key = keyFactory.newKey(1); verifyKey(key, 1L, ""); key = keyFactory.newKey("n"); verifyKey(key, "n", ""); PathElement p1 = PathElement.of("k1", "n"); PathElement p2 = PathElement.of("k2", 10); key = keyFactory.setNamespace("ns").addAncestors(p1, p2).newKey("k3"); verifyKey(key, "k3", "ns", p1, p2); }
@Test public void testAllocateIdArray() { KeyFactory keyFactory = DATASTORE.newKeyFactory().setKind(KIND1); IncompleteKey incompleteKey1 = keyFactory.newKey(); IncompleteKey incompleteKey2 = keyFactory.setKind(KIND2).addAncestors(PathElement.of(KIND1, 10)).newKey(); List<Key> result = DATASTORE.allocateId(incompleteKey1, incompleteKey2, incompleteKey1); assertEquals(3, result.size()); assertEquals(Key.newBuilder(incompleteKey1, result.get(0).getId()).build(), result.get(0)); assertEquals(Key.newBuilder(incompleteKey1, result.get(2).getId()).build(), result.get(2)); assertEquals(Key.newBuilder(incompleteKey2, result.get(1).getId()).build(), result.get(1)); }
@Test public void testAllocateIdArray() { KeyFactory keyFactory = datastore.newKeyFactory().setKind(KIND1); IncompleteKey incompleteKey1 = keyFactory.newKey(); IncompleteKey incompleteKey2 = keyFactory.setKind(KIND2).addAncestor(PathElement.of(KIND1, 10)).newKey(); Key key3 = keyFactory.newKey("name"); List<Key> result1 = datastore.allocateId(incompleteKey1, incompleteKey2, incompleteKey1); assertEquals(3, result1.size()); assertEquals(Key.newBuilder(incompleteKey1, result1.get(0).getId()).build(), result1.get(0)); assertEquals(Key.newBuilder(incompleteKey1, result1.get(2).getId()).build(), result1.get(2)); assertEquals(Key.newBuilder(incompleteKey2, result1.get(1).getId()).build(), result1.get(1)); try { datastore.allocateId(incompleteKey1, incompleteKey2, key3); fail("expecting a failure"); } catch (IllegalArgumentException expected) { assertEquals(expected.getMessage(), "keys must be IncompleteKey instances"); } }
.setProjectId("ds1") .setNamespace("ns1") .addAncestor(PathElement.of("p", 1)) .build(); assertEquals("k", key.getKind());
@Override public Key build() { ImmutableList.Builder<PathElement> pathBuilder = ImmutableList.<PathElement>builder().addAll(ancestors); if (id == null) { pathBuilder.add(PathElement.of(kind, name)); } else { pathBuilder.add(PathElement.of(kind, id)); } return new Key(projectId, namespace, pathBuilder.build()); } }
public IncompleteKey newKey() { ImmutableList<PathElement> path = ImmutableList.<PathElement>builder().addAll(ancestors).add(PathElement.of(kind)).build(); return new IncompleteKey(projectId, namespace, path); }
public Key newKey(long id) { ImmutableList<PathElement> path = ImmutableList.<PathElement>builder() .addAll(ancestors) .add(PathElement.of(kind, id)) .build(); return new Key(projectId, namespace, path); }
public Key newKey(String name) { ImmutableList<PathElement> path = ImmutableList.<PathElement>builder() .addAll(ancestors) .add(PathElement.of(kind, name)) .build(); return new Key(projectId, namespace, path); }
static Key workflowKey(KeyFactory keyFactory, WorkflowId workflowId) { return keyFactory.addAncestor(PathElement.of(KIND_COMPONENT, workflowId.componentId())) .setKind(KIND_WORKFLOW) .newKey(workflowId.id()); }