Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4 spaces indentation and Group example #93

Merged
merged 18 commits into from
Aug 21, 2013
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions doc/ref/examples/GroupIntro.java
Original file line number Diff line number Diff line change
@@ -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@@
79 changes: 79 additions & 0 deletions doc/ref/examples/SharedGroupIntro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @@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 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();
}


//-------------------------------------------------------------------
//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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmunkholm perhaps we should reconsider keeping this Assert method, as assert (built-in) does not do anything unless when running in test mode. I guess that was your original thinking

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes - that was it...

// Brian

On Thu, Aug 15, 2013 at 11:29 AM, mekjaer notifications@github.com wrote:

In doc/ref/examples/SharedGroupIntro.java:

  •        //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) {

@bmunkholm https://github.com/bmunkholm perhaps we should reconsider
keeping this Assert method, as assert (built-in) does not do anything
unless when running in test mode. I guess that was your original thinking


Reply to this email directly or view it on GitHubhttps://github.com//pull/93/files#r5786331
.

if (!check) {
throw new RuntimeException();
}
}
}
//@@EndExample@@
2 changes: 2 additions & 0 deletions examples/ant-setup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin
/bin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At most one "/bin" required

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