protected void commitOff (Space sp) { if (sp instanceof JDBMSpace) { ((JDBMSpace) sp).setAutoCommit(false); } } protected void commitOn (Space sp) {
protected void commitOn (Space sp) { if (sp instanceof JDBMSpace) { JDBMSpace jsp = (JDBMSpace) sp; jsp.commit (); jsp.setAutoCommit(true); } } protected void syncTail () {
public boolean existAny (Object[] keys, long timeout) { long now = System.currentTimeMillis(); long end = now + timeout; while ((now = System.currentTimeMillis()) < end) { if (existAny (keys)) return true; synchronized (this) { try { wait (end - now); } catch (InterruptedException ignored) { } } } return false; } public synchronized void put (K key, V value, long timeout) {
Profiler prof = new Profiler (); synchronized (sp) { sp.setAutoCommit (false); for (int i=0; i<COUNT; i++) { sp.out ("testNoAutoCommit_Key" + Integer.toString (i), s); if (i % 100 == 0) prof.checkPoint ("out " + i); sp.commit(); sp.setAutoCommit (true); prof.checkPoint ("commit"); sp.setAutoCommit (false); for (int i=0; i<COUNT; i++) { assertTrue (s.equals (sp.in ("testNoAutoCommit_Key" + Integer.toString (i)))); if (i % 100 == 0) prof.checkPoint ("in " + i); sp.commit(); sp.setAutoCommit (true); prof.checkPoint ("commit");
public void testPush() { sp.push ("PUSH", "ONE"); sp.push ("PUSH", "TWO"); sp.push ("PUSH", "THREE"); sp.out ("PUSH", "FOUR"); assertEquals ("THREE", sp.rdp ("PUSH")); assertEquals ("THREE", sp.inp ("PUSH")); assertEquals ("TWO", sp.inp ("PUSH")); assertEquals ("ONE", sp.inp ("PUSH")); assertEquals ("FOUR", sp.inp ("PUSH")); assertNull (sp.rdp ("PUSH")); } public void testExist() {
public void testOutRdpInpRdp() throws Exception { Object o = Boolean.TRUE; String k = "testOutRdpInpRdp_Key"; sp.out (k, o); assertTrue (o.equals (sp.rdp (k))); assertTrue (o.equals (sp.rd (k))); assertTrue (o.equals (sp.rd (k, 1000))); assertTrue (o.equals (sp.inp (k))); assertTrue (sp.rdp (k) == null); assertTrue (sp.rd (k, 100) == null); } public void testMultiKeyLoad() throws Exception {
public void testTemplate () throws Exception { String key = "TemplateTest_Key"; sp.out (key, "Value 1"); sp.out (key, "Value 2"); sp.out (key, "Value 3"); String k2r = (String)sp.rdp (new MD5Template (key, "Value 2")); assertEquals (k2r, "Value 2"); String k2i = (String)sp.inp (new MD5Template (key, "Value 2")); assertEquals (k2i, "Value 2"); assertEquals ("Value 1", (String) sp.inp (key)); assertEquals ("Value 3", (String) sp.inp (key)); } public void testPush() {
public void testSingleKeyLoad() throws Exception { String s = "The quick brown fox jumped over the lazy dog"; String k = "testSingleKeyLoad_Key"; Profiler prof = new Profiler (); for (int i=0; i<COUNT; i++) { sp.out (k, s); if (i % 100 == 0) prof.checkPoint ("out " + i); } // prof.dump (System.err, "SingleKeyLoad out >"); prof = new Profiler (); for (int i=0; i<COUNT; i++) { assertTrue (s.equals (sp.in (k))); if (i % 100 == 0) prof.checkPoint ("in " + i); } // prof.dump (System.err, "SingleKeyLoad in >"); assertTrue (sp.rdp (k) == null); } public void testTemplate () throws Exception {
public boolean existAny (Object[] keys) { for (Object key : keys) { if (rdp(key) != null) return true; } return false; } public boolean existAny (Object[] keys, long timeout) {
public void testExist() { sp.out ("KEYA", Boolean.TRUE); sp.out ("KEYB", Boolean.TRUE); assertTrue ( "existAny ([KEYA])", sp.existAny (new String[] { "KEYA" }) ); assertTrue ( "existAny ([KEYB])", sp.existAny (new String[] { "KEYB" }) ); assertTrue ( "existAny ([KEYA,KEYB])", sp.existAny (new String[] { "KEYA", "KEYB" }) ); assertFalse ( "existAny ([KEYC,KEYD])", sp.existAny (new String[] { "KEYC", "KEYD" }) ); } public void testExistWithTimeout() {
public void testExistWithTimeout() { assertFalse ( "existAnyWithTimeout ([KA,KB])", sp.existAny (new String[] { "KA", "KB" }) ); assertFalse ( "existAnyWithTimeout ([KA,KB], delay)", sp.existAny (new String[] { "KA", "KB" }, 1000L) ); new Thread() { public void run() { ISOUtil.sleep (1000L); sp.out ("KA", Boolean.TRUE); } }.start(); long now = System.currentTimeMillis(); assertTrue ( "existAnyWithTimeout ([KA,KB], delay)", sp.existAny (new String[] { "KA", "KB" }, 2000L) ); long elapsed = System.currentTimeMillis() - now; assertTrue ( "delay was > 1000", elapsed > 900L); assertNotNull ("Entry should not be null", sp.inp ("KA")); } public void testPut () {
/** * @return reference to default JDBMSpace */ public static JDBMSpace getSpace() { return getSpace ("space"); } /**
/** * Write a new entry into the Space * @param key Entry's key * @param value Object value */ public void out (K key, V value) { out (key, value, -1); } /**
public synchronized V in (Object key) { Object obj; while ((obj = inp (key)) == null) { try { this.wait (); } catch (InterruptedException ignored) { } } return (V) obj; } /**