use of org.cojen.tupl.diag.EventListener in project Tupl by cojen.
the class ChainedEventListener method make.
public static EventListener make(EventListener... listeners) {
if (listeners == null || listeners.length == 0) {
return null;
}
int count = 0;
EventListener first = null;
for (EventListener listener : listeners) {
if (listener != null) {
count++;
if (first == null) {
first = listener;
}
}
}
if (count <= 1) {
return first;
}
var rest = new EventListener[count - 1];
for (int i = 0, j = 0; i < listeners.length; i++) {
EventListener listener = listeners[i];
if (listener != null && listener != first) {
rest[j++] = listener;
}
}
return new ChainedEventListener(first, rest);
}
use of org.cojen.tupl.diag.EventListener in project Tupl by cojen.
the class Checkpointer method run.
@Override
public void run() {
try {
if (mState == STATE_INIT) {
// Start with an initial forced checkpoint.
CoreDatabase db = mDatabaseRef.get();
if (db != null) {
db.checkpoint();
}
mState = STATE_RUNNING;
}
if (mRefQueue != null) {
// When the checkpoint rate is negative (infinite delay), this thread is
// suspended until the database isn't referenced anymore, or until the database
// is explicitly closed.
mRefQueue.remove();
close(null);
return;
}
long lastDurationNanos = 0;
while (true) {
long delayMillis = (mRateNanos - lastDurationNanos) / 1000000L;
if (delayMillis > 0) {
Thread.sleep(delayMillis);
}
CoreDatabase db = mDatabaseRef.get();
if (db == null) {
close(null);
return;
}
if (isSuspended()) {
// Don't actually suspend the thread, allowing for weak reference checks.
lastDurationNanos = 0;
} else
try {
long startNanos = System.nanoTime();
db.checkpoint(mSizeThreshold, mDelayThresholdNanos);
long endNanos = System.nanoTime();
lastDurationNanos = endNanos - startNanos;
} catch (DatabaseException e) {
EventListener listener = db.eventListener();
if (listener != null) {
listener.notify(EventType.CHECKPOINT_FAILED, "Checkpoint failed: %1$s", e);
}
if (!e.isRecoverable()) {
throw e;
}
lastDurationNanos = 0;
}
}
} catch (Throwable e) {
if (mState != STATE_CLOSED) {
CoreDatabase db = mDatabaseRef.get();
if (db != null) {
Utils.closeQuietly(db, e);
}
}
close(e);
}
}
use of org.cojen.tupl.diag.EventListener in project Tupl by cojen.
the class EventListenerTest method observe.
@Test
public void observe() throws Exception {
var listener = new Listener();
listener.notify(EventType.DEBUG, "hello");
assertEquals("[DEBUG:hello:[]]", listener.mEvents.toString());
listener = new Listener();
EventListener filtered = listener.observe(EventType.Category.DEBUG);
filtered.notify(EventType.DEBUG, "hello");
assertEquals("[DEBUG:hello:[]]", listener.mEvents.toString());
assertSame(filtered, filtered.observe(EventType.Category.DEBUG));
listener = new Listener();
filtered = listener.observe(new EventType.Category[0]);
filtered.notify(EventType.DEBUG, "hello");
assertEquals("[]", listener.mEvents.toString());
assertSame(filtered, filtered.observe(EventType.Category.DEBUG));
listener = new Listener();
filtered = listener.observe(EventType.Category.DEBUG, EventType.Category.DELETION);
filtered.notify(EventType.DEBUG, "hello");
filtered.notify(EventType.DELETION_FAILED, "world", 1);
assertEquals("[DEBUG:hello:[], DELETION_FAILED:world:[1]]", listener.mEvents.toString());
filtered = listener.observe(Level.WARNING);
listener.mEvents.clear();
filtered.notify(EventType.DEBUG, "hello");
filtered.notify(EventType.DELETION_FAILED, "world", 2);
assertEquals("[DELETION_FAILED:world:[2]]", listener.mEvents.toString());
filtered = filtered.observe(Level.INFO);
listener.mEvents.clear();
filtered.notify(EventType.DEBUG, "hello");
filtered.notify(EventType.DELETION_FAILED, "world", 3);
assertEquals("[]", listener.mEvents.toString());
}
use of org.cojen.tupl.diag.EventListener in project Tupl by cojen.
the class RedoEventPrinter method main.
/**
* @param args [0]: base file, [1]: first log number to read from, [2]: optional crypto
* class; remaining args are passed to its constructor as separate parameters
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
var baseFile = new java.io.File(args[0]);
long logId = Long.parseLong(args[1]);
Crypto crypto = null;
if (args.length > 2) {
Class clazz = Class.forName(args[2]);
var types = new Class[args.length - 3];
var params = new String[types.length];
for (int i = 0; i < types.length; i++) {
types[i] = String.class;
params[i] = args[i + 3];
}
crypto = (Crypto) clazz.getConstructor(types).newInstance((Object[]) params);
}
EventListener listener = (type, message, messageArgs) -> {
System.out.println(String.format(message, messageArgs));
};
new RedoLog(crypto, baseFile, logId, 0, null).replay(new RedoEventPrinter(listener, EventType.DEBUG), null, null, null);
}
use of org.cojen.tupl.diag.EventListener in project Tupl by cojen.
the class EventListenerTest method ignore.
@Test
public void ignore() throws Exception {
var listener = new Listener();
assertSame(listener, listener.ignore(new EventType.Category[0]));
assertSame(listener, listener.ignore(new Level[0]));
EventListener filtered = listener.ignore(EventType.Category.DEBUG);
filtered.notify(EventType.DEBUG, "hello");
filtered.notify(EventType.CHECKPOINT_BEGIN, "world");
assertEquals("[CHECKPOINT_BEGIN:world:[]]", listener.mEvents.toString());
assertSame(filtered, filtered.ignore(EventType.Category.DEBUG));
listener = new Listener();
filtered = listener.ignore(Level.DEBUG);
filtered.notify(EventType.DEBUG, "hello");
filtered.notify(EventType.CHECKPOINT_FAILED, "world");
assertEquals("[CHECKPOINT_FAILED:world:[]]", listener.mEvents.toString());
assertSame(filtered, filtered.ignore(Level.DEBUG));
listener.mEvents.clear();
filtered = filtered.ignore(Level.WARNING);
filtered.notify(EventType.DEBUG, "hello");
filtered.notify(EventType.CHECKPOINT_FAILED, "world");
assertEquals("[]", listener.mEvents.toString());
listener.mEvents.clear();
filtered = filtered.observe(Level.WARNING);
filtered.notify(EventType.DEBUG, "hello");
filtered.notify(EventType.CHECKPOINT_FAILED, "world");
assertEquals("[]", listener.mEvents.toString());
}
Aggregations