Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.storemate.backend.bdbje;
  
  import java.io.File;
  
  import com.sleepycat.je.*;
  
Helper object used for configuring and instantiating com.fasterxml.storemate.store.StorableStore instances.
 
 public class BDBJEBuilder extends StoreBackendBuilder<BDBJEConfig>
 {
     protected StoreConfig _storeConfig;
     protected BDBJEConfig _bdbConfig;
 
     public BDBJEBuilder() { this(nullnull); }
 
     public BDBJEBuilder(StoreConfig storeConfigBDBJEConfig bdbConfig)
     {
         super(BDBJEConfig.class);
          = storeConfig;
          = bdbConfig;
     }
 
     @Override
     public BDBJEStoreBackend build() {
         return buildCreateAndInit();
     }

    
Method that will open an existing BDB database if one exists, or create one if not, and create a store with that BDB. Underlying data storage can do reads and writes.
 
     public BDBJEStoreBackend buildCreateAndInit() {
         return _buildAndInit(truetrue);
     }
 
         return _buildAndInit(falsefalse);
     }
 
         return _buildAndInit(falsetrue);
     }
     
     protected BDBJEStoreBackend _buildAndInit(boolean canCreateboolean canWrite)
     {
         if ( == nullthrow new IllegalStateException("Missing StoreConfig");
         if ( == nullthrow new IllegalStateException("Missing BDBJEConfig");
 
         File dbRoot = .;
         if (dbRoot == null) {
             throw new IllegalStateException("Missing BDBJEConfig.dataRoot");
         }
         if (!dbRoot.exists() || !dbRoot.isDirectory()) {
             if (!canCreate) {
                 throw new IllegalArgumentException("Directory '"+dbRoot.getAbsolutePath()+"' does not exist, not allowed to (try to) create");
             }
             if (!dbRoot.mkdirs()) {
                 throw new IllegalArgumentException("Directory '"+dbRoot.getAbsolutePath()+"' did not exist: failed to create it");
             }
         }
 
         StorableConverter storableConv = .createStorableConverter();
         Environment env = new Environment(dbRootenvConfig(canCreatecanWrite));
         Database entryDB = env.openDatabase(null// no TX
                 "entryMetadata"dbConfig(env));
         SecondaryDatabase index = env.openSecondaryDatabase(null"lastModIndex"entryDB,
                 indexConfig(env));
         BDBJEStoreBackend physicalStore = new BDBJEStoreBackend(storableConv,
                 dbRootentryDBindex);
 
         try {
             physicalStore.start();
         } catch (DatabaseException e) {
             throw new IllegalStateException("Failed to open StorableStore: "+e.getMessage(), e);
         }
         return physicalStore;
     }
 
     /*
     /**********************************************************************
     /* Fluent methods
     /**********************************************************************
      */
     
     @Override
     public BDBJEBuilder with(StoreConfig config) {
          = config;
         return this;
    }
    @Override
    public BDBJEBuilder with(StoreBackendConfig config) {
        if (!(config instanceof BDBJEConfig)) {
            String desc = (config == null) ? "NULL" : config.getClass().getName();
            throw new IllegalArgumentException("BDB-JE must be configured with a BDBJEConfig instance, not "
                    +desc);
        }
         = (BDBJEConfigconfig;
        return this;
    }
    
    /*
    /**********************************************************************
    /* Internal methods
    /**********************************************************************
     */
    
    protected EnvironmentConfig envConfig(boolean allowCreateboolean writeAccess)
    {
        EnvironmentConfig config = new EnvironmentConfig();
        config.setAllowCreate(allowCreate);
        config.setReadOnly(!writeAccess);
        config.setSharedCache(false);
        // Default of 500 msec way too low; usually set to higher value:
        // Default of 1 for lock count is not good; let's see what to use instead:
        return config;
    }
    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;
    }
    protected SecondaryConfig indexConfig(Environment env)
    {
        LastModKeyCreator keyCreator = new LastModKeyCreator();
        SecondaryConfig config = new SecondaryConfig();
        config.setAllowCreate(env.getConfig().getAllowCreate());
        // should not need to auto-populate ever:
        config.setAllowPopulate(false);
        config.setKeyCreator(keyCreator);
        // important: timestamps are not unique, need to allow dups:
        config.setSortedDuplicates(true);
        // no, it is not immutable (entries will be updated with new timestamps)
        config.setImmutableSecondaryKey(false);
        return config;
    }
New to GrepCode? Check out our FAQ X