1) Versant数据库可以直接支持复杂的业务模型:
public class Person {
String firstName;
String lastName;
String gender;
String ethnicity;
String language;
// 新增的节点
int index = 5;
Contact info;
Location location;
public String primaryCountry;
public String primaryAreaCode;
HashSet<Person> friends = new HashSet<Person>();
HashSet<Person> colleagues = new HashSet<Person>();
HashSet<Person> family = new HashSet<Person>();
HashSet<Person> relations = new HashSet<Person>();
}
Versant数据库可以直接支持包括HashSet、LinkedList在内的复杂数据结构。
2)Versant数据库可以直接支持复杂的对象间的关系
如下的代码中展示了一个两层的关系结构。
public void addFriend( Person p ){
friends.add(p);
addRelation(p);
p.getFriends().add(this);
}
3)Versant数据库可以很容易的建立和数据库之间的连接:
Iterator<DatabaseLoginHelper> ite = this.dblist.iterator();
DatabaseLoginHelper helper = (DatabaseLoginHelper)ite.next();
session = new TransSession(helper.getDatabaseNodeProperty());
session.setSchemaOption(TransSession.SCHEMA_ADD_DROP_ATTRIBUTES);
// System.out.println("Define Logical database:");
session.newLogicalDatabase(HPC_DEMO_NETWORK_NAME);
// System.out.println("Add to logical database:"+dbList[0]);
session.addToLogicalDatabase(HPC_DEMO_NETWORK_NAME, helper.databaseName);
System.out.println("Add to logical database:" + helper.databaseName);
4)Versant数据库可以很容易地创建对象,并保存到数据库中。
TransSession session = DistributedDatabaseManager.getInstance()
.createNewSession();
session.setDefaultDatabase("dbnodeb");
// TransSession session = new TransSession("dbnodea");
/**
* generate 500 random objects
*/
for (int i = 0; i < 1500; i++) {
Person person = new Person();
person.setFirstName("TFistName" + i);
person.setLastName("TListName" + i);
// set storage schema
DistributedDatabaseManager.getInstance()
.setRoundRobinPersistentSchema();
session.makePersistent(person);
session.commit();
}
System.out.println("Demo data generated.");
session.commit();
上面的例子中,可以实现自动将数据对象配载到分布式数据库的不同节点中。
5)创建复杂的对象关联,在Versant数据库中也非常容易,可以直接理解为内存对象的操作。
public void createKnownPerson() {
TransSession session = DistributedDatabaseManager.getInstance()
.createNewSession();
session.setDefaultDatabase("dbnodeb");
Person personA = new Person();
personA.setFirstName("AAF1");
personA.setLastName("AAL1");
Person personB = new Person();
personB.setFirstName("BBF1");
personB.setLastName("BBL1");
personB.addFriend(personA);
Person personC = new Person();
personC.setFirstName("CCF1");
personC.setLastName("CCL1");
personC.addFriend(personB);
Person personD = new Person();
personD.setFirstName("DDF1");
personD.setLastName("DDL1");
personD.addFriend(personC);
session.makePersistent(personA, "dbnodea");
session.makePersistent(personB, "dbnodeb");
session.makePersistent(personC, "dbnodea");
session.makePersistent(personD, "dbnodeb");
System.out.println("Special Test Data created.");
session.commit();
}
6)Versant数据库的对象查询
Versant数据库可以支持SQL查询和NOSQL查询两种模式,以下为SQL查询的例子:
TransSession session = DistributedDatabaseManager.getInstance()
.createNewSession();
VQLQuery q = new VQLQuery(
session,
DistributedDatabaseManager.getInstance().HPC_DEMO_NETWORK_NAME,
"select selfoid from com.versant.domain.Person where firstName='AAF1' and lastName='AAL1'");
//"select * from com.versant.domain.Person");
System.out.println("About to execute query, and load root object.");
VEnumeration results = q.execute();
// 创建已经走过的朋友路径,避免回环
System.out
.println("--------------------------------------------------------------------------");
7)Versant数据库的对象查询
Versant数据库可以支持SQL查询和NOSQL查询两种模式,以下为在查到第一个目标对象,之后采用NOSQL方式,自动执行朋友圈子遍历的例子:
VQLQuery q = new VQLQuery(
session,
DistributedDatabaseManager.getInstance().HPC_DEMO_NETWORK_NAME,
"select selfoid from com.versant.domain.Person where firstName='AAF1' and lastName='AAL1'");
//"select * from com.versant.domain.Person");
System.out.println("About to execute query, and load root object.");
VEnumeration results = q.execute();
// 创建已经走过的朋友路径,避免回环
System.out
.println("--------------------------------------------------------------------------");
long middleTime = System.currentTimeMillis();
HashSet<Person> friendSet = new HashSet<Person>();
if (results.hasMoreElements()) {
Person person = (Person) results.nextElement();
friendSet.add(person);
System.out.println("Start Person found:" + person.getFirstName()
+ "/" + person.getLastName()
+ ", about to print friend path.");
Iterator ite = person.getFriends().iterator();
System.out.print("<<< -> " + person.getFirstName() + "/"
+ person.getLastName());
while (ite.hasNext()) {
Person aFriend = (Person) ite.next();
if (!inFriendCircle(aFriend, friendSet)) {
System.out.print("--> " + aFriend.getFirstName() + "/"
+ aFriend.getLastName());
printFriendPath("--> ", aFriend, friendSet);
}
}
System.out.println(" >>>");
} else {
System.out.println("No root person found.");
}
long endTime = System.currentTimeMillis();
public class Person {
String firstName;
String lastName;
String gender;
String ethnicity;
String language;
// 新增的节点
int index = 5;
Contact info;
Location location;
public String primaryCountry;
public String primaryAreaCode;
HashSet<Person> friends = new HashSet<Person>();
HashSet<Person> colleagues = new HashSet<Person>();
HashSet<Person> family = new HashSet<Person>();
HashSet<Person> relations = new HashSet<Person>();
}
Versant数据库可以直接支持包括HashSet、LinkedList在内的复杂数据结构。
2)Versant数据库可以直接支持复杂的对象间的关系
如下的代码中展示了一个两层的关系结构。
public void addFriend( Person p ){
friends.add(p);
addRelation(p);
p.getFriends().add(this);
}
3)Versant数据库可以很容易的建立和数据库之间的连接:
Iterator<DatabaseLoginHelper> ite = this.dblist.iterator();
DatabaseLoginHelper helper = (DatabaseLoginHelper)ite.next();
session = new TransSession(helper.getDatabaseNodeProperty());
session.setSchemaOption(TransSession.SCHEMA_ADD_DROP_ATTRIBUTES);
// System.out.println("Define Logical database:");
session.newLogicalDatabase(HPC_DEMO_NETWORK_NAME);
// System.out.println("Add to logical database:"+dbList[0]);
session.addToLogicalDatabase(HPC_DEMO_NETWORK_NAME, helper.databaseName);
System.out.println("Add to logical database:" + helper.databaseName);
4)Versant数据库可以很容易地创建对象,并保存到数据库中。
TransSession session = DistributedDatabaseManager.getInstance()
.createNewSession();
session.setDefaultDatabase("dbnodeb");
// TransSession session = new TransSession("dbnodea");
/**
* generate 500 random objects
*/
for (int i = 0; i < 1500; i++) {
Person person = new Person();
person.setFirstName("TFistName" + i);
person.setLastName("TListName" + i);
// set storage schema
DistributedDatabaseManager.getInstance()
.setRoundRobinPersistentSchema();
session.makePersistent(person);
session.commit();
}
System.out.println("Demo data generated.");
session.commit();
上面的例子中,可以实现自动将数据对象配载到分布式数据库的不同节点中。
5)创建复杂的对象关联,在Versant数据库中也非常容易,可以直接理解为内存对象的操作。
public void createKnownPerson() {
TransSession session = DistributedDatabaseManager.getInstance()
.createNewSession();
session.setDefaultDatabase("dbnodeb");
Person personA = new Person();
personA.setFirstName("AAF1");
personA.setLastName("AAL1");
Person personB = new Person();
personB.setFirstName("BBF1");
personB.setLastName("BBL1");
personB.addFriend(personA);
Person personC = new Person();
personC.setFirstName("CCF1");
personC.setLastName("CCL1");
personC.addFriend(personB);
Person personD = new Person();
personD.setFirstName("DDF1");
personD.setLastName("DDL1");
personD.addFriend(personC);
session.makePersistent(personA, "dbnodea");
session.makePersistent(personB, "dbnodeb");
session.makePersistent(personC, "dbnodea");
session.makePersistent(personD, "dbnodeb");
System.out.println("Special Test Data created.");
session.commit();
}
6)Versant数据库的对象查询
Versant数据库可以支持SQL查询和NOSQL查询两种模式,以下为SQL查询的例子:
TransSession session = DistributedDatabaseManager.getInstance()
.createNewSession();
VQLQuery q = new VQLQuery(
session,
DistributedDatabaseManager.getInstance().HPC_DEMO_NETWORK_NAME,
"select selfoid from com.versant.domain.Person where firstName='AAF1' and lastName='AAL1'");
//"select * from com.versant.domain.Person");
System.out.println("About to execute query, and load root object.");
VEnumeration results = q.execute();
// 创建已经走过的朋友路径,避免回环
System.out
.println("--------------------------------------------------------------------------");
7)Versant数据库的对象查询
Versant数据库可以支持SQL查询和NOSQL查询两种模式,以下为在查到第一个目标对象,之后采用NOSQL方式,自动执行朋友圈子遍历的例子:
VQLQuery q = new VQLQuery(
session,
DistributedDatabaseManager.getInstance().HPC_DEMO_NETWORK_NAME,
"select selfoid from com.versant.domain.Person where firstName='AAF1' and lastName='AAL1'");
//"select * from com.versant.domain.Person");
System.out.println("About to execute query, and load root object.");
VEnumeration results = q.execute();
// 创建已经走过的朋友路径,避免回环
System.out
.println("--------------------------------------------------------------------------");
long middleTime = System.currentTimeMillis();
HashSet<Person> friendSet = new HashSet<Person>();
if (results.hasMoreElements()) {
Person person = (Person) results.nextElement();
friendSet.add(person);
System.out.println("Start Person found:" + person.getFirstName()
+ "/" + person.getLastName()
+ ", about to print friend path.");
Iterator ite = person.getFriends().iterator();
System.out.print("<<< -> " + person.getFirstName() + "/"
+ person.getLastName());
while (ite.hasNext()) {
Person aFriend = (Person) ite.next();
if (!inFriendCircle(aFriend, friendSet)) {
System.out.print("--> " + aFriend.getFirstName() + "/"
+ aFriend.getLastName());
printFriendPath("--> ", aFriend, friendSet);
}
}
System.out.println(" >>>");
} else {
System.out.println("No root person found.");
}
long endTime = System.currentTimeMillis();