-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3274916
Changed the name in the tutorial.
1d84f3a
Added shared grouo documentation
d9536ca
Changed back to Bill
35376fa
Changed from TableSpec to addColumn
7c544d9
Space between last keyword and { + 4 spaces as indentation
c02adf5
4 spaces as indentation
02babd4
Added Group Intro example
82899c0
Comment style better matching existing examples
58403fb
tighter documentation and assert using the built-in java method
4899c46
Reinserting Assert
13d4fa3
TableView example
e904ce9
Added doc for ReadTransaction
3e23f20
WriteTransaction doc
20fcac2
Added method that takes a Group as parameter
8fef151
More description before using transaction as group parameter
a76ddac
Write transaction example updated with wt used as group parameter
408db43
minor refrasing
f5d1895
more precise description on the read transaction example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,5 @@ _ReSharper*/ | |
|
||
# sh build.sh test | ||
/test_output | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: