@Test public void testAllocateId() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build(); Key key1 = Key.newBuilder(pKey1, 1).build(); expect(datastore.allocateId(new IncompleteKey[] {pKey1})) .andReturn(Collections.singletonList(key1)); replay(datastore); assertEquals(key1, DatastoreHelper.allocateId(datastore, pKey1)); verify(datastore); }
@Test public void testGetWithDatastore() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build(); Key key1 = Key.newBuilder(pKey1, 1).build(); Entity entity1 = Entity.newBuilder(key1).build(); Key key2 = Key.newBuilder(pKey1, 2).build(); ReadOption eventualConsistency = ReadOption.eventualConsistency(); expect(datastore.get(Collections.singletonList(key1))) .andReturn(Collections.singletonList(entity1).iterator()); expect(datastore.get(Collections.singletonList(key2))) .andReturn(Collections.<Entity>emptyIterator()); expect(datastore.get(Collections.singletonList(key1), eventualConsistency)) .andReturn(Collections.singletonList(entity1).iterator()); replay(datastore); assertEquals(entity1, DatastoreHelper.get(datastore, key1)); assertNull(DatastoreHelper.get(datastore, key2)); assertEquals(entity1, DatastoreHelper.get(datastore, key1, eventualConsistency)); verify(datastore); }
@Test public void testGetWithTransaction() throws Exception { Transaction transaction = createStrictMock(Transaction.class); IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build(); Key key1 = Key.newBuilder(pKey1, 1).build(); Entity entity1 = Entity.newBuilder(key1).build(); Key key2 = Key.newBuilder(pKey1, 2).build(); expect(transaction.get(new Key[] {key1})) .andReturn(Collections.singletonList(entity1).iterator()); expect(transaction.get(new Key[] {key2})).andReturn(Collections.<Entity>emptyIterator()); replay(transaction); assertEquals(entity1, DatastoreHelper.get(transaction, key1)); assertNull(DatastoreHelper.get(transaction, key2)); verify(transaction); }
/** * Returns an IncompleteKey of the given entity. * * @param entity * the entity * @return the incomplete key */ private IncompleteKey getIncompleteKey(Object entity) { EntityMetadata entityMetadata = EntityIntrospector.introspect(entity.getClass()); String kind = entityMetadata.getKind(); ParentKeyMetadata parentKeyMetadata = entityMetadata.getParentKeyMetadata(); DatastoreKey parentKey = null; IncompleteKey incompleteKey = null; if (parentKeyMetadata != null) { parentKey = (DatastoreKey) IntrospectionUtils.getFieldValue(parentKeyMetadata, entity); } if (parentKey != null) { incompleteKey = IncompleteKey.newBuilder(parentKey.nativeKey(), kind).build(); } else { incompleteKey = IncompleteKey.newBuilder(datastore.getOptions().getProjectId(), kind) .setNamespace(getEffectiveNamespace()).build(); } return incompleteKey; }
/** * Creates an IncompleteKey. * * @param parent * the parent key, may be <code>null</code>. */ private void createIncompleteKey(Key parent) { String kind = entityMetadata.getKind(); if (parent == null) { key = entityManager.newNativeKeyFactory().setKind(kind).newKey(); } else { key = IncompleteKey.newBuilder(parent, kind).build(); } }
private IncompleteKey trimNameOrId(IncompleteKey key) { if (key instanceof Key) { return IncompleteKey.newBuilder(key).build(); } return key; }
/** * Preallocate a contiguous range of unique ids within the namespace of the * specified entity class and the parent key. These ids can be used in concert with the normal * automatic allocation of ids when put()ing entities with null Long id fields. * * @param parentKeyOrEntity must be a legitimate parent for the class type. It need not * point to an existent entity, but it must be the correct type for clazz. * @param clazz must be a registered entity class with a Long or long id field, and * a parent key of the correct type. * @param num must be >= 1 and <= 1 billion */ public <T> KeyRange<T> allocateIds(final Object parentKeyOrEntity, final Class<T> clazz, final int num) { final Key<?> parent = keys().anythingToKey(parentKeyOrEntity); final String kind = Key.getKind(clazz); final IncompleteKey incompleteKey = com.google.cloud.datastore.Key.newBuilder(parent.getRaw(), kind).build(); return allocate(incompleteKey, num); }
/** * @param parent can be null for root keys */ public IncompleteKey createRawIncomplete(final com.google.cloud.datastore.Key parent, final String kind) { if (parent == null) { return datastore.newKeyFactory().setKind(kind).newKey(); } else { return IncompleteKey.newBuilder(parent, kind).build(); } }
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors()); assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind()); assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build()); assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors()); assertNotEquals(PARTIAL_ENTITY2.getKey(), entity6.getKey()); assertEquals(1, generatedKeys.size()); assertEquals(PARTIAL_ENTITY3.getNames(), DATASTORE.get(generatedKeys.get(0)).getNames()); assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors()); assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind()); assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build()); assertNotEquals(PARTIAL_ENTITY2.getKey().getPath(), entity6.getKey().getPath()); assertNotEquals(PARTIAL_ENTITY2.getKey(), entity6.getKey()); assertEquals( PARTIAL_ENTITY3.getProperties(), datastore.get(generatedKeys.get(0)).getProperties()); assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
@Test public void testFetchWithDatastore() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build(); Key key1 = Key.newBuilder(pKey1, 1).build(); Key key2 = Key.newBuilder(pKey1, "a").build(); Entity entity1 = Entity.newBuilder(key1).build(); Entity entity2 = Entity.newBuilder(key2).build(); ReadOption eventualConsistency = ReadOption.eventualConsistency(); expect(datastore.get(ImmutableList.of(key1, key2))) .andReturn(Iterators.forArray(entity1, entity2)) .once(); expect(datastore.get(ImmutableList.of(key1, key2), eventualConsistency)) .andReturn(Iterators.forArray(entity1, entity2)) .once(); replay(datastore); List<Entity> values = DatastoreHelper.fetch(datastore, new Key[] {key1, key2}); assertEquals(2, values.size()); assertEquals(entity1, values.get(0)); assertEquals(entity2, values.get(1)); values = DatastoreHelper.fetch(datastore, new Key[] {key1, key2}, eventualConsistency); assertEquals(2, values.size()); assertEquals(entity1, values.get(0)); assertEquals(entity2, values.get(1)); verify(datastore); }
@Override public void run(Transaction tx, Key userKey, String content) { Entity user = tx.get(userKey); if (user == null) { System.out.println("Adding a new user."); user = Entity.newBuilder(userKey).set("count", 1).build(); tx.add(user); } else { user = Entity.newBuilder(user).set("count", user.getLong("count") + 1L).build(); tx.update(user); } IncompleteKey commentKey = IncompleteKey.newBuilder(userKey, COMMENT_KIND).build(); FullEntity<IncompleteKey> comment = FullEntity.newBuilder(commentKey) .set("content", content) .set("timestamp", Timestamp.now()) .build(); tx.addWithDeferredIdAllocation(comment); System.out.printf("Adding a comment to user '%s'.%n", userKey.getName()); }
@Test public void testFetchWithTransaction() throws Exception { Transaction transaction = createStrictMock(Transaction.class); IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build(); Key key1 = Key.newBuilder(pKey1, 1).build(); Key key2 = Key.newBuilder(pKey1, "a").build(); Entity entity1 = Entity.newBuilder(key1).build(); Entity entity2 = Entity.newBuilder(key2).build(); expect(transaction.get(new Key[] {key1, key2})) .andReturn(Iterators.forArray(entity1, entity2)) .once(); replay(transaction); List<Entity> values = DatastoreHelper.fetch(transaction, new Key[] {key1, key2}); assertEquals(2, values.size()); assertEquals(entity1, values.get(0)); assertEquals(entity2, values.get(1)); verify(transaction); } }
@Test public void testParent() { assertNull(pk1.getParent()); assertEquals(parent1, pk2.getParent()); Key parent2 = Key.newBuilder("ds", "kind3", "name").setNamespace("ns").build(); IncompleteKey pk3 = IncompleteKey.newBuilder(parent2, "kind3").build(); assertEquals(parent2, pk3.getParent()); } }
@Test public void testKeyFactory() { KeyFactory keyFactory = datastore.newKeyFactory().setKind(KIND1); assertEquals(INCOMPLETE_KEY1, keyFactory.newKey()); assertEquals( IncompleteKey.newBuilder(INCOMPLETE_KEY1).setKind(KIND2).build(), datastore.newKeyFactory().setKind(KIND2).newKey()); assertEquals(KEY1, keyFactory.newKey("name")); assertEquals(Key.newBuilder(KEY1).setId(2).build(), keyFactory.newKey(2)); }
@Test public void testBuilders() throws Exception { assertEquals("ds", pk1.getProjectId()); assertEquals("kind1", pk1.getKind()); assertTrue(pk1.getAncestors().isEmpty()); assertEquals("ds", pk2.getProjectId()); assertEquals("kind3", pk2.getKind()); assertEquals(parent1.getPath(), pk2.getAncestors()); assertEquals(pk2, IncompleteKey.newBuilder(pk2).build()); IncompleteKey pk3 = IncompleteKey.newBuilder(pk2).setKind("kind4").build(); assertEquals("ds", pk3.getProjectId()); assertEquals("kind4", pk3.getKind()); assertEquals(parent1.getPath(), pk3.getAncestors()); }
@Test public void testAdd() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey = IncompleteKey.newBuilder("ds", "k").build(); Key key = Key.newBuilder(pKey, 1).build(); Entity entity = Entity.newBuilder(key).build(); expect(datastore.add(new Entity[] {entity})).andReturn(Collections.singletonList(entity)); replay(datastore); assertEquals(entity, DatastoreHelper.add(datastore, entity)); verify(datastore); }
private IncompleteKey trimNameOrId(IncompleteKey key) { if (key instanceof Key) { return IncompleteKey.newBuilder(key).build(); } return key; }