All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.async.client.gridfs.GridFSIndexCheckImpl Maven / Gradle / Ivy

There is a newer version: 3.12.14
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.async.client.gridfs;

import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.ClientSession;
import com.mongodb.async.client.FindIterable;
import com.mongodb.async.client.ListIndexesIterable;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.lang.Nullable;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Map;

import static com.mongodb.ReadPreference.primary;
import static com.mongodb.assertions.Assertions.notNull;

final class GridFSIndexCheckImpl implements GridFSIndexCheck {
    private static final Document PROJECTION = new Document("_id", 1);
    private final ClientSession clientSession;
    private final MongoCollection filesCollection;
    private final MongoCollection chunksCollection;

    GridFSIndexCheckImpl(@Nullable final ClientSession clientSession, final MongoCollection filesCollection,
                         final MongoCollection chunksCollection) {
        this.clientSession = clientSession;
        this.filesCollection = notNull("files collection", filesCollection);
        this.chunksCollection = notNull("chunks collection", chunksCollection);
    }

    @Override
    public void checkAndCreateIndex(final SingleResultCallback callback) {
        MongoCollection collection = filesCollection.withDocumentClass(Document.class).withReadPreference(primary());
        FindIterable findIterable;
        if (clientSession != null) {
            findIterable = collection.find(clientSession);
        } else {
            findIterable = collection.find();
        }

        findIterable.projection(PROJECTION).first(
                new SingleResultCallback() {
                    @Override
                    public void onResult(final Document result, final Throwable t) {
                        if (t != null) {
                            callback.onResult(null, t);
                        } else if (result == null) {
                            checkFilesIndex(callback);
                        } else {
                            callback.onResult(null, null);
                        }
                    }
                });
    }

    private  void hasIndex(final MongoCollection collection, final Document index, final SingleResultCallback callback) {
        ListIndexesIterable listIndexesIterable;
        if (clientSession != null) {
            listIndexesIterable = collection.listIndexes(clientSession);
        } else {
            listIndexesIterable = collection.listIndexes();
        }

        listIndexesIterable.into(new ArrayList(), new SingleResultCallback>() {
            @Override
            public void onResult(final ArrayList indexes, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else {
                    boolean hasIndex = false;
                    for (Document result : indexes) {
                        Document indexDoc = result.get("key", new Document());
                        for (final Map.Entry entry : indexDoc.entrySet()) {
                            if (entry.getValue() instanceof Number) {
                                entry.setValue(((Number) entry.getValue()).intValue());
                            }
                        }
                        if (indexDoc.equals(index)) {
                            hasIndex = true;
                            break;
                        }
                    }
                    callback.onResult(hasIndex, null);
                }
            }
        });
    }

    private void checkFilesIndex(final SingleResultCallback callback) {
        final Document filesIndex = new Document("filename", 1).append("uploadDate", 1);
        hasIndex(filesCollection.withReadPreference(primary()), filesIndex,
                new SingleResultCallback() {
                    @Override
                    public void onResult(final Boolean result, final Throwable t) {
                        if (t != null) {
                            callback.onResult(null, t);
                        } else if (!result) {
                            SingleResultCallback createIndexCallback = new SingleResultCallback() {
                                @Override
                                public void onResult(final String result, final Throwable t) {
                                    if (t != null) {
                                        callback.onResult(null, t);
                                    } else {
                                        checkChunksIndex(callback);
                                    }
                                }
                            };
                            if (clientSession != null) {
                                filesCollection.createIndex(clientSession, filesIndex, createIndexCallback);
                            } else {
                                filesCollection.createIndex(filesIndex, createIndexCallback);
                            }
                        } else {
                            checkChunksIndex(callback);
                        }
                    }
                });
    }

    private void checkChunksIndex(final SingleResultCallback callback) {
        final Document chunksIndex = new Document("files_id", 1).append("n", 1);
        hasIndex(chunksCollection.withReadPreference(primary()), chunksIndex, new SingleResultCallback() {
            @Override
            public void onResult(final Boolean result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else if (!result) {
                    SingleResultCallback createIndexCallback = new SingleResultCallback() {
                        @Override
                        public void onResult(final String result, final Throwable t) {
                            if (t != null) {
                                callback.onResult(null, t);
                            } else {
                                callback.onResult(null, null);
                            }
                        }
                    };
                    if (clientSession != null) {
                        chunksCollection.createIndex(clientSession, chunksIndex, new IndexOptions().unique(true), createIndexCallback);
                    } else {
                        chunksCollection.createIndex(chunksIndex, new IndexOptions().unique(true), createIndexCallback);
                    }
                } else {
                    callback.onResult(null, null);
                }
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy