/** * @param db * @return */ public Datastore copy(String db) { return new DatastoreImpl(mapr, mongo, db); }
/** * @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 type * @param background * @param fields * @param <T> */ public <T> void ensureIndex(Class<T> type, boolean background, IndexFieldDef... fields) { ensureIndex(type, null, fields, false, false, background); }
/** * @param entity * @param <T> * @return */ public <T> WriteResult delete(T entity) { return delete(entity, getWriteConcern(entity)); }
DBCollection dbColl = getCollection(entity); 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(); throwOnError(wc, wr); throw new UpdateException("Not updated: " + gle); postSaveOperations(entity, dbObj, involvedObjects); return key;
/** * Ensure indexes from field annotations, and embedded entities. * @param mc * @param background * @param parentMCs * @param parentMFs */ private void ensureIndexesFromFieldsAndEmbeddedEntities(MappedClass mc, boolean background, ArrayList<MappedClass> parentMCs, ArrayList<MappedField> parentMFs) { for (MappedField mf : mc.getMappedFields()) { Class<?> indexedClass = (parentMCs.isEmpty() ? mc : parentMCs.get(0)).getClazz(); String field = getFullFieldName(parentMCs, parentMFs, mf); if (mf.hasAnnotation(Indexed.class)) { Indexed index = mf.getAnnotation(Indexed.class); ensureIndex(indexedClass, index.name(), new BasicDBObject(field.toString(), index.value().toIndexValue()), index.unique(), index.dropDups(), index.background() ? index.background() : background, index.sparse() ? index.sparse() : false, index.expireAfterSeconds()); } if (mf.hasAnnotation(SimpleTextIndex.class)) { createTextIndex(mf, indexedClass, field); } if (!mf.isTypeMongoCompatible() && !mf.hasAnnotation(Reference.class) && !mf.hasAnnotation(Serialized.class)) { ArrayList<MappedClass> newParentClasses = (ArrayList<MappedClass>) parentMCs.clone(); ArrayList<MappedField> newParents = (ArrayList<MappedField>) parentMFs.clone(); newParentClasses.add(mc); newParents.add(mf); ensureIndexes(mapr.getMappedClass(mf.isSingleValue() ? mf.getType() : mf.getSubClass()), background, newParentClasses, newParents); } } }
/** * @param query * @param <T> * @return */ public <T> T findAndDelete(Query<T> query) { DBCollection dbColl = ((QueryImpl<T>) query).getCollection(); //TODO remove this after testing. if (dbColl == null) dbColl = getCollection(((QueryImpl<T>) query).getEntityClass()); QueryImpl<T> qi = ((QueryImpl<T>) query); EntityCache cache = createCache(); if (log.isTraceEnabled()) log.trace("Executing findAndModify(" + dbColl.getName() + ") with delete ..."); DBObject result = dbColl.findAndModify(qi.getQueryObject(), qi.getFieldsObject(), qi.getSortObject(), true, null, false, false); if (result != null) { T entity = (T) mapr.fromDBObject(qi.getEntityClass(), result, cache); return entity; } return null; }
/** * @param mc * @param background * @param parentMCs * @param parentMFs */ protected void ensureIndexes(MappedClass mc, boolean background, ArrayList<MappedClass> parentMCs, ArrayList<MappedField> parentMFs) { if (parentMCs.contains(mc)) return; //skip embedded types if (mc.getEmbeddedAnnotation() != null && parentMCs.isEmpty()) return; ensureIndexesFromClassAnnotation(mc, background); ensureIndexesFromFieldsAndEmbeddedEntities(mc, background, parentMCs, parentMFs); }
/** * @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 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); }
/** * Causes a (foreground, blocking) index creation across mapped classes. * A proxy to {@link #ensureIndexes(boolean)} passing false in. */ public void ensureIndexes() { ensureIndexes(false); }
/** * @param clazz * @param <T> * @return */ public <T> Query<T> find(Class<T> clazz) { return createQuery(clazz); }
/** * @param clazz * @param ref * @param <T> * @return */ public <T> T get(Class<T> clazz, DBRef ref) { return (T) mapr.fromDBObject(clazz, ref.fetch(), createCache()); }
/** * @param kind * @param ops * @param <T> * @return */ public <T> UpdateOperations<T> createUpdateOperations(Class<T> kind, DBObject ops) { UpdateOpsImpl<T> upOps = (UpdateOpsImpl<T>) createUpdateOperations(kind); upOps.setOps(ops); return upOps; }
/** * @param entity * @param <T> * @return */ public <T> DBRef createRef(T entity) { entity = ProxyHelper.unwrap(entity); Object id = mapr.getId(entity); if (id == null) throw new MappingException("Could not get id for " + entity.getClass().getName()); return createRef(entity.getClass(), id); }
/** * @param entity * @param wc * @param <T> * @return */ public <T> WriteResult delete(T entity, WriteConcern wc) { entity = ProxyHelper.unwrap(entity); if (entity instanceof Class<?>) throw new MappingException("Did you mean to delete all documents? -- delete(ds.createQuery(???.class))"); try { Object id = mapr.getId(entity); return delete(entity.getClass(), id, wc); } catch (Exception e) { throw new RuntimeException(e); } }
private void createTextIndex(MappedField mf, Class<?> indexedClass, String field) { SimpleTextIndex txtIndex = mf.getAnnotation(SimpleTextIndex.class); TextIndexCommand cmd = new TextIndexCommand(); cmd.setName(field); if (!txtIndex.name().isEmpty()) { cmd.setName(txtIndex.name()); } cmd.setDefaultLanguage(txtIndex.defaultLanguage()); List<String> fields = new ArrayList(); fields.add(field); cmd.setFields(fields); createTextIndex(indexedClass, cmd); }
/** * @param clazz * @param id * @param <T> * @param <V> * @return */ public <T, V> WriteResult delete(Class<T> clazz, V id) { return delete(clazz, id, getWriteConcern(clazz)); }