0

I use mapDB to store my Data, which are inserted in maps. I followed the instructions on mapDB-Site and was able to set up an Database and also fill it with values. But my problem is right here, I insert my data into the maps and then call the Database-Class to insert the Map into the DB. The current map is then added to the DB, but automatically overwrites the previous entry, so that the number of entries are always 1.

Here is my code:

    for(Element objects : objectInstanceList) 
                    {   

                    mapID = objects.getName().toString();
                    List<Element> listObjects1 = objects.getChildren();
                    Multimap<String, Values> mm = HashMultimap.create();

                    for(Element objectClasses : listObjects1 )
                      {     
                            List<Element> listObjects2 = objectClasses.getChildren();   

                            for(Element objectAttributes : listObjects2)
                                {
                                    String name = objectAttributes.getAttributeValue("name");
                                    String type = objectAttributes.getAttributeValue("type");
                                    String value = objectAttributes.getAttributeValue("value"); 
                                    Values v = new Values(name, type, value);
                                    mm.put(objectClasses.getName(), v);                     
                                }

                      }                         
                        DataBase.putHW(mapID, mm);
                        System.out.println(mm);

                    }

Like I said I fill the Multimap mm with some values and the Method Database.putHW, which looks like this(created like in the examples of the mapDB page).

public class DataBase {

static DB dbHW = DBMaker.newMemoryDB().make();
static NavigableSet<Fun.Tuple2<String, Multimap<String,Values>>> multimapHW 
= dbHW.getTreeSet("Applications");

public static void putHW(String mapID, Multimap<String,Values> dbMap) {

multimapHW = dbHW.createTreeSet("Delta").serializer(BTreeKeySerializer.TUPLE2).make();
multimapHW.add(Fun.t2(mapID, dbMap)); // Fun means functional, its the Function to add values in the map    
   }
}

So why does the multimapHW in the Database just contains 1 entry, instead of many entries?

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
Alika87
  • 301
  • 1
  • 5
  • 22

1 Answers1

4

MapDB requires keys and values to be immutable. This makes using collections as keys/values very problematic.

Using Multimap<String,Values> in sorted set is completely wrong since MapDB passes it to comparator which probably fails.

For your usage I would recommend TreeSet and split multimap into triple tuple:

static NavigableSet<Fun.Tuple3<String, String,Values>> multimapHW =      
      dbHW.createTreeSet("Applications")
          .serializer(BTreeKeySerializer.TUPLE3)
          .makeOrGet();

It uses TUPLE3 serializer which ensures efficient space usage. Bind class has static methods which allows to find keys at second and third level for given primary keys. Some example are here:

https://github.com/jankotek/MapDB/blob/master/src/test/java/examples/MultiMap.java

Jan Kotek
  • 1,084
  • 7
  • 4
  • Thanks for your answer. The Datatype Values contains 3 Strings in it. It represents a certain attribute of the Object. The problem is, its not conform with java.lang.comparable, thus I cannot add it into a Tuple. The datastructure with the Multimap for the Values looked okay to me, I just needed a database to save them. Guess my datastructure with 1 key (the Name of the Object) and a Map with all its attributes dont fit in mapDB. Am I right with this assumption? – Alika87 Oct 04 '13 at 15:38