Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples;
  
  import org.mapdb.DB;
  import org.mapdb.DBMaker;
  import org.mapdb.Utils;
  
  import java.io.*;
  import java.util.Map;
 
 /*
  *	Demonstrates HashMaps with non-standard types of objects as key or value.
  */
 public class Custom_Value {


    
MapDB uses custom serialization which stores class metadata at single place. Thanks to it is 10x more efficient than standard Java serialization. Using custom values in MapDB has three conditions: 1) classes should be immutable. There is instance cache, background serialization etc Modifing your classes after they were inserted into MapDB may leed to unexpected things. 2) You should implement `Serializable` marker interface. MapDB tries to stay compatible with standard Java serialization. 3) Even your values should implement equalsTo method for CAS (compare-and-swap) operations.
 
     public static class Person implements Serializable{
         final String name;
         final String city;
 
         public Person(String nString c){
             super();
             this. = n;
             this. = c;
         }
 
         public String getName() {
             return ;
         }
 
         public String getCity() {
             return ;
         }
 
         @Override
         public boolean equals(Object o) {
             if (this == oreturn true;
             if (o == null || getClass() != o.getClass()) return false;
 
             Person person = (Persono;
 
             if ( != null ? !.equals(person.city) : person.city != nullreturn false;
             if ( != null ? !.equals(person.name) : person.name != nullreturn false;
 
             return true;
         }
 
     }
 
     public static void main(String[] args) {
 
         // Open db in temp directory
         File f = Utils.tempDbFile();
 		DB db = DBMaker.newFileDB(f)
 				.make();
 		
 		// Open or create table
 		Map<String,PersondbMap = db.getTreeMap("personAndCity");
 		
 		// Add data
 		Person bilbo = new Person("Bilbo","The Shire");
 		Person sauron = new Person("Sauron","Mordor");
 		Person radagast = new Person("Radagast","Crazy Farm");
 		
 		dbMap.put("west",bilbo);
 		dbMap.put("south",sauron);
 		dbMap.put("mid",radagast);
 
 		// Commit and close
 		db.commit();
 		db.close();
 
 
         //
         // Second option for using cystom values is to use your own serializer.
         // This usually leads to better performance as MapDB does not have to
         // analyze the class structure.
         //
 
         class CustomSerializer implements Serializer<Person>, Serializable{
 
             @Override
             public void serialize(DataOutput outPerson valuethrows IOException {
                 out.writeUTF(value.getName());
                out.writeUTF(value.getCity());
            }
            @Override
            public Person deserialize(DataInput inint availablethrows IOException {
                return new Person(in.readUTF(), in.readUTF());
            }
        }
        Serializer<Personserializer = new CustomSerializer();
        DB db2 = DBMaker.newTempFileDB().make();
        Map<String,Personmap2 = db2.createHashMap("map").valueSerializer(serializer).make();
        map2.put("North"new Person("Yet another dwarf","Somewhere"));
        db2.close();
	}
}





New to GrepCode? Check out our FAQ X