0

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?

1 Answers1

1

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.

Vyacheslav Lukianov
  • 1,913
  • 8
  • 12
  • Thanks, but doesn't this still make getting a certain SynsetID O(n) instead of O(1)? The code where I print stuff is not important, it was just an example of how I could be reading the entities. I'm just worried because I expect to have 13 million different SynsetIDs, so searching for them everytime would be very slow compared to a get(x) in a key-value style DB. –  Feb 13 '20 at 16:25
  • 2
    Both `find(...)` and `getProperty(...)` are O(log n). – Vyacheslav Lukianov Feb 13 '20 at 16:43