From e8847361b00c991c7d2183ed913b75408f27e064 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Thu, 27 Mar 2008 00:26:33 +0000 Subject: Support multiple databases in one file. --- .../carbonado/repo/sleepycat/BDBRepository.java | 36 +++++++++++++++++++--- .../repo/sleepycat/BDBRepositoryBuilder.java | 31 ++++++++++++++++++- 2 files changed, 61 insertions(+), 6 deletions(-) (limited to 'src/main/java/com/amazon/carbonado') diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java index f12a58e..bbd4ea3 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java @@ -112,6 +112,7 @@ abstract class BDBRepository extends AbstractRepository final File mDataHome; final File mEnvHome; final String mSingleFileName; + final Map, String> mFileNameMap; private LayoutFactory mLayoutFactory; @@ -156,6 +157,7 @@ abstract class BDBRepository extends AbstractRepository mDataHome = builder.getDataHomeFile(); mEnvHome = builder.getEnvironmentHomeFile(); mSingleFileName = builder.getSingleFileName(); + mFileNameMap = builder.getFileNameMap(); } @SuppressWarnings("unchecked") @@ -352,16 +354,40 @@ abstract class BDBRepository extends AbstractRepository return mIsMaster; } - String getDatabaseFileName(String dbName) { - if (mSingleFileName != null) { - dbName = mSingleFileName; + String getDatabaseFileName(final String dbName) { + String singleFileName = mSingleFileName; + if (singleFileName == null && mFileNameMap != null) { + singleFileName = mFileNameMap.get(dbName); + if (singleFileName == null && dbName != null) { + singleFileName = mFileNameMap.get(null); + } + } + + String dbFileName = dbName; + + if (singleFileName != null) { + dbFileName = singleFileName; } if (mDataHome != null && !mDataHome.equals(mEnvHome)) { - dbName = new File(mDataHome, dbName).getPath(); + dbFileName = new File(mDataHome, dbFileName).getPath(); } - return dbName; + return dbFileName; + } + + /** + * Returns null if name should not be used. + */ + String getDatabaseName(final String dbName) { + if (mFileNameMap == null) { + return null; + } + String name = mFileNameMap.get(dbName); + if (name == null && dbName != null) { + name = mFileNameMap.get(null); + } + return name == null ? null : dbName; } StorableCodecFactory getStorableCodecFactory() { diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java index fa24624..c965eff 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java @@ -85,6 +85,7 @@ public class BDBRepositoryBuilder extends AbstractRepositoryBuilder { private File mEnvHome; private File mDataHome; private String mSingleFileName; + private Map, String> mFileNames; private boolean mIndexSupport = true; private boolean mIndexRepairEnabled = true; private double mIndexThrottle = 1.0; @@ -290,10 +291,13 @@ public class BDBRepositoryBuilder extends AbstractRepositoryBuilder { * *

Note: When setting this option, the storable codec factory must also * be changed, since the default storable codec factory is unable to - * distinguish storable types that reside in a single database file. + * distinguish storable types that reside in a single database file. Call + * setFileName instead to use built-in BDB feature for supporting multiple + * databases in one file. */ public void setSingleFileName(String filename) { mSingleFileName = filename; + mFileNames = null; } /** @@ -303,6 +307,31 @@ public class BDBRepositoryBuilder extends AbstractRepositoryBuilder { return mSingleFileName; } + /** + * Specify the file that a BDB database should reside in, except for log + * files and caches. The filename is relative to the environment home, + * unless data directories have been specified. For BDBRepositories that + * are log files only, this configuration is ignored. + * + * @param filename BDB database filename + * @param type type to store in file; if null, the file is used by default + * for all types + */ + public void setFileName(String filename, Class type) { + mSingleFileName = null; + if (mFileNames == null) { + mFileNames = new HashMap, String>(); + } + mFileNames.put(type, filename); + } + + Map, String> getFileNameMap() { + if (mFileNames == null) { + return null; + } + return new HashMap, String>(mFileNames); + } + /** * By default, user specified indexes are supported. Pass false to disable * this, and no indexes will be built. Another consequence of this option -- cgit v1.2.3