When you create a new record specifying its Class, OrientDB automatically selects the Class where to store the physical record, by using configurable strategies.
The available strategies are:
defaultClusterId
property. This was the default before 1.7To create your custom strategy follow the following steps:
The class must implements interface OClusterSelectionStrategy. Example:
package mypackage;
public class RandomSelectionStrategy implements OClusterSelectionStrategy {
public int getCluster(final OClass iClass, final ODocument doc) {
final int[] clusters = iClass.getClusterIds();
// RETURN A RANDOM CLUSTER ID IN THE LIST
return new Random().nextInt(clusters.length);
}
public String getName(){ return "random"; }
}
Note that the method getCluster()
receives also the ODocument to insert. This is useful if you want to assign the clusterId based on the Document content.
Create a new file under META-INF/services called com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionStrategy
and write your class with full package.
Example of the content:
mypackage.RandomSelectionStrategy
This is the default content in OrientDB core is:
com.orientechnologies.orient.core.metadata.schema.clusterselection.ORoundRobinClusterSelectionStrategy
com.orientechnologies.orient.core.metadata.schema.clusterselection.ODefaultClusterSelectionStrategy
com.orientechnologies.orient.core.metadata.schema.clusterselection.OBalancedClusterSelectionStrategy
To assign your new strategy to a class, use the ALTER CLASS command. Example:
ALTER CLASS Employee CLUSTERSELECTION random