コード例 #1
0
 /*
  * (non-Javadoc)
  * @see org.springframework.data.mongodb.core.IndexOperations#dropIndex(java.lang.String)
  */
 public void dropIndex(final String name) {
   mongoOperations.execute(
       collectionName,
       new CollectionCallback<Void>() {
         public Void doInCollection(DBCollection collection)
             throws MongoException, DataAccessException {
           collection.dropIndex(name);
           return null;
         }
       });
 }
コード例 #2
0
 /*
  * (non-Javadoc)
  * @see org.springframework.data.mongodb.core.IndexOperations#resetIndexCache()
  */
 public void resetIndexCache() {
   mongoOperations.execute(
       collectionName,
       new CollectionCallback<Void>() {
         public Void doInCollection(DBCollection collection)
             throws MongoException, DataAccessException {
           collection.resetIndexCache();
           return null;
         }
       });
 }
コード例 #3
0
 /*
  * (non-Javadoc)
  * @see org.springframework.data.mongodb.core.IndexOperations#ensureIndex(org.springframework.data.mongodb.core.index.IndexDefinition)
  */
 public void ensureIndex(final IndexDefinition indexDefinition) {
   mongoOperations.execute(
       collectionName,
       new CollectionCallback<Object>() {
         public Object doInCollection(DBCollection collection)
             throws MongoException, DataAccessException {
           DBObject indexOptions = indexDefinition.getIndexOptions();
           if (indexOptions != null) {
             collection.ensureIndex(indexDefinition.getIndexKeys(), indexOptions);
           } else {
             collection.ensureIndex(indexDefinition.getIndexKeys());
           }
           return null;
         }
       });
 }
コード例 #4
0
  /*
   * (non-Javadoc)
   * @see org.springframework.data.mongodb.core.IndexOperations#getIndexInfo()
   */
  public List<IndexInfo> getIndexInfo() {

    return mongoOperations.execute(
        collectionName,
        new CollectionCallback<List<IndexInfo>>() {
          public List<IndexInfo> doInCollection(DBCollection collection)
              throws MongoException, DataAccessException {
            List<DBObject> dbObjectList = collection.getIndexInfo();
            return getIndexData(dbObjectList);
          }

          private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) {

            List<IndexInfo> indexInfoList = new ArrayList<IndexInfo>();

            for (DBObject ix : dbObjectList) {

              DBObject keyDbObject = (DBObject) ix.get("key");
              int numberOfElements = keyDbObject.keySet().size();

              List<IndexField> indexFields = new ArrayList<IndexField>(numberOfElements);

              for (String key : keyDbObject.keySet()) {

                Object value = keyDbObject.get(key);

                if (TWO_D_IDENTIFIERS.contains(value)) {
                  indexFields.add(IndexField.geo(key));
                } else if ("text".equals(value)) {

                  DBObject weights = (DBObject) ix.get("weights");
                  for (String fieldName : weights.keySet()) {
                    indexFields.add(
                        IndexField.text(
                            fieldName, Float.valueOf(weights.get(fieldName).toString())));
                  }

                } else {

                  Double keyValue = new Double(value.toString());

                  if (ONE.equals(keyValue)) {
                    indexFields.add(IndexField.create(key, ASC));
                  } else if (MINUS_ONE.equals(keyValue)) {
                    indexFields.add(IndexField.create(key, DESC));
                  }
                }
              }

              String name = ix.get("name").toString();

              boolean unique = ix.containsField("unique") ? (Boolean) ix.get("unique") : false;
              boolean dropDuplicates =
                  ix.containsField("dropDups") ? (Boolean) ix.get("dropDups") : false;
              boolean sparse = ix.containsField("sparse") ? (Boolean) ix.get("sparse") : false;
              String language =
                  ix.containsField("default_language") ? (String) ix.get("default_language") : "";
              indexInfoList.add(
                  new IndexInfo(indexFields, name, unique, dropDuplicates, sparse, language));
            }

            return indexInfoList;
          }
        });
  }