Skip to content

Commit

Permalink
Merge pull request #93 from mekjaer/master
Browse files Browse the repository at this point in the history
4 spaces indentation and Group example
  • Loading branch information
mekjaer committed Aug 21, 2013
2 parents b51adab + f5d1895 commit 0535883
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ _ReSharper*/

# sh build.sh test
/test_output

.DS_Store
101 changes: 101 additions & 0 deletions doc/ref/examples/GroupIntro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// @@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) throws IOException {
// @@Show@@
//Create a new empty group
Group group = new Group();

//Create a new table with 2 columns and add 3 rows of data
Table table = group.getTable("table1");
table.addColumn(ColumnType.ColumnTypeInt, "ID");
table.addColumn(ColumnType.ColumnTypeString, "Animal");
table.add(1, "Lion");
table.add(2, "Monkey");
table.add(3, "Elephant");

//-------------------------------------------------------------------
//Serialization of the group
//-------------------------------------------------------------------

//A new file pointing to the location of the database
File file = new File("mydatabase.tightdb");

//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);
}

//-------------------------------------------------------------------
//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 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@@
}

static void Assert(boolean check) {
if (!check) {
throw new RuntimeException();
}
}
}
//@@EndExample@@
53 changes: 53 additions & 0 deletions doc/ref/examples/ReadTransactionIntro.java
Original file line number Diff line number Diff line change
@@ -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 immutable view of the group
try {
//Get a table from the group
Table table = rt.getTable("table");

//Actions inside a ReadTransacton will never affect the original group and tables
String value = table.getString(1, 0);

//Do more table read operations here...

//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 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@@

static void Assert(boolean check) {
if (!check) {
throw new RuntimeException();
}
}
}
//@@EndExample@@
66 changes: 66 additions & 0 deletions doc/ref/examples/SharedGroupIntro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// @@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("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 2 columns and add 3 rows of data
table.addColumn(ColumnType.ColumnTypeInt, "ID");
table.addColumn(ColumnType.ColumnTypeString, "City");
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) {
wt.rollback();
}

//-------------------------------------------------------------------
//Reading from the group using transaction
//-------------------------------------------------------------------

//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@@
63 changes: 63 additions & 0 deletions doc/ref/examples/TableViewIntro.java
Original file line number Diff line number Diff line change
@@ -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@@
44 changes: 44 additions & 0 deletions doc/ref/examples/WriteTransactionIntro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// @@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 {
//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();
} catch (Throwable t) {
//In case of an error, rollback to close the transaction and discard all changes
wt.rollback();
}
}

public static void update(Group group){
Table people = group.getTable("people");
//All updates here will be written when wt.commit() is called
//...
} // @@EndShow@@

static void Assert(boolean check) {
if (!check) {
throw new RuntimeException();
}
}
}
//@@EndExample@@
1 change: 1 addition & 0 deletions examples/ant-setup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions examples/intro-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin

0 comments on commit 0535883

Please sign in to comment.