From 2d61ce5b66fc923b219689cba50239efff8b55ad Mon Sep 17 00:00:00 2001 From: Don Anderson Date: Fri, 2 Jan 2015 17:09:01 -0500 Subject: Fixed a timing error in the example startup, as well as closing handles. Refs #1524. --- .../java/com/wiredtiger/examples/ex_thread.java | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/java/com/wiredtiger/examples/ex_thread.java b/examples/java/com/wiredtiger/examples/ex_thread.java index c6b9a5479a9..7338ccee874 100644 --- a/examples/java/com/wiredtiger/examples/ex_thread.java +++ b/examples/java/com/wiredtiger/examples/ex_thread.java @@ -59,6 +59,8 @@ class ScanThread extends Thread { if (ret != wiredtiger.WT_NOTFOUND) System.err.println("Cursor.next: " + wiredtiger.wiredtiger_strerror(ret)); + cursor.close(); + session.close(null); } catch (WiredTigerException wte) { System.err.println("Exception " + wte); } @@ -73,7 +75,7 @@ public class ex_thread { public static final int NUM_THREADS = 10; /*! [thread main] */ - static int main(String[] argv) + public static void main(String[] argv) { try { Thread[] threads = new Thread[NUM_THREADS]; @@ -85,7 +87,7 @@ public class ex_thread { * environment variable isn't already set (as is done by make check). */ if (System.getenv("WIREDTIGER_HOME") == null) { - home = "WT_HOME"; + home = "WT_HOME"; try { Process proc = Runtime.getRuntime().exec("/bin/rm -rf " + home); BufferedReader br = new BufferedReader( @@ -93,17 +95,19 @@ public class ex_thread { while(br.ready()) System.out.println(br.readLine()); br.close(); - new File(home).mkdir(); - } catch (IOException ioe) { - System.err.println("IOException: " + home + ": " + ioe); - return(1); + proc.waitFor(); + if (!(new File(home)).mkdir()) + System.err.println("mkdir: failed"); + } catch (Exception ex) { + System.err.println("Exception: " + home + ": " + ex); + System.exit(1); } } else home = null; if ((conn = wiredtiger.open(home, "create")) == null) { System.err.println("Error connecting to " + home); - return(1); + System.exit(1); } /* Note: further error checking omitted for clarity. */ @@ -114,6 +118,7 @@ public class ex_thread { cursor.putKeyString("key1"); cursor.putValueString("value1"); ret = cursor.insert(); + cursor.close(); ret = session.close(null); for (i = 0; i < NUM_THREADS; i++) { @@ -130,11 +135,11 @@ public class ex_thread { } ret = conn.close(null); - return (ret); + System.exit(ret); } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); - return (-1); + System.exit(1); } } /*! [thread main] */ -- cgit v1.2.1 From 9af563c265b65fd220d085818a1cc46e541ebf7e Mon Sep 17 00:00:00 2001 From: Don Anderson Date: Fri, 9 Jan 2015 09:56:04 -0500 Subject: Set of fixes to get most java examples to run. Refs #1526. In all java examples: declared main() correctly for picky JVMs, print stack traces on exception, brought examples in line with C code. In java examples, use Process.waitFor() to wait for 'rm' to complete. Modified examples to use new record methods (e.g. getKeyRecord() instead of getKeyLong()) when appropriate. Fixed key/value comparisions in ex_log.java example. --- .../java/com/wiredtiger/examples/ex_async.java | 12 +++---- .../com/wiredtiger/examples/ex_call_center.java | 24 +++++++------ .../java/com/wiredtiger/examples/ex_cursor.java | 14 ++++---- examples/java/com/wiredtiger/examples/ex_log.java | 39 ++++++++++++---------- .../java/com/wiredtiger/examples/ex_schema.java | 31 ++++++++++++----- examples/java/com/wiredtiger/examples/ex_stat.java | 19 ++++++----- .../java/com/wiredtiger/examples/ex_thread.java | 1 + 7 files changed, 83 insertions(+), 57 deletions(-) (limited to 'examples') diff --git a/examples/java/com/wiredtiger/examples/ex_async.java b/examples/java/com/wiredtiger/examples/ex_async.java index c6cb0550571..3e794629286 100644 --- a/examples/java/com/wiredtiger/examples/ex_async.java +++ b/examples/java/com/wiredtiger/examples/ex_async.java @@ -59,6 +59,7 @@ class AsyncKeys implements AsyncCallback { long id = op.getId(); /*! [async get identifier] */ + /* If doing a search, retrieve the key/value pair. */ if (optype == AsyncOpType.WT_AOP_SEARCH) { /*! [async get the operation's string key] */ String key = op.getKeyString(); @@ -72,10 +73,6 @@ class AsyncKeys implements AsyncCallback { System.out.println("Id " + id + " got record: " + key + " : " + value); } - else { - notifyError("unexpected optype"); - ret = 1; - } } catch (Exception e) { System.err.println("ERROR: exception in notify: " + e.toString() + @@ -208,15 +205,16 @@ public class ex_async { return (ret); } - public static int + public static void main(String[] argv) { try { - return (asyncExample()); + System.exit(asyncExample()); } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); - return (-1); + wte.printStackTrace(); + System.exit(1); } } } diff --git a/examples/java/com/wiredtiger/examples/ex_call_center.java b/examples/java/com/wiredtiger/examples/ex_call_center.java index 553f63612bd..a8f1768c71f 100644 --- a/examples/java/com/wiredtiger/examples/ex_call_center.java +++ b/examples/java/com/wiredtiger/examples/ex_call_center.java @@ -125,10 +125,11 @@ public class ex_call_center { while(br.ready()) System.out.println(br.readLine()); br.close(); + proc.waitFor(); new File("WT_HOME").mkdir(); - } catch (IOException ioe) { - System.err.println("IOException: WT_HOME: " + ioe); - return(1); + } catch (Exception ex) { + System.err.println("Exception: " + ex); + return (1); } } else home = null; @@ -196,8 +197,8 @@ public class ex_call_center { cursor = session.open_cursor("table:calls", null, "append"); for (Call call : callSample) { cursor.putValueLong(call.call_date); - cursor.putValueLong(call.cust_id); - cursor.putValueLong(call.emp_id); + cursor.putValueRecord(call.cust_id); + cursor.putValueRecord(call.emp_id); cursor.putValueString(call.call_type); cursor.putValueString(call.notes); ret = cursor.insert(); @@ -223,7 +224,7 @@ public class ex_call_center { ret = cursor.search(); if (ret == 0) { Customer cust = new Customer(); - cust.id = cursor.getValueLong(); + cust.id = cursor.getValueRecord(); cust.name = cursor.getValueString(); System.out.println("Read customer record for " + cust.name + " (ID " + cust.id + ")"); @@ -253,7 +254,7 @@ public class ex_call_center { * backwards. */ long custid = 1; - cursor.putKeyLong(custid + 1); + cursor.putKeyRecord(custid + 1); cursor.putKeyLong(0); nearstatus = cursor.search_near(); @@ -268,7 +269,7 @@ public class ex_call_center { ret = cursor.prev(); for (count = 0; ret == 0 && count < 3; ++count) { Call call = new Call(); - call.cust_id = cursor.getValueLong(); + call.cust_id = cursor.getValueRecord(); call.call_type = cursor.getValueString(); call.notes = cursor.getValueString(); if (call.cust_id != custid) @@ -285,15 +286,16 @@ public class ex_call_center { return (ret); } - public static int + public static void main(String[] argv) { try { - return (callCenterExample()); + System.exit(callCenterExample()); } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); - return (-1); + wte.printStackTrace(); + System.exit(1); } } } diff --git a/examples/java/com/wiredtiger/examples/ex_cursor.java b/examples/java/com/wiredtiger/examples/ex_cursor.java index 7b8de7739d2..088a9016bba 100644 --- a/examples/java/com/wiredtiger/examples/ex_cursor.java +++ b/examples/java/com/wiredtiger/examples/ex_cursor.java @@ -177,10 +177,11 @@ public class ex_cursor { while(br.ready()) System.out.println(br.readLine()); br.close(); + proc.waitFor(); new File("WT_HOME").mkdir(); - } catch (IOException ioe) { - System.err.println("IOException: WT_HOME: " + ioe); - return(1); + } catch (Exception ex) { + System.err.println("Exception: " + ex); + return (1); } } else home = null; @@ -225,15 +226,16 @@ public class ex_cursor { return (ret); } - public static int + public static void main(String[] argv) { try { - return (cursorExample()); + System.exit(cursorExample()); } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); - return (-1); + wte.printStackTrace(); + System.exit(1); } } } diff --git a/examples/java/com/wiredtiger/examples/ex_log.java b/examples/java/com/wiredtiger/examples/ex_log.java index d7bc6987878..51908170c32 100644 --- a/examples/java/com/wiredtiger/examples/ex_log.java +++ b/examples/java/com/wiredtiger/examples/ex_log.java @@ -56,7 +56,7 @@ public class ex_log { conn = wiredtiger.open(home2, CONN_CONFIG); Session session = conn.open_session(null); - session.create(uri, "key_format=S,value_format=S"); + session.create(uri, "key_format=U,value_format=U"); return (session); } @@ -71,16 +71,17 @@ public class ex_log { while ((ret = cursor.next()) == 0) { ret = curs_copy.next(); - String key = cursor.getKeyString(); - String value = cursor.getValueString(); - String key_copy = curs_copy.getKeyString(); - String value_copy = curs_copy.getValueString(); - if (!key.equals(key_copy) || !value.equals(value_copy)) { + byte[] key = cursor.getKeyByteArray(); + byte[] value = cursor.getValueByteArray(); + byte[] key_copy = curs_copy.getKeyByteArray(); + byte[] value_copy = curs_copy.getValueByteArray(); + if (!Arrays.equals(key, key_copy) || + !Arrays.equals(value, value_copy)) { System.err.println( - "Mismatched: key " + key + - ", key_copy " + key_copy + - " value " + value + - " value_copy " + value_copy); + "Mismatched: key " + new String(key) + + ", key_copy " + new String(key_copy) + + ", value " + new String(value) + + ", value_copy " + new String(value_copy)); return (1); } } @@ -109,7 +110,7 @@ public class ex_log { ": record type " + rectype + " optype " + optype + " txnid " + txnid + " fileid " + fileid); System.out.println(" key size " + key.length + - "value size " + value.length); + " value size " + value.length); if (rectype == wiredtiger.WT_LOGREC_MESSAGE) System.out.println("Application Record: " + new String(value)); } @@ -301,10 +302,11 @@ public class ex_log { while(br.ready()) System.out.println(br.readLine()); br.close(); + proc.waitFor(); new File(home1).mkdir(); new File(home2).mkdir(); - } catch (IOException ioe) { - System.err.println("IOException: " + ioe); + } catch (Exception ex) { + System.err.println("Exception: " + ex); return (1); } if ((wt_conn = wiredtiger.open(home1, CONN_CONFIG)) == null) { @@ -344,6 +346,7 @@ public class ex_log { ret = session.log_printf("Wrote " + record_count + " records"); /*! [log cursor printf] */ + session.close(null); /* * Close and reopen the connection so that the log ends up with * a variety of records such as file sync and checkpoint. We @@ -358,19 +361,21 @@ public class ex_log { session = wt_conn.open_session(null); ret = simple_walk_log(session); ret = walk_log(session); + ret = session.close(null); ret = wt_conn.close(null); return (ret); } - public static int - main() + public static void + main(String[] args) { try { - return (logExample()); + System.exit(logExample()); } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); - return (-1); + wte.printStackTrace(); + System.exit(1); } } } diff --git a/examples/java/com/wiredtiger/examples/ex_schema.java b/examples/java/com/wiredtiger/examples/ex_schema.java index 18926f47008..f239c6da344 100644 --- a/examples/java/com/wiredtiger/examples/ex_schema.java +++ b/examples/java/com/wiredtiger/examples/ex_schema.java @@ -91,10 +91,11 @@ public class ex_schema { while(br.ready()) System.out.println(br.readLine()); br.close(); + proc.waitFor(); new File("WT_HOME").mkdir(); - } catch (IOException ioe) { - System.err.println("IOException: WT_HOME: " + ioe); - return(1); + } catch (Exception ex) { + System.err.println("Exception: " + ex); + return (1); } } else home = null; @@ -158,10 +159,23 @@ public class ex_schema { } ret = cursor.close(); + /* Update records in the table. */ + cursor = session.open_cursor("table:poptable", null, null); + while ((ret = cursor.next()) == 0) { + recno = cursor.getKeyRecord(); + country = cursor.getValueString(); + year = cursor.getValueShort(); + population = cursor.getValueLong(); + cursor.putValueString(country); + cursor.putValueShort(year); + cursor.putValueLong(population + 1); + } + ret = cursor.close(); + /* List the records in the table. */ cursor = session.open_cursor("table:poptable", null, null); while ((ret = cursor.next()) == 0) { - recno = cursor.getKeyLong(); + recno = cursor.getKeyRecord(); country = cursor.getValueString(); year = cursor.getValueShort(); population = cursor.getValueLong(); @@ -281,7 +295,7 @@ public class ex_schema { while ((ret = cursor.next()) == 0) { country = cursor.getKeyString(); year = cursor.getKeyShort(); - recno = cursor.getValueLong(); + recno = cursor.getValueRecord(); System.out.println("row ID " + recno + ": country " + country + ", year " + year); } @@ -325,15 +339,16 @@ public class ex_schema { return (ret); } - public static int + public static void main(String[] argv) { try { - return (schemaExample()); + System.exit(schemaExample()); } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); - return (-1); + wte.printStackTrace(); + System.exit(1); } } } diff --git a/examples/java/com/wiredtiger/examples/ex_stat.java b/examples/java/com/wiredtiger/examples/ex_stat.java index c81bb64c22a..a9c78b2fe7f 100644 --- a/examples/java/com/wiredtiger/examples/ex_stat.java +++ b/examples/java/com/wiredtiger/examples/ex_stat.java @@ -200,16 +200,18 @@ public class ex_stat { if (System.getenv("WIREDTIGER_HOME") == null) { home = "WT_HOME"; try { - Process proc = Runtime.getRuntime().exec("/bin/rm -rf WT_HOME"); + Process proc = Runtime.getRuntime().exec("/bin/rm -rf " + home); BufferedReader br = new BufferedReader( new InputStreamReader(proc.getInputStream())); while(br.ready()) System.out.println(br.readLine()); br.close(); - new File("WT_HOME").mkdir(); - } catch (IOException ioe) { - System.err.println("IOException: WT_HOME: " + ioe); - return(1); + proc.waitFor(); + if (!(new File(home)).mkdir()) + System.err.println("mkdir: failed"); + } catch (Exception ex) { + System.err.println("Exception: " + home + ": " + ex); + System.exit(1); } } else home = null; @@ -238,15 +240,16 @@ public class ex_stat { return (conn.close(null) == 0 ? ret : -1); } - public static int + public static void main(String[] argv) { try { - return ((new ex_stat()).statExample()); + System.exit((new ex_stat()).statExample()); } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); - return (-1); + wte.printStackTrace(); + System.exit(1); } } } diff --git a/examples/java/com/wiredtiger/examples/ex_thread.java b/examples/java/com/wiredtiger/examples/ex_thread.java index 7338ccee874..f0af95fcc10 100644 --- a/examples/java/com/wiredtiger/examples/ex_thread.java +++ b/examples/java/com/wiredtiger/examples/ex_thread.java @@ -139,6 +139,7 @@ public class ex_thread { } catch (WiredTigerException wte) { System.err.println("Exception: " + wte); + wte.printStackTrace(); System.exit(1); } } -- cgit v1.2.1