Mikkel

how to reset correctly

8 posts in this topic

Hy,
 

I am trying to evaluate the peformance of perstdb for our specific project. Therefore I try to use a method for insert ten times. First iteration is okay, then reset and in second iteration I got an error.

FATAL EXCEPTION: IntentService[MessService]
                                                                    Process: de.gerding.dbcompare, PID: 4755
                                                                    java.util.NoSuchElementException
                                                                        at org.garret.perst.impl.Btree$BtreeSelectionIterator.next(Btree.java:984)
                                                                        at com.example.perstmanager.PerstDataManager.getFahrtDatabyName(PerstDataManager.java:289)
                                                                        at com.example.perstmanager.PerstDataManager.insertDataInFahrt(PerstDataManager.java:95)

 

First the whole method:
private FahrtData getFahrtDatabyName(String fahrtdataname) { 
    boolean found = false;
    Iterator<FahrtData> iterator = root.dataKeyIndex.iterator();
    FahrtData fahrtxData;
    do {
        fahrtxData = (FahrtData) iterator.next();
        if(fahrtxData.strKey.equalsIgnoreCase(fahrtdataname)) {
            return fahrtxData;
        }
    }while(iterator.hasNext());
    return null;
}

Line 289 is :
fahrtxData = (FahrtData) iterator.next();

My code for reset is the following:
@Override
public void reset() {
    if(root.fahrtKeyIndex.size()>0)
    {
        root.fahrtKeyIndex.clear();
    }
    if(root.dataKeyIndex.size()>0)
    {
        root.dataKeyIndex.clear();
    }
    db.commit();
}

 

Can you help me find out why this is only working for one iteration?  may be I have to modify my reset code.

Share this post


Link to post
Share on other sites

Sorry, maybe we do not understand something in your code, but your iteration loop assumes that the index is not empty, i.e. that there is at least one element in the index. But it is not true after reset which clears both index. 

while (iterator.hasNext()) {
    fahrtxData = (FahrtData) iterator.next();
    ...
}

and not do .. while loop.

Share this post


Link to post
Share on other sites

Hello again, maybe you can help me again.

I think I have a second error in my code for delete.

 

public void deleteFahrt(String fahrtname) 
{
    Iterator<Fahrt> fahrtIterator = root.fahrtKeyIndex.iterator();
    Fahrt fahrtx;
    while(fahrtIterator.hasNext()) {
        fahrtx = (Fahrt) fahrtIterator.next();
        if (fahrtx.getFahrtName().equalsIgnoreCase(fahrtname)) {
            for(int i = 0; i <= fahrtx.getLastList(); i++){
                root.fahrtKeyIndex.remove(fahrtx);
            }

        }
    }
}

This time I used the debugger (as you can see in attached screenshot) before writing here. There I can see my root.fahrtKeyIndex has a size of 1.

Why I am getting the following error in the line:     while(fahrtIterator.hasNext()) {

09-30 11:49:06.552 4401-4418/de.gerding.dbcompare E/AndroidRuntime: FATAL EXCEPTION: IntentService[MessService]
                                                                    Process: de.gerding.dbcompare, PID: 4401
                                                                    java.util.ConcurrentModificationException
                                                                        at org.garret.perst.impl.Btree$BtreeSelectionIterator.hasNext(Btree.java:976)
                                                                        at com.example.perstmanager.PerstDataManager.deleteFahrt(PerstDataManager.java:188)
                                                                        at de.gerding.dbcompare.MessService.szenario10(MessService.java:501)
                                                                        at de.gerding.dbcompare.MessService.debug(MessService.java:184)
                                                                        at de.gerding.dbcompare.MessService.onHandleIntent(MessService.java:59)
                                                                        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.os.HandlerThread.run(HandlerThread.java:61)

 

perst deleteFahrt error.PNG

Share this post


Link to post
Share on other sites

I think the error is because I try to delete some objects that are part of the iterator. If I change my code to the following everything works fine. 

public void deleteFahrt(String fahrtname) {
        Iterator<Fahrt> fahrtIterator = root.fahrtKeyIndex.iterator();
        Fahrt fahrtx;
        zuLoeschen =  new Vector();
        while(fahrtIterator.hasNext()) {
            fahrtx = (Fahrt) fahrtIterator.next();
            if (fahrtx.getFahrtName().equalsIgnoreCase(fahrtname)) {
                zuLoeschen.add(fahrtx);
                //root.fahrtKeyIndex.remove(fahrtx);
            }
        }
        for(int i = 0; i < zuLoeschen.size(); i++){
            root.fahrtKeyIndex.remove(zuLoeschen.get(i));
        }
        root.modify();
    }

Is there another solution as first adding the objects to a vector or something else and delete them at the end in an separate for loop?

Share this post


Link to post
Share on other sites

You should either remove records using the Iterator.remove() method or set the "perst.concurrent.iterator" property.

Share this post


Link to post
Share on other sites
On 9/30/2016 at 4:54 PM, Mikkel said:

Hello again, maybe you can help me again.

I think I have a second error in my code for delete.

Sorry, maybe we do not understand something in your code, but your iteration loop assumes that review for phenq the index is not empty, i.e. that there is at least one element in the index. But it is not true after reset which clears both index. 

while (iterator.hasNext()) {
    fahrtxData = (FahrtData) iterator.next();
    ...
}

and not do .. while loop.


public void deleteFahrt(String fahrtname) 
{
    Iterator<Fahrt> fahrtIterator = root.fahrtKeyIndex.iterator();
    Fahrt fahrtx;
    while(fahrtIterator.hasNext()) {
        fahrtx = (Fahrt) fahrtIterator.next();
        if (fahrtx.getFahrtName().equalsIgnoreCase(fahrtname)) {
            for(int i = 0; i <= fahrtx.getLastList(); i++){
                root.fahrtKeyIndex.remove(fahrtx);
            }

        }
    }
}

This time I used the debugger (as you can see in attached screenshot) before writing here. There I can see my root.fahrtKeyIndex has a size of 1.

Why I am getting the following error in the line:     while(fahrtIterator.hasNext()) {

09-30 11:49:06.552 4401-4418/de.gerding.dbcompare E/AndroidRuntime: FATAL EXCEPTION: IntentService[MessService]
                                                                    Process: de.gerding.dbcompare, PID: 4401
                                                                    java.util.ConcurrentModificationException
                                                                        at org.garret.perst.impl.Btree$BtreeSelectionIterator.hasNext(Btree.java:976)
                                                                        at com.example.perstmanager.PerstDataManager.deleteFahrt(PerstDataManager.java:188)
                                                                        at de.gerding.dbcompare.MessService.szenario10(MessService.java:501)
                                                                        at de.gerding.dbcompare.MessService.debug(MessService.java:184)
                                                                        at de.gerding.dbcompare.MessService.onHandleIntent(MessService.java:59)
                                                                        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.os.HandlerThread.run(HandlerThread.java:61)

 

perst deleteFahrt error.PNG

yes this does the trick,  iterator.remove thanks 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now