Search code examples
javaxodus

Is there a way to "get" a property instead of "finding" it in Xodus?


I have two entities in Xodus. One is called SynsetID which has only one property: synsetID. Then I have Gloss, which has a property called gloss. One SynsetID may be linked to multiple glosses. I use the following code to populate the database:

        PersistentEntityStore store = PersistentEntityStores.newInstance("glosses-test");
        final StoreTransaction txn = store.beginTransaction(); 

        Entity synsetID;
        Entity gloss;
        String id;

        for (BabelSynset synset : synsetList){

            id = synset.getId().getID();

            System.out.println(id + " : ");

            List<BabelGloss> glosses = synset.getGlosses(chosenLang);

            synsetID = txn.newEntity("SynsetID");
            synsetID.setProperty("synsetID", id);


            String glossInLang;

            for (BabelGloss g : glosses){
                glossInLang = g.getGloss();
                gloss = txn.newEntity("Gloss");
                gloss.setProperty("gloss", glossInLang);

                gloss.addLink("synsetID", synsetID);
                synsetID.addLink("gloss", gloss);

                System.out.println(id + " : " + glossInLang);
            }

            txn.flush();
        }

        txn.commit();

But of course, then if I want to get a specific SynsetID, instead of getting it directly in a key-value fashion I have to search for it:

    PersistentEntityStore store = PersistentEntityStores.newInstance("glosses-test");
    final StoreTransaction txn = store.beginReadonlyTransaction();

    Entity synsetID;
    Entity gloss;
    String id;

    for (BabelSynset synset : synsetList){

        id = synset.getId().getID();

        EntityIterable candidates = txn.find("SynsetID", "synsetID", id);

        if (!candidates.isEmpty()){
            System.out.println(id + ": ");
        }

        for (Entity s : candidates){
            for (Entity g : s.getLinks("gloss")){
                System.out.println(id + " : " + g.getProperty("gloss"));
            }
        }


    }

Isn't this very inefficient? Can I do it differently?


Solution

  • You can do like this:

    for (Entity s: txn.getAll("SynsetID")) {
        Comparable id = s.getProperty("synsetID");
        for (Entity g : s.getLinks("gloss")) {
            System.out.println(id + " : " + g.getProperty("gloss"));
        }
    }
    

    This code has no assumptions about uniqueness of the synsetID property. I don't expect its performance would be better, but it looks more concise. Your code is ok, in particular if grouping by id is a must.