Created
March 19, 2013 06:44
-
-
Save xiaodongw/5194160 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.orientdb.test; | |
| import static org.junit.Assert.*; | |
| import java.io.File; | |
| import java.net.URI; | |
| import java.net.URISyntaxException; | |
| import java.util.Date; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.UUID; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; | |
| import com.orientechnologies.orient.core.db.record.OIdentifiable; | |
| import com.orientechnologies.orient.core.index.OIndex; | |
| import com.orientechnologies.orient.core.metadata.schema.OClass; | |
| import com.orientechnologies.orient.core.metadata.schema.OType; | |
| import com.orientechnologies.orient.core.query.nativ.ONativeSynchQuery; | |
| import com.orientechnologies.orient.core.query.nativ.OQueryContextNative; | |
| import com.orientechnologies.orient.core.record.impl.ODocument; | |
| import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; | |
| public class OrientDbTest { | |
| ODatabaseDocumentTx db; | |
| File dir; | |
| @Before | |
| public void setUp() throws URISyntaxException { | |
| if(dir == null) { | |
| String curDir = new java.io.File(UUID.randomUUID().toString()).getAbsolutePath(); | |
| dir = new File(curDir); | |
| } | |
| URI uri = dir.toURI(); | |
| String path = uri.toString(); | |
| path = path.replace("file:/", "local:"); | |
| db = new ODatabaseDocumentTx(path); | |
| if (!db.exists()) { | |
| db.create(); | |
| //db.getMetadata().getIndexManager().cr | |
| OClass doc = db.getMetadata().getSchema().createClass("Document"); | |
| doc.createProperty("id", OType.STRING).createIndex(OClass.INDEX_TYPE.UNIQUE); | |
| doc.createProperty("birthday", OType.DATE); | |
| //doc.createIndex("Document.id", OClass.INDEX_TYPE.UNIQUE, "id"); | |
| db.getMetadata().getSchema().save(); | |
| } | |
| else | |
| db.open("admin", "admin"); | |
| } | |
| @After | |
| public void tearDown() { | |
| db.close(); | |
| db = null; | |
| dir.delete(); | |
| dir = null; | |
| } | |
| private Map<String, Object> newObj() { | |
| Map<String, Object> obj = new HashMap<String, Object>(); | |
| obj.put("id", UUID.randomUUID().toString()); | |
| obj.put("name", "Alfred"); | |
| obj.put("sex", "male"); | |
| obj.put("age", 30); | |
| obj.put("birthday", new Date()); | |
| return obj; | |
| } | |
| private void wrap(Map<String, Object> obj, ODocument doc) { | |
| // ODocument doc = db.newInstance(); | |
| for (Map.Entry<String, Object> entry : obj.entrySet()) { | |
| doc.field(entry.getKey(), entry.getValue()); | |
| } | |
| } | |
| private Map<String, Object> unwrap(ODocument doc) { | |
| Map<String, Object> obj = new HashMap<String, Object>(); | |
| for (Map.Entry<String, Object> entry : doc) { | |
| obj.put(entry.getKey(), entry.getValue()); | |
| } | |
| return obj; | |
| } | |
| @Test | |
| public void testBasicOps() { | |
| Map<String, Object> obj = newObj(); | |
| ODocument doc = db.newInstance("Document"); | |
| wrap(obj, doc); | |
| doc.save(); | |
| } | |
| @Test | |
| public void testPerformance() throws URISyntaxException { | |
| long start = System.currentTimeMillis(); | |
| final int NUM = 10000; | |
| Map<String, Object>[] objs = new Map[NUM]; | |
| final String[] ids = new String[NUM]; | |
| OIndex index = db.getMetadata().getIndexManager().getIndex("Document.id"); | |
| for (int i = 0; i < NUM; i++) { | |
| Map<String, Object> obj = newObj(); | |
| //ODocument doc = new ODocument("Document"); | |
| ODocument doc = db.newInstance("Document"); | |
| //doc.setClassName("Document"); | |
| wrap(obj, doc); | |
| doc.save(); | |
| //index.put(obj.get("id"), doc); | |
| ids[i] = (String) obj.get("id"); | |
| objs[i] = obj; | |
| } | |
| long end = System.currentTimeMillis(); | |
| System.out.println("Time used for writing: " + (end - start)); | |
| db.close(); | |
| setUp(); | |
| start = System.currentTimeMillis(); | |
| /*for (ODocument doc : db.browseClass("Document")) { | |
| System.out.println( doc.field("id") ); | |
| }*/ | |
| for (int i = 0; i < NUM; i++) { | |
| //final int a = i; | |
| List<ODocument> result = db.query( | |
| new OSQLSynchQuery<ODocument>("select id, name from Document where id = '"+ids[i] + "'")); | |
| /*List<ODocument> result = db | |
| .query(new ONativeSynchQuery<OQueryContextNative>(db, | |
| "Document", new OQueryContextNative()) { | |
| @Override | |
| public boolean filter(OQueryContextNative context) { | |
| return context.field("id").eq(ids[a]).go(); | |
| } | |
| });*/ | |
| assertTrue(result.size() == 1); | |
| ODocument doc = result.get(0); | |
| //OIdentifiable id = (OIdentifiable) index.get(ids[i]); | |
| //ODocument doc = db.getRecord(id); | |
| Map<String, Object> obj = unwrap(doc); | |
| assertEquals(obj.get("id"), objs[i].get("id")); | |
| } | |
| end = System.currentTimeMillis(); | |
| System.out.println("Time used for reading: " + (end - start)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment