All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sirix.cache.PageCache Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 0.11.0
Show newest version
package io.sirix.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;
import io.sirix.page.IndirectPage;
import io.sirix.page.PageReference;
import io.sirix.page.interfaces.Page;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public final class PageCache implements Cache {

  private final com.github.benmanes.caffeine.cache.Cache pageCache;

  public PageCache(final int maxSize) {
    RemovalListener removalListener = (PageReference key, Page value, RemovalCause cause) -> {
      key.setPage(null);
      //      if (value instanceof KeyValueLeafPage keyValueLeafPage) {
      //        keyValueLeafPage.clearPage();
      //      }
    };

    pageCache = Caffeine.newBuilder()
                        .maximumSize(maxSize)
                        .expireAfterAccess(5, TimeUnit.MINUTES)
                        .scheduler(scheduler)
                        .removalListener(removalListener)
                        .build();
  }

  @Override
  public void clear() {
    pageCache.invalidateAll();
  }

  @Override
  public Page get(PageReference key) {
    var page = pageCache.getIfPresent(key);

    if (page instanceof IndirectPage indirectPage) {
      page = new IndirectPage(indirectPage);
    }

    return page;
  }

  @Override
  public void put(PageReference key, Page value) {
    pageCache.put(key, value);
  }

  @Override
  public void putAll(Map map) {
    pageCache.putAll(map);
  }

  @Override
  public void toSecondCache() {
    throw new UnsupportedOperationException();
  }

  @Override
  public Map getAll(Iterable keys) {
    return pageCache.getAllPresent(keys);
  }

  @Override
  public void remove(PageReference key) {
    pageCache.invalidate(key);
  }

  @Override
  public void close() {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy