Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.storemate.backend.leveldb;
  
  import java.io.File;
  
 
 
 {
    
For Node Stae stores we do not really need much any caching; but throw dog a bone of, say, nice round 200k.
 
     private final static long NODE_STATE_CACHE_SIZE = 200L * 1024L;

    
For LevelDB we actually need two separate 'tables'; one for data, another for last-modified index. Hence sub-directories.
 
     public final static String DATA_DIR = "entries";
     
     public final static String LAST_MOD_DIR = "lastmod";
     
     protected StoreConfig _storeConfig;
     protected LevelDBConfig _levelDBConfig;
 
     protected final LdbLogger _ldbLogger = new LdbLogger();
     
     public LevelDBBuilder() { this(nullnull); }
 
     public LevelDBBuilder(StoreConfig storeConfigLevelDBConfig levelDBConfig)
     {
         super(LevelDBConfig.class);
          = storeConfig;
          = levelDBConfig;
     }
 
     @Override
     public LevelDBStoreBackend build() {
         return buildCreateAndInit();
     }
 
     @Override
     public <K,V> NodeStateStore<K,V> buildNodeStateStore(File metadataRoot,
             RawEntryConverter<K> keyConv,
             RawEntryConverter<V> valueConv)
     {
         _verifyConfig();
         final String path = .;
         if (path == null || path.isEmpty()) {
             throw new IllegalStateException("Missing 'nodeStateDir'");
         }
         File nodeStateDir = metadataRoot;
         for (String part : path.split("/")) {
             nodeStateDir = new File(nodeStateDirpart);
         }
         if (!nodeStateDir.exists() || !nodeStateDir.isDirectory()) {
             if (!nodeStateDir.mkdirs()) {
                 throw new IllegalArgumentException("Directory '"+nodeStateDir.getAbsolutePath()+"' did not exist: failed to create it");
             }
         }
         _verifyDir(metadataRoottrue);
 
         Iq80DBFactory factory = .;
         Options options = new Options();
         options = options
                 .createIfMissing(true)
                 .logger()
                 // better safe than sorry, for store data?
                 .verifyChecksums(true)
                 .cacheSize()
                 ;
         
         DB nodeStateDB;
         try {
             nodeStateDB = factory.open(nodeStateDiroptions);
         } catch (IOException e) {
             throw new IllegalStateException("Failed to open Node state LevelDB: "+e.getMessage(), e);
         }
         return new LevelDBNodeStateStore<K,V>(nullkeyConvvalueConvnodeStateDB);
     }
    
    
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.
 
         return _buildAndInit(truetrue);
    }
        return _buildAndInit(falsefalse);
    }
        return _buildAndInit(falsetrue);
    }
    protected LevelDBStoreBackend _buildAndInit(boolean canCreateboolean canWrite)
    {
        _verifyConfig();
        File dbRoot = .;
        if (dbRoot == null) {
            throw new IllegalStateException("Missing LevelDBConfig.dataRoot");
        }
        _verifyDir(dbRootcanCreate);
        File dataDir = new File(dbRoot);
        _verifyDir(dataDircanCreate);
        File lastModDir = new File(dbRoot);
        _verifyDir(lastModDircanCreate);
        
        StorableConverter storableConv = .createStorableConverter();
        Iq80DBFactory factory = .;
        Options options = new Options();
        options = options
                .createIfMissing(canCreate)
                .logger()
                .verifyChecksums(false)
                ;
        
        DB dataDB;
        try {
            options = options.cacheSize(..getNumberOfBytes());
            dataDB = factory.open(dataDiroptions);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to open main data LevelDB: "+e.getMessage(), e);
        }
        DB indexDB;
        try {
            options = options.cacheSize(..getNumberOfBytes());
            indexDB = factory.open(lastModDiroptions);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to open last-mod index LevelDB: "+e.getMessage(), e);
        }
        return new LevelDBStoreBackend(storableConvdbRootdataDBindexDB);
    }
    
    /*
    /**********************************************************************
    /* Fluent methods
    /**********************************************************************
     */
    
    @Override
    public LevelDBBuilder with(StoreConfig config) {
         = config;
        return this;
    }
    @Override
    public LevelDBBuilder with(StoreBackendConfig config) {
        if (!(config instanceof LevelDBConfig)) {
            String desc = (config == null) ? "NULL" : config.getClass().getName();
            throw new IllegalArgumentException("LevelDB must be configured with a LevelDBConfig instance, not "
                    +desc);
        }
         = (LevelDBConfigconfig;
        return this;
    }
    
    /*
    /**********************************************************************
    /* Internal methods
    /**********************************************************************
     */
    protected void _verifyDir(File dirboolean canCreate)
    {
        if (!dir.exists() || !dir.isDirectory()) {
            if (!canCreate) {
                throw new IllegalArgumentException("Directory '"+dir.getAbsolutePath()+"' does not exist, not allowed to (try to) create");
            }
            if (!dir.mkdirs()) {
                throw new IllegalArgumentException("Directory '"+dir.getAbsolutePath()+"' did not exist: failed to create it");
            }
        }
    }
    protected void _verifyConfig() {
        if ( == nullthrow new IllegalStateException("Missing StoreConfig");
        if ( == nullthrow new IllegalStateException("Missing LevelDBConfig");
    }
    
    /*
    /**********************************************************************
    /* Helper types
    /**********************************************************************
     */
    static class LdbLogger implements Logger
    {
        protected final org.slf4j.Logger _slf4Logger;
        public LdbLogger()
        {
             = org.slf4j.LoggerFactory.getLogger(LevelDBBuilder.class);
        }
        
        @Override
        public void log(String msg) {
            .warn(msg);
        }
    }
New to GrepCode? Check out our FAQ X