001package org.cache2k.impl;
002
003/*
004 * #%L
005 * cache2k core package
006 * %%
007 * Copyright (C) 2000 - 2015 headissue GmbH, Munich
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the 
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public 
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025import org.cache2k.ClosableIterator;
026import org.cache2k.storage.StorageEntry;
027
028import java.util.concurrent.Future;
029
030/**
031* @author Jens Wilke; created: 2014-05-08
032*/
033public abstract class StorageAdapter {
034
035  public abstract void open();
036
037  /**
038   * Cancel all schedules timer jobs in the storage.
039   */
040  public abstract Future<Void> cancelTimerJobs();
041
042  public abstract Future<Void> shutdown();
043  public abstract void flush();
044  public abstract void purge();
045  public abstract boolean checkStorageStillDisconnectedForClear();
046  public abstract void disconnectStorageForClear();
047
048  /** Starts the parallel clearing process, returns immediatly */
049  public abstract Future<Void> clearAndReconnect();
050
051  /**
052   *
053   * @param _nextRefreshTime value expiry time in millis, 0: expire immediately, {@link Long#MAX_VALUE}: no expiry
054   */
055  public abstract void put(Entry e, long _nextRefreshTime);
056  public abstract StorageEntry get(Object key);
057  public abstract boolean remove(Object key);
058  public abstract void evict(Entry e);
059
060  /**
061   * TODO: Relly needed?
062   */
063  public abstract void expire(Entry e);
064  public abstract ClosableIterator<Entry> iterateAll();
065
066  /**
067   * Return the total number of entries within the heap and
068   * the storage. Should apply simple calculations to give and exact
069   * number. No heavy operation e.g. checking for duplicates.
070   *
071   * @see org.cache2k.Cache#getTotalEntryCount()
072   */
073  public abstract int getTotalEntryCount();
074
075  /** 0 means no alert, 1 orange, 2, red alert */
076  public abstract int getAlert();
077  public abstract void disable(Throwable t);
078
079  /** Implemented by a storage user, a cache or aggregator */
080  static interface Parent {
081
082    /** Change the storage implementation to another one or null for a disconnect */
083    void resetStorage(StorageAdapter _current, StorageAdapter _new);
084
085  }
086
087  protected static Throwable buildThrowable(String txt, Throwable ex) {
088    if (ex instanceof Error || ex.getCause() instanceof Error) {
089      return new CacheInternalError(txt, ex);
090    }
091    return new CacheStorageException(txt, ex);
092  }
093
094  protected static void rethrow(String txt, Throwable ex) {
095    if (ex instanceof Error || ex.getCause() instanceof Error) {
096      throw new CacheInternalError(txt, ex);
097    }
098    throw new CacheStorageException(txt, ex);
099  }
100
101}