Beispiel #1
0
  @Override
  public void start() {
    try {
      // open the data file
      String location = configuration.location();
      if (location == null || location.trim().length() == 0)
        location = "Infinispan-SingleFileStore";

      file = new File(location, ctx.getCache().getName() + ".dat");
      if (!file.exists()) {
        File dir = file.getParentFile();
        if (!dir.mkdirs() && !dir.exists()) {
          throw log.directoryCannotBeCreated(dir.getAbsolutePath());
        }
      }
      channel = new RandomAccessFile(file, "rw").getChannel();

      // initialize data structures
      entries = newEntryMap();
      freeList = Collections.synchronizedSortedSet(new TreeSet<FileEntry>());

      // check file format and read persistent state if enabled for the cache
      byte[] header = new byte[MAGIC.length];
      if (channel.read(ByteBuffer.wrap(header), 0) == MAGIC.length
          && Arrays.equals(MAGIC, header)) {
        rebuildIndex();
        processFreeEntries();
      } else clear(); // otherwise (unknown file format or no preload) just reset the file

      // Initialize the fragmentation factor
      fragmentationFactor = configuration.fragmentationFactor();
    } catch (Exception e) {
      throw new PersistenceException(e);
    }
  }
Beispiel #2
0
 /**
  * Try to evict an entry if the capacity of the cache store is reached.
  *
  * @return FileEntry to evict, or null (if unbounded or capacity is not yet reached)
  */
 private FileEntry evict() {
   if (configuration.maxEntries() > 0) {
     synchronized (entries) {
       if (entries.size() > configuration.maxEntries()) {
         Iterator<FileEntry> it = entries.values().iterator();
         FileEntry fe = it.next();
         it.remove();
         return fe;
       }
     }
   }
   return null;
 }
Beispiel #3
0
 private void writeFileStore(
     XMLExtendedStreamWriter writer, SingleFileStoreConfiguration configuration)
     throws XMLStreamException {
   writer.writeStartElement(Element.FILE_STORE);
   configuration.attributes().write(writer);
   writeCommonStoreSubAttributes(writer, configuration);
   writeCommonStoreElements(writer, configuration);
   writer.writeEndElement();
 }
Beispiel #4
0
  private <Key> Map<Key, FileEntry> newEntryMap() {
    // only use LinkedHashMap (LRU) for entries when cache store is bounded
    final Map<Key, FileEntry> entryMap;
    Equivalence<Object> keyEq =
        ctx.getCache().getCacheConfiguration().dataContainer().keyEquivalence();
    if (configuration.maxEntries() > 0)
      entryMap = CollectionFactory.makeLinkedMap(16, 0.75f, true, null, null);
    else entryMap = CollectionFactory.makeMap(keyEq, AnyEquivalence.<FileEntry>getInstance());

    return Collections.synchronizedMap(entryMap);
  }