Ejemplo n.º 1
0
  public static void main(String[] args) throws IOException {

    DB db = DBMaker.newMemoryDB().make();
    //
    // TreeMap has build in support for lazily loaded values.
    // In that case each value are not stored inside node,
    // but in separate record.
    //
    // use DB.createTreeMap to create TreeMap with non-default parameters

    Map map = db.createTreeMap("name").valuesOutsideNodesEnable().make();
    map.put("key", "this string is loaded lazily with 'map.get(key)' ");

    //
    // Other option for lazily loaded record is to use Atomic.Var.
    // In this case you have singleton record with name.
    // As bonus you can update reference in thread-safe atomic manner.
    //
    Atomic.Var<String> record = db.createAtomicVar("lazyRecord", "aaa", db.getDefaultSerializer());

    record.set("some value");
    System.out.println(record.get());

    // Last option is to use low level Engine storage directly.
    // Each stored record gets assigned unique recid (record id),
    // which is latter used to get or update record.
    // Your code should store only recid as reference to object.
    // All MapDB collections are written this way.

    // insert new record
    long recid = db.getEngine().put("something", SerializerBase.STRING_NOSIZE);

    // load record
    String lazyString = db.getEngine().get(recid, SerializerBase.STRING_NOSIZE);

    // update record
    db.getEngine().update(recid, "new value", SerializerBase.STRING_NOSIZE);

    // I hope this example helped!
    db.close();
  }
Ejemplo n.º 2
0
  public static void main(String[] args) throws IOException {

    /** max number of elements to import */
    final long max = (int) 1e6;

    /** Open database in temporary directory */
    File dbFile = File.createTempFile("mapdb", "temp");
    DB db =
        DBMaker.newFileDB(dbFile)
            /** disabling Write Ahead Log makes import much faster */
            .transactionDisable()
            .make();

    long time = System.currentTimeMillis();

    /**
     * Source of data which randomly generates strings. In real world this would return data from
     * file.
     */
    Iterator<String> source =
        new Iterator<String>() {

          long counter = 0;

          @Override
          public boolean hasNext() {
            return counter < max;
          }

          @Override
          public String next() {
            counter++;
            return randomString(10);
          }

          @Override
          public void remove() {}
        };

    /**
     * BTreeMap Data Pump requires data source to be presorted in reverse order (highest to lowest).
     * There is method in Data Pump we can use to sort data. It uses temporarly files and can handle
     * fairly large data sets.
     */
    source =
        Pump.sort(
            source,
            true,
            100000,
            Collections.reverseOrder(BTreeMap.COMPARABLE_COMPARATOR), // reverse  order comparator
            db.getDefaultSerializer());

    /**
     * Disk space used by serialized keys should be minimised. Keys are sorted, so only difference
     * between consequential keys is stored. This method is called delta-packing and typically saves
     * 60% of disk space.
     */
    BTreeKeySerializer<String> keySerializer = BTreeKeySerializer.STRING;

    /** Translates Map Key into Map Value. */
    Fun.Function1<Integer, String> valueExtractor =
        new Fun.Function1<Integer, String>() {
          @Override
          public Integer run(String s) {
            return s.hashCode();
          }
        };

    /** Create BTreeMap and fill it with data */
    Map<String, Integer> map =
        db.createTreeMap("map")
            .pumpSource(source, valueExtractor)
            // .pumpPresort(100000) // for presorting data we could also use this method
            .keySerializer(keySerializer)
            .make();

    System.out.println(
        "Finished; total time: "
            + (System.currentTimeMillis() - time) / 1000
            + "s; there are "
            + map.size()
            + " items in map");
    db.close();
  }