D00py

CollectionAccessor.java

Dec 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4.  
  5. import org.dizitart.no2.Document;
  6. import org.dizitart.no2.Filter;
  7. import org.dizitart.no2.IndexOptions;
  8. import org.dizitart.no2.IndexType;
  9. import org.dizitart.no2.Nitrite;
  10. import org.dizitart.no2.NitriteCollection;
  11. import org.dizitart.no2.filters.Filters;
  12.  
  13. class CollectionAccessor implements Iterable<byte[]> {
  14.  
  15.     static final String SUBJECT = "subject";
  16.     static final String PROPERTY = "property";
  17.     static final String OBJECT = "object";
  18.     static final String CONTAINMENT = "containment";
  19.  
  20.     private final Nitrite db;
  21.  
  22.     private final NitriteCollection collection;
  23.  
  24.     public CollectionAccessor(Nitrite db, NitriteCollection collection) {
  25.         Filters.and(Filters.ALL, Filters.ALL);
  26.         this.db = db;
  27.         this.collection = collection;
  28.         if (!collection.hasIndex(SUBJECT)) {
  29.             IndexOptions indexOptions = IndexOptions.indexOptions(IndexType.NonUnique);
  30.             collection.createIndex(SUBJECT, indexOptions);
  31.             collection.createIndex(PROPERTY, indexOptions);
  32.             collection.createIndex(OBJECT, indexOptions);
  33.         }
  34.     }
  35.  
  36.     public void put(byte[] content) {
  37.         // Put will only be called from SPO multimap
  38.         collection.insert(byteArray2Document(content, AccessingStyle.SPO));
  39.     }
  40.  
  41.     private Document byteArray2Document(byte[] array, AccessingStyle accessingStyle) {
  42.         Document doc = new Document();
  43.         if (array.length == 0) {
  44.             return doc;
  45.         }
  46.         String[] order = new String[0];
  47.         switch (accessingStyle) {
  48.         case SPO:
  49.             order = new String[] { SUBJECT, PROPERTY, OBJECT };
  50.             break;
  51.         case POS:
  52.             order = new String[] { PROPERTY, OBJECT, SUBJECT };
  53.             break;
  54.         case OSP:
  55.             order = new String[] { OBJECT, SUBJECT, PROPERTY };
  56.             break;
  57.         }
  58.         long value1 = NumberConversion.bytes2long(array, 0);
  59.         doc.put(order[0], value1);
  60.         if (array.length == (1 * Long.BYTES)) {
  61.             return doc;
  62.         }
  63.         long value2 = NumberConversion.bytes2long(array, 1 * Long.BYTES);
  64.         doc.put(order[1], value2);
  65.         if (array.length == (2 * Long.BYTES)) {
  66.             return doc;
  67.         }
  68.         long value3 = NumberConversion.bytes2long(array, 2 * Long.BYTES);
  69.         doc.put(order[2], value3);
  70.         if (array.length == (3 * Long.BYTES)) {
  71.             return doc;
  72.         }
  73.         byte[] containment = new byte[array.length - (3 * Long.BYTES)];
  74.         System.arraycopy(array, 3 * Long.BYTES, containment, 0, containment.length);
  75.         return doc.put(CONTAINMENT, containment);
  76.     }
  77.  
  78.     public Iterator<byte[]> iterator(AccessingStyle accessingStyle) {
  79.         return new IteratorWrapper(new byte[0], collection.find().iterator(), accessingStyle);
  80.     }
  81.  
  82.     @Override
  83.     public Iterator<byte[]> iterator() {
  84.         return iterator(AccessingStyle.SPO);
  85.     }
  86.  
  87.     public void flush() {
  88.         if (db.hasUnsavedChanges()) {
  89.             db.commit();
  90.             // Necessary?
  91.             // collection.rebuildIndex(SUBJECT, false);
  92.             // collection.rebuildIndex(PROPERTY, false);
  93.             // collection.rebuildIndex(OBJECT, false);
  94.         }
  95.     }
  96.  
  97.     public void close() {
  98.         if ((collection != null) && !collection.isClosed()) {
  99.             collection.close();
  100.         }
  101.         db.close();
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment