@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try (ODatabaseDocumentTx db = databasePool.acquire()) { final ODocument document = new ODocument(CLASS); for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { document.field(entry.getKey(), entry.getValue()); } document.save(); final ODictionary<ORecord> dictionary = db.getMetadata().getIndexManager().getDictionary(); dictionary.put(key, document); return Status.OK; } catch (Exception e) { e.printStackTrace(); } return Status.ERROR; }
@Override public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) { try (ODatabaseDocumentTx db = databasePool.acquire()) { final ODictionary<ORecord> dictionary = db.getMetadata().getIndexManager().getDictionary(); final ODocument document = dictionary.get(key); if (document != null) { if (fields != null) { for (String field : fields) { result.put(field, new StringByteIterator((String) document.field(field))); } } else { for (String field : document.fieldNames()) { result.put(field, new StringByteIterator((String) document.field(field))); } } return Status.OK; } } catch (Exception e) { e.printStackTrace(); } return Status.ERROR; }
@Override public Status update(String table, String key, Map<String, ByteIterator> values) { while (true) { try (ODatabaseDocumentTx db = databasePool.acquire()) { final ODictionary<ORecord> dictionary = db.getMetadata().getIndexManager().getDictionary(); final ODocument document = dictionary.get(key); if (document != null) { for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { document.field(entry.getKey(), entry.getValue()); } document.save(); return Status.OK; } } catch (OConcurrentModificationException cme) { continue; } catch (Exception e) { e.printStackTrace(); return Status.ERROR; } } }
ODocument doc = new ODocument(documentType); doc.fromMap(fileContents); boolean cached = fileContents.get(String.valueOf(DocumentAttributes.CACHED)) != null ? Boolean.valueOf((String) fileContents.get(String.valueOf(DocumentAttributes.CACHED))) : true; doc.field(String.valueOf(DocumentAttributes.CACHED), cached); doc.save(); } else { LOGGER.warn("{} has an invalid header, it has been ignored!", sourceFile);
public void build() { try { if ( ! hasSourceUri() ) { this.withRandomSourceUri(); } if ( ! hasSha1() ) { this.withRandomSha1(); } if ( ! hasDate() ) { this.withCurrentDate(); } ODocument document = new ODocument(type).fromMap(fileModel); document.save(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
/** * Get a document by sourceUri and update it from the given map. * @param incomingDocMap The document's db columns. * @return The saved document. * @throws IllegalArgumentException if sourceUri or docType are null, or if the document doesn't exist. */ public ODocument mergeDocument(Map<String, ? extends Object> incomingDocMap) { String sourceUri = (String) incomingDocMap.get(DocumentAttributes.SOURCE_URI.toString()); if (null == sourceUri) throw new IllegalArgumentException("Document sourceUri is null."); String docType = (String) incomingDocMap.get(Crawler.Attributes.TYPE); if (null == docType) throw new IllegalArgumentException("Document docType is null."); // Get a document by sourceUri String sql = "SELECT * FROM " + docType + " WHERE sourceuri=?"; activateOnCurrentThread(); List<ODocument> results = db.command(new OSQLSynchQuery<ODocument>(sql)).execute(sourceUri); if (results.size() == 0) throw new JBakeException("No document with sourceUri '"+sourceUri+"'."); // Update it from the given map. ODocument incomingDoc = new ODocument(docType); incomingDoc.fromMap(incomingDocMap); ODocument merged = results.get(0).merge(incomingDoc, true, false); return merged; }
public OEdgeDocument delete() { OEdgeDelegate.deleteLinks(this); super.delete(); return this; }
@Override public <RET extends ORecord> RET fromJSON(String iJson) { if (element == null) { promoteToRegularEdge(); } element.fromJSON(iJson); return (RET) this; }
@Override public OVertexDocument delete() { deleteLinks(this); super.delete(); return this; }
@Override public OVertex delete() { deleteLinks(this); element.delete(); return this; }
@Override public Object executeTransform(ODatabaseDocument db, final Object input) { ODocument doc = (ODocument) input; doc.setClassName(vertexClass); final OVertex v = new OVertexDelegate(doc); return v; } }
protected void clearDocumentTracking(final ORecord record) { if (record instanceof ODocument && ((ODocument) record).isTrackingChanges()) { ODocumentInternal.clearTrackData((ODocument) record); } }
@Override public <RET extends ORecord> RET copy() { return (RET) new OVertexDelegate(element.copy()); }
@Override public OEdge copy() { if (element != null) { return new OEdgeDelegate(element.copy()); } else { return new OEdgeDelegate(vOut, vIn, lightweightEdgeType, lightwightEdgeLabel); } }
@Override public void setProperty(String name, Object value) { if (element == null) { promoteToRegularEdge(); } element.setProperty(name, value); }
public static Map<String, Object> documentToModel(ODocument doc) { Map<String, Object> result = new HashMap<>(); Iterator<Map.Entry<String, Object>> fieldIterator = doc.iterator(); while (fieldIterator.hasNext()) { Map.Entry<String, Object> entry = fieldIterator.next(); result.put(entry.getKey(), entry.getValue()); } return result; }
map.put(field, new StringByteIterator((String) document.field(field))); for (String field : document.fieldNames()) { map.put(field, new StringByteIterator((String) document.field(field)));
@Test public void testMergeDocument() { final String uri = "test/testMergeDocument"; ODocument doc = new ODocument(DOC_TYPE_POST); Map<String, String> values = new HashMap(); values.put(Crawler.Attributes.TYPE, DOC_TYPE_POST); values.put(DocumentAttributes.SOURCE_URI.toString(), uri); values.put("foo", "originalValue"); doc.fromMap(values); doc.save(); // 1st values.put("foo", "newValue"); db.mergeDocument(values); DocumentList docs = db.getDocumentByUri(DOC_TYPE_POST, uri); assertEquals(1, docs.size()); assertEquals("newValue", docs.get(0).get("foo")); // 2nd values.put("foo", "anotherValue"); db.mergeDocument(values); docs = db.getDocumentByUri(DOC_TYPE_POST, uri); assertEquals(1, docs.size()); assertEquals("anotherValue", docs.get(0).get("foo")); db.deleteContent(DOC_TYPE_POST, uri); docs = db.getDocumentByUri(DOC_TYPE_POST, uri); assertEquals(0, docs.size()); }
public OEdge delete() { deleteLinks(this); if (element != null) { element.delete(); } return this; }
@Override public void setProperty(String name, Object value, OType... fieldType) { if (element == null) { promoteToRegularEdge(); } element.setProperty(name, value, fieldType); }