-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 8 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 |
---|---|---|
@@ -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@@ |
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,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) { | ||
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,2 @@ | ||
/bin | ||
/bin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At most one "/bin" required |
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: