use of com.orientechnologies.orient.core.index.OIndex in project orientdb by orientechnologies.
the class LuceneInsertUpdateTest method testInsertUpdateWithIndex.
@Test
public void testInsertUpdateWithIndex() throws Exception {
OSchema schema = db.getMetadata().getSchema();
ODocument doc = new ODocument("City");
doc.field("name", "Rome");
db.save(doc);
OIndex idx = schema.getClass("City").getClassIndex("City.name");
Collection<?> coll = (Collection<?>) idx.get("Rome");
Assert.assertEquals(coll.size(), 1);
OIdentifiable next = (OIdentifiable) coll.iterator().next();
doc = db.load(next.<ORecord>getRecord());
Assert.assertEquals(doc.field("name"), "Rome");
doc.field("name", "London");
db.save(doc);
coll = (Collection<?>) idx.get("Rome");
Assert.assertEquals(coll.size(), 0);
coll = (Collection<?>) idx.get("London");
Assert.assertEquals(coll.size(), 1);
next = (OIdentifiable) coll.iterator().next();
doc = db.load(next.<ORecord>getRecord());
Assert.assertEquals(doc.field("name"), "London");
doc.field("name", "Berlin");
db.save(doc);
coll = (Collection<?>) idx.get("Rome");
Assert.assertEquals(coll.size(), 0);
coll = (Collection<?>) idx.get("London");
Assert.assertEquals(coll.size(), 0);
coll = (Collection<?>) idx.get("Berlin");
Assert.assertEquals(coll.size(), 1);
}
use of com.orientechnologies.orient.core.index.OIndex in project orientdb by orientechnologies.
the class LuceneInsertDeleteTest method testInsertUpdateWithIndex.
@Test
public void testInsertUpdateWithIndex() throws Exception {
db.getMetadata().reload();
OSchema schema = db.getMetadata().getSchema();
ODocument doc = new ODocument("City");
doc.field("name", "Rome");
db.save(doc);
OIndex idx = schema.getClass("City").getClassIndex("City.name");
Collection<?> coll = (Collection<?>) idx.get("Rome");
assertThat(coll).hasSize(1);
assertThat(idx.getSize()).isEqualTo(1);
OIdentifiable next = (OIdentifiable) coll.iterator().next();
doc = db.load(next.<ORecord>getRecord());
db.delete(doc);
coll = (Collection<?>) idx.get("Rome");
assertThat(coll).hasSize(0);
assertThat(idx.getSize()).isEqualTo(0);
}
use of com.orientechnologies.orient.core.index.OIndex in project orientdb by orientechnologies.
the class LuceneInsertUpdateSingleDocumentNoTxTest method testInsertUpdateTransactionWithIndex.
@Test
public void testInsertUpdateTransactionWithIndex() throws Exception {
db.close();
db.open("admin", "admin");
OSchema schema = db.getMetadata().getSchema();
schema.reload();
ODocument doc = new ODocument("City");
doc.field("name", "");
ODocument doc1 = new ODocument("City");
doc1.field("name", "");
doc = db.save(doc);
doc1 = db.save(doc1);
doc = db.load(doc);
doc1 = db.load(doc1);
doc.field("name", "Rome");
doc1.field("name", "Rome");
db.save(doc);
db.save(doc1);
OIndex idx = schema.getClass("City").getClassIndex("City.name");
Collection<?> coll = (Collection<?>) idx.get("Rome");
Assert.assertEquals(coll.size(), 2);
Assert.assertEquals(idx.getSize(), 2);
}
use of com.orientechnologies.orient.core.index.OIndex in project orientdb by orientechnologies.
the class OGraphImporterMTAPITest method main.
public static void main(String[] args) throws IOException, InterruptedException {
OGlobalConfiguration.ENVIRONMENT_LOCK_MANAGER_CONCURRENCY_LEVEL.setValue(64);
// String dbUrl = "memory:amazonReviews";
final String dbUrl = "plocal:/temp/databases/amazonReviews";
final File f = new File("/temp/databases/amazonReviews");
if (f.exists())
OFileUtils.deleteRecursively(f);
final OrientGraph roGraph = new OrientGraph(dbUrl, "admin", "admin");
final OrientGraphNoTx graph = new OrientGraphNoTx(dbUrl, "admin", "admin");
OrientVertexType user = graph.createVertexType("User", 64);
user.createProperty("uid", OType.STRING);
user.createIndex("User.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
OrientVertexType product = graph.createVertexType("Product", 64);
product.createProperty("uid", OType.STRING);
product.createIndex("Product.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
graph.createEdgeType("Reviewed");
final File file = new File("/Users/luca/Downloads/ratings_Books.csv");
final BufferedReader br = new BufferedReader(new FileReader(file));
final AtomicLong retry = new AtomicLong();
Orient.instance().scheduleTask(new TimerTask() {
@Override
public void run() {
roGraph.makeActive();
final long vertexCount = roGraph.countVertices();
final long edgeCount = roGraph.countEdges();
System.out.println(String.format("%d vertices=%d %d/sec edges=%d %d/sec retry=%d", row, vertexCount, ((vertexCount - lastVertexCount) * 1000 / 2000), edgeCount, ((edgeCount - lastEdgeCount) * 1000 / 2000), retry.get()));
lastVertexCount = vertexCount;
lastEdgeCount = edgeCount;
}
}, 2000, 2000);
final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10000);
final Thread[] threads = new Thread[parallel];
for (int i = 0; i < parallel; ++i) {
threads[i] = new Thread() {
@Override
public void run() {
final OrientGraph localGraph = new OrientGraph(dbUrl, "admin", "admin", false);
final OIndex<?> userIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("User.uid");
final OIndex<?> productIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("Product.uid");
localGraph.begin();
for (int i = 0; ; ++i) {
final String line;
try {
line = queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
final String[] parts = line.split(",");
if (parts.length != 4) {
// SKIP IT
System.out.print("Skipped invalid line " + i + ": " + line);
continue;
}
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("score", new Float(parts[2]).intValue());
properties.put("date", Long.parseLong(parts[3]));
for (int localRetry = 0; localRetry < 100; ++localRetry) {
try {
final Object k1 = userIndex.get(parts[0]);
OrientVertex v1;
if (k1 == null) {
v1 = localGraph.addVertex("class:User", "uid", parts[0]);
} else
v1 = localGraph.getVertex(k1);
final Object k2 = productIndex.get(parts[1]);
OrientVertex v2;
if (k2 == null) {
v2 = localGraph.addVertex("class:Product", "uid", parts[1]);
} else
v2 = localGraph.getVertex(k2);
final OrientEdge edge = localGraph.addEdge(null, v1, v2, "Reviewed");
edge.setProperties(properties);
if (i % 2 == 0) {
localGraph.commit();
localGraph.begin();
}
break;
} catch (ONeedRetryException e) {
// RETRY
retry.incrementAndGet();
} catch (ORecordDuplicatedException e) {
// RETRY
retry.incrementAndGet();
}
}
}
}
};
}
for (int i = 0; i < parallel; ++i) threads[i].start();
try {
for (String line; (line = br.readLine()) != null; ) {
row++;
queue.put(line);
}
} finally {
br.close();
}
for (int i = 0; i < parallel; ++i) threads[i].join();
graph.shutdown();
roGraph.shutdown();
}
use of com.orientechnologies.orient.core.index.OIndex in project orientdb by orientechnologies.
the class OrientJdbcDatabaseMetaData method getPrimaryKeys.
@Override
public ResultSet getPrimaryKeys(final String catalog, final String schema, final String table) throws SQLException {
database.activateOnCurrentThread();
final Set<OIndex<?>> classIndexes = database.getMetadata().getIndexManager().getClassIndexes(table);
final Set<OIndex<?>> uniqueIndexes = new HashSet<OIndex<?>>();
for (OIndex<?> oIndex : classIndexes) {
if (oIndex.getType().equals(INDEX_TYPE.UNIQUE.name()))
uniqueIndexes.add(oIndex);
}
final List<ODocument> records = new ArrayList<ODocument>();
for (OIndex<?> unique : uniqueIndexes) {
int keyFiledSeq = 1;
for (String keyFieldName : unique.getDefinition().getFields()) {
ODocument doc = new ODocument().field("TABLE_CAT", catalog).field("TABLE_SCHEM", catalog).field("TABLE_NAME", table).field("COLUMN_NAME", keyFieldName).field("KEY_SEQ", Integer.valueOf(keyFiledSeq), OType.INTEGER).field("PK_NAME", unique.getName());
keyFiledSeq++;
records.add(doc);
}
}
return new OrientJdbcResultSet(new OrientJdbcStatement(connection), records, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
Aggregations