1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: DatabaseNamer.java,v 1.3 2008/02/19 19:41:50 mark Exp $ 7 */ 8 9package com.sleepycat.persist; 10 11import com.sleepycat.db.Database; // for javadoc 12import com.sleepycat.persist.impl.Store; 13 14/** 15 * Determines the file names to use for primary and secondary databases. 16 * 17 * <p>Each {@link PrimaryIndex} and {@link SecondaryIndex} is represented 18 * internally as a Berkeley DB {@link Database}. The file names of primary and 19 * secondary indices must be unique within the environment, so that each index 20 * is stored in a separate database file.</p> 21 * 22 * <p>By default, the file names of primary and secondary databases are 23 * defined as follows.</p> 24 * 25 * <p>The syntax of a primary index database file name is:</p> 26 * <pre> STORE_NAME-ENTITY_CLASS</pre> 27 * <p>Where STORE_NAME is the name parameter passed to {@link 28 * EntityStore#EntityStore EntityStore} and ENTITY_CLASS is name of the class 29 * passed to {@link EntityStore#getPrimaryIndex getPrimaryIndex}.</p> 30 * 31 * <p>The syntax of a secondary index database file name is:</p> 32 * <pre> STORE_NAME-ENTITY_CLASS-KEY_NAME</pre> 33 * <p>Where KEY_NAME is the secondary key name passed to {@link 34 * EntityStore#getSecondaryIndex getSecondaryIndex}.</p> 35 * 36 * <p>The default naming described above is implemented by the built-in {@link 37 * DatabaseNamer#DEFAULT} object. An application may supply a custom {@link 38 * DatabaseNamer} to overrride the default naming scheme. For example, a 39 * custom namer could place all database files in a subdirectory with the name 40 * of the store. A custom namer could also be used to name files according to 41 * specific file system restrictions.</p> 42 * 43 * <p>The custom namer object must be an instance of the {@code DatabaseNamer} 44 * interface and is configured using {@link StoreConfig#setDatabaseNamer 45 * setDatabaseNamer}.</p> 46 * 47 * <p>When copying or removing all databases in a store, there is one further 48 * consideration. There are two internal databases that must be kept with the 49 * other databases in the store in order for the store to be used. These 50 * contain the data formats and sequences for the store. Their entity class 51 * names are:</p> 52 * 53 * <pre> com.sleepycat.persist.formats</pre> 54 * <pre> com.sleepycat.persist.sequences</pre> 55 * 56 * <p>With default database naming, databases with the following names will be 57 * present each store.</p> 58 * 59 * <pre> STORE_NAME-com.sleepycat.persist.formats</pre> 60 * <pre> STORE_NAME-com.sleepycat.persist.sequences</pre> 61 * 62 * <p>These databases must normally be included with copies of other databases 63 * in the store. They should not be modified by the application.</p> 64 */ 65public interface DatabaseNamer { 66 67 /** 68 * Returns the name of the file to be used to store the dataabase for the 69 * given store, entity class and key. This method may not return null. 70 * 71 * @param storeName the name of the {@link EntityStore}. 72 * 73 * @param entityClassName the complete name of the entity class for a 74 * primary or secondary index. 75 * 76 * @param keyName the key name identifying a secondary index, or null for 77 * a primary index. 78 */ 79 public String getFileName(String storeName, 80 String entityClassName, 81 String keyName); 82 83 /** 84 * The default database namer. 85 * 86 * <p>The {@link #getFileName getFileName} method of this namer returns the 87 * {@code storeName}, {@code entityClassName} and {@code keyName} 88 * parameters as follows:<p> 89 * 90 * <pre class="code"> 91 * if (keyName != null) { 92 * return storeName + '-' + entityClassName + '-' + keyName; 93 * } else { 94 * return storeName + '-' + entityClassName; 95 * }</pre> 96 */ 97 public static final DatabaseNamer DEFAULT = new DatabaseNamer() { 98 99 public String getFileName(String storeName, 100 String entityClassName, 101 String keyName) { 102 if (keyName != null) { 103 return storeName + '-' + entityClassName + '-' + keyName; 104 } else { 105 return storeName + '-' + entityClassName; 106 } 107 } 108 }; 109} 110