| 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.evolve; |
| 9 |
|
| 10 |
import java.io.Serializable; |
| 11 |
import java.util.Collection; |
| 12 |
import java.util.HashMap; |
| 13 |
import java.util.Map; |
| 14 |
|
| 15 |
import com.sleepycat.persist.EntityStore; |
| 16 |
import com.sleepycat.persist.StoreConfig; |
| 17 |
|
| 18 |
/** |
| 19 |
* A collection of mutations for configuring class evolution. |
| 20 |
* |
| 21 |
* <p>Mutations are configured when a store is opened via {@link |
| 22 |
* StoreConfig#setMutations StoreConfig.setMutations}. For example:</p> |
| 23 |
* |
| 24 |
* <pre class="code"> |
| 25 |
* Mutations mutations = new Mutations(); |
| 26 |
* // Add mutations... |
| 27 |
* StoreConfig config = new StoreConfig(); |
| 28 |
* config.setMutations(mutations); |
| 29 |
* EntityStore store = new EntityStore(env, "myStore", config);</pre> |
| 30 |
* |
| 31 |
* <p>Mutations cause data conversion to occur lazily as instances are read |
| 32 |
* from the store. The {@link EntityStore#evolve EntityStore.evolve} method |
| 33 |
* may also be used to perform eager conversion.</p> |
| 34 |
* |
| 35 |
* <p>Not all incompatible class changes can be handled via mutations. For |
| 36 |
* example, complex refactoring may require a transformation that manipulates |
| 37 |
* multiple entity instances at once. Such changes are not possible with |
| 38 |
* mutations but can made by performing a <a |
| 39 |
* href="package-summary.html#storeConversion">store conversion</a>.</p> |
| 40 |
* |
| 41 |
* @see com.sleepycat.persist.evolve Class Evolution |
| 42 |
* @author Mark Hayes |
| 43 |
*/ |
| 44 |
public class Mutations implements Serializable { |
| 45 |
|
| 46 |
private static final long serialVersionUID = -1744401530444812916L; |
| 47 |
|
| 48 |
private Map<Mutation, Renamer> renamers; |
| 49 |
private Map<Mutation, Deleter> deleters; |
| 50 |
private Map<Mutation, Converter> converters; |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates an empty set of mutations. |
| 54 |
*/ |
| 55 |
public Mutations() { |
| 56 |
renamers = new HashMap<Mutation, Renamer>(); |
| 57 |
deleters = new HashMap<Mutation, Deleter>(); |
| 58 |
converters = new HashMap<Mutation, Converter>(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns true if no mutations are present. |
| 63 |
*/ |
| 64 |
public boolean isEmpty() { |
| 65 |
return renamers.isEmpty() && |
| 66 |
deleters.isEmpty() && |
| 67 |
converters.isEmpty(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Adds a renamer mutation. |
| 72 |
*/ |
| 73 |
public void addRenamer(Renamer renamer) { |
| 74 |
renamers.put(new Key(renamer), renamer); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the renamer mutation for the given class, version and field, or |
| 79 |
* null if none exists. A null field name should be specified to get a |
| 80 |
* class renamer. |
| 81 |
*/ |
| 82 |
public Renamer getRenamer(String className, |
| 83 |
int classVersion, |
| 84 |
String fieldName) { |
| 85 |
return renamers.get(new Key(className, classVersion, fieldName)); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns an unmodifiable collection of all renamer mutations. |
| 90 |
*/ |
| 91 |
public Collection<Renamer> getRenamers() { |
| 92 |
return renamers.values(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Adds a deleter mutation. |
| 97 |
*/ |
| 98 |
public void addDeleter(Deleter deleter) { |
| 99 |
deleters.put(new Key(deleter), deleter); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the deleter mutation for the given class, version and field, or |
| 104 |
* null if none exists. A null field name should be specified to get a |
| 105 |
* class deleter. |
| 106 |
*/ |
| 107 |
public Deleter getDeleter(String className, |
| 108 |
int classVersion, |
| 109 |
String fieldName) { |
| 110 |
return deleters.get(new Key(className, classVersion, fieldName)); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns an unmodifiable collection of all deleter mutations. |
| 115 |
*/ |
| 116 |
public Collection<Deleter> getDeleters() { |
| 117 |
return deleters.values(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Adds a converter mutation. |
| 122 |
*/ |
| 123 |
public void addConverter(Converter converter) { |
| 124 |
converters.put(new Key(converter), converter); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns the converter mutation for the given class, version and field, |
| 129 |
* or null if none exists. A null field name should be specified to get a |
| 130 |
* class converter. |
| 131 |
*/ |
| 132 |
public Converter getConverter(String className, |
| 133 |
int classVersion, |
| 134 |
String fieldName) { |
| 135 |
return converters.get(new Key(className, classVersion, fieldName)); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Returns an unmodifiable collection of all converter mutations. |
| 140 |
*/ |
| 141 |
public Collection<Converter> getConverters() { |
| 142 |
return converters.values(); |
| 143 |
} |
| 144 |
|
| 145 |
private static class Key extends Mutation { |
| 146 |
static final long serialVersionUID = 2793516787097085621L; |
| 147 |
|
| 148 |
Key(String className, int classVersion, String fieldName) { |
| 149 |
super(className, classVersion, fieldName); |
| 150 |
} |
| 151 |
|
| 152 |
Key(Mutation mutation) { |
| 153 |
super(mutation.getClassName(), |
| 154 |
mutation.getClassVersion(), |
| 155 |
mutation.getFieldName()); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Returns true if this collection has the same set of mutations as the |
| 161 |
* given collection and all mutations are equal. |
| 162 |
*/ |
| 163 |
@Override |
| 164 |
public boolean equals(Object other) { |
| 165 |
if (other instanceof Mutations) { |
| 166 |
Mutations o = (Mutations) other; |
| 167 |
return renamers.equals(o.renamers) && |
| 168 |
deleters.equals(o.deleters) && |
| 169 |
converters.equals(o.converters); |
| 170 |
} else { |
| 171 |
return false; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public int hashCode() { |
| 177 |
return renamers.hashCode() + |
| 178 |
deleters.hashCode() + |
| 179 |
converters.hashCode(); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public String toString() { |
| 184 |
StringBuilder buf = new StringBuilder(); |
| 185 |
if (renamers.size() > 0) { |
| 186 |
buf.append(renamers.values()); |
| 187 |
} |
| 188 |
if (deleters.size() > 0) { |
| 189 |
buf.append(deleters.values()); |
| 190 |
} |
| 191 |
if (converters.size() > 0) { |
| 192 |
buf.append(converters.values()); |
| 193 |
} |
| 194 |
if (buf.length() > 0) { |
| 195 |
return buf.toString(); |
| 196 |
} else { |
| 197 |
return "[Empty Mutations]"; |
| 198 |
} |
| 199 |
} |
| 200 |
} |