public Key<?> exists(Object entityOrKey) { entityOrKey = ProxyHelper.unwrap(entityOrKey); Key<?> key = getKey(entityOrKey); Object id = key.getId(); if (id == null) throw new MappingException("Could not get id for " + entityOrKey.getClass().getName()); String collName = key.getKind(); if (collName == null) collName = getCollection(key.getKindClass()).getName(); return find(collName, key.getKindClass()).filter(Mapper.ID_KEY, key.getId()).getKey(); }
/** Test key from kind and name. */ public void testKeyFromKindAndName() { Key key = new Key(kind, name); assertEquals(kind, key.getKind()); assertEquals(name, key.getName()); assertNull(key.getParent()); assertEquals(0L, key.getId()); }
@SuppressWarnings("rawtypes") public <T> List<T> getByKeys(Class<T> clazz, Iterable<Key<T>> keys) { Map<String, List<Key>> kindMap = new HashMap<String, List<Key>>(); List<T> entities = new ArrayList<T>(); // String clazzKind = (clazz==null) ? null : // getMapper().getCollectionName(clazz); for (Key<?> key : keys) { mapr.updateKind(key); // if (clazzKind != null && !key.getKind().equals(clazzKind)) // throw new IllegalArgumentException("Types are not equal (" + // clazz + "!=" + key.getKindClass() + // ") for key and method parameter clazz"); // if (kindMap.containsKey(key.getKind())) kindMap.get(key.getKind()).add(key); else kindMap.put(key.getKind(), new ArrayList<Key>(Collections.singletonList((Key) key))); } for (String kind : kindMap.keySet()) { List<Object> objIds = new ArrayList<Object>(); List<Key> kindKeys = kindMap.get(kind); for (Key key : kindKeys) { objIds.add(key.getId()); } List kindResults = find(kind, null).disableValidation().filter("_id in", objIds).asList(); entities.addAll(kindResults); } // TODO: order them based on the incoming Keys. return entities; }
@Test public void testMarshall_Embedded() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); ChildEntity embeddedObject = new ChildEntity("Old Test City"); embeddedObject.setId(1L); embeddedObject.setType("EmbeddedType"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity rootObject.setEmbeddedEntity(embeddedObject); // not included, @Embedded IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); EmbeddedEntity ee = (EmbeddedEntity) rootObjectEntity.getProperty("embeddedEntity"); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertEquals(EmbeddedEntity.class, ee.getClass()); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); assertEquals(1L, ee.getProperties().get("id")); assertEquals("EmbeddedType", ee.getProperties().get("type")); }
/** Test key from kind, parent and name. */ public void testKeyFromKindParentAndName() { Key parent = new Key(kind); Key key = new Key(kind, parent, name); assertEquals(kind, key.getKind()); assertEquals(name, key.getName()); assertEquals(parent, key.getParent()); assertEquals(0L, key.getId()); }
/** Test key from kind, parent and id. */ public void testKeyFromKindParentAndId() { Key parent = new Key(kind); Key key = new Key(kind, parent, id); assertEquals(kind, key.getKind()); assertNull(key.getName()); assertEquals(id, key.getId()); assertEquals(parent, key.getParent()); }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Key other = (Key) obj; if (klass != other.getClass() && (klass == null || !klass.equals(other.getKlass()))) { return false; } if ((id == null) ? (other.getId() != null) : !id.equals(other.getId())) { return false; } return true; }
public <T> T getByKey(Class<T> clazz, Key<T> key) { String kind = mapr.getCollectionName(clazz); String keyKind = mapr.updateKind(key); if (!kind.equals(keyKind)) throw new RuntimeException( "collection names don't match for key and class: " + kind + " != " + keyKind); return get(clazz, key.getId()); }
public String toString() { StringBuilder b = new StringBuilder(); Key k = KeyFactory.stringToKey(key); b.append("Question (").append(k.getId()).append(") : ").append(" Text=").append(questionText); b.append(" Date Posted=").append(datePosted); b.append(" ParentInfo = ").append(k.getParent().toString()); b.append("\n"); for (Comment c : comments) { b.append("\t\t").append(c.toString()).append("\n"); } return b.toString(); }
@Test public void testMarshall_Child() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); }
/** @return The size of the range. */ public long getSize() { return end.getId() - start.getId() + 1; }
public <T> UpdateResults<T> update(Key<T> key, UpdateOperations<T> ops) { Class<T> clazz = (Class<T>) key.getKindClass(); if (clazz == null) clazz = (Class<T>) mapr.getClassFromKind(key.getKind()); return updateFirst( createQuery(clazz).disableValidation().filter(Mapper.ID_KEY, key.getId()), ops); }
@Test public void testSaveAndDelete() { getDs().getCollection(Rectangle.class).drop(); final Rectangle rect = new Rectangle(10, 10); ObjectId id = new ObjectId(); rect.setId(id); // test delete(entity) getDs().save(rect); assertEquals(1, getDs().getCount(rect)); getDs().delete(rect); assertEquals(0, getDs().getCount(rect)); // test delete(entity, id) getDs().save(rect); assertEquals(1, getDs().getCount(rect)); getDs().delete(rect.getClass(), 1); assertEquals(1, getDs().getCount(rect)); getDs().delete(rect.getClass(), id); assertEquals(0, getDs().getCount(rect)); // test delete(entity, {id}) getDs().save(rect); assertEquals(1, getDs().getCount(rect)); getDs().delete(rect.getClass(), Arrays.asList(rect.getId())); assertEquals(0, getDs().getCount(rect)); // test delete(entity, {id,id}) ObjectId id1 = (ObjectId) getDs().save(new Rectangle(10, 10)).getId(); ObjectId id2 = (ObjectId) getDs().save(new Rectangle(10, 10)).getId(); assertEquals(2, getDs().getCount(rect)); getDs().delete(rect.getClass(), Arrays.<ObjectId>asList(id1, id2)); assertEquals(0, getDs().getCount(rect)); // test delete(Class, {id,id}) id1 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId(); id2 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId(); assertEquals( "datastore should have saved two entities with autogenerated ids", 2, getDs().getCount(rect)); getDs().delete(rect.getClass(), Arrays.asList(id1, id2)); assertEquals( "datastore should have deleted two entities with autogenerated ids", 0, getDs().getCount(rect)); // test delete(entity, {id}) with one left id1 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId(); id2 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId(); assertEquals(2, getDs().getCount(rect)); getDs().delete(rect.getClass(), Arrays.asList(id1)); assertEquals(1, getDs().getCount(rect)); getDs().getCollection(Rectangle.class).drop(); // test delete(Class, {id}) with one left id1 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId(); Key<Rectangle> save = getDs().save(new Rectangle(20, 20)); id2 = (ObjectId) save.getId(); assertEquals(2, getDs().getCount(rect)); getDs().delete(Rectangle.class, Arrays.asList(id1)); assertEquals(1, getDs().getCount(rect)); }