<commit_before>/*
 * RESTHeart - the Web API for MongoDB
 * Copyright (C) SoftInstigate Srl
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.restheart.db;

import com.mongodb.MongoClient;
import com.mongodb.client.ListIndexesIterable;
import static com.mongodb.client.model.Filters.eq;
import com.mongodb.client.model.IndexOptions;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.restheart.utils.HttpStatus;

/**
 *
 * @author <NAME> {@literal <<EMAIL>>}
 */
class IndexDAO {

    public static final Bson METADATA_QUERY
            = eq("_id", "_properties");

    private static final BsonDocument FIELDS_TO_RETURN_INDEXES;

    static {
        FIELDS_TO_RETURN_INDEXES = new BsonDocument();
        FIELDS_TO_RETURN_INDEXES.put("key", new BsonInt32(1));
        FIELDS_TO_RETURN_INDEXES.put("name", new BsonInt32(1));
    }
    private final MongoClient client;

    IndexDAO(MongoClient client) {
        this.client = client;
    }

    /**
     *
     * @param dbName
     * @param collName
     * @return
     */
    List<BsonDocument> getCollectionIndexes(String dbName, String collName) {
        List<BsonDocument> ret = new ArrayList<>();

        ListIndexesIterable<Document> indexes = client
                .getDatabase(dbName)
                .getCollection(collName, BsonDocument.class)
                .listIndexes();

        indexes.iterator().forEachRemaining(
                i -> {
                    BsonDocument bi = BsonDocument.parse(i.toJson());

                    BsonValue name = bi.remove("name");
                    bi.put("_id", name);

                    ret.add(bi);
                });

        return ret;
    }

    /**
     *
     * @param dbName
     * @param collection
     * @param keys
     * @param options
     */
    void createIndex(
            String dbName,
            String collection,
            BsonDocument keys,
            BsonDocument options) {
        if (options == null) {
            client
                    .getDatabase(dbName)
                    .getCollection(collection)
                    .createIndex(keys);
        } else {
            // need to find a way to get IndexOptions from json
            IndexOptions io = new IndexOptions();

            io.background(true);

            client
                    .getDatabase(dbName)
                    .getCollection(collection)
                    .createIndex(keys, getIndexOptions(options));
        }
    }

    /**
     *
     * @param db
     * @param collection
     * @param indexId
     * @return
     */
    int deleteIndex(
            String dbName,
            String collection,
            String indexId) {
        client
                .getDatabase(dbName)
                .getCollection(collection)
                .dropIndex(indexId);

        return HttpStatus.SC_NO_CONTENT;<commit_msg>Remove useless object<commit_after>/*
 * RESTHeart - the Web API for MongoDB
 * Copyright (C) SoftInstigate Srl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.restheart.db;

import com.mongodb.MongoClient;
import com.mongodb.client.ListIndexesIterable;
import static com.mongodb.client.model.Filters.eq;
import com.mongodb.client.model.IndexOptions;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.restheart.utils.HttpStatus;

/**
 *
 * @author <NAME> {@literal <<EMAIL>>}
 */
class IndexDAO {

    public static final Bson METADATA_QUERY
            = eq("_id", "_properties");

    private static final BsonDocument FIELDS_TO_RETURN_INDEXES;

    static {
        FIELDS_TO_RETURN_INDEXES = new BsonDocument();
        FIELDS_TO_RETURN_INDEXES.put("key", new BsonInt32(1));
        FIELDS_TO_RETURN_INDEXES.put("name", new BsonInt32(1));
    }
    private final MongoClient client;

    IndexDAO(MongoClient client) {
        this.client = client;
    }

    /**
     *
     * @param dbName
     * @param collName
     * @return
     */
    List<BsonDocument> getCollectionIndexes(String dbName, String collName) {
        List<BsonDocument> ret = new ArrayList<>();

        ListIndexesIterable<Document> indexes = client
                .getDatabase(dbName)
                .getCollection(collName, BsonDocument.class)
                .listIndexes();

        indexes.iterator().forEachRemaining(
                i -> {
                    BsonDocument bi = BsonDocument.parse(i.toJson());

                    BsonValue name = bi.remove("name");
                    bi.put("_id", name);

                    ret.add(bi);
                });

        return ret;
    }

    /**
     *
     * @param dbName
     * @param collection
     * @param keys
     * @param options
     */
    void createIndex(
            String dbName,
            String collection,
            BsonDocument keys,
            BsonDocument options) {
        if (options == null) {
            client
                    .getDatabase(dbName)
                    .getCollection(collection)
                    .createIndex(keys);
        } else {
            client
                    .getDatabase(dbName)
                    .getCollection(collection)
                    .createIndex(keys, getIndexOptions(options));
        }
    }

    /**
     *
     * @param db
     * @param collection
     * @param indexId
     * @return
     */
    int deleteIndex(
            String dbName,
            String collection,
            String indexId) {
        client
                .getDatabase(dbName)
                .getCollection(collection)
                .dropIndex(indexId);

        return HttpStatus.SC_NO_CONTENT;
{'fraction_non_alphanumeric': 0.0906570557249792, 'fraction_numerical': 0.00249514832270585, 'mean_word_length': 2.059796437659033, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6878852', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1951, 'n_words': 544}