| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.raw; |
| 9 |
|
| 10 |
import java.io.Closeable; |
| 11 |
|
| 12 |
import com.sleepycat.compat.DbCompat; |
| 13 |
import com.sleepycat.db.DatabaseException; |
| 14 |
import com.sleepycat.db.Environment; |
| 15 |
import com.sleepycat.persist.PrimaryIndex; |
| 16 |
import com.sleepycat.persist.SecondaryIndex; |
| 17 |
import com.sleepycat.persist.StoreConfig; |
| 18 |
import com.sleepycat.persist.StoreExistsException; |
| 19 |
import com.sleepycat.persist.StoreNotFoundException; |
| 20 |
import com.sleepycat.persist.evolve.IncompatibleClassException; |
| 21 |
import com.sleepycat.persist.evolve.Mutations; |
| 22 |
import com.sleepycat.persist.impl.Store; |
| 23 |
import com.sleepycat.persist.model.EntityModel; |
| 24 |
|
| 25 |
/** |
| 26 |
* Provides access to the raw data in a store for use by general purpose tools. |
| 27 |
* A <code>RawStore</code> provides access to stored entities without using |
| 28 |
* entity classes or key classes. Keys are represented as simple type objects |
| 29 |
* or, for composite keys, as {@link RawObject} instances, and entities are |
| 30 |
* represented as {@link RawObject} instances. |
| 31 |
* |
| 32 |
* <p>{@code RawStore} objects are thread-safe. Multiple threads may safely |
| 33 |
* call the methods of a shared {@code RawStore} object.</p> |
| 34 |
* |
| 35 |
* <p>When using a {@code RawStore}, the current persistent class definitions |
| 36 |
* are not used. Instead, the previously stored metadata and class definitions |
| 37 |
* are used. This has several implications:</p> |
| 38 |
* <ol> |
| 39 |
* <li>An {@code EntityModel} may not be specified using {@link |
| 40 |
* StoreConfig#setModel}. In other words, the configured model must be |
| 41 |
* null (the default).</li> |
| 42 |
* <li>When storing entities, their format will not automatically be evolved |
| 43 |
* to the current class definition, even if the current class definition has |
| 44 |
* changed.</li> |
| 45 |
* </ol> |
| 46 |
* |
| 47 |
* @author Mark Hayes |
| 48 |
*/ |
| 49 |
public class RawStore |
| 50 |
{ |
| 51 |
|
| 52 |
private Store store; |
| 53 |
|
| 54 |
/** |
| 55 |
* Opens an entity store for raw data access. |
| 56 |
* |
| 57 |
* @param env an open Berkeley DB environment. |
| 58 |
* |
| 59 |
* @param storeName the name of the entity store within the given |
| 60 |
* environment. |
| 61 |
* |
| 62 |
* @param config the store configuration, or null to use default |
| 63 |
* configuration properties. |
| 64 |
* |
| 65 |
* @throws IllegalArgumentException if the <code>Environment</code> is |
| 66 |
* read-only and the <code>config ReadOnly</code> property is false. |
| 67 |
*/ |
| 68 |
public RawStore(Environment env, String storeName, StoreConfig config) |
| 69 |
throws StoreNotFoundException, DatabaseException { |
| 70 |
|
| 71 |
try { |
| 72 |
store = new Store(env, storeName, config, true /*rawAccess*/); |
| 73 |
} catch (StoreExistsException e) { |
| 74 |
/* Should never happen, ExclusiveCreate not used. */ |
| 75 |
throw DbCompat.unexpectedException(e); |
| 76 |
} catch (IncompatibleClassException e) { |
| 77 |
/* Should never happen, evolution is not performed. */ |
| 78 |
throw DbCompat.unexpectedException(e); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Opens the primary index for a given entity class. |
| 84 |
* |
| 85 |
* @throws DatabaseException the base class for all BDB exceptions. |
| 86 |
*/ |
| 87 |
public PrimaryIndex<Object, RawObject> getPrimaryIndex(String entityClass) |
| 88 |
throws DatabaseException { |
| 89 |
|
| 90 |
return store.getPrimaryIndex |
| 91 |
(Object.class, null, RawObject.class, entityClass); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Opens the secondary index for a given entity class and secondary key |
| 96 |
* name. |
| 97 |
* |
| 98 |
* @throws DatabaseException the base class for all BDB exceptions. |
| 99 |
*/ |
| 100 |
public SecondaryIndex<Object, Object, RawObject> |
| 101 |
getSecondaryIndex(String entityClass, String keyName) |
| 102 |
throws DatabaseException { |
| 103 |
|
| 104 |
return store.getSecondaryIndex |
| 105 |
(getPrimaryIndex(entityClass), RawObject.class, entityClass, |
| 106 |
Object.class, null, keyName); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the environment associated with this store. |
| 111 |
*/ |
| 112 |
public Environment getEnvironment() { |
| 113 |
return store.getEnvironment(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns a copy of the entity store configuration. |
| 118 |
*/ |
| 119 |
public StoreConfig getConfig() { |
| 120 |
return store.getConfig(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns the name of this store. |
| 125 |
*/ |
| 126 |
public String getStoreName() { |
| 127 |
return store.getStoreName(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns the last configured and stored entity model for this store. |
| 132 |
*/ |
| 133 |
public EntityModel getModel() { |
| 134 |
return store.getModel(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Returns the set of mutations that were configured and stored previously. |
| 139 |
*/ |
| 140 |
public Mutations getMutations() { |
| 141 |
return store.getMutations(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Closes all databases and sequences that were opened by this model. No |
| 146 |
* databases opened via this store may be in use. |
| 147 |
* |
| 148 |
* <p>WARNING: To guard against memory leaks, the application should |
| 149 |
* discard all references to the closed handle. While BDB makes an effort |
| 150 |
* to discard references from closed objects to the allocated memory for an |
| 151 |
* environment, this behavior is not guaranteed. The safe course of action |
| 152 |
* for an application is to discard all references to closed BDB |
| 153 |
* objects.</p> |
| 154 |
* |
| 155 |
* @throws DatabaseException the base class for all BDB exceptions. |
| 156 |
*/ |
| 157 |
public void close() |
| 158 |
throws DatabaseException { |
| 159 |
|
| 160 |
store.close(); |
| 161 |
} |
| 162 |
} |