use of com.orientechnologies.orient.core.storage.OStorageProxy in project orientdb by orientechnologies.
the class ORidBagTest method testFromEmbeddedToSBTreeAndBack.
public void testFromEmbeddedToSBTreeAndBack() throws IOException {
OGlobalConfiguration.RID_BAG_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD.setValue(7);
OGlobalConfiguration.RID_BAG_SBTREEBONSAI_TO_EMBEDDED_THRESHOLD.setValue(-1);
if (database.getStorage() instanceof OStorageProxy) {
OServerAdmin server = new OServerAdmin(database.getURL()).connect("root", ODatabaseHelper.getServerRootPassword());
server.setGlobalConfiguration(OGlobalConfiguration.RID_BAG_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD, 7);
server.setGlobalConfiguration(OGlobalConfiguration.RID_BAG_SBTREEBONSAI_TO_EMBEDDED_THRESHOLD, -1);
server.close();
}
ORidBag ridBag = new ORidBag();
ODocument document = new ODocument();
document.field("ridBag", ridBag);
Assert.assertTrue(ridBag.isEmbedded());
document.save();
document.reload();
ridBag = document.field("ridBag");
Assert.assertTrue(ridBag.isEmbedded());
List<OIdentifiable> addedItems = new ArrayList<OIdentifiable>();
for (int i = 0; i < 6; i++) {
ODocument docToAdd = new ODocument();
docToAdd.save();
ridBag.add(docToAdd);
addedItems.add(docToAdd);
}
document.save();
document.reload();
ridBag = document.field("ridBag");
Assert.assertTrue(ridBag.isEmbedded());
ODocument docToAdd = new ODocument();
ridBag.add(docToAdd);
addedItems.add(docToAdd);
document.save();
Assert.assertTrue(!ridBag.isEmbedded());
List<OIdentifiable> addedItemsCopy = new ArrayList<OIdentifiable>(addedItems);
for (OIdentifiable id : ridBag) Assert.assertTrue(addedItems.remove(id));
Assert.assertTrue(addedItems.isEmpty());
document.reload();
ridBag = document.field("ridBag");
Assert.assertTrue(!ridBag.isEmbedded());
addedItems.addAll(addedItemsCopy);
for (OIdentifiable id : ridBag) Assert.assertTrue(addedItems.remove(id));
Assert.assertTrue(addedItems.isEmpty());
addedItems.addAll(addedItemsCopy);
for (int i = 0; i < 3; i++) ridBag.remove(addedItems.remove(i));
addedItemsCopy.clear();
addedItemsCopy.addAll(addedItems);
document.save();
Assert.assertTrue(!ridBag.isEmbedded());
for (OIdentifiable id : ridBag) Assert.assertTrue(addedItems.remove(id));
Assert.assertTrue(addedItems.isEmpty());
document.reload();
ridBag = document.field("ridBag");
Assert.assertTrue(!ridBag.isEmbedded());
addedItems.addAll(addedItemsCopy);
for (OIdentifiable id : ridBag) Assert.assertTrue(addedItems.remove(id));
Assert.assertTrue(addedItems.isEmpty());
}
use of com.orientechnologies.orient.core.storage.OStorageProxy in project orientdb by orientechnologies.
the class OMetadataDefault method init.
private void init(final boolean iLoad) {
final ODatabaseDocumentInternal database = getDatabase();
schemaClusterId = database.getClusterIdByName(CLUSTER_INTERNAL_NAME);
final AtomicBoolean schemaLoaded = new AtomicBoolean(false);
schema = new OSchemaProxy(database.getStorage().getResource(OSchema.class.getSimpleName(), new Callable<OSchemaShared>() {
public OSchemaShared call() {
ODatabaseDocumentInternal database = getDatabase();
final OSchemaShared instance = new OSchemaShared(database.getStorageVersions().classesAreDetectedByClusterId());
if (iLoad)
instance.load();
schemaLoaded.set(true);
return instance;
}
}), database);
indexManager = new OIndexManagerProxy(database.getStorage().getResource(OIndexManager.class.getSimpleName(), new Callable<OIndexManager>() {
public OIndexManager call() {
OIndexManager instance;
if (database.getStorage() instanceof OStorageProxy)
instance = new OIndexManagerRemote(database);
else
instance = new OIndexManagerShared(database);
if (iLoad)
try {
instance.load();
} catch (Exception e) {
OLogManager.instance().error(this, "[OMetadata] Error on loading index manager, reset index configuration", e);
instance.create();
}
return instance;
}
}), database);
security = new OSecurityProxy(database.getStorage().getResource(OSecurity.class.getSimpleName(), new Callable<OSecurity>() {
public OSecurity call() {
final OSecurity instance = OSecurityManager.instance().newSecurity();
if (iLoad) {
security = instance;
instance.load();
}
return instance;
}
}), database);
commandCache = database.getStorage().getResource(OCommandCache.class.getSimpleName(), new Callable<OCommandCache>() {
public OCommandCache call() {
return new OCommandCacheSoftRefs(database.getName());
}
});
final Class<? extends OSecurity> securityClass = (Class<? extends OSecurity>) database.getProperty(ODatabase.OPTIONS.SECURITY.toString());
if (securityClass != null)
// INSTALL CUSTOM WRAPPED SECURITY
try {
final OSecurity wrapped = security;
security = securityClass.getDeclaredConstructor(OSecurity.class, ODatabaseDocumentInternal.class).newInstance(wrapped, database);
} catch (Exception e) {
throw OException.wrapException(new OSecurityException("Cannot install custom security implementation (" + securityClass + ")"), e);
}
functionLibrary = new OFunctionLibraryProxy(database.getStorage().getResource(OFunctionLibrary.class.getSimpleName(), new Callable<OFunctionLibrary>() {
public OFunctionLibrary call() {
final OFunctionLibraryImpl instance = new OFunctionLibraryImpl();
if (iLoad && !(database.getStorage() instanceof OStorageProxy))
instance.load();
return instance;
}
}), database);
sequenceLibrary = new OSequenceLibraryProxy(database.getStorage().getResource(OSequenceLibrary.class.getSimpleName(), new Callable<OSequenceLibrary>() {
@Override
public OSequenceLibrary call() throws Exception {
final OSequenceLibraryImpl instance = new OSequenceLibraryImpl();
if (iLoad) {
instance.load();
}
return instance;
}
}), database);
scheduler = new OSchedulerProxy(database.getStorage().getResource(OScheduler.class.getSimpleName(), new Callable<OScheduler>() {
public OScheduler call() {
final OSchedulerImpl instance = new OSchedulerImpl();
if (iLoad && !(database.getStorage() instanceof OStorageProxy))
instance.load();
return instance;
}
}), database);
if (schemaLoaded.get())
schema.onPostIndexManagement();
}
use of com.orientechnologies.orient.core.storage.OStorageProxy in project orientdb by orientechnologies.
the class OSchemaShared method doCreateClass.
private OClass doCreateClass(final String className, int[] clusterIds, int retry, OClass... superClasses) throws ClusterIdsAreEmptyException {
OClass result;
final ODatabaseDocumentInternal db = getDatabase();
final OStorage storage = db.getStorage();
StringBuilder cmd = null;
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE);
if (superClasses != null)
OClassImpl.checkParametersConflict(Arrays.asList(superClasses));
acquireSchemaWriteLock();
try {
final String key = className.toLowerCase();
if (classes.containsKey(key) && retry == 0)
throw new OSchemaException("Class '" + className + "' already exists in current database");
if (!executeThroughDistributedStorage())
checkClustersAreAbsent(clusterIds);
if (clusterIds == null || clusterIds.length == 0) {
clusterIds = createClusters(className, getDatabase().getStorage().getConfiguration().getMinimumClusters());
}
cmd = new StringBuilder("create class ");
if (getDatabase().getStorage().getConfiguration().isStrictSql())
cmd.append('`');
cmd.append(className);
if (getDatabase().getStorage().getConfiguration().isStrictSql())
cmd.append('`');
List<OClass> superClassesList = new ArrayList<OClass>();
if (superClasses != null && superClasses.length > 0) {
boolean first = true;
for (OClass superClass : superClasses) {
// Filtering for null
if (superClass != null) {
if (first)
cmd.append(" extends ");
else
cmd.append(", ");
cmd.append('`').append(superClass.getName()).append('`');
first = false;
superClassesList.add(superClass);
}
}
}
if (clusterIds != null) {
if (clusterIds.length == 1 && clusterIds[0] == -1)
cmd.append(" abstract");
else {
cmd.append(" cluster ");
for (int i = 0; i < clusterIds.length; ++i) {
if (i > 0)
cmd.append(',');
else
cmd.append(' ');
cmd.append(clusterIds[i]);
}
}
}
if (executeThroughDistributedStorage()) {
createClassInternal(className, clusterIds, superClassesList);
final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage;
OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
commandSQL.addExcludedNode(autoshardedStorage.getNodeId());
final Object res = db.command(commandSQL).execute();
} else if (storage instanceof OStorageProxy) {
db.command(new OCommandSQL(cmd.toString())).execute();
reload();
} else
createClassInternal(className, clusterIds, superClassesList);
result = classes.get(className.toLowerCase());
// WAKE UP DB LIFECYCLE LISTENER
for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onCreateClass(getDatabase(), result);
} finally {
releaseSchemaWriteLock();
}
return result;
}
use of com.orientechnologies.orient.core.storage.OStorageProxy in project orientdb by orientechnologies.
the class OSecurityExternal method authenticate.
@Override
public OUser authenticate(final String iUsername, final String iUserPassword) {
OUser user = null;
final String dbName = getDatabase().getName();
if (!(getDatabase().getStorage() instanceof OStorageProxy)) {
if (Orient.instance().getSecurity() == null)
throw new OSecurityAccessException(dbName, "External Security System is null!");
// Uses the external authenticator.
// username is returned if authentication is successful, otherwise null.
String username = Orient.instance().getSecurity().authenticate(iUsername, iUserPassword);
if (username != null) {
user = getUser(username);
if (user == null)
throw new OSecurityAccessException(dbName, "User or password not valid for username: " + username + ", database: '" + dbName + "'");
if (user.getAccountStatus() != OSecurityUser.STATUSES.ACTIVE)
throw new OSecurityAccessException(dbName, "User '" + username + "' is not active");
} else {
// Will use the local database to authenticate.
if (Orient.instance().getSecurity().isDefaultAllowed()) {
user = super.authenticate(iUsername, iUserPassword);
} else {
// WAIT A BIT TO AVOID BRUTE FORCE
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
throw new OSecurityAccessException(dbName, "User or password not valid for username: " + iUsername + ", database: '" + dbName + "'");
}
}
}
return user;
}
use of com.orientechnologies.orient.core.storage.OStorageProxy in project orientdb by orientechnologies.
the class OPropertyImpl method setDescription.
@Override
public OPropertyImpl setDescription(final String iDescription) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final OStorage storage = database.getStorage();
if (storage instanceof OStorageProxy) {
final String cmd = String.format("alter property %s description %s", getFullNameQuoted(), quoteString(iDescription));
database.command(new OCommandSQL(cmd)).execute();
} else if (isDistributedCommand()) {
final String cmd = String.format("alter property %s description %s", getFullNameQuoted(), quoteString(iDescription));
final OCommandSQL commandSQL = new OCommandSQL(cmd);
commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
database.command(new OCommandSQL(cmd)).execute();
setDescriptionInternal(iDescription);
} else
setDescriptionInternal(iDescription);
} finally {
releaseSchemaWriteLock();
}
return this;
}
Aggregations