diff options
Diffstat (limited to 'src/main/java/com/amazon/carbonado/spi')
-rw-r--r-- | src/main/java/com/amazon/carbonado/spi/TriggerManager.java | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/src/main/java/com/amazon/carbonado/spi/TriggerManager.java b/src/main/java/com/amazon/carbonado/spi/TriggerManager.java index a67b134..c4caf72 100644 --- a/src/main/java/com/amazon/carbonado/spi/TriggerManager.java +++ b/src/main/java/com/amazon/carbonado/spi/TriggerManager.java @@ -32,6 +32,9 @@ import com.amazon.carbonado.Storable; import com.amazon.carbonado.Trigger;
import com.amazon.carbonado.TriggerFactory;
+import com.amazon.carbonado.lob.Blob;
+import com.amazon.carbonado.lob.Clob;
+
/**
* Used by Storage implementations to manage triggers and consolidate them into
* single logical triggers. This class is thread-safe and ensures that changes
@@ -44,6 +47,7 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { private static final int FOR_INSERT = 1;
private static final int FOR_UPDATE = 2;
private static final int FOR_DELETE = 4;
+ private static final int FOR_ADAPT_LOB = 8;
private static final Method
BEFORE_INSERT_METHOD,
@@ -62,7 +66,10 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { BEFORE_TRY_DELETE_METHOD,
AFTER_DELETE_METHOD,
AFTER_TRY_DELETE_METHOD,
- FAILED_DELETE_METHOD;
+ FAILED_DELETE_METHOD,
+
+ ADAPT_BLOB_METHOD,
+ ADAPT_CLOB_METHOD;
static {
Class<?> triggerClass = Trigger.class;
@@ -87,6 +94,11 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { AFTER_DELETE_METHOD = triggerClass.getMethod("afterDelete", TWO_PARAMS);
AFTER_TRY_DELETE_METHOD = triggerClass.getMethod("afterTryDelete", TWO_PARAMS);
FAILED_DELETE_METHOD = triggerClass.getMethod("failedDelete", TWO_PARAMS);
+
+ ADAPT_BLOB_METHOD = triggerClass
+ .getMethod("adaptBlob", Object.class, String.class, Blob.class);
+ ADAPT_CLOB_METHOD = triggerClass
+ .getMethod("adaptClob", Object.class, String.class, Clob.class);
} catch (NoSuchMethodException e) {
Error error = new NoSuchMethodError();
error.initCause(e);
@@ -97,6 +109,7 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { private final ForInsert<S> mForInsert = new ForInsert<S>();
private final ForUpdate<S> mForUpdate = new ForUpdate<S>();
private final ForDelete<S> mForDelete = new ForDelete<S>();
+ private final ForAdaptLob<S> mForAdaptLob = new ForAdaptLob<S>();
public TriggerManager() {
}
@@ -143,6 +156,18 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { return forDelete.isEmpty() ? null : forDelete;
}
+ /**
+ * Returns a consolidated trigger to call for adapt LOB operations, or null
+ * if none. If not null, the consolidated trigger is not a snapshot -- it
+ * will change as the set of triggers in this manager changes.
+ *
+ * @since 1.2
+ */
+ public Trigger<? super S> getAdaptLobTrigger() {
+ ForAdaptLob<S> forAdaptLob = mForAdaptLob;
+ return forAdaptLob.isEmpty() ? null : forAdaptLob;
+ }
+
public boolean addTrigger(Trigger<? super S> trigger) {
if (trigger == null) {
throw new IllegalArgumentException();
@@ -161,6 +186,9 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { if ((types & FOR_DELETE) != 0) {
retValue |= mForDelete.add(trigger);
}
+ if ((types & FOR_ADAPT_LOB) != 0) {
+ retValue |= mForAdaptLob.add(trigger);
+ }
return retValue;
}
@@ -183,6 +211,9 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { if ((types & FOR_DELETE) != 0) {
retValue |= mForDelete.remove(trigger);
}
+ if ((types & FOR_ADAPT_LOB) != 0) {
+ retValue |= mForAdaptLob.remove(trigger);
+ }
return retValue;
}
@@ -207,6 +238,7 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { mForInsert.localDisable();
mForUpdate.localDisable();
mForDelete.localDisable();
+ mForAdaptLob.localDisable();
}
/**
@@ -217,6 +249,7 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { mForInsert.localEnable();
mForUpdate.localEnable();
mForDelete.localEnable();
+ mForAdaptLob.localEnable();
}
@Override
@@ -294,6 +327,16 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { mForDelete.failedDelete(storable, state);
}
+ @Override
+ public Blob adaptBlob(S storable, String name, Blob blob) {
+ return mForAdaptLob.adaptBlob(storable, name, blob);
+ }
+
+ @Override
+ public Clob adaptClob(S storable, String name, Clob clob) {
+ return mForAdaptLob.adaptClob(storable, name, clob);
+ }
+
/**
* Determines which operations the given trigger overrides.
*/
@@ -329,6 +372,12 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { types |= FOR_DELETE;
}
+ if (overridesMethod(triggerClass, ADAPT_BLOB_METHOD) ||
+ overridesMethod(triggerClass, ADAPT_CLOB_METHOD))
+ {
+ types |= FOR_ADAPT_LOB;
+ }
+
return types;
}
@@ -897,4 +946,40 @@ public class TriggerManager<S extends Storable> extends Trigger<S> { }
}
}
+
+ private static class ForAdaptLob<S> extends ForSomething<S> {
+ @Override
+ public Blob adaptBlob(S storable, String name, Blob blob) {
+ if (isLocallyDisabled()) {
+ return blob;
+ }
+
+ Trigger<? super S>[] triggers = mTriggers;
+
+ int length = triggers.length;
+
+ for (int i=0; i<length; i++) {
+ blob = triggers[i].adaptBlob(storable, name, blob);
+ }
+
+ return blob;
+ }
+
+ @Override
+ public Clob adaptClob(S storable, String name, Clob clob) {
+ if (isLocallyDisabled()) {
+ return clob;
+ }
+
+ Trigger<? super S>[] triggers = mTriggers;
+
+ int length = triggers.length;
+
+ for (int i=0; i<length; i++) {
+ clob = triggers[i].adaptClob(storable, name, clob);
+ }
+
+ return clob;
+ }
+ }
}
|