Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.LinkedList;
- import org.dizitart.no2.Document;
- import org.dizitart.no2.Filter;
- import org.dizitart.no2.IndexOptions;
- import org.dizitart.no2.IndexType;
- import org.dizitart.no2.Nitrite;
- import org.dizitart.no2.NitriteCollection;
- import org.dizitart.no2.filters.Filters;
- class CollectionAccessor implements Iterable<byte[]> {
- static final String SUBJECT = "subject";
- static final String PROPERTY = "property";
- static final String OBJECT = "object";
- static final String CONTAINMENT = "containment";
- private final Nitrite db;
- private final NitriteCollection collection;
- public CollectionAccessor(Nitrite db, NitriteCollection collection) {
- Filters.and(Filters.ALL, Filters.ALL);
- this.db = db;
- this.collection = collection;
- if (!collection.hasIndex(SUBJECT)) {
- IndexOptions indexOptions = IndexOptions.indexOptions(IndexType.NonUnique);
- collection.createIndex(SUBJECT, indexOptions);
- collection.createIndex(PROPERTY, indexOptions);
- collection.createIndex(OBJECT, indexOptions);
- }
- }
- public void put(byte[] content) {
- // Put will only be called from SPO multimap
- collection.insert(byteArray2Document(content, AccessingStyle.SPO));
- }
- private Document byteArray2Document(byte[] array, AccessingStyle accessingStyle) {
- Document doc = new Document();
- if (array.length == 0) {
- return doc;
- }
- String[] order = new String[0];
- switch (accessingStyle) {
- case SPO:
- order = new String[] { SUBJECT, PROPERTY, OBJECT };
- break;
- case POS:
- order = new String[] { PROPERTY, OBJECT, SUBJECT };
- break;
- case OSP:
- order = new String[] { OBJECT, SUBJECT, PROPERTY };
- break;
- }
- long value1 = NumberConversion.bytes2long(array, 0);
- doc.put(order[0], value1);
- if (array.length == (1 * Long.BYTES)) {
- return doc;
- }
- long value2 = NumberConversion.bytes2long(array, 1 * Long.BYTES);
- doc.put(order[1], value2);
- if (array.length == (2 * Long.BYTES)) {
- return doc;
- }
- long value3 = NumberConversion.bytes2long(array, 2 * Long.BYTES);
- doc.put(order[2], value3);
- if (array.length == (3 * Long.BYTES)) {
- return doc;
- }
- byte[] containment = new byte[array.length - (3 * Long.BYTES)];
- System.arraycopy(array, 3 * Long.BYTES, containment, 0, containment.length);
- return doc.put(CONTAINMENT, containment);
- }
- public Iterator<byte[]> iterator(AccessingStyle accessingStyle) {
- return new IteratorWrapper(new byte[0], collection.find().iterator(), accessingStyle);
- }
- @Override
- public Iterator<byte[]> iterator() {
- return iterator(AccessingStyle.SPO);
- }
- public void flush() {
- if (db.hasUnsavedChanges()) {
- db.commit();
- // Necessary?
- // collection.rebuildIndex(SUBJECT, false);
- // collection.rebuildIndex(PROPERTY, false);
- // collection.rebuildIndex(OBJECT, false);
- }
- }
- public void close() {
- if ((collection != null) && !collection.isClosed()) {
- collection.close();
- }
- db.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment