Refine search
/** Returns the key's parent. */ @Override public Key getParent() { List<PathElement> ancestors = getAncestors(); if (ancestors.isEmpty()) { return null; } PathElement parent = ancestors.get(ancestors.size() - 1); Key.Builder keyBuilder; if (parent.hasName()) { keyBuilder = Key.newBuilder(getProjectId(), parent.getKind(), parent.getName()); } else { keyBuilder = Key.newBuilder(getProjectId(), parent.getKind(), parent.getId()); } String namespace = getNamespace(); if (namespace != null) { keyBuilder.setNamespace(namespace); } return keyBuilder.addAncestors(ancestors.subList(0, ancestors.size() - 1)).build(); }
@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()); } }
static IncompleteKey fromPb(com.google.datastore.v1.Key keyPb) { String projectId = ""; String namespace = ""; if (keyPb.hasPartitionId()) { com.google.datastore.v1.PartitionId partitionIdPb = keyPb.getPartitionId(); projectId = partitionIdPb.getProjectId(); namespace = partitionIdPb.getNamespaceId(); } List<com.google.datastore.v1.Key.PathElement> pathElementsPb = keyPb.getPathList(); Preconditions.checkArgument(!pathElementsPb.isEmpty(), "Path must not be empty"); ImmutableList.Builder<PathElement> pathBuilder = ImmutableList.builder(); for (com.google.datastore.v1.Key.PathElement pathElementPb : pathElementsPb) { pathBuilder.add(PathElement.fromPb(pathElementPb)); } ImmutableList<PathElement> path = pathBuilder.build(); PathElement leaf = path.get(path.size() - 1); if (leaf.getNameOrId() != null) { return new Key(projectId, namespace, path); } return new IncompleteKey(projectId, namespace, path); }
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 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)); }
private int comparePathElements(final PathElement here, final PathElement there) { final int result = here.getKind().compareTo(there.getKind()); if (result != 0) { return result; } else if (here.getNameOrId() == null && there.getNameOrId() == null) { return compareToWithIdentityHash(here, there); } else if (here.hasId()) { return there.hasId() ? Long.compare(here.getId(), there.getId()) : -1; } else { return there.hasId() ? 1 : here.getName().compareTo(there.getName()); } }
private WorkflowId parseWorkflowId(Entity workflow) { final String componentId = workflow.getKey().getAncestors().get(0).getName(); final String id = workflow.getKey().getName(); return WorkflowId.create(componentId, id); }
/** Returns the key's ID (as {@link Long}) or name (as {@link String}). Never {@code null}. */ public Object getNameOrId() { return getLeaf().getNameOrId(); }
/** Returns the key's kind. */ public String getKind() { return getLeaf().getKind(); }
@Test public void testNameOrId() throws Exception { assertNull(PE_1.getNameOrId()); assertEquals("n", PE_2.getNameOrId()); assertEquals(Long.valueOf(1), PE_3.getNameOrId()); } }