org.mapdb.HTreeMap Java Examples

The following examples show how to use org.mapdb.HTreeMap. 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: Histogram.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    HTreeMap<Long, Double> map = DBMaker.newTempHashMap();

    // histogram, category is a key, count is a value
    ConcurrentMap<String, Long> histogram = new ConcurrentHashMap<String, Long>(); //any map will do

    // bind histogram to primary map
    // we need function which returns category for each map entry
    Bind.histogram(map, histogram, new Fun.Function2<String, Long, Double>(){
        @Override
        public String run(Long key, Double value) {
            if(value<0.25) return "first quarter";
            else if(value<0.5) return "second quarter";
            else if(value<0.75) return "third quarter";
            else return "fourth quarter";
        }
    });

    //insert some random stuff
    for(long key=0;key<1e4;key++){
        map.put(key, Math.random());
    }

    System.out.println(histogram);
}
 
Example #2
Source File: Zerospike.java    From bidder with Apache License 2.0 6 votes vote down vote up
/**
 * Load the expirer up from the database.
 */
private void initializeLoad() {
    long now = System.currentTimeMillis();
    Set elements = objects.entrySet();
    logger.info("Initialization starts with {} elements", elements.size());
    int count = 0;

    for (Object e : elements) {
        HTreeMap.Entry xx = (HTreeMap.Entry) e;
        String key = (String) xx.getKey();
        Map x = (Map) xx.getValue();
        Number time = (Number) x.get("expire");
        if (now > time.longValue()) {
            objects.remove(key);
            count++;
        } else {
            worker.addKey(key, time.longValue());
        }
        ;
    }
    logger.info("Initialization complete, {} keys expired, {} are left.", count, objects.size());
    if (kafkaLogger != null) {
        kafkaLogger.add(String.format("Initialization complete, %d keys expired, %d are left.", count, objects.size()));
    }
}
 
Example #3
Source File: MapDBStorage.java    From TarsosLSH with GNU Lesser General Public License v3.0 5 votes vote down vote up
public MapDBStorage(int numberOfTables,String fileName){
	
	hashtables = new ArrayList<>();
	
	db = DBMaker.fileDB(fileName).fileMmapEnable().closeOnJvmShutdown().make();
	
	for(int i = 0 ; i < numberOfTables ; i++){
		HTreeMap<Integer, long[]> map = db.hashMap("map_"+i)
				.keySerializer(Serializer.INTEGER)
				.valueSerializer(Serializer.LONG_ARRAY)
				.counterEnable()
				.createOrOpen();
		hashtables.add(map);
	}
}
 
Example #4
Source File: MapTest.java    From bidder with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {

        HTreeMap objects;
        DB db = DBMaker
                .fileDB("junk.db")
                .fileMmapEnable()            // Always enable mmap
                .fileMmapEnableIfSupported() // Only enable mmap on supported platforms
                .fileMmapPreclearDisable()   // Make mmap file faster

                // Unmap (release resources) file when its closed.
                // That can cause JVM crash if file is accessed after it was unmapped
                // (there is possible race condition).
                .cleanerHackEnable()
                .transactionEnable()
                .make();

//optionally preload file content into disk cache
        db.getStore().fileLoad();

        objects = db.hashMap("objects").createOrOpen();

        double time = System.currentTimeMillis();

        Map m = new HashMap();

        for (int i=0;i<1000000;i++) {
            objects.put("ben", "faul");
        }

        time = System.currentTimeMillis() - time;

        time = time/1000;
        double d = 1000000/time;

        System.out.println("D: " + d + " writes/second");

        db.close();

	}
 
Example #5
Source File: InMemoryModesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDBCreatedBaseOnDirectByteBuffer_whenUsed_checkUsageCorrect() {

    DB heapDB = DBMaker.memoryDirectDB().make();

    HTreeMap<Integer, String> map = heapDB
      .hashMap("myMap")
      .keySerializer(Serializer.INTEGER)
      .valueSerializer(Serializer.STRING)
      .createOrOpen();

    map.put(1, "ONE");

    assertEquals("ONE", map.get(1));
}
 
Example #6
Source File: InMemoryModesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDBCreatedBaseOnByteArray_whenUsed_checkUsageCorrect() {

    DB heapDB = DBMaker.memoryDB().make();

    HTreeMap<Integer, String> map = heapDB
      .hashMap("myMap")
      .keySerializer(Serializer.INTEGER)
      .valueSerializer(Serializer.STRING)
      .createOrOpen();

    map.put(1, "ONE");

    assertEquals("ONE", map.get(1));
}
 
Example #7
Source File: MapDBCachePooFactory.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CachePool createCachePool(String poolName, int cacheSize,
                                 int expiredSeconds) {

    HTreeMap<Object, Object> cache = this.db.createHashMap(poolName).
            expireMaxSize(cacheSize).
            expireAfterAccess(expiredSeconds, TimeUnit.SECONDS).
            makeOrGet();
    return new MapDBCachePool(cache, cacheSize);

}
 
Example #8
Source File: MapDBExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    DB db = DBMaker.memoryDB().make();

    HTreeMap diskCache = db.hashMap("testCache")
            .expireStoreSize(10 * 1024)
            .expireMaxSize(1000)
            .expireAfterCreate(10, TimeUnit.SECONDS)
            .createOrOpen();

    HTreeMap cache = db.hashMap("testCache")
            .expireMaxSize(100)
            .expireOverflow(diskCache)
            .createOrOpen();
}
 
Example #9
Source File: MapDBCachePooFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CachePool createCachePool(String poolName, int cacheSize,
                                    int expiredSeconds) {

	HTreeMap<Object, Object> cache = this.db.createHashMap(poolName)
			.expireMaxSize(cacheSize)
			.expireAfterAccess(expiredSeconds, TimeUnit.SECONDS)
			.makeOrGet();
	return new MapDBCachePool(cache, cacheSize);

}
 
Example #10
Source File: KeyDeletionScheduler.java    From bidder with Apache License 2.0 5 votes vote down vote up
/**
 * Every 1/2 second this worker gathers all the expired keys up, then deletes them from the database file. Note,
 * it does not need to let the subscribers know, as their own caches will also delete the data.
 * @param object HTreeMap. The interface to the map database on disk.
 * @param logger Logger. The error logger.
 */
public KeyDeletionScheduler(HTreeMap object, Logger logger) {
    this.object = object;
    this.logger = logger;

    scratchdb = DBMaker.tempFileDB()
            .fileMmapEnable()            // Always enable mmap
            .fileMmapEnableIfSupported() // Only enable mmap on supported platforms
            .fileMmapPreclearDisable()   // Make mmap file faster
            // Unmap (release resources) file when its closed.
            // That can cause JVM crash if file is accessed after it was unmapped
            // (there is possible race condition).
            .cleanerHackEnable()
            .make();

    map = (HTreeMap<String, PostponedWorkItem>) scratchdb.hashMap("scratch").createOrOpen();
    map.clear();

    ScheduledExecutorService execService = Executors.newScheduledThreadPool(5);
    execService.scheduleAtFixedRate(() -> {
        try {
            process();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }, 500, 500, TimeUnit.MILLISECONDS);
}
 
Example #11
Source File: FileServer.java    From bidder with Apache License 2.0 5 votes vote down vote up
/**
 * Create a file relay for the database.
 * @param logger Logger. Error logger object.
 * @param objects HTreeMap. The data on disk.
 * @param client Socket. The connection to the subscriber.
 */
public Relay(Logger logger, HTreeMap objects, Socket client) {
    this.logger = logger;
    this.objects = objects;
    this.client = client;
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    me = new Thread(this);
    me.start();
}
 
Example #12
Source File: FileServer.java    From bidder with Apache License 2.0 5 votes vote down vote up
/**
 * A Server that returns unexpired key/data from the database.
 * @param logger Logger. The error logger.
 * @param objects HTreeMap. The map of data on disk.
 * @param port int. The port we will listen for clients to connect.
 * @throws Exception on network and file errors.
 */
public FileServer( Logger logger, HTreeMap objects, int port) throws Exception {
    this.objects = objects;
    this.port = port;
    this.logger = logger;
    ss = new ServerSocket(port);

    logger.info("File server listening on port: {}", port);
    me = new Thread(this);
    me.start();
}
 
Example #13
Source File: ListenToZeroMQ.java    From bidder with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for the spy listening to context topic
 * @param anyPort int. THe port used to connect to the broker.
 * @param objects HTreeMap. The mapping to the file on disk.
 * @param worker KeyDeletionScheduler. The worker responsible for deleting data on disk when the key expires.
 * @param trace boolean. Set to true to watch activity on the context topic.
 */
public ListenToZeroMQ(int anyPort, HTreeMap objects, KeyDeletionScheduler worker, boolean trace) {
    this.objects = objects;
    this.worker = worker;
    this.trace = trace;
    ZMQ.Context context = ZMQ.context(1);
    pipe = context.socket(ZMQ.PAIR);
    pipe.bind("tcp://*:"+anyPort);

    me = new Thread(this);
    me.start();
}
 
Example #14
Source File: ProportionalRandomCollection.java    From bidder with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for a hand-made ProportionalRandomCollection.
 *
 * @param name String. The name of the symbol.
 * @throws Exception on file i/o errors opening the navamp file off-heap.
 */
public ProportionalRandomCollection(String name) throws Exception {
    String message = "Initialize ProportionalRandomCollection: " + name + " as " + name;
    symbols.put(name, this);
    logger.info("{}", message);
    dbMap = (HTreeMap<String, ProportionalEntry>) db.hashMap("scratch").create();
    dbMap.clear();
}
 
Example #15
Source File: MapDBCachePool.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public MapDBCachePool(HTreeMap<Object, Object> htreeMap, long maxSize) {
	this.htreeMap = htreeMap;
	this.maxSize=maxSize;
	cacheStati.setMaxSize(maxSize);
}
 
Example #16
Source File: MapDBCachePool.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public MapDBCachePool(HTreeMap<Object, Object> hTreeMap, long maxSize) {
    this.hTreeMap = hTreeMap;
    this.maxSize = maxSize;
    cacheStatistics.setMaxSize(maxSize);
}
 
Example #17
Source File: GitFilterHelper.java    From git-as-svn with GNU General Public License v2.0 4 votes vote down vote up
@NotNull
public static HTreeMap<String, String> getCacheMd5(@NotNull GitFilter filter, @NotNull DB cacheDb) {
  return cacheDb.hashMap("cache.filter." + filter.getName() + ".md5", Serializer.STRING, Serializer.STRING).createOrOpen();
}
 
Example #18
Source File: GitFilterHelper.java    From git-as-svn with GNU General Public License v2.0 4 votes vote down vote up
@NotNull
public static HTreeMap<String, Long> getCacheSize(@NotNull GitFilter filter, @NotNull DB cacheDb) {
  return cacheDb.hashMap("cache.filter." + filter.getName() + ".size", Serializer.STRING, Serializer.LONG).createOrOpen();
}
 
Example #19
Source File: HelloBaeldungUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenInMemoryDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {

    DB db = DBMaker.memoryDB().make();

    String welcomeMessageKey = "Welcome Message";
    String welcomeMessageString = "Hello Baeldung!";

    HTreeMap myMap = db.hashMap("myMap").createOrOpen();
    myMap.put(welcomeMessageKey, welcomeMessageString);

    String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);

    db.close();

    assertEquals(welcomeMessageString, welcomeMessageFromDB);
}
 
Example #20
Source File: HelloBaeldungUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenInFileDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {

    DB db = DBMaker.fileDB("file.db").make();

    String welcomeMessageKey = "Welcome Message";
    String welcomeMessageString = "Hello Baeldung!";

    HTreeMap myMap = db.hashMap("myMap").createOrOpen();
    myMap.put(welcomeMessageKey, welcomeMessageString);

    String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);

    db.close();

    assertEquals(welcomeMessageString, welcomeMessageFromDB);
}
 
Example #21
Source File: InMemoryModesUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenDBCreatedOnHeap_whenUsed_checkUsageCorrect() {

    DB heapDB = DBMaker.heapDB().make();

    HTreeMap<Integer, String> map = heapDB
      .hashMap("myMap")
      .keySerializer(Serializer.INTEGER)
      .valueSerializer(Serializer.STRING)
      .createOrOpen();

    map.put(1, "ONE");

    assertEquals("ONE", map.get(1));

}
 
Example #22
Source File: ProportionalRandomCollection.java    From bidder with Apache License 2.0 4 votes vote down vote up
/**
 * Use a buffered reader to load the entries into the collection.
 *
 * @param br BufferedReader. The file reader object.
 * @throws Exception on I/O errors.
 */
void makeFilter(BufferedReader br) throws Exception {
    String line;
    dbMap = (HTreeMap<String, ProportionalEntry>) db.hashMap("scratch").create();
    dbMap.clear();
    final List<String> throwThis = new ArrayList<String>();
    while ((line = br.readLine()) != null) {
        if (throwThis.size()>0)
            throw new Exception(throwThis.get(0));

        final String sline = line;
        Runnable task = ()  -> {
            final String[] parts = eatquotedStrings(sline);
            if (parts.length < 2) {
                throwThis.add("ProportionalRandomCollection requires a K/V entry");
                return;
            }
            String key = parts[0];

            ProportionalEntry e = new ProportionalEntry();

            for (int i = 1; i < parts.length; i++) {
                String[] wparts = parts[i].split("=");
                if (wparts.length != 2) {
                    throwThis.add("ProportionalRandomCollection requires weights in K=V,... pairs");
                    return;
                }
                try {
                    Integer weight = Integer.parseInt(wparts[1]);
                    if (weight <= 1) {
                        throwThis.add("ProportionalRandomCollection requires a weight of >= 1, not " + wparts[1]);
                        return;
                    }
                    e.add(weight, wparts[0]);
                } catch (Exception error) {
                    throwThis.add("ProportionalRandomCollection requires weights in K=INTEGER,... pairs");
                    return;
                }
            }

            dbMap.put(key, e);

        };
        Thread thread = new Thread(task);
        thread.start();
    }
    br.close();
}