上一篇筆記中主要嘗試了一下SequoiaDB 的 Sequoiadb,CollectionSpace,DBCollection 這幾個類給出的基本接口。實現了數據庫實例的創建,集合空間的創建,查詢,集合的創建,數據的插入,和數據集的插入等一系列操作。這次嘗試一下在集合中的刪除,更新,查詢等操作。
前半部分代碼是將上次的代碼稍作了修改,實現的功能還是一樣,連接database,創建collection space,創建collection,插入兩種格式的6條記錄。
<span style="font-size:18px;">import java.util.ArrayList;
import java.util.List;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import com.sequoiadb.base.CollectionSpace;
import com.sequoiadb.base.DBCollection;
import com.sequoiadb.base.DBCursor;
import com.sequoiadb.base.Sequoiadb;
import com.sequoiadb.exception.BaseException;
public class BlogCollectionSpace {
static String CS_NAME = "test_cs";
static String CL_NAME = "test_cl";
public static void main(String[] args) {
String host = "192.168.20.46";
String port = "11810";
String usr = "admin";
String password = "admin";
Sequoiadb sdb = null;
try {
sdb = new Sequoiadb(host + ":" + port, usr, password);
} catch (BaseException e) {
e.printStackTrace();
System.exit(1);
}
if (sdb.isCollectionSpaceExist(CS_NAME)) {
sdb.dropCollectionSpace(CS_NAME);
}
CollectionSpace cs = sdb.createCollectionSpace(CS_NAME);
if (sdb.isCollectionSpaceExist(CS_NAME) && cs.getName() == CS_NAME) {
System.out.println("The CS " + CS_NAME + " is created");
} else {
System.exit(2);
}
DBCursor CSList = sdb.listCollectionSpaces();
while (CSList.hasNext()) {
String name = (String) CSList.getNext().get("Name");
System.out.println("Collection Space: " + name);
}
if (cs.isCollectionExist(CL_NAME)) {
cs.dropCollection(CL_NAME);
}
DBCollection cl = cs.createCollection(CL_NAME);
if (cs.isCollectionExist(CL_NAME) && cl.getName() == CL_NAME) {
System.out.println("The Collection " + CL_NAME + " is created");
} else {
System.exit(3);
}
System.out.println("There are " + cl.getCount() + " record(s) in the collection");
System.out.println("Inserting one record...");
insertOneRecord(cl);
System.out.println("There are " + cl.getCount() + " record(s) in the collection");
System.out.println("Inserting 5 records...");
insertSomeRecords(cl);
System.out.println("There are " + cl.getCount() + " record(s) in the collection");
System.out.println("Deleting one record...");
deleteOneRecord(cl);
System.out.println("There are " + cl.getCount() + " record(s) in the collection");
System.out.println("Deleting 5 records...");
deleteSomeRecords(cl);
System.out.println("There are " + cl.getCount() + " record(s) in the collection");
System.out.println("Inserting 6 records again...");
insertOneRecord(cl);
insertSomeRecords(cl);
System.out.println("There are " + cl.getCount() + " record(s) in the collection");
System.out.println("Querying all records with the value of 'Id' from 0 to 8...");
queryId(cl);
System.out.println("Updateing all the records, add 10 to all the 'Id'...");
updateId(cl);
System.out.println("Querying all records with the value of 'Id' from 0 to 8...");
queryId(cl);
}
static private void insertOneRecord(DBCollection cl){
BSONObject insertor = null;
insertor = new BasicBSONObject();
BSONObject phone = new BasicBSONObject();
insertor.put("Name", "foo");
insertor.put("Age", 10);
phone.put("home", "123456789");
phone.put("mobile", "987654321");
insertor.put("Phone", phone);
cl.insert(insertor);
}
static private void insertSomeRecords(DBCollection cl) {
List<BSONObject> list = null;
try {
list = new ArrayList<BSONObject>(5);
for (int i = 0; i < 5; i++) {
BSONObject obj = new BasicBSONObject();
BSONObject addressObj = new BasicBSONObject();
BSONObject phoneObj = new BasicBSONObject();
addressObj.put("city", "foo");
addressObj.put("province", "bar");
phoneObj.put("Type", "Office");
phoneObj.put("Number", "88888888");
obj.put("name", "test");
obj.put("Id", i);
obj.put("Phonenumber", phoneObj);
obj.put("Address", addressObj);
list.add(obj);
}
} catch (Exception e) {
System.out.println("Failed to create name list record.");
e.printStackTrace();
}
cl.bulkInsert(list, 1);
}
static private void deleteOneRecord(DBCollection cl) {
BSONObject matcher = new BasicBSONObject();
matcher.put("Name","foo");
cl.delete(matcher);
}
static private void deleteSomeRecords(DBCollection cl) {
BSONObject matcher = new BasicBSONObject();
matcher.put("name", "test");
cl.delete(matcher);
}
static private void queryId(DBCollection cl) {
BSONObject matcher = new BasicBSONObject();
BSONObject condition = new BasicBSONObject();
condition.put("$gte", 0);
condition.put("$lte", 8);
matcher.put("Id",condition);
DBCursor records = cl.query(matcher,null,null,null);
System.out.println("Query Results:");
int count = 0;
while(records.hasNext()){
count++;
BSONObject record = records.getNext();
System.out.println("Record with Id from 0 to 8:" + record.get("Id").toString());
}
System.out.println("There are " + count + " records in the result set");
}
static private void updateId(DBCollection cl) {
BSONObject matcher = new BasicBSONObject();
BSONObject modifier = new BasicBSONObject();
BSONObject condition = new BasicBSONObject();
BSONObject operation = new BasicBSONObject();
condition.put("$gte", 0);
condition.put("$lte", 8);
matcher.put("Id",condition);
operation.put("Id",10);
modifier.put("$inc", operation);
cl.update(matcher,modifier,null);
}
}
</span>
運行代碼,程序的輸出爲:
The CS test_cs is created
Collection Space: test_cs
The Collection test_cl is created
There are 0 record(s) in the collection
Inserting one record...
There are 1 record(s) in the collection
Inserting 5 records...
There are 6 record(s) in the collection
Deleting one record...
There are 5 record(s) in the collection
Deleting 5 records...
There are 0 record(s) in the collection
Inserting 6 records again...
There are 6 record(s) in the collection
Querying all records with the value of 'Id' from 0 to 8...
Query Results:
Record with Id from 0 to 8:0
Record with Id from 0 to 8:1
Record with Id from 0 to 8:2
Record with Id from 0 to 8:3
Record with Id from 0 to 8:4
There are 5 records in the result set
Updateing all the records, add 10 to all the 'Id'...
Querying all records with the value of 'Id' from 0 to 8...
Query Results:
There are 0 records in the result set
可以看出,上面的代碼先是創建了一個名爲test_cs的集合空間,一個名爲test_cl的集合,並分兩次在集合中分別插入了1條和5條記錄,總共6條記錄。
之後刪除了包含“Name“域的值爲”foo“的一條記錄。
然後又刪除了”Id“域的值爲0-8的5條記錄。
此時,集合裏所有的記錄都被刪除。
所以又分兩次重新插入了6條記錄。
查詢所有”Id“域的值爲0-8的記錄,返回5條記錄,Id的值分別爲0,1,2,3,4
將所有”Id”的值增加10,增加後的Id的值應爲10,11,12,13,14
再次查詢所有”Id“域的值爲0-8的記錄,返回0條記錄。