Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.service.bdb;
  
  import com.sleepycat.je.*;
  
Class that encapsulates BDB-JE backed storage of last-accessed information.

Keys are derived from entry keys, so that grouped entries typically map to a single entry, whereas individual entries just use key as is or do not use last-accessed information at all.

Note that a concrete implementation is required due to details of actual key being used.

 
 public abstract class LastAccessStore<K extends EntryKey, E extends StoredEntry<K>>
 {
     /*
     /**********************************************************************
     /* BDB store for last-accessed timestamps
     /**********************************************************************
      */

    
Underlying BDB entity store ("table") for last-accessed timestamps
 
     protected final Database _store;
 
     protected final StoredEntryConverter<K,E,?> _entryConverter;
     
     /*
     /**********************************************************************
     /* Life cycle
     /**********************************************************************
      */
 
     public LastAccessStore(Environment envStoredEntryConverter<K,E,?> conv)
         throws DatabaseException
     {
          = env.openDatabase(null// no TX
                 "LastAccessed"dbConfig(env));
          = conv;
     }
 
     public void stop()
     {
         .close();
     }
 
     /*
     /**********************************************************************
     /* Public API
     /**********************************************************************
      */
 
     public long findLastAccessTime(E entry) {
         EntryLastAccessed acc = findLastAccessEntry(entry.getKey(), entry.getLastAccessUpdateMethod());
         return (acc == null) ? 0L : acc.lastAccessTime;
     }
     
     public long findLastAccessTime(K keyLastAccessUpdateMethod method)
     {
         EntryLastAccessed entry = findLastAccessEntry(keymethod);
         return (entry == null) ? 0L : entry.lastAccessTime;
     }
     
     public EntryLastAccessed findLastAccessEntry(E entry) {
         return findLastAccessEntry(entry.getKey(), entry.getLastAccessUpdateMethod());
     }
     
     {
         DatabaseEntry lastAccessKey = lastAccessKey(keymethod);
         if (lastAccessKey == null) {
             return null;
         }
         DatabaseEntry entry = new DatabaseEntry();
         OperationStatus status = .get(nulllastAccessKeyentrynull);
         switch (status) {
         case :
         case :
             return .createLastAccessed(entry.getData(), entry.getOffset(), entry.getSize());
         case // was deleted during operation.. shouldn't be getting
         case :
             // fall through
         }
         return null;
     }
 
     public void updateLastAccess(E entrylong timestamp)
     {
         DatabaseEntry lastAccessKey = lastAccessKey(entry);
         if (lastAccessKey != null) {
            /* 18-Sep-2012, tatu: Should we try to enforce constraint on monotonically
             *   increasing timestamps? Since this is not used for peer-to-peer syncing,
             *   minor deviations from exact value are ok (deletion occurs after hours,
             *   or at most minutes since last access), so let's avoid extra lookup.
             *   Same goes for other settings
             */
            EntryLastAccessed acc = .createLastAccessed(entrytimestamp);
            .put(nulllastAccessKeynew DatabaseEntry(acc.asBytes()));
        }
    }

    

Returns:
True if an entry was deleted; false otherwise (usually since there was no entry to delete)
    public boolean removeLastAccess(K keyLastAccessUpdateMethod methodlong timestamp)
    {
        DatabaseEntry lastAccessKey = lastAccessKey(keymethod);
        if (lastAccessKey != null) {
            OperationStatus status = .delete(nulllastAccessKey);
            switch (status) {
            case :
            case :
                return true;
            default:
            }
        }
        return false;
    }
    
    /*
    /**********************************************************************
    /* Internal methods
    /**********************************************************************
     */
    protected DatabaseConfig dbConfig(Environment env)
    {
        DatabaseConfig dbConfig = new DatabaseConfig();
        EnvironmentConfig econfig = env.getConfig();
        dbConfig.setReadOnly(econfig.getReadOnly());
        dbConfig.setAllowCreate(econfig.getAllowCreate());
        dbConfig.setSortedDuplicates(false);
        return dbConfig;
    }
    private DatabaseEntry lastAccessKey(E entry) {
        return lastAccessKey(entry.getKey(), entry.getLastAccessUpdateMethod());
    }
    
    protected abstract DatabaseEntry lastAccessKey(K keyLastAccessUpdateMethod acc);
New to GrepCode? Check out our FAQ X