/** * @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(), newHashMap((Map) struct)); else return Pair.of(entry.getKey(), struct); }).collect(toMap(pair -> (String) pair.a(), Pair::b)); }
/** * Return all the {@link Collection}s contained in the database. * * @return a {@link Map} containing all the {@link Collection}s contained in the database and their associated names */ @VisibleForTesting public Map<String, Object> getAll() { return db.getAll(); }
public Map<String, Object> getAll() { this.assertNotClosed(); return this.tx.getAll(); }
@Override public Collection<HobsonUser> getUsers() { ClassLoader old = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); List<HobsonUser> users = new ArrayList<>(); Map<String,Object> map = db.getAll(); for (String username : map.keySet()) { users.add(createUser(username, (Map<String,Object>)map.get(username))); } return users; } finally { Thread.currentThread().setContextClassLoader(old); } }
@Override @SuppressWarnings({"unchecked", "rawtypes"}) protected void internalCopyTo(DataMapper target) { AbstractMapDbBackend to = (AbstractMapDbBackend) target; for (Map.Entry<String, Object> entry : database.getAll().entrySet()) { Object collection = entry.getValue(); if (collection instanceof Map) { Map fromMap = (Map) collection; Map toMap = to.database.hashMap(entry.getKey()).open(); toMap.putAll(fromMap); } else { throw new UnsupportedOperationException(String.format("Cannot copy MapDB backend: store type %s is not supported", collection.getClass().getSimpleName())); } } }
/** * Copies all the contents of this this back-end to the target one. * * @param target the {@code MapDbPersistenceBackend} to copy the database contents to * * @throws UnsupportedOperationException if the current {@link DB} contains {@link Collection}s which are not {@link * Map}s */ @SuppressWarnings({"unchecked", "rawtypes"}) // Unchecked cast: 'Map' to 'Map<...>' public void copyTo(MapDbPersistenceBackend target) { for (Map.Entry<String, Object> entry : db.getAll().entrySet()) { Object collection = entry.getValue(); if (collection instanceof Map) { Map fromMap = (Map) collection; Map toMap = target.db.hashMap(entry.getKey()).createOrOpen(); toMap.putAll(fromMap); } else { throw new UnsupportedOperationException("Cannot copy MapDB backend: store type " + collection.getClass().getSimpleName() + " is not supported"); } } } }
for (String collectionName : db.getAll().keySet()) { String prefix = this.name + "/enumerated/"; if (collectionName.startsWith(prefix)) {
@Deactivate public void deactivate() { timer.cancel(); for (Map.Entry<String, Object> entry : localDB.getAll().entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (value instanceof Map) { // This is a map implementation to be handled as such Map asMap = (Map) value; if (asMap.isEmpty()) { //the map is empty and may be deleted localDB.delete(key); } } else if (value instanceof Set) { // This is a set implementation and can be handled as such Set asSet = (Set) value; if (asSet.isEmpty()) { //the set is empty and may be deleted localDB.delete(key); } } } localDB.commit(); localDB.close(); log.info("Stopped"); }
for (String collectionName : db.getAll().keySet()) { String prefix = this.name + "/enumerated/"; if (collectionName.startsWith(prefix)) {
if (db.getAll().isEmpty()) { LOG.info("No OSM tables exist yet, they will be created.");