Skip to content

Instantly share code, notes, and snippets.

@adithyank
Last active August 29, 2015 14:14
Show Gist options
  • Save adithyank/7494a2a6ed3f0bb01ca8 to your computer and use it in GitHub Desktop.
Save adithyank/7494a2a6ed3f0bb01ca8 to your computer and use it in GitHub Desktop.
package adi.orientdb.eval;
import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexManagerProxy;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
/**
*
* @author K Adithyan
*/
public class IndexDeleteNotWorking
{
static final String URL = "plocal:/tmp/plocaldb";
static final String DATATYPE = "docnode";
static final String KEYFIELD = "name";
private static ODatabaseDocumentTx db()
{
System.out.println("starting.....");
try (ODatabaseDocumentTx base = new ODatabaseDocumentTx(URL))
{
if (!base.exists())
{
System.out.println("creating....");
base.create();
base.getMetadata().getSecurity().createUser("root", "root", "admin");
}
base.close();
}
OPartitionedDatabasePool pool = new OPartitionedDatabasePool(URL, "root", "root");
ODatabaseDocumentTx db = pool.acquire();
createIndexIfNeeded(db);
final OIndexManagerProxy im = db.getMetadata().getIndexManager();
OIndex index = im.getIndex(indexName());
System.out.println("index = " + index.getConfiguration().toJSON());
System.out.println("------------------------------");
index.delete();
System.out.println("index deleted");
System.out.println("------------------------------");
boolean existsIndex = im.existsIndex(indexName());
System.out.println("existsIndex after deleting index = " + existsIndex);
System.out.println("------------------------------");
createIndexIfNeeded(db);
return db;
}
private static void createIndexIfNeeded(ODatabaseDocumentTx db)
{
OClass dataTypeClass;
if (!db.getMetadata().getSchema().existsClass(DATATYPE))
{
dataTypeClass = db.getMetadata().getSchema().createClass(DATATYPE);
System.out.println("created class = " + dataTypeClass);
}
else
{
System.out.println("class exists");
dataTypeClass = db.getMetadata().getSchema().getClass(DATATYPE);
}
OIndexManagerProxy im = db.getMetadata().getIndexManager();
boolean existsIndex = im.existsIndex(indexName());
System.out.println("existsIndex before creation = " + existsIndex);
if (!existsIndex)
{
dataTypeClass.createProperty(KEYFIELD, OType.STRING);
dataTypeClass.createIndex(indexName(), OClass.INDEX_TYPE.NOTUNIQUE_HASH_INDEX, KEYFIELD);
System.out.println("index created");
}
}
private static String indexName()
{
return DATATYPE + "." + KEYFIELD;
}
public static void main(String[] args) throws Exception
{
db();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment