Ejemplo n.º 1
0
 /**
  * Creates indices for faster lookups in the database
  *
  * @since 1.12.15
  */
 public static void createIndices() {
   MongoDatabase lettersdb = mongoClient.getDatabase(databaseName);
   for (final String name : lettersdb.listCollectionNames()) {
     System.out.println("collection:" + name);
     if (name.startsWith(prefixCollectionPrefix)) {
       MongoCollection<Document> prefixCollection = lettersdb.getCollection(name);
       prefixCollection.createIndex(ascending(prefixAttribute));
     }
   }
   MongoCollection<Document> wordCollection = lettersdb.getCollection(wordCollectionName);
   wordCollection.createIndex(ascending(wordAttribute));
 }
Ejemplo n.º 2
0
  public void ensureSchema() {
    // ensures collections and indexes
    MongoCollection<Document> snapshots = snapshotsCollection();
    snapshots.createIndex(new BasicDBObject(GLOBAL_ID_KEY, ASC));
    snapshots.createIndex(new BasicDBObject(GLOBAL_ID_VALUE_OBJECT, ASC));
    snapshots.createIndex(new BasicDBObject(GLOBAL_ID_OWNER_ID_ENTITY, ASC));
    snapshots.createIndex(new BasicDBObject(CHANGED_PROPERTIES, ASC));
    snapshots.createIndex(
        new BasicDBObject(COMMIT_PROPERTIES + ".key", ASC)
            .append(COMMIT_PROPERTIES + ".value", ASC));

    headCollection();

    // schema migration script from JaVers 2.0 to 2.1
    dropIndexIfExists(snapshots, GLOBAL_ID_ENTITY);

    // schema migration script from JaVers 1.1 to 1.2
    Document doc = snapshots.find().first();
    if (doc != null) {
      Object stringCommitId = ((Map) doc.get("commitMetadata")).get("id");
      if (stringCommitId instanceof String) {
        logger.info("executing db migration script, from JaVers 1.1 to 1.2 ...");

        Document update =
            new Document(
                "eval",
                "function() { \n"
                    + "    db.jv_snapshots.find().forEach( \n"
                    + "      function(snapshot) { \n"
                    + "        snapshot.commitMetadata.id = Number(snapshot.commitMetadata.id); \n"
                    + "        db.jv_snapshots.save(snapshot); } \n"
                    + "    ); "
                    + "    return 'ok'; \n"
                    + "}");

        Document ret = mongo.runCommand(update);
        logger.info("result: \n " + ret.toJson());
      }
    }
  }
Ejemplo n.º 3
0
  void createIndex(
      final MongoCollection collection,
      final MappedClass mc,
      final Index index,
      final boolean background) {
    Index normalized = IndexBuilder.normalize(index);

    BsonDocument keys = calculateKeys(mc, normalized);
    com.mongodb.client.model.IndexOptions indexOptions = convert(normalized.options(), background);
    calculateWeights(normalized, indexOptions);

    collection.createIndex(keys, indexOptions);
  }
  @PostConstruct
  public void abrirConexao() {
    String dbMongoHost = appConfig.getDbMongoHost();
    String dbMongoPort = appConfig.getDbMongoPort();
    String dbMongoDatabase = appConfig.getDbMongoDatabase();
    String dbMongoUser = appConfig.getDbMongoUser();
    String dbMongoPass = appConfig.getDbMongoPass();

    if (Verificador.isValorado(dbMongoUser)) {
      String uriConexao =
          String.format(
              "mongodb://%s:%s@%s:%s/%s",
              dbMongoUser, dbMongoPass, dbMongoHost, dbMongoPort, dbMongoDatabase);
      MongoClientURI uri = new MongoClientURI(uriConexao);
      conexao = new MongoClient(uri);
    } else {
      conexao = new MongoClient(dbMongoHost, Integer.parseInt(dbMongoPort));
    }
    banco = conexao.getDatabase(dbMongoDatabase);

    colecaoUsuario = banco.getCollection(COLECAO_USUARIO);
    if (colecaoUsuario == null) {
      banco.createCollection(COLECAO_USUARIO);
      colecaoUsuario = banco.getCollection(COLECAO_USUARIO);

      BasicDBObject campos = new BasicDBObject();
      campos.append("username", 1);

      IndexOptions opcoes = new IndexOptions();
      opcoes.unique(true);

      colecaoUsuario.createIndex(campos, opcoes);

      BasicDBObject campos2 = new BasicDBObject();
      campos2.append("rg", 1);

      colecaoUsuario.createIndex(campos2, opcoes);
    }

    colecaoRdoUsuario = banco.getCollection(COLECAO_RDO_USUARIO);
    if (colecaoRdoUsuario == null) {
      banco.createCollection(COLECAO_RDO_USUARIO);
      colecaoRdoUsuario = banco.getCollection(COLECAO_RDO_USUARIO);

      BasicDBObject campos = new BasicDBObject();
      campos.append("RG_USUARIO", 1);

      IndexOptions opcoes = new IndexOptions();
      opcoes.unique(true);

      colecaoRdoUsuario.createIndex(campos, opcoes);
    }

    colecaoRdoDelegacia = banco.getCollection(COLECAO_RDO_DELEGACIA);
    if (colecaoRdoDelegacia == null) {
      banco.createCollection(COLECAO_RDO_DELEGACIA);
      colecaoRdoDelegacia = banco.getCollection(COLECAO_RDO_DELEGACIA);

      BasicDBObject campos = new BasicDBObject();
      campos.append("ID_DELEGACIA", 1);

      IndexOptions opcoes = new IndexOptions();
      opcoes.unique(true);

      colecaoRdoDelegacia.createIndex(campos, opcoes);
    }
  }
Ejemplo n.º 5
0
 @Override
 public String createIndex(Bson arg0) {
   return coll.createIndex(arg0);
 }
Ejemplo n.º 6
0
 @Override
 public String createIndex(Bson arg0, IndexOptions arg1) {
   return coll.createIndex(arg0, arg1);
 }