Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Commit

Permalink
Fixed formatting and added file header
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-kraemer committed Jan 24, 2015
1 parent 488ac7e commit 7b637d7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 39 deletions.
5 changes: 4 additions & 1 deletion src/main/java/de/fhg/igd/mongomvcc/impl/internal/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ private void readCommit(Commit c, Tree tree) {
//iteratively read parent commit (if there is any)
stack.addLast(c);
long cid = c.getParentCID();
if (cid == 0)
if (cid == 0) {
break;
}
c = tree.resolveCommit(cid);
}

while (!stack.isEmpty()) {
c = stack.removeLast();

//read objects from the given commit and put them into the index
for (Map.Entry<String, IdMap> e : c.getObjects().entrySet()) {
IdMap m = getObjects(e.getKey());
Expand Down
118 changes: 80 additions & 38 deletions src/test/java/de/fhg/igd/mongomvcc/impl/internal/IndexTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// This file is part of MongoMVCC.
//
// Copyright (c) 2012-2015 Fraunhofer IGD
//
// MongoMVCC is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// MongoMVCC 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with MongoMVCC. If not, see <http://www.gnu.org/licenses/>.

package de.fhg.igd.mongomvcc.impl.internal;

import de.fhg.igd.mongomvcc.VBranch;
Expand All @@ -11,43 +28,67 @@
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;

/**
* Tests {@link de.fhg.igd.mongomvcc.impl.internal.Index}
* @author Qualtagh
*/
public class IndexTest extends AbstractMongoDBVDatabaseTest {
/**
* This test checks if indexes are loaded in the correct order: oldest should be first, newest come later.
* This test checks if indexes are loaded in the correct order:
* oldest should be first, newest come later.
*/
@Test
public void loadingOrder() {
VCollection persons = _master.getCollection( "persons" );
Map< String, Object > elvis = _factory.createDocument();
elvis.put( "name", "elvis" );
elvis.put( "age", "2" );
persons.insert( elvis );
Map< String, Object > max = _factory.createDocument();
max.put( "name", "max" );
max.put( "age", "3" );
persons.insert( max );
VCollection persons = _master.getCollection("persons");

// add two test objects to 'persons' collection
Map<String, Object> elvis = _factory.createDocument();
elvis.put("name", "elvis");
elvis.put("age", "2");
persons.insert(elvis);

Map<String, Object> max = _factory.createDocument();
max.put("name", "max");
max.put("age", "3");
persons.insert(max);

// save commit and objects currently stored in the database
long first = _master.commit();
ArrayList< String > beforeInSession = new ArrayList<String>();
for ( Map< String, Object > person : persons.find() )
ArrayList<String> beforeInSession = new ArrayList<String>();
for (Map<String, Object> person : persons.find()) {
beforeInSession.add( person.toString() );
elvis.put( "age", "4" );
persons.insert(elvis );
}

// change an object and insert it again (i.e. update it)
elvis.put("age", "4");
persons.insert(elvis);

// save commit and objects now stored in the database
long second = _master.commit();
ArrayList< String > afterInSession = new ArrayList<String>();
for ( Map< String, Object > person : persons.find() )
afterInSession.add( person.toString() );
VBranch oldMaster = _db.checkout( first );
VCollection oldWells = oldMaster.getCollection( "persons" );
ArrayList< String > beforeOutOfSession = new ArrayList<String>();
for ( Map< String, Object > person : oldWells.find() )
beforeOutOfSession.add( person.toString() );
VBranch newMaster = _db.checkout( second );
VCollection newWells = newMaster.getCollection( "persons" );
ArrayList< String > afterOutOfSession = new ArrayList<String>();
for ( Map< String, Object > person : newWells.find() )
afterOutOfSession.add( person.toString() );
assertArrayEquals( beforeInSession.toArray(), beforeOutOfSession.toArray() );
assertArrayEquals( afterInSession.toArray(), afterOutOfSession.toArray() );
ArrayList<String> afterInSession = new ArrayList<String>();
for (Map<String, Object> person : persons.find()) {
afterInSession.add(person.toString());
}

// checkout old commit and save objects stored in the database
VBranch oldMaster = _db.checkout(first);
VCollection oldWells = oldMaster.getCollection("persons");
ArrayList<String> beforeOutOfSession = new ArrayList<String>();
for (Map<String, Object> person : oldWells.find()) {
beforeOutOfSession.add(person.toString());
}

// checkout new commit and save objects stored in the database
VBranch newMaster = _db.checkout(second);
VCollection newWells = newMaster.getCollection("persons");
ArrayList<String> afterOutOfSession = new ArrayList<String>();
for (Map<String, Object> person : newWells.find()) {
afterOutOfSession.add(person.toString());
}

// compare stored objects
assertArrayEquals(beforeInSession.toArray(), beforeOutOfSession.toArray());
assertArrayEquals(afterInSession.toArray(), afterOutOfSession.toArray());
}

/**
Expand All @@ -56,17 +97,18 @@ public void loadingOrder() {
*/
@Test
public void stackOverflow() throws StackOverflowError {
VCollection persons = _master.getCollection( "stack" );
Map< String, Object > elvis = _factory.createDocument();
elvis.put( "name", "elvis" );
for ( int i = 0; i < 2500; i++ ) {
persons.insert( elvis );
VCollection persons = _master.getCollection("stack");
Map<String, Object> elvis = _factory.createDocument();
elvis.put("name", "elvis");
for (int i = 0; i < 2500; i++) {
persons.insert(elvis);
_master.commit();
}

VDatabase db = _factory.createDatabase();
db.connect( ( ( MongoDBVDatabase )_db ).getDB().getName() );
VBranch master = db.checkout( VConstants.MASTER );
persons = master.getCollection( "stack" );
db.connect(((MongoDBVDatabase)_db).getDB().getName());
VBranch master = db.checkout(VConstants.MASTER);
persons = master.getCollection("stack");
persons.find().size();
}
}
}

0 comments on commit 7b637d7

Please sign in to comment.