/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.jackrabbit.mk.testing;
import java.util.Arrays;
import java.util.Collections;
import org.apache.jackrabbit.mongomk.impl.MongoConnection;
import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
import org.apache.jackrabbit.mongomk.impl.model.MongoNode;
import org.apache.jackrabbit.mongomk.impl.model.MongoSync;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
public class MongoClearCollections {
public static final String INITIAL_COMMIT_MESSAGE = "This is an autogenerated initial commit";
public static final String INITIAL_COMMIT_PATH = "";
public static final String INITIAL_COMMIT_DIFF = "+\"/\" : {}";
public static final String COLLECTION_COMMITS = "commits";
public static final String COLLECTION_NODES = "nodes";
public static final String COLLECTION_SYNC = "sync";
/**
* Removes all documents from nodes collection.
*
* @param mongoConnection
*/
public static void clearNodesCollection(MongoConnection mongoConnection) {
if (!mongoConnection.getDB().collectionExists(COLLECTION_NODES)) {
return;
}
DBCollection nodeCollection = mongoConnection.getDB().getCollection(
COLLECTION_NODES);
nodeCollection.remove(new BasicDBObject());
MongoNode root = new MongoNode();
root.setRevisionId(0L);
root.setPath("/");
nodeCollection.insert(root);
}
/**
* Removes all documents from commits collection.
*
* @param mongoConnection
*/
public static void clearCommitsCollection(MongoConnection mongoConnection) {
if (!mongoConnection.getDB().collectionExists(COLLECTION_COMMITS)) {
return;
}
DBCollection commitCollection = mongoConnection.getDB().getCollection(
COLLECTION_COMMITS);
commitCollection.remove(new BasicDBObject());
MongoCommit commit = new MongoCommit();
commit.setAffectedPaths(Collections.singleton("/"));
commit.setBaseRevisionId(0L);
commit.setDiff(INITIAL_COMMIT_DIFF);
commit.setMessage(INITIAL_COMMIT_MESSAGE);
commit.setRevisionId(0L);
commit.setPath(INITIAL_COMMIT_PATH);
commitCollection.insert(commit);
}
/**
* Removes all documents from head collection.
*
* @param mongoConnection
*/
public static void clearHeadCollection(MongoConnection mongoConnection) {
if (!mongoConnection.getDB().collectionExists(COLLECTION_SYNC)) {
return;
}
DBCollection headCollection = mongoConnection.getDB().getCollection(
COLLECTION_SYNC);
headCollection.remove(new BasicDBObject());
MongoSync headMongo = new MongoSync();
headMongo.setHeadRevisionId(0L);
headMongo.setNextRevisionId(1L);
headCollection.insert(headMongo);
}
/**
* Removes all documents from all microkernel collections.
*
* @param mongoConnection
*/
public static void clearAllCollections(MongoConnection mongoConnection) {
clearNodesCollection(mongoConnection);
clearCommitsCollection(mongoConnection);
clearHeadCollection(mongoConnection);
}
}