org.mapdb.Atomic Java Examples
The following examples show how to use
org.mapdb.Atomic.
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: SQL_Auto_Incremental_Unique_Key.java From scava with Eclipse Public License 2.0 | 6 votes |
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 #2
Source File: HistoDataSource.java From ipst with Mozilla Public License 2.0 | 6 votes |
public void saveReferenceNetwork(Network network) throws IOException { Objects.requireNonNull(network); if (!config.getMapDb().isPersistent()) { this.referenceNetwork = network; } else if (this.referenceNetwork == null || !this.referenceNetwork.getId().equals(network.getId())) { DataSource dataSource = new FileDataSource(storeDir, network.getId()); Properties parameters = new Properties(); parameters.setProperty("iidm.export.xml.indent", "true"); Exporters.export("XIIDM", network, parameters, dataSource); Atomic.String reference = db.atomicString("referenceNetwotk").createOrOpen(); String oldNet = reference.get(); reference.set(network.getId() + ".xiidm"); this.referenceNetwork = network; if (oldNet != null) { Files.delete(Paths.get(storeDir.toString(), oldNet)); } } }
Example #3
Source File: MapDBContext.java From TelegramBots with MIT License | 5 votes |
/** * @return a local non-thread safe copy of the database */ private Map<String, Object> localCopy() { return db.getAll().entrySet().stream().map(entry -> { Object struct = entry.getValue(); if (struct instanceof Set) return Pair.of(entry.getKey(), newHashSet((Set) struct)); else if (struct instanceof List) return Pair.of(entry.getKey(), newArrayList((List) struct)); else if (struct instanceof Map) return Pair.of(entry.getKey(), new BackupMap((Map) struct)); else if (struct instanceof Atomic.Var) return Pair.of(entry.getKey(), BackupVar.createVar(((Atomic.Var) struct).get())); return Pair.of(entry.getKey(), struct); }).collect(toMap(pair -> (String) pair.a(), Pair::b)); }
Example #4
Source File: Lazily_Loaded_Records.java From scava with Eclipse Public License 2.0 | 4 votes |
public static void main(String[] args) { 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 boolean valuesStoredOutsideNodes = true; Map map = db.createTreeMap("name",32, valuesStoredOutsideNodes, null, null, null); 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 = Atomic.createVar(db, "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", Serializer.STRING_SERIALIZER); //load record String lazyString = db.getEngine().get(recid, Serializer.STRING_SERIALIZER); //update record db.getEngine().update(recid, "new value", Serializer.STRING_SERIALIZER); //I hope this example helped! db.close(); }
Example #5
Source File: MapDBContext.java From TelegramBots with MIT License | 4 votes |
@Override public <T> Var<T> getVar(String name) { return new MapDBVar<>((Atomic.Var<T>) db.atomicVar(name).createOrOpen()); }
Example #6
Source File: MapDBVar.java From TelegramBots with MIT License | 4 votes |
public MapDBVar(Atomic.Var<T> var) { this.var = var; }