Jump to content
McObject Forums
Mikkel

getObjectby StrKey

Recommended Posts

Mikkel    0

Hello, 
I want to know if there is any option to get an object by defined strKey without using the iterator. 
My problem is that I have much data, and using the iterator for getting single objects is sometimes very slow.
Regards

Mikkel

Share this post


Link to post
Share on other sites
perstmco    0

Sorry, we do not completely understand the question.

Which API you using?

Perst provides a large number of various indexes and these indexes provide various access methods.

If you just need to get an object by a unique key, then you can use get(Object key) method.  Otherwise, show us a class definition and explain how you want to access objects of this class and we can advise further.

Share this post


Link to post
Share on other sites
Mikkel    0

hello,

I have posted in another Thread that I have problems beause of outofmemory exceptions.

At the moment the problem is because I use the IPersistentList

Because of this I tried to use another structure. I will post my new DataType Below.

If I use the iterator to get one of my FahrtData It takes a long time. So I need another way to get one special object of my Datatype Fahrtdata from the FiledIndex<FahrtData>. 
I know the exact strKey Value of the object I need. But How can I get this object without iterating all objects of Type FahrtData?

Is there a Way to set the objectid to the value of strKey or something else to get exactly the object with the strKey value "xyz" for example?

 

My Perst Root Class:

package com.example.perstmanager;

import org.garret.perst.FieldIndex;
import org.garret.perst.Index;
import org.garret.perst.Persistent;
import org.garret.perst.Storage;

public class PerstRootClass extends Persistent
{
    // index on MyPersistentClass.intKey
    public FieldIndex<Fahrt> fahrtKeyIndex;
    // index on MyPersistentClass.strKey
    public FieldIndex<FahrtData> dataKeyIndex;
    // index on MyPersistentClass, which key doesn't
    // belong to the class
    public Index<Fahrt> foreignIndex;
    public PerstRootClass(Storage db)
    {
         super(db);
         fahrtKeyIndex = db.<Fahrt>createFieldIndex(
             Fahrt.class, // indexed class
             "strKey", // name of indexed field
             true); // unique index
        dataKeyIndex = db.<FahrtData>createFieldIndex(
            FahrtData.class, // indexed class
            "strKey", // name of indexed field
            true); // unique index
    }
    // Default constructor is needed for Perst to be able to
    // instantiate instances of loaded objects
    public PerstRootClass() {}
}

 

and My Datatype :
package com.example.perstmanager;

import org.garret.perst.IPersistentList;
import org.garret.perst.Persistent;
import de.gerding.common.CANDaten;
import de.gerding.common.CANDatenList;

public class FahrtData extends Persistent
{
    private String fahrtname;
    private String zeitstempel;
    private String header;
    private String canid;
    private String data;
    public String strKey;
    public FahrtData(String fahrtname, String zeitstempel, String header, String canid, String data, int count) {
        this.fahrtname = fahrtname;
        this.strKey = fahrtname+"|"+count;
        this.zeitstempel = zeitstempel;
        this.header = header;
        this.canid = canid;
        this.data = data;
    }
    public FahrtData() {

    }

    public String getFahrtname()
    {
        return this.fahrtname;
    }

    public void updateFahrtname(String name){ //Methode wird nur intern von der DB benötigt, muss aber public sein
        this.strKey = this.strKey.replace(this.fahrtname, name);
        this.fahrtname = name;
        modify();
    }

    public String getZeitStempel(){
        return this.zeitstempel;
    }

    public String getHeader() {
        return this.header;
    }

    public String getCanid() {
        return this.canid;
    }

    public String getData() {
        return this.data;
    }

    public int getCount(){
        return Integer.valueOf(this.strKey.replace(this.fahrtname,""));
    }

    public String toString()
    {
        return this.fahrtname + " | " + this.strKey.replace(this.fahrtname,"") + " | " + this.zeitstempel + " | " + this.header + " | " + this.canid + " | " + this.data;
    }
}
 

Share this post


Link to post
Share on other sites
perstmco    0

>> Is there a Way to set the objectid to the value of strKey or something else to get exactly the object with the strKey value "xyz" for example?

Something like this:
    Fahrt fahrt = root.foreignIndex.dataKeyIndex .get("xyz");

 

Share this post


Link to post
Share on other sites
Mikkel    0

I have problems.

root.foreignIndex is okay. but root.foreignIndex.dataKeyIndex can not be found.

root.dataKeyIndex.get(Key Key) is a existing method. Can I use this instead?

Share this post


Link to post
Share on other sites
Mikkel    0

Okay, I tried it. root.datakeyIndex.get(Key Key) can be used by me. But I found out that adding single objects is to slow. I have to add multiple Objects as a List. Can you help me to fix my Assertion Failed Exception when using the IPersistentList?
I made another topic for that problem.


 I need a working solution for adding IPersistentLists of my Datatype. Can you help me with that? May be some example code that I can  compare where I make mistakes

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×