Java Code Examples for org.mapdb.DB#getTreeMap()

The following examples show how to use org.mapdb.DB#getTreeMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Huge_Read.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(String[] args){
DB db = DBMaker.newFileDB(new File("/tmp/db2"))
        .journalDisable()
                //.asyncWriteDisable()
        .make();

Map<Integer, String> map = db.getTreeMap("map");

long time = System.currentTimeMillis();
long max = (int) 1e8;
long step = max/100;
for(int i=0;i<max;i++){
    map.get(i);
    if(i%step == 0){
        System.out.println(100.0 * i/max);
    }

}

System.out.println("Closing");
db.close();

System.out.println(System.currentTimeMillis() - time);
}
 
Example 2
Source File: SQL_Auto_Incremental_Unique_Key.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    DB db = DBMaker.newTempFileDB().make();

    //open or create new map
    Map<Long, String> map = db.getTreeMap("map");

    // open existing or create new Atomic record with given name
    // if no record with given name exist, new recid is created with value `0`
    Atomic.Long keyinc = Atomic.getLong(db, "map_keyinc");


    // Allocate new unique key to use in map
    // Atomic.Long will use `compare-and-swap` operation to atomically store incremented value
    // Key values can be used only for single insert
    // key == 1
    Long key = keyinc.incrementAndGet();
    map.put(key, "some string");

    // insert second entry,
    // key==2
    map.put(keyinc.incrementAndGet(), "some other string");

    System.out.println(map);

}
 
Example 3
Source File: _HelloWorld.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static void main(String[] args){

        //Configure and open database using builder pattern.
        //All options are available with code auto-completion.
        File dbFile = Utils.tempDbFile();
        DB db = DBMaker.newFileDB(dbFile)
                .closeOnJvmShutdown()
                .encryptionEnable("password")
                .make();

        //open an collection, TreeMap has better performance then HashMap
        ConcurrentNavigableMap<Integer,String> map = db.getTreeMap("collectionName");

        map.put(1,"one");
        map.put(2,"two");
        //map.keySet() is now [1,2] even before commit

        db.commit();  //persist changes into disk

        map.put(3,"three");
        //map.keySet() is now [1,2,3]
        db.rollback(); //revert recent changes
        //map.keySet() is now [1,2]

        db.close();

    }
 
Example 4
Source File: Huge_Insert.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static void main(String[] args){
    DB db = DBMaker
            //.newFileDB(new File("/mnt/big/db/aa"))
            .newAppendFileDB(new File("/mnt/big/db/aa" + System.currentTimeMillis()))
            .make();

    Map map = db
            .getTreeMap("map");
            //.getHashMap("map");

    long time = System.currentTimeMillis();
    long max = (int) 1e8;
    AtomicLong progress = new AtomicLong(0);
    Utils.printProgress(progress);

    while(progress.incrementAndGet()<max){
        Long val = Utils.RANDOM.nextLong();
        map.put(val, "test"+val);
    }
    progress.set(-1);

    System.out.println("Closing");
    db.close();

    System.out.println(System.currentTimeMillis() - time);

}
 
Example 5
Source File: Custom_Value.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	
	// Open or create db file
	DB db = DBMaker.newFileDB(new File("dbCustomValue"))
			.make();
	
	// Open or create table
	Map<String,Person> dbMap = 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 out, Person value) throws IOException {
               out.writeUTF(value.getName());
               out.writeUTF(value.getCity());
           }

           @Override
           public Person deserialize(DataInput in, int available) throws IOException {
               return new Person(in.readUTF(), in.readUTF());
           }
       }

       Serializer<Person> serializer = new CustomSerializer();

       DB db2 = DBMaker.newTempFileDB().make();

       Map<String,Person> map2 = db2.createHashMap("map",null, serializer); //key serializer is null (use default)

       map2.put("North", new Person("Yet another dwarf","Somewhere"));

       db2.close();


}
 
Example 6
Source File: ReferenceMap.java    From Qora with MIT License 4 votes vote down vote up
@Override
protected Map<String, byte[]> getMap(DB database) 
{
	//OPEN MAP
	return database.getTreeMap("references");
}
 
Example 7
Source File: AccountMap.java    From Qora with MIT License 4 votes vote down vote up
public AccountMap(WalletDatabase walletDatabase, DB database) 
{
	//OPEN MAP
	this.addressMap = database.getTreeMap(ADDRESSES);
}