From 32749162b9047edfcd8713e35c7057f82235c476 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Wed, 14 Aug 2013 11:11:50 +0200 Subject: [PATCH 01/18] Changed the name in the tutorial. --- examples/ant-setup/.gitignore | 2 ++ examples/intro-example/.gitignore | 1 + .../src/com/tightdb/examples/tutorial/tutorial.java | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 examples/ant-setup/.gitignore create mode 100644 examples/intro-example/.gitignore diff --git a/examples/ant-setup/.gitignore b/examples/ant-setup/.gitignore new file mode 100644 index 0000000000..167fb2c027 --- /dev/null +++ b/examples/ant-setup/.gitignore @@ -0,0 +1,2 @@ +/bin +/bin diff --git a/examples/intro-example/.gitignore b/examples/intro-example/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/examples/intro-example/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java b/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java index bc59ce4f94..21a42a4018 100644 --- a/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java +++ b/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java @@ -178,7 +178,7 @@ public static void main(String[] args) { try { PeopleTable person = new PeopleTable(wrtTrans); // Add row to table - person.add("Bill", 53, true); + person.add("John", 53, true); wrtTrans.commit(); // End transaction } catch (Throwable e) { wrtTrans.rollback(); // or Rollback From 1d84f3aba1e46b6760e6ba494e9c4026fb3a7181 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Wed, 14 Aug 2013 16:31:24 +0200 Subject: [PATCH 02/18] Added shared grouo documentation --- doc/ref/examples/SharedGroupIntro.java | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 doc/ref/examples/SharedGroupIntro.java diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java new file mode 100644 index 0000000000..63387bfbe2 --- /dev/null +++ b/doc/ref/examples/SharedGroupIntro.java @@ -0,0 +1,72 @@ +// @@Example: ex_java_shared_group_intro @@ + +package com.tightdb.refdoc; + +import com.tightdb.*; + +public class SharedGroupIntro { + + public static void main(String[] args) { + // @@Show@@ + //Opens an existing database file or creates a new database file and opens it into a shared group + SharedGroup group = new SharedGroup("mydata.tightdb"); + + //Begins a write transaction + WriteTransaction wt = group.beginWrite(); + + try{ + + //Creates a new table by using getTable with the new table name as parameter + Table table = wt.getTable("newTable"); + + //Create a TableSpec and specify the column types and names + TableSpec spec = new TableSpec(); + spec.addColumn(ColumnType.ColumnTypeInt, "ID"); + spec.addColumn(ColumnType.ColumnTypeString, "City"); + + //Update the table from the spec + table.updateFromSpec(spec); + + //Add data to the table + table.add(1, "Washington"); + table.add(2, "Los Angeles"); + table.add(3, "New York"); + + //Commit the changes, otherwise no data is written to the table + wt.commit(); + + } catch (Throwable t){ + t.printStackTrace(); + wt.rollback(); + } + + //Create a read transaction from the group + ReadTransaction rt = group.beginRead(); + + try{ + + //Get the newly created table + Table table = rt.getTable("newTable"); + + //Get the size of the table + long size = table.size(); + + //Size should be 3, as we have added 3 rows + Assert(size == 3); + + + } finally{ + //Always end the read transaction + rt.endRead(); + } + + // @@EndShow@@ + } + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } +} +//@@EndExample@@ \ No newline at end of file From d9536caec65c1d2597396f30c97646a210cd1623 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Wed, 14 Aug 2013 16:36:00 +0200 Subject: [PATCH 03/18] Changed back to Bill --- .../src/com/tightdb/examples/tutorial/tutorial.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java b/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java index 21a42a4018..bc59ce4f94 100644 --- a/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java +++ b/examples/intro-example/src/com/tightdb/examples/tutorial/tutorial.java @@ -178,7 +178,7 @@ public static void main(String[] args) { try { PeopleTable person = new PeopleTable(wrtTrans); // Add row to table - person.add("John", 53, true); + person.add("Bill", 53, true); wrtTrans.commit(); // End transaction } catch (Throwable e) { wrtTrans.rollback(); // or Rollback From 35376fad6e062391369f7bcf818f2898899ecf0d Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 09:52:35 +0200 Subject: [PATCH 04/18] Changed from TableSpec to addColumn --- doc/ref/examples/SharedGroupIntro.java | 37 ++++++++++++-------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java index 63387bfbe2..03990578f2 100644 --- a/doc/ref/examples/SharedGroupIntro.java +++ b/doc/ref/examples/SharedGroupIntro.java @@ -7,25 +7,22 @@ public class SharedGroupIntro { public static void main(String[] args) { - // @@Show@@ + // @@Show@@ //Opens an existing database file or creates a new database file and opens it into a shared group - SharedGroup group = new SharedGroup("mydata.tightdb"); + SharedGroup group = new SharedGroup("mydatabase.tightdb"); //Begins a write transaction - WriteTransaction wt = group.beginWrite(); + WriteTransaction wt = group.beginWrite(); try{ //Creates a new table by using getTable with the new table name as parameter Table table = wt.getTable("newTable"); - //Create a TableSpec and specify the column types and names - TableSpec spec = new TableSpec(); - spec.addColumn(ColumnType.ColumnTypeInt, "ID"); - spec.addColumn(ColumnType.ColumnTypeString, "City"); + //Specify the column types and names + table.addColumn(ColumnType.ColumnTypeInt, "ID"); + table.addColumn(ColumnType.ColumnTypeString, "City"); - //Update the table from the spec - table.updateFromSpec(spec); //Add data to the table table.add(1, "Washington"); @@ -44,29 +41,29 @@ public static void main(String[] args) { ReadTransaction rt = group.beginRead(); try{ - + //Get the newly created table Table table = rt.getTable("newTable"); - + //Get the size of the table long size = table.size(); - + //Size should be 3, as we have added 3 rows - Assert(size == 3); + Assert(size == 3); + - } finally{ //Always end the read transaction rt.endRead(); } - + // @@EndShow@@ } - + static void Assert(boolean check) { - if (!check) { - throw new RuntimeException(); - } - } + if (!check) { + throw new RuntimeException(); + } + } } //@@EndExample@@ \ No newline at end of file From 7c544d95444d4dbb2b7b8a0a010b539f26a8f530 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 09:59:50 +0200 Subject: [PATCH 05/18] Space between last keyword and { + 4 spaces as indentation --- doc/ref/examples/SharedGroupIntro.java | 118 ++++++++++++------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java index 03990578f2..c9ee093ed6 100644 --- a/doc/ref/examples/SharedGroupIntro.java +++ b/doc/ref/examples/SharedGroupIntro.java @@ -6,64 +6,64 @@ public class SharedGroupIntro { - public static void main(String[] args) { - // @@Show@@ - //Opens an existing database file or creates a new database file and opens it into a shared group - SharedGroup group = new SharedGroup("mydatabase.tightdb"); - - //Begins a write transaction - WriteTransaction wt = group.beginWrite(); - - try{ - - //Creates a new table by using getTable with the new table name as parameter - Table table = wt.getTable("newTable"); - - //Specify the column types and names - table.addColumn(ColumnType.ColumnTypeInt, "ID"); - table.addColumn(ColumnType.ColumnTypeString, "City"); - - - //Add data to the table - table.add(1, "Washington"); - table.add(2, "Los Angeles"); - table.add(3, "New York"); - - //Commit the changes, otherwise no data is written to the table - wt.commit(); - - } catch (Throwable t){ - t.printStackTrace(); - wt.rollback(); - } - - //Create a read transaction from the group - ReadTransaction rt = group.beginRead(); - - try{ - - //Get the newly created table - Table table = rt.getTable("newTable"); - - //Get the size of the table - long size = table.size(); - - //Size should be 3, as we have added 3 rows - Assert(size == 3); - - - } finally{ - //Always end the read transaction - rt.endRead(); - } - - // @@EndShow@@ - } - - static void Assert(boolean check) { - if (!check) { - throw new RuntimeException(); - } - } + public static void main(String[] args) { + // @@Show@@ + //Opens an existing database file or creates a new database file and opens it into a shared group + SharedGroup group = new SharedGroup("mydatabase.tightdb"); + + //Begins a write transaction + WriteTransaction wt = group.beginWrite(); + + try { + + //Creates a new table by using getTable with the new table name as parameter + Table table = wt.getTable("newTable"); + + //Specify the column types and names + table.addColumn(ColumnType.ColumnTypeInt, "ID"); + table.addColumn(ColumnType.ColumnTypeString, "City"); + + + //Add data to the table + table.add(1, "Washington"); + table.add(2, "Los Angeles"); + table.add(3, "New York"); + + //Commit the changes, otherwise no data is written to the table + wt.commit(); + + } catch (Throwable t) { + t.printStackTrace(); + wt.rollback(); + } + + //Create a read transaction from the group + ReadTransaction rt = group.beginRead(); + + try { + + //Get the newly created table + Table table = rt.getTable("newTable"); + + //Get the size of the table + long size = table.size(); + + //Size should be 3, as we have added 3 rows + Assert(size == 3); + + + } finally { + //Always end the read transaction + rt.endRead(); + } + + // @@EndShow@@ + } + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } } //@@EndExample@@ \ No newline at end of file From c02adf517bf1089256f32a7cbf7ca25e6094483f Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 10:19:46 +0200 Subject: [PATCH 06/18] 4 spaces as indentation --- doc/ref/examples/SharedGroupIntro.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java index c9ee093ed6..a1ef947367 100644 --- a/doc/ref/examples/SharedGroupIntro.java +++ b/doc/ref/examples/SharedGroupIntro.java @@ -14,7 +14,7 @@ public static void main(String[] args) { //Begins a write transaction WriteTransaction wt = group.beginWrite(); - try { + try { //Creates a new table by using getTable with the new table name as parameter Table table = wt.getTable("newTable"); From 02babd483ea05bf0fa72b401a7a7bdea62b494c8 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 10:38:56 +0200 Subject: [PATCH 07/18] Added Group Intro example --- doc/ref/examples/GroupIntro.java | 123 +++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 doc/ref/examples/GroupIntro.java diff --git a/doc/ref/examples/GroupIntro.java b/doc/ref/examples/GroupIntro.java new file mode 100644 index 0000000000..a09a8ab5e7 --- /dev/null +++ b/doc/ref/examples/GroupIntro.java @@ -0,0 +1,123 @@ +// @@Example: ex_java_group_intro @@ +package com.tightdb.refdoc; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.Socket; + +import com.tightdb.*; + +public class GroupIntro { + + public static void main(String[] args) { + // @@Show@@ + + //Create a new empty group + Group group = new Group(); + + //Create a new table + Table table = group.getTable("table1"); + + + //Specify the column types and names + table.addColumn(ColumnType.ColumnTypeInt, "ID"); + table.addColumn(ColumnType.ColumnTypeString, "Animal"); + + //Add data to the table + table.add(1, "Lion"); + table.add(2, "Monkey"); + table.add(3, "Elephant"); + + + //------------------------------------------------------------------- + //Serialization of the group + //------------------------------------------------------------------- + + //A file pointing to the location of the database + File file = new File("mydatabase.tightdb"); + + + try { + //Serialize the database to the file + group.writeToFile(file); + + } catch (IOException e) { + e.printStackTrace(); + } + + //------------------------------------------------------------------- + //Initialize a group from a database file + //------------------------------------------------------------------- + + //Initialize a group object from file + group = new Group(file); + + //Get the number of tables in the group. In this case, only 1 table has been added + assert(group.size() == 1); + + //Returns the name of the first (zero-indexed) table in the group. In this case 'table1' + String tableName = group.getTableName(0); + + //Checks if the group contains the specified table name + assert(group.hasTable(tableName)); + + + //------------------------------------------------------------------- + //Writing to byte array and transfer over a socket + //------------------------------------------------------------------- + + + //Write group to byte array + byte[] byteArray = group.writeToMem(); + + //Transfer the byte array using sockets + try { + Socket socket = new Socket("host", 1234); + DataOutputStream dOut = new DataOutputStream(socket.getOutputStream()); + + + dOut.writeInt(byteArray.length); // write length of the array + dOut.write(byteArray); // write the array + + + + //------------------------------------------------------------------- + //Receive byte array from socket and initialize group + //------------------------------------------------------------------- + + DataInputStream dIn = new DataInputStream(socket.getInputStream()); + + int length = dIn.readInt(); // read length of incoming byte array + byte[] receivedByteArray = new byte[length]; + dIn.readFully(receivedByteArray, 0, receivedByteArray.length); // read the byte array + + + //Initialize group from the received byte array + Group fromArray = new Group(receivedByteArray); + + //Get the number of tables in the group. In this case, only 1 table has been added + assert(fromArray.size() == 1); + + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + // @@EndShow@@ + + } + + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } + + +} +//@@EndExample@@ \ No newline at end of file From 82899c0aca8804214e1643e5f337bc79ccadf586 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 10:41:59 +0200 Subject: [PATCH 08/18] Comment style better matching existing examples --- doc/ref/examples/SharedGroupIntro.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java index a1ef947367..e64f9161e1 100644 --- a/doc/ref/examples/SharedGroupIntro.java +++ b/doc/ref/examples/SharedGroupIntro.java @@ -10,6 +10,11 @@ public static void main(String[] args) { // @@Show@@ //Opens an existing database file or creates a new database file and opens it into a shared group SharedGroup group = new SharedGroup("mydatabase.tightdb"); + + + //------------------------------------------------------------------- + //Writing to the group using transaction + //------------------------------------------------------------------- //Begins a write transaction WriteTransaction wt = group.beginWrite(); @@ -37,6 +42,11 @@ public static void main(String[] args) { wt.rollback(); } + + //------------------------------------------------------------------- + //Reading from the group using transaction + //------------------------------------------------------------------- + //Create a read transaction from the group ReadTransaction rt = group.beginRead(); From 58403fba47e00dbcb3f49b9349f58db73aa44d5d Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 11:26:22 +0200 Subject: [PATCH 09/18] tighter documentation and assert using the built-in java method --- doc/ref/examples/GroupIntro.java | 51 ++++++-------------------- doc/ref/examples/SharedGroupIntro.java | 28 ++------------ 2 files changed, 15 insertions(+), 64 deletions(-) diff --git a/doc/ref/examples/GroupIntro.java b/doc/ref/examples/GroupIntro.java index a09a8ab5e7..963423b320 100644 --- a/doc/ref/examples/GroupIntro.java +++ b/doc/ref/examples/GroupIntro.java @@ -11,40 +11,30 @@ public class GroupIntro { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { // @@Show@@ - //Create a new empty group Group group = new Group(); - //Create a new table + //Create a new table with 2 columns and add 3 rows of data Table table = group.getTable("table1"); - - - //Specify the column types and names table.addColumn(ColumnType.ColumnTypeInt, "ID"); table.addColumn(ColumnType.ColumnTypeString, "Animal"); - - //Add data to the table table.add(1, "Lion"); table.add(2, "Monkey"); table.add(3, "Elephant"); - //------------------------------------------------------------------- //Serialization of the group //------------------------------------------------------------------- - //A file pointing to the location of the database + //A new file pointing to the location of the database File file = new File("mydatabase.tightdb"); - - try { + //Serializing to a file that already exists is an error and would case undefined behaviour + if(file.exists() == false){ //Serialize the database to the file group.writeToFile(file); - - } catch (IOException e) { - e.printStackTrace(); } //------------------------------------------------------------------- @@ -63,12 +53,10 @@ public static void main(String[] args) { //Checks if the group contains the specified table name assert(group.hasTable(tableName)); - //------------------------------------------------------------------- //Writing to byte array and transfer over a socket //------------------------------------------------------------------- - //Write group to byte array byte[] byteArray = group.writeToMem(); @@ -77,12 +65,9 @@ public static void main(String[] args) { Socket socket = new Socket("host", 1234); DataOutputStream dOut = new DataOutputStream(socket.getOutputStream()); - dOut.writeInt(byteArray.length); // write length of the array dOut.write(byteArray); // write the array - - //------------------------------------------------------------------- //Receive byte array from socket and initialize group //------------------------------------------------------------------- @@ -93,31 +78,17 @@ public static void main(String[] args) { byte[] receivedByteArray = new byte[length]; dIn.readFully(receivedByteArray, 0, receivedByteArray.length); // read the byte array - //Initialize group from the received byte array Group fromArray = new Group(receivedByteArray); - - //Get the number of tables in the group. In this case, only 1 table has been added - assert(fromArray.size() == 1); + //Get a table from the group, and read the value from column 1, row 2 (zero-indexed) + Table tableFromArray = fromArray.getTable(tableName); + String value = tableFromArray.getString(1, 2); + assert(value.equals("Elephant")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); - } - - - // @@EndShow@@ - + } // @@EndShow@@ } - - - static void Assert(boolean check) { - if (!check) { - throw new RuntimeException(); - } - } - - -} -//@@EndExample@@ \ No newline at end of file +} //@@EndExample@@ \ No newline at end of file diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java index e64f9161e1..472e80eefb 100644 --- a/doc/ref/examples/SharedGroupIntro.java +++ b/doc/ref/examples/SharedGroupIntro.java @@ -11,38 +11,29 @@ public static void main(String[] args) { //Opens an existing database file or creates a new database file and opens it into a shared group SharedGroup group = new SharedGroup("mydatabase.tightdb"); - //------------------------------------------------------------------- //Writing to the group using transaction //------------------------------------------------------------------- //Begins a write transaction WriteTransaction wt = group.beginWrite(); - try { - //Creates a new table by using getTable with the new table name as parameter Table table = wt.getTable("newTable"); - //Specify the column types and names + //Specify 2 columns and add 3 rows of data table.addColumn(ColumnType.ColumnTypeInt, "ID"); table.addColumn(ColumnType.ColumnTypeString, "City"); - - - //Add data to the table table.add(1, "Washington"); table.add(2, "Los Angeles"); table.add(3, "New York"); //Commit the changes, otherwise no data is written to the table wt.commit(); - } catch (Throwable t) { - t.printStackTrace(); wt.rollback(); } - //------------------------------------------------------------------- //Reading from the group using transaction //------------------------------------------------------------------- @@ -51,7 +42,6 @@ public static void main(String[] args) { ReadTransaction rt = group.beginRead(); try { - //Get the newly created table Table table = rt.getTable("newTable"); @@ -59,21 +49,11 @@ public static void main(String[] args) { long size = table.size(); //Size should be 3, as we have added 3 rows - Assert(size == 3); - + assert(size == 3); } finally { //Always end the read transaction rt.endRead(); - } - - // @@EndShow@@ - } - - static void Assert(boolean check) { - if (!check) { - throw new RuntimeException(); - } + } // @@EndShow@@ } -} -//@@EndExample@@ \ No newline at end of file +} //@@EndExample@@ \ No newline at end of file From 4899c46e39a295df2e0c45f15e9cf28ce5dfc5e4 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 11:34:08 +0200 Subject: [PATCH 10/18] Reinserting Assert --- doc/ref/examples/GroupIntro.java | 12 +++++++++--- doc/ref/examples/SharedGroupIntro.java | 8 +++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/doc/ref/examples/GroupIntro.java b/doc/ref/examples/GroupIntro.java index 963423b320..2326792bfb 100644 --- a/doc/ref/examples/GroupIntro.java +++ b/doc/ref/examples/GroupIntro.java @@ -45,13 +45,13 @@ public static void main(String[] args) throws IOException { group = new Group(file); //Get the number of tables in the group. In this case, only 1 table has been added - assert(group.size() == 1); + Assert(group.size() == 1); //Returns the name of the first (zero-indexed) table in the group. In this case 'table1' String tableName = group.getTableName(0); //Checks if the group contains the specified table name - assert(group.hasTable(tableName)); + Assert(group.hasTable(tableName)); //------------------------------------------------------------------- //Writing to byte array and transfer over a socket @@ -84,11 +84,17 @@ public static void main(String[] args) throws IOException { //Get a table from the group, and read the value from column 1, row 2 (zero-indexed) Table tableFromArray = fromArray.getTable(tableName); String value = tableFromArray.getString(1, 2); - assert(value.equals("Elephant")); + Assert(value.equals("Elephant")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // @@EndShow@@ } + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } } //@@EndExample@@ \ No newline at end of file diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java index 472e80eefb..ab5ae1f7d4 100644 --- a/doc/ref/examples/SharedGroupIntro.java +++ b/doc/ref/examples/SharedGroupIntro.java @@ -49,11 +49,17 @@ public static void main(String[] args) { long size = table.size(); //Size should be 3, as we have added 3 rows - assert(size == 3); + Assert(size == 3); } finally { //Always end the read transaction rt.endRead(); } // @@EndShow@@ } + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } } //@@EndExample@@ \ No newline at end of file From 13d4fa3d2662630618bff77fa3a8e26f9ed07975 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 14:45:22 +0200 Subject: [PATCH 11/18] TableView example --- doc/ref/examples/TableViewIntro.java | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 doc/ref/examples/TableViewIntro.java diff --git a/doc/ref/examples/TableViewIntro.java b/doc/ref/examples/TableViewIntro.java new file mode 100644 index 0000000000..9d3856f270 --- /dev/null +++ b/doc/ref/examples/TableViewIntro.java @@ -0,0 +1,63 @@ +// @@Example: ex_java_table_view_intro @@ + +package com.tightdb.refdoc; + +import java.io.FileNotFoundException; +import java.io.PrintWriter; + +import com.tightdb.*; + +public class TableViewIntro { + + public static void main(String[] args) throws FileNotFoundException { + // @@Show@@ + //Creates a new table by using getTable with the new table name as parameter + Table table = new Table(); + + //Specify the column types and names + table.addColumn(ColumnType.ColumnTypeInt, "ID"); + table.addColumn(ColumnType.ColumnTypeString, "City"); + + //Add data to the table + table.add(100, "Washington"); + table.add(200, "Los Angeles"); + table.add(300, "New York"); + + //Create a query object from the table without any filters and execute it to retrieve a table view + TableView view = table.where().findAll(); + + //Remove the first row from the view and thereby also the original table and check that the number of rows in the original table is 2 + view.remove(0); + Assert(table.size() == 2); + + //Change the value of column 1, row 1 to 'London' and ckech that it is propagated to the original table + view.setString(1, 1, "London"); + Assert(table.getString(1, 1).equals("London")); + + //------------------------------------------------------------------- + //Simple aggregations + //------------------------------------------------------------------- + + Assert(view.sum(0) == 500); + Assert(view.maximum(0) == 300); + Assert(view.maximum(0) == 300); + Assert(view.average(0) == 250); + + //------------------------------------------------------------------- + //Dumping to JSON + //------------------------------------------------------------------- + + //Get JSON representation of the data in the view and print it using e.g. a PrintWriter object + PrintWriter out = new PrintWriter("fromServlet"); + out.print(view.toJson()); + out.close(); + // @@EndShow@@ + } + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } + +} //@@EndExample@@ \ No newline at end of file From e904ce97ec4535f441632f25176cffed8f854a0d Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 15:18:40 +0200 Subject: [PATCH 12/18] Added doc for ReadTransaction --- doc/ref/examples/ReadTransactionIntro.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 doc/ref/examples/ReadTransactionIntro.java diff --git a/doc/ref/examples/ReadTransactionIntro.java b/doc/ref/examples/ReadTransactionIntro.java new file mode 100644 index 0000000000..1b20bf214f --- /dev/null +++ b/doc/ref/examples/ReadTransactionIntro.java @@ -0,0 +1,53 @@ +// @@Example: ex_java_read_transaction_intro @@ + +package com.tightdb.refdoc; + +import com.tightdb.*; + +public class ReadTransactionIntro { + + public static void main(String[] args) { + // @@Show@@ + //Opens an existing database file. We assume the file is also being accessed by other processes, thats why we use a SharedGroup object + SharedGroup group = new SharedGroup("mydatabase.tightdb"); + + //------------------------------------------------------------------- + //Reading from the group using a transaction + //------------------------------------------------------------------- + + //Create a read transaction from the group + ReadTransaction rt = group.beginRead(); + + //Inside the read transaction we have a fully consistent and non mutable view of the group + try { + //Get the the name of the first table in the group, and retrieve the table using the name + String tableName = rt.getTableName(0); + Table table = rt.getTable(tableName); + + //Get the size and column number of the table + long size = table.size(); + long columnCount = table.getColumnCount(); + + System.out.println(tableName + " has " + columnCount + " columns and " + size + " rows!"); + + //Determine the type of the first column + ColumnType type = table.getColumnType(0); + + if(type.equals(ColumnType.ColumnTypeString)){ + //Get the String value from column 0, row 0, if the type is a String + String value = table.getString(0, 0); + + System.out.println("Value is " + value); + } + } finally { + //Always end the read transaction + rt.endRead(); + } // @@EndShow@@ + } + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } +} //@@EndExample@@ \ No newline at end of file From 3e23f20e2cccb899650ee27ead71eed70d5a2d25 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 15:53:01 +0200 Subject: [PATCH 13/18] WriteTransaction doc --- doc/ref/examples/WriteTransactionIntro.java | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 doc/ref/examples/WriteTransactionIntro.java diff --git a/doc/ref/examples/WriteTransactionIntro.java b/doc/ref/examples/WriteTransactionIntro.java new file mode 100644 index 0000000000..ddd67c89ad --- /dev/null +++ b/doc/ref/examples/WriteTransactionIntro.java @@ -0,0 +1,40 @@ +// @@Example: ex_java_write_transaction_intro @@ + +package com.tightdb.refdoc; + +import com.tightdb.*; + +public class WriteTransactionIntro { + + public static void main(String[] args) { + // @@Show@@ + //Opens an existing database file. We assume the file is also being accessed by other processes, thats why we use a SharedGroup object + SharedGroup group = new SharedGroup("mydatabase.tightdb"); + + //------------------------------------------------------------------- + //Writing to the group using transactions + //------------------------------------------------------------------- + + //Begins a write transaction. Any other process trying to initiate a write transaction will be stalled until this transaction ends. + WriteTransaction wt = group.beginWrite(); + try { + //Gets and existing table from the group, and adds values + Table table = wt.getTable("PeopleTable"); + table.add("Peter", "Johnson", 314); + table.add("Miranda", "Flint", 502); + table.add("Jessica", "Appleton", 220); + + //Closes the transaction and all changes are written to the shared group + wt.commit(); + } catch (Throwable t) { + //In case of an error, rollback to close the transaction and discard all changes + wt.rollback(); + } + } + + static void Assert(boolean check) { + if (!check) { + throw new RuntimeException(); + } + } +} //@@EndExample@@ \ No newline at end of file From 20fcac236ff1e641aafc786eb7b6f68a34822495 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Thu, 15 Aug 2013 17:23:14 +0200 Subject: [PATCH 14/18] Added method that takes a Group as parameter --- doc/ref/examples/ReadTransactionIntro.java | 54 +++++++++++----------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/doc/ref/examples/ReadTransactionIntro.java b/doc/ref/examples/ReadTransactionIntro.java index 1b20bf214f..ccec60a81b 100644 --- a/doc/ref/examples/ReadTransactionIntro.java +++ b/doc/ref/examples/ReadTransactionIntro.java @@ -8,46 +8,44 @@ public class ReadTransactionIntro { public static void main(String[] args) { // @@Show@@ - //Opens an existing database file. We assume the file is also being accessed by other processes, thats why we use a SharedGroup object + //Opens an existing database file. + //We assume the file is also being accessed by other processes, + //thats why we use a SharedGroup object SharedGroup group = new SharedGroup("mydatabase.tightdb"); - + //------------------------------------------------------------------- //Reading from the group using a transaction //------------------------------------------------------------------- - + //Create a read transaction from the group ReadTransaction rt = group.beginRead(); - - //Inside the read transaction we have a fully consistent and non mutable view of the group + + //Inside the read transaction we have a fully consistent and immutable view of the group try { - //Get the the name of the first table in the group, and retrieve the table using the name - String tableName = rt.getTableName(0); - Table table = rt.getTable(tableName); - - //Get the size and column number of the table - long size = table.size(); - long columnCount = table.getColumnCount(); - - System.out.println(tableName + " has " + columnCount + " columns and " + size + " rows!"); - - //Determine the type of the first column - ColumnType type = table.getColumnType(0); - - if(type.equals(ColumnType.ColumnTypeString)){ - //Get the String value from column 0, row 0, if the type is a String - String value = table.getString(0, 0); - - System.out.println("Value is " + value); - } + //Get a table from the group + Table table = rt.getTable("table"); + + //Do all table read operations here + //Actions inside a ReadTransacton will never affect the original group and tables + String value = table.getString(1, 0); + + //As a Transaction extends Group, it can be passed as a Group parameter + analyzeGroup(rt); + } finally { - //Always end the read transaction + //Always end the read transaction in a finally block, if something should go + //wrong inside the transaction. If it is not closed, a new transaction can not be initialized rt.endRead(); - } // @@EndShow@@ + } } - + + private static void analyzeGroup(Group group){ + String tableName = group.getTableName(0); + }// @@EndShow@@ //@@EndExample@@ + static void Assert(boolean check) { if (!check) { throw new RuntimeException(); } } -} //@@EndExample@@ \ No newline at end of file +} \ No newline at end of file From 8fef151276089fdd215e1a21d33bab16b3a8f09b Mon Sep 17 00:00:00 2001 From: mekjaer Date: Fri, 16 Aug 2013 10:31:34 +0200 Subject: [PATCH 15/18] More description before using transaction as group parameter --- doc/ref/examples/ReadTransactionIntro.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/ref/examples/ReadTransactionIntro.java b/doc/ref/examples/ReadTransactionIntro.java index ccec60a81b..b042ab272d 100644 --- a/doc/ref/examples/ReadTransactionIntro.java +++ b/doc/ref/examples/ReadTransactionIntro.java @@ -28,6 +28,10 @@ public static void main(String[] args) { //Do all table read operations here //Actions inside a ReadTransacton will never affect the original group and tables String value = table.getString(1, 0); + + //------------------------------------------------------------------- + //ReadTransaction as a Group parameter in methods + //------------------------------------------------------------------- //As a Transaction extends Group, it can be passed as a Group parameter analyzeGroup(rt); From a76ddac11dadaed4b59099ae9fbf3f3e1155122f Mon Sep 17 00:00:00 2001 From: mekjaer Date: Fri, 16 Aug 2013 16:45:36 +0200 Subject: [PATCH 16/18] Write transaction example updated with wt used as group parameter --- .gitignore | 2 ++ doc/ref/examples/WriteTransactionIntro.java | 17 ++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index fda4eb6356..3be91c264f 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,5 @@ _ReSharper*/ # sh build.sh test /test_output + +.DS_Store diff --git a/doc/ref/examples/WriteTransactionIntro.java b/doc/ref/examples/WriteTransactionIntro.java index ddd67c89ad..d579ba7732 100644 --- a/doc/ref/examples/WriteTransactionIntro.java +++ b/doc/ref/examples/WriteTransactionIntro.java @@ -10,7 +10,7 @@ public static void main(String[] args) { // @@Show@@ //Opens an existing database file. We assume the file is also being accessed by other processes, thats why we use a SharedGroup object SharedGroup group = new SharedGroup("mydatabase.tightdb"); - + //------------------------------------------------------------------- //Writing to the group using transactions //------------------------------------------------------------------- @@ -18,11 +18,8 @@ public static void main(String[] args) { //Begins a write transaction. Any other process trying to initiate a write transaction will be stalled until this transaction ends. WriteTransaction wt = group.beginWrite(); try { - //Gets and existing table from the group, and adds values - Table table = wt.getTable("PeopleTable"); - table.add("Peter", "Johnson", 314); - table.add("Miranda", "Flint", 502); - table.add("Jessica", "Appleton", 220); + //Transaction extends from Group and can be used on methods that take Group object as input + update(wt); //Closes the transaction and all changes are written to the shared group wt.commit(); @@ -31,7 +28,13 @@ public static void main(String[] args) { wt.rollback(); } } - + + public static void update(Group group){ + Table people = group.getTable("people"); + //All updates here will be persisted when wt.commit() is called + //... + } // @@EndShow@@ + static void Assert(boolean check) { if (!check) { throw new RuntimeException(); From 408db436d6fd2f57c6e7ef7373fb3c6b22bdc26c Mon Sep 17 00:00:00 2001 From: mekjaer Date: Mon, 19 Aug 2013 10:22:15 +0200 Subject: [PATCH 17/18] minor refrasing --- doc/ref/examples/WriteTransactionIntro.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/ref/examples/WriteTransactionIntro.java b/doc/ref/examples/WriteTransactionIntro.java index d579ba7732..57192cd4af 100644 --- a/doc/ref/examples/WriteTransactionIntro.java +++ b/doc/ref/examples/WriteTransactionIntro.java @@ -31,7 +31,7 @@ public static void main(String[] args) { public static void update(Group group){ Table people = group.getTable("people"); - //All updates here will be persisted when wt.commit() is called + //All updates here will be written when wt.commit() is called //... } // @@EndShow@@ From f5d189577a18860ecaf685ff843905e8caeb7663 Mon Sep 17 00:00:00 2001 From: mekjaer Date: Wed, 21 Aug 2013 11:19:24 +0200 Subject: [PATCH 18/18] more precise description on the read transaction example --- doc/ref/examples/GroupIntro.java | 3 ++- doc/ref/examples/ReadTransactionIntro.java | 18 ++++++++---------- doc/ref/examples/SharedGroupIntro.java | 3 ++- doc/ref/examples/TableViewIntro.java | 4 ++-- doc/ref/examples/WriteTransactionIntro.java | 3 ++- examples/ant-setup/.gitignore | 3 +-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/doc/ref/examples/GroupIntro.java b/doc/ref/examples/GroupIntro.java index 2326792bfb..0d4a7f7a3c 100644 --- a/doc/ref/examples/GroupIntro.java +++ b/doc/ref/examples/GroupIntro.java @@ -97,4 +97,5 @@ static void Assert(boolean check) { throw new RuntimeException(); } } -} //@@EndExample@@ \ No newline at end of file +} +//@@EndExample@@ \ No newline at end of file diff --git a/doc/ref/examples/ReadTransactionIntro.java b/doc/ref/examples/ReadTransactionIntro.java index b042ab272d..31e61ec98a 100644 --- a/doc/ref/examples/ReadTransactionIntro.java +++ b/doc/ref/examples/ReadTransactionIntro.java @@ -25,31 +25,29 @@ public static void main(String[] args) { //Get a table from the group Table table = rt.getTable("table"); - //Do all table read operations here //Actions inside a ReadTransacton will never affect the original group and tables String value = table.getString(1, 0); + + //Do more table read operations here... - //------------------------------------------------------------------- - //ReadTransaction as a Group parameter in methods - //------------------------------------------------------------------- - - //As a Transaction extends Group, it can be passed as a Group parameter + //A Transaction extends Group, and can be passed as a Group parameter analyzeGroup(rt); } finally { - //Always end the read transaction in a finally block, if something should go - //wrong inside the transaction. If it is not closed, a new transaction can not be initialized + //Always end the read transaction in a finally block. If the read-transaction is not + //closed, a new one cannot be started using the same SharedGroup instance. rt.endRead(); } } private static void analyzeGroup(Group group){ String tableName = group.getTableName(0); - }// @@EndShow@@ //@@EndExample@@ + } // @@EndShow@@ static void Assert(boolean check) { if (!check) { throw new RuntimeException(); } } -} \ No newline at end of file +} +//@@EndExample@@ \ No newline at end of file diff --git a/doc/ref/examples/SharedGroupIntro.java b/doc/ref/examples/SharedGroupIntro.java index ab5ae1f7d4..0bee262ccf 100644 --- a/doc/ref/examples/SharedGroupIntro.java +++ b/doc/ref/examples/SharedGroupIntro.java @@ -62,4 +62,5 @@ static void Assert(boolean check) { throw new RuntimeException(); } } -} //@@EndExample@@ \ No newline at end of file +} +//@@EndExample@@ \ No newline at end of file diff --git a/doc/ref/examples/TableViewIntro.java b/doc/ref/examples/TableViewIntro.java index 9d3856f270..171791afd7 100644 --- a/doc/ref/examples/TableViewIntro.java +++ b/doc/ref/examples/TableViewIntro.java @@ -59,5 +59,5 @@ static void Assert(boolean check) { throw new RuntimeException(); } } - -} //@@EndExample@@ \ No newline at end of file +} +//@@EndExample@@ \ No newline at end of file diff --git a/doc/ref/examples/WriteTransactionIntro.java b/doc/ref/examples/WriteTransactionIntro.java index 57192cd4af..3d16748981 100644 --- a/doc/ref/examples/WriteTransactionIntro.java +++ b/doc/ref/examples/WriteTransactionIntro.java @@ -40,4 +40,5 @@ static void Assert(boolean check) { throw new RuntimeException(); } } -} //@@EndExample@@ \ No newline at end of file +} +//@@EndExample@@ \ No newline at end of file diff --git a/examples/ant-setup/.gitignore b/examples/ant-setup/.gitignore index 167fb2c027..8b13789179 100644 --- a/examples/ant-setup/.gitignore +++ b/examples/ant-setup/.gitignore @@ -1,2 +1 @@ -/bin -/bin +