|
9 | 9 | import com.toddfast.mutagen.basic.BasicPlanner;
|
10 | 10 | import com.toddfast.mutagen.cassandra.AbstractCassandraMutation;
|
11 | 11 | import com.toddfast.mutagen.cassandra.dao.SchemaVersionDao;
|
| 12 | +import com.toddfast.mutagen.cassandra.premutation.Premutation; |
12 | 13 |
|
13 | 14 | import java.lang.reflect.Constructor;
|
14 | 15 | import java.lang.reflect.InvocationTargetException;
|
15 | 16 | import java.util.ArrayList;
|
16 | 17 | import java.util.Collection;
|
| 18 | +import java.util.Iterator; |
17 | 19 | import java.util.List;
|
18 | 20 |
|
19 | 21 | /**
|
20 |
| - * |
21 | 22 | * @author Todd Fast
|
22 | 23 | */
|
23 | 24 | public class CassandraPlanner extends BasicPlanner<Integer> {
|
24 | 25 |
|
25 |
| - /** |
26 |
| - * |
27 |
| - * |
28 |
| - */ |
29 |
| - protected CassandraPlanner(List<Mutation<Integer>> mutations) { |
30 |
| - super(mutations,null); |
31 |
| - } |
32 |
| - |
33 |
| - |
34 |
| - /** |
35 |
| - * |
36 |
| - * |
37 |
| - */ |
38 |
| - public static List<Mutation<Integer>> loadMutations(Session session, SchemaVersionDao schemaVersionDao, CassandraMutagenConfig config, Collection<String> resources) { |
39 |
| - |
40 |
| - List<Mutation<Integer>> result = new ArrayList<>(); |
41 |
| - |
42 |
| - for (String resource: resources) { |
43 |
| - |
44 |
| - // Allow .sql files because some editors have syntax highlighting |
45 |
| - // for SQL but not CQL |
46 |
| - if (resource.endsWith(".cql") || resource.endsWith(".sql")) { |
47 |
| - CQLMutation mutation = new CQLMutation(session, schemaVersionDao, resource); |
48 |
| - mutation.setConfig(config); |
49 |
| - result.add(mutation); |
50 |
| - } |
51 |
| - else |
52 |
| - if (resource.endsWith(".class")) { |
53 |
| - result.add( |
54 |
| - loadMutationClass(session, schemaVersionDao, config, resource)); |
55 |
| - } |
56 |
| - else { |
57 |
| - throw new IllegalArgumentException("Unknown type for "+ |
58 |
| - "mutation resource \""+resource+"\""); |
59 |
| - } |
60 |
| - } |
61 |
| - |
62 |
| - return result; |
63 |
| - } |
64 |
| - |
65 |
| - |
66 |
| - /** |
67 |
| - * |
68 |
| - * |
69 |
| - */ |
70 |
| - private static Mutation<Integer> loadMutationClass(Session session, SchemaVersionDao schemaVersionDao, CassandraMutagenConfig config, String resource) { |
71 |
| - |
72 |
| - assert resource.endsWith(".class"): |
73 |
| - "Class resource name \""+resource+"\" should end with .class"; |
74 |
| - |
75 |
| - int index=resource.indexOf(".class"); |
76 |
| - String className=resource.substring(0,index).replace('/','.'); |
77 |
| - |
78 |
| - // Load the class specified by the resource |
79 |
| - Class<?> clazz=null; |
80 |
| - try { |
81 |
| - clazz=Class.forName(className); |
| 26 | + private List<Premutation> premutations; |
| 27 | + private List<Mutation<Integer>> mutations; |
| 28 | + private Session session; |
| 29 | + private CassandraMutagenConfig config; |
| 30 | + |
| 31 | + /** |
| 32 | + * |
| 33 | + * |
| 34 | + */ |
| 35 | + protected CassandraPlanner(List<Mutation<Integer>> mutations, List<Premutation> premutations, Session session, CassandraMutagenConfig config) { |
| 36 | + super(mutations, null); |
| 37 | + this.mutations = mutations; |
| 38 | + this.premutations = premutations; |
| 39 | + this.session = session; |
| 40 | + this.config = config; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * |
| 46 | + * |
| 47 | + */ |
| 48 | + public static List<Mutation<Integer>> loadMutations(Session session, SchemaVersionDao schemaVersionDao, CassandraMutagenConfig config, Collection<String> resources) { |
| 49 | + |
| 50 | + List<Mutation<Integer>> result = new ArrayList<>(); |
| 51 | + |
| 52 | + for (String resource : resources) { |
| 53 | + |
| 54 | + // Allow .sql files because some editors have syntax highlighting |
| 55 | + // for SQL but not CQL |
| 56 | + if (resource.endsWith(".cql") || resource.endsWith(".sql")) { |
| 57 | + CQLMutation mutation = new CQLMutation(session, schemaVersionDao, resource); |
| 58 | + mutation.setConfig(config); |
| 59 | + result.add(mutation); |
| 60 | + } else if (resource.endsWith(".class")) { |
| 61 | + result.add(loadMutationClass(session, schemaVersionDao, config, resource)); |
| 62 | + } else { |
| 63 | + throw new IllegalArgumentException("Unknown type for " + |
| 64 | + "mutation resource \"" + resource + "\""); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return result; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * |
| 74 | + * |
| 75 | + */ |
| 76 | + private static Mutation<Integer> loadMutationClass(Session session, SchemaVersionDao schemaVersionDao, CassandraMutagenConfig config, String resource) { |
| 77 | + |
| 78 | + assert resource.endsWith(".class") : |
| 79 | + "Class resource name \"" + resource + "\" should end with .class"; |
| 80 | + |
| 81 | + int index = resource.indexOf(".class"); |
| 82 | + String className = resource.substring(0, index).replace('/', '.'); |
| 83 | + |
| 84 | + // Load the class specified by the resource |
| 85 | + Class<?> clazz = null; |
| 86 | + try { |
| 87 | + clazz = Class.forName(className); |
82 | 88 | if (!AbstractCassandraMutation.class.isAssignableFrom(clazz)) {
|
83 | 89 | throw new MutagenException("Class [" + resource + "] doesn't inherit AbstractCassandraMutation");
|
84 | 90 | }
|
85 |
| - } |
86 |
| - catch (ClassNotFoundException e) { |
87 |
| - // Should never happen |
88 |
| - throw new MutagenException("Could not load mutagen class \""+ |
89 |
| - resource+"\"",e); |
90 |
| - } |
91 |
| - |
92 |
| - // Instantiate the class |
93 |
| - try { |
94 |
| - Constructor<?> constructor; |
95 |
| - AbstractCassandraMutation mutation=null; |
96 |
| - |
97 |
| - try { |
98 |
| - // Try a constructor taking a keyspace |
99 |
| - constructor = clazz.getConstructor(Session.class, SchemaVersionDao.class); |
100 |
| - mutation = (AbstractCassandraMutation) constructor.newInstance(session, schemaVersionDao); |
101 |
| - } |
102 |
| - catch (NoSuchMethodException e) { |
103 |
| - // Wrong assumption |
104 |
| - } |
105 |
| - |
106 |
| - if (mutation==null) { |
107 |
| - // Try the null constructor |
108 |
| - try { |
109 |
| - constructor=clazz.getConstructor(); |
110 |
| - mutation=(AbstractCassandraMutation) constructor.newInstance(); |
111 |
| - } |
112 |
| - catch (NoSuchMethodException e) { |
113 |
| - throw new MutagenException("Could not find comparible "+ |
114 |
| - "constructor for class \""+className+"\"",e); |
115 |
| - } |
116 |
| - } |
| 91 | + } catch (ClassNotFoundException e) { |
| 92 | + // Should never happen |
| 93 | + throw new MutagenException("Could not load mutagen class \"" + |
| 94 | + resource + "\"", e); |
| 95 | + } |
| 96 | + |
| 97 | + // Instantiate the class |
| 98 | + try { |
| 99 | + Constructor<?> constructor; |
| 100 | + AbstractCassandraMutation mutation = null; |
| 101 | + |
| 102 | + try { |
| 103 | + // Try a constructor taking a keyspace |
| 104 | + constructor = clazz.getConstructor(Session.class, SchemaVersionDao.class); |
| 105 | + mutation = (AbstractCassandraMutation) constructor.newInstance(session, schemaVersionDao); |
| 106 | + } catch (NoSuchMethodException e) { |
| 107 | + // Wrong assumption |
| 108 | + } |
| 109 | + |
| 110 | + if (mutation == null) { |
| 111 | + // Try the null constructor |
| 112 | + try { |
| 113 | + constructor = clazz.getConstructor(); |
| 114 | + mutation = (AbstractCassandraMutation) constructor.newInstance(); |
| 115 | + } catch (NoSuchMethodException e) { |
| 116 | + throw new MutagenException("Could not find comparible " + |
| 117 | + "constructor for class \"" + className + "\"", e); |
| 118 | + } |
| 119 | + } |
117 | 120 |
|
118 | 121 | mutation.setConfig(config);
|
119 | 122 |
|
120 |
| - return mutation; |
121 |
| - } |
122 |
| - catch (InstantiationException e) { |
123 |
| - throw new MutagenException("Could not instantiate class \""+ |
124 |
| - className+"\"",e); |
125 |
| - } |
126 |
| - catch (InvocationTargetException e) { |
127 |
| - if (e.getTargetException() instanceof RuntimeException) { |
128 |
| - throw (RuntimeException)e.getTargetException(); |
129 |
| - } |
130 |
| - else { |
131 |
| - throw new MutagenException("Exception instantiating class \""+ |
132 |
| - className+"\"",e); |
133 |
| - } |
134 |
| - } |
135 |
| - catch (IllegalAccessException e) { |
136 |
| - throw new MutagenException("Could not access constructor for "+ |
137 |
| - "mutation class \""+className+"\"",e); |
138 |
| - } |
139 |
| - } |
140 |
| - |
141 |
| - |
142 |
| - /** |
143 |
| - * |
144 |
| - * |
145 |
| - */ |
146 |
| - @Override |
147 |
| - protected Mutation.Context createContext(Subject<Integer> subject, |
148 |
| - Coordinator<Integer> coordinator) { |
149 |
| - return new CassandraContext(subject,coordinator); |
150 |
| - } |
151 |
| - |
152 |
| - |
153 |
| - /** |
154 |
| - * |
155 |
| - * |
156 |
| - */ |
157 |
| - @Override |
158 |
| - public Plan<Integer> getPlan(Subject<Integer> subject, |
159 |
| - Coordinator<Integer> coordinator) { |
160 |
| - return super.getPlan(subject,coordinator); |
161 |
| - } |
| 123 | + return mutation; |
| 124 | + } catch (InstantiationException e) { |
| 125 | + throw new MutagenException("Could not instantiate class \"" + |
| 126 | + className + "\"", e); |
| 127 | + } catch (InvocationTargetException e) { |
| 128 | + if (e.getTargetException() instanceof RuntimeException) { |
| 129 | + throw (RuntimeException) e.getTargetException(); |
| 130 | + } else { |
| 131 | + throw new MutagenException("Exception instantiating class \"" + |
| 132 | + className + "\"", e); |
| 133 | + } |
| 134 | + } catch (IllegalAccessException e) { |
| 135 | + throw new MutagenException("Could not access constructor for " + |
| 136 | + "mutation class \"" + className + "\"", e); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * |
| 143 | + * |
| 144 | + */ |
| 145 | + @Override |
| 146 | + protected Mutation.Context createContext(Subject<Integer> subject, |
| 147 | + Coordinator<Integer> coordinator) { |
| 148 | + return new CassandraContext(subject, coordinator); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + /** |
| 153 | + * |
| 154 | + * |
| 155 | + */ |
| 156 | + @Override |
| 157 | + public Plan<Integer> getPlan(Subject<Integer> subject, |
| 158 | + Coordinator<Integer> coordinator) { |
| 159 | + List<Mutation<Integer>> subjectMutations = new ArrayList<>(mutations); |
| 160 | + // Filter out the mutations that are unacceptable to the subject |
| 161 | + for (Iterator<Mutation<Integer>> i = subjectMutations.iterator(); i.hasNext(); ) { |
| 162 | + Mutation<Integer> mutation = i.next(); |
| 163 | + if (!coordinator.accept(subject, mutation.getResultingState())) { |
| 164 | + i.remove(); |
| 165 | + } |
| 166 | + } |
| 167 | + return new CassandraPlan(subject, coordinator, subjectMutations, |
| 168 | + premutations, session, config); |
| 169 | + } |
162 | 170 | }
|
0 commit comments