io.sirix.access.PathBasedPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sirix-core Show documentation
Show all versions of sirix-core Show documentation
SirixDB is a hybrid on-disk and in-memory document oriented, versioned database system. It has a lightweight buffer manager, stores everything in a huge persistent and durable tree and allows efficient reconstruction of every revision. Furthermore, SirixDB implements change tracking, diffing and supports time travel queries.
package io.sirix.access;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.checkerframework.checker.nullness.qual.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import static java.util.Collections.unmodifiableMap;
/**
* Manages a pool of objects which can be indexed by a {@link Path}.
*
* @author Joao Sousa
*/
@ThreadSafe
public class PathBasedPool {
/**
* Logger for {@link PathBasedPool}.
*/
private static final Logger logger = LoggerFactory.getLogger(PathBasedPool.class);
/**
* The thread safe map of objects in the pool.
*/
private final Map> sessions;
/**
* Default Constructor
*/
public PathBasedPool() {
this.sessions = new ConcurrentHashMap<>();
}
/**
* Persists a file/object into the pool.
*
* @param file the file that identifies the object.
* @param object object to be persisted to the pool.
*/
public void putObject(final Path file, final E object) {
this.sessions.compute(file, (key, value) -> append(key, value, object));
}
/**
* Checks if the provided {@code file} has any registered object.
*
* @param file The file which might have registered objects.
* @return {@code true} if the provided {@code file} has 1 or more registered objects.
*/
public boolean containsAnyEntry(final Path file) {
return this.sessions.containsKey(file);
}
/**
* Package private method to remove a database.
*
* @param file file that represents object.
* @param object The object to be removed from the pool.
*/
public void removeObject(final Path file, final E object) {
this.sessions.computeIfPresent(file, (key, value) -> remove(key, value, object));
}
@Nullable
private Set remove(final Path path, final Set objects, final E object) {
logger.trace("Removing session in path {}", path);
objects.remove(object);
// coalesce to null if empty
return objects.isEmpty() ? null : objects;
}
private Set append(final Path path, @Nullable final Set objects,
final E object) {
final Set coalescedSessions = objects == null ? new HashSet<>() : objects;
logger.trace("Registering new session in path {}", path);
coalescedSessions.add(object);
return coalescedSessions;
}
public Map> asMap() {
return unmodifiableMap(this.sessions);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy