/** * @param clazz * @param property * @param value * @param <T> * @param <V> * @return */ public <T, V> Query<T> find(Class<T> clazz, String property, V value) { Query<T> query = createQuery(clazz); return query.filter(property, value); }
/** * @param kind * @param clazz * @param id * @param <T> * @param <V> * @return */ public <T, V> WriteResult delete(String kind, Class<T> clazz, V id) { return delete(find(kind, clazz).filter(Mapper.ID_KEY, id)); }
/** * @param clazz * @param id * @param wc * @param <T> * @param <V> * @return */ public <T, V> WriteResult delete(Class<T> clazz, V id, WriteConcern wc) { return delete(createQuery(clazz).filter(Mapper.ID_KEY, id), wc); }
/** * @param clazz * @param ids * @param <T> * @param <V> * @return */ public <T, V> WriteResult delete(Class<T> clazz, Iterable<V> ids) { Query<T> q = find(clazz).disableValidation().filter(Mapper.ID_KEY + " in", ids); return delete(q); }
/** * Queries the server to check for each DBRef. */ public <T> List<Key<T>> getKeysByRefs(List<DBRef> refs) { ArrayList<Key<T>> tempKeys = new ArrayList<Key<T>>(refs.size()); Map<String, List<DBRef>> kindMap = new HashMap<String, List<DBRef>>(); for (DBRef ref : refs) { if (kindMap.containsKey(ref.getRef())) kindMap.get(ref.getRef()).add(ref); else kindMap.put(ref.getRef(), new ArrayList<DBRef>(Collections.singletonList((DBRef) ref))); } for (String kind : kindMap.keySet()) { List<Object> objIds = new ArrayList<Object>(); List<DBRef> kindRefs = kindMap.get(kind); for (DBRef key : kindRefs) { objIds.add(key.getId()); } List<Key<T>> kindResults = this.<T>find(kind, null).disableValidation().filter("_id in", objIds).asKeyList(); tempKeys.addAll(kindResults); } //put them back in order, minus the missing ones. ArrayList<Key<T>> keys = new ArrayList<Key<T>>(refs.size()); for (DBRef ref : refs) { Key<T> testKey = mapr.refToKey(ref); if (tempKeys.contains(testKey)) keys.add(testKey); } return keys; }
/** * @param clazz * @param ids * @param <T> * @param <V> * @return */ public <T, V> Query<T> get(Class<T> clazz, Iterable<V> ids) { return find(clazz).disableValidation().filter(Mapper.ID_KEY + " in", ids).enableValidation(); }
/** * @param clazz * @param property * @param value * @param offset * @param size * @param <T> * @param <V> * @return */ public <T, V> Query<T> find(Class<T> clazz, String property, V value, int offset, int size) { Query<T> query = createQuery(clazz); query.offset(offset); query.limit(size); return query.filter(property, value); }
objIds.add(key.getId()); List kindResults = find(kind, kindClass).disableValidation().filter("_id in", objIds).asList(); entities.addAll(kindResults);
/** * @param ent * @param ops * @param <T> * @return */ public <T> UpdateResults<T> update(T ent, UpdateOperations<T> ops) { if (ent instanceof Query) return update((Query<T>) ent, ops); MappedClass mc = mapr.getMappedClass(ent); Query<T> q = (Query<T>) createQuery(mc.getClazz()); q.disableValidation().filter(Mapper.ID_KEY, mapr.getId(ent)); if (mc.getFieldsAnnotatedWith(Version.class).size() > 0) { MappedField versionMF = mc.getFieldsAnnotatedWith(Version.class).get(0); Long oldVer = (Long) versionMF.getFieldValue(ent); q.filter(versionMF.getNameToStore(), oldVer); ops.set(versionMF.getNameToStore(), VersionHelper.nextValue(oldVer)); } return update(q, ops); }
/** * @param kind * @param clazz * @param property * @param value * @param offset * @param size * @param validate * @param <T> * @param <V> * @return */ public <T, V> Query<T> find(String kind, Class<T> clazz, String property, V value, int offset, int size, boolean validate) { Query<T> query = find(kind, clazz); if (!validate) query.disableValidation(); query.offset(offset); query.limit(size); return query.filter(property, value).enableValidation(); }
/** * @param key * @param ops * @param <T> * @return */ 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); }
/** * @param entityOrKey * @return */ public Key<?> exists(Object entityOrKey) { entityOrKey = ProxyHelper.unwrap(entityOrKey); Key<?> key = mapr.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(); }
/** * @param query * @param ops * @param createIfMissing * @param multi * @param wc * @param <T> * @return */ @SuppressWarnings("rawtypes") private <T> UpdateResults<T> update(Query<T> query, UpdateOperations ops, boolean createIfMissing, boolean multi, WriteConcern wc) { DBObject u = ((UpdateOpsImpl) ops).getOps(); if (((UpdateOpsImpl) ops).isIsolated()) { Query<T> q = query.clone(); q.disableValidation().filter("$atomic", true); return update(q, u, createIfMissing, multi, wc); } return update(query, u, createIfMissing, multi, wc); }
boolean exists(Class c, final DBRef dbRef, EntityCache cache, Mapper mapr) { Key key = mapr.refToKey(dbRef); Boolean cached = cache.exists(key); if (cached != null) return cached; Datastore dsi = mapr.getDatastoreProvider().get(); DBCollection dbColl = dsi.getCollection(c); if (!dbColl.getName().equals(dbRef.getRef())) log.warning("Class " + c.getName() + " is stored in the '" + dbColl.getName() + "' collection but a reference was found for this type to another collection, '" + dbRef.getRef() + "'. The reference will be loaded using the class anyway. " + dbRef); boolean exists = (dsi.find(dbRef.getRef(), c).disableValidation().filter("_id", dbRef.getId()).asKeyList() .size() == 1); cache.notifyExists(key, exists); return exists; }
wr = tryVersionedUpdate(dbColl, entity, dbObj, wc, db, mc); } else { Query<T> query = (Query<T>) createQuery(entity.getClass()).filter(Mapper.ID_KEY, id); wr = update(query, new BasicDBObject("$set", dbObj), false, false, wc).getWriteResult();