-
Notifications
You must be signed in to change notification settings - Fork 8
Indexing
on DB4O set Index on Field, not Property.
package com.db4odoc.performance;
import com.db4o.config.annotations.Indexed;
import org.joda.time.DateTime;
import java.util.Date;
public class Item {
@Indexed
private final String indexedString;
@Indexed
private final int indexNumber;
@Indexed
private final Date indexDate;
public Item(int number) {
this.indexedString = dataString(number);
this.indexNumber = number;
DateTime dt = new DateTime(number);
this.indexDate = dt.toDate();
}
public String getIndexedString() {
return indexedString;
}
public static String dataString(int number){
return "data for "+number;
}
public boolean complexMethod(){
return indexedString.split("for")[0].contains("5");
}
public int getIndexNumber() {
return indexNumber;
}
public Date getIndexDate() {
return indexDate;
}
}
//use final to value;
final String criteria = Item.dataString(rnd.nextInt(NUMBER_OF_ITEMS));
final List<Item> result = container.query(new Predicate<Item>() {
@Override
public boolean match(Item o) {
return o.getIndexedString().equals(criteria);
}
});
//dotn't Computing expression in query
//return o.getIndexedString().equals("data for " + number);
package com.db4odoc.indexing.check;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.ext.StoredClass;
import com.db4o.ext.StoredField;
public class CheckForAndIndex {
public static void main(String[] args) {
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().objectClass(IndexedClass.class).objectField("id").indexed(true);
ObjectContainer container = Db4oEmbedded.openFile(configuration, "database.db4o");
try {
container.store(new IndexedClass(1));
// #example: Check for a index
StoredClass metaInfo = container.ext().storedClass(IndexedClass.class);
// list a fields and check if they have a index
for (StoredField field : metaInfo.getStoredFields()) {
if(field.hasIndex()){
System.out.println("The field '"+field.getName()+"' is indexed");
} else{
System.out.println("The field '"+field.getName()+"' isn't indexed");
}
}
// #end example
} finally {
container.close();
}
}
static class IndexedClass {
private int id;
private String data;
public IndexedClass(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
}
package com.db4odoc.features.uniqueconstrain;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.constraints.UniqueFieldValueConstraint;
import com.db4o.constraints.UniqueFieldValueConstraintViolationException;
public class UniqueConstrainExample {
public static void main(String[] args) {
uniqueConstrainOnEmbedded();
}
private static void uniqueConstrainOnEmbedded() {
EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
// #example: Add the index the field and then the unique constrain
config.common().objectClass(UniqueId.class).objectField("id").indexed(true);
config.common().add(new UniqueFieldValueConstraint(UniqueId.class, "id"));
// #end example
ObjectContainer container = Db4oEmbedded.openFile(config, "database.db4o");
try {
container.store(new UniqueId(44));
// #example: Violating the constrain throws an exception
container.store(new UniqueId(42));
container.store(new UniqueId(42));
try {
container.commit();
} catch (UniqueFieldValueConstraintViolationException e) {
e.printStackTrace();
}
// #end example
} finally {
container.close();
}
}
private static class UniqueId {
private final int id;
private UniqueId(int id) {
this.id = id;
}
}
}
Unique constraints allow a user to define a field to be unique across all the objects of a particular class stored to db4o. This means that you cannot save an object where a previously committed object has the same field value for fields marked as unique. A Unique Constraint is checked at commit-time and a constraint violation will cause a UniqueFieldValueConstraintViolationException to be thrown. This functionality is based on Commit-Time Callbacks feature.
First you need to add an index on the field which should be unique. After that you add the UniqueFieldValueConstraint to the configuration for the unique field.
After that, the unique constrain is applied. When you commit a transaction the uniqueness of the field is checked. If there's any violation, the UniqueFieldValueConstraintViolationException will be thrown.
In client server mode you need to configure the unique constrains on the server side.