use of com.sleepycat.je.DatabaseEntry in project jvarkit by lindenb.
the class PubmedOrcidGraph method scanArticle.
private void scanArticle(final String rootName, final XMLEventReader r, int depth) throws XMLStreamException, IOException {
final Article article = new Article();
boolean PubDate = false;
final List<Author> authors = new ArrayList<>();
while (r.hasNext()) {
final XMLEvent evt = r.nextEvent();
if (evt.isStartElement()) {
final StartElement start = evt.asStartElement();
final String eltName = start.getName().getLocalPart();
if (article.pmid == null && eltName.equals("PMID")) {
article.pmid = r.getElementText();
} else if (article.ISOAbbreviation == null && eltName.equals("ISOAbbreviation")) {
article.ISOAbbreviation = r.getElementText();
} else if (article.ArticleTitle == null && eltName.equals("ArticleTitle")) {
article.ArticleTitle = this.textContent(r);
} else if (eltName.equals("PubDate")) {
PubDate = true;
} else if (article.doi == null && eltName.equals("ArticleId")) {
final Attribute idType = start.getAttributeByName(new QName("IdType"));
if (idType != null && idType.getValue().equalsIgnoreCase("doi")) {
article.doi = r.getElementText().trim();
}
} else if (article.Year == null && PubDate && eltName.equals("Year")) {
article.Year = r.getElementText();
} else if (eltName.equals("Author")) {
final Author author = parseAuthor(r, article.pmid, depth + 1);
if (author != null && author.orcid != null) {
authors.add(author);
}
}
} else if (evt.isEndElement()) {
final EndElement end = evt.asEndElement();
final String eltName = end.getName().getLocalPart();
if (eltName.equals("PubDate")) {
PubDate = false;
}
if (eltName.equals(rootName))
break;
}
}
if (authors.isEmpty()) {
// do nothing
return;
}
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
Collections.sort(authors);
for (int x = 0; x + 1 < authors.size(); ++x) {
for (int y = x + 1; y < authors.size(); ++y) {
Link L = null;
final String orcid1 = authors.get(x).orcid;
final String orcid2 = authors.get(y).orcid;
if (this.all_links_between_authors) {
LongBinding.longToEntry(++ID_GENERATOR, key);
} else {
StringBinding.stringToEntry(orcid1 + "~" + orcid2, key);
if (this.linkDatabase.get(txn, key, data, LockMode.DEFAULT) != OperationStatus.NOTFOUND) {
L = this.linkBinding.entryToObject(data);
}
}
if (L == null) {
L = new Link();
L.orcid1 = orcid1;
L.orcid2 = orcid2;
}
L.pmids.add(article.pmid);
this.linkBinding.objectToEntry(L, data);
if (this.linkDatabase.put(txn, key, data) != OperationStatus.SUCCESS) {
throw new JvarkitException.BerkeleyDbError("Cannot put in article db");
}
}
}
// for comparing names
final Collator collator = Collator.getInstance(Locale.US);
collator.setStrength(Collator.PRIMARY);
StringBinding.stringToEntry(article.pmid, key);
if (this.articleDatabase.get(txn, key, data, LockMode.DEFAULT) != OperationStatus.NOTFOUND) {
LOG.debug("Article already in database : " + article.pmid);
} else {
LOG.debug("inserting article " + article.pmid);
this.articleBinding.objectToEntry(article, data);
if (this.articleDatabase.put(txn, key, data) != OperationStatus.SUCCESS) {
throw new JvarkitException.BerkeleyDbError("Cannot put in article db");
}
for (final Author au : authors) {
StringBinding.stringToEntry(au.orcid, key);
if (this.authorDatabase.get(txn, key, data, LockMode.DEFAULT) != OperationStatus.NOTFOUND) {
LOG.debug("Author already in database : " + au.orcid);
final Author other = this.authorBinding.entryToObject(data);
if (!StringUtil.isBlank(other.lastName) && !StringUtil.isBlank(au.lastName) && collator.compare(au.lastName, other.lastName) != 0) {
this.errPrintWriter.println("Conflict\t" + au.orcid + "\t" + au.foreName + "\t" + other.foreName);
}
} else {
this.authorBinding.objectToEntry(au, data);
if (this.authorDatabase.put(txn, key, data) != OperationStatus.SUCCESS) {
throw new JvarkitException.BerkeleyDbError("Cannot put in author db");
}
}
}
}
}
use of com.sleepycat.je.DatabaseEntry in project jvarkit by lindenb.
the class PubmedOrcidGraph method dumpGexf.
private void dumpGexf() {
final XMLOutputFactory xof = XMLOutputFactory.newFactory();
PrintWriter pw = null;
XMLStreamWriter w = null;
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
Cursor c = null;
try {
pw = openFileOrStdoutAsPrintWriter(this.outputFile);
w = xof.createXMLStreamWriter(pw);
w.writeStartDocument("UTF-8", "1.0");
w.writeStartElement("gexf");
w.writeAttribute("xmlns", GexfConstants.XMLNS);
w.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
w.writeAttribute("xsi:schemaLocation", GexfConstants.XSI_SCHEMA_LOCATION);
w.writeAttribute("version", GexfConstants.VERSION);
/* meta */
w.writeStartElement("meta");
w.writeAttribute("lastmodifieddate", "2017-04-03");
w.writeStartElement("creator");
w.writeCharacters("PumedOrcidGraph");
w.writeEndElement();
w.writeStartElement("description");
w.writeCharacters("PumedOrcidGraph");
w.writeEndElement();
w.writeEndElement();
/* graph */
w.writeStartElement("graph");
w.writeAttribute("mode", "static");
w.writeAttribute("defaultedgetype", "undirected");
/* attributes */
w.writeStartElement("attributes");
w.writeAttribute("class", "node");
w.writeAttribute("mode", "static");
gexfAttDecl(w, "orcid", "string");
gexfAttDecl(w, "foreName", "string");
gexfAttDecl(w, "lastName", "string");
gexfAttDecl(w, "initials", "string");
gexfAttDecl(w, "affiliation", "string");
// attributes
w.writeEndElement();
if (!this.hide_edge_attributes) {
w.writeStartElement("attributes");
w.writeAttribute("class", "edge");
w.writeAttribute("mode", "static");
gexfAttDecl(w, "pmid", "string");
gexfAttDecl(w, "title", "string");
gexfAttDecl(w, "doi", "string");
gexfAttDecl(w, "year", "string");
gexfAttDecl(w, "journal", "string");
// attributes
w.writeEndElement();
}
/* nodes */
w.writeStartElement("nodes");
c = this.authorDatabase.openCursor(txn, null);
while (c.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
final Author au = this.authorBinding.entryToObject(data);
if (NAME_NOT_FOUND.equals(au.foreName)) {
w.writeComment("Orcid " + au.orcid + " not found in pubmed");
continue;
}
au.gexf(w);
}
c.close();
// nodes
w.writeEndElement();
w.writeStartElement("edges");
key = new DatabaseEntry();
c = this.linkDatabase.openCursor(txn, null);
while (c.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
final Link L = this.linkBinding.entryToObject(data);
w.writeStartElement("edge");
w.writeAttribute("id", "E" + (++ID_GENERATOR));
w.writeAttribute("type", "undirected");
w.writeAttribute("weight", String.valueOf(L.pmids.size()));
w.writeAttribute("source", L.orcid1);
w.writeAttribute("target", L.orcid2);
for (String pmid : L.pmids) {
DatabaseEntry key2 = new DatabaseEntry();
DatabaseEntry data2 = new DatabaseEntry();
StringBinding.stringToEntry(pmid, key2);
if (this.articleDatabase.get(txn, key2, data2, LockMode.DEFAULT) != OperationStatus.SUCCESS) {
throw new JvarkitException.BerkeleyDbError("cannot get article");
}
final Article article = this.articleBinding.entryToObject(data2);
if (this.all_links_between_authors) {
w.writeAttribute("label", String.valueOf(L.pmids.size()));
} else {
w.writeAttribute("label", "" + article.ArticleTitle + ". " + article.ISOAbbreviation + ". (" + article.Year + ")");
}
if (!this.hide_edge_attributes) {
w.writeStartElement("attributes");
w.writeEmptyElement("attribute");
w.writeAttribute("for", "pmid");
w.writeAttribute("value", article.pmid);
w.writeEmptyElement("attribute");
w.writeAttribute("for", "title");
w.writeAttribute("value", article.ArticleTitle);
w.writeEmptyElement("attribute");
w.writeAttribute("for", "doi");
w.writeAttribute("value", article.doi);
w.writeEmptyElement("attribute");
w.writeAttribute("for", "year");
w.writeAttribute("value", article.Year);
w.writeEmptyElement("attribute");
w.writeAttribute("for", "journal");
w.writeAttribute("value", article.ISOAbbreviation);
// attributes
w.writeEndElement();
}
break;
}
//
// edge
w.writeEndElement();
}
c.close();
// edges
w.writeEndElement();
w.writeEndElement();
w.writeEndDocument();
w.flush();
pw.flush();
pw.close();
pw = null;
} catch (Exception err) {
throw new RuntimeIOException(err);
} finally {
CloserUtil.close(w);
CloserUtil.close(pw);
}
}
use of com.sleepycat.je.DatabaseEntry in project jvarkit by lindenb.
the class PubmedOrcidGraph method getAuthorByOrcid.
private Author getAuthorByOrcid(final String orcid) {
final DatabaseEntry key = new DatabaseEntry();
final DatabaseEntry data = new DatabaseEntry();
StringBinding.stringToEntry(orcid, key);
if (this.authorDatabase.get(txn, key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
return this.authorBinding.entryToObject(data);
} else {
return null;
}
}
use of com.sleepycat.je.DatabaseEntry in project bboxdb by jnidzwetzki.
the class BDBTupleStore method readTuple.
@Override
public Tuple readTuple(final String key) throws IOException {
final DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes());
final DatabaseEntry value = new DatabaseEntry();
Transaction txn = null;
if (USE_TRANSACTIONS) {
txn = environment.beginTransaction(null, null);
}
final OperationStatus result = database.get(null, keyEntry, value, LockMode.DEFAULT);
if (result != OperationStatus.SUCCESS) {
throw new RuntimeException("Data fetch got status " + result + " for " + key);
}
if (txn != null) {
txn.commit();
}
final ByteBuffer byteBuffer = ByteBuffer.wrap(value.getData());
return TupleHelper.decodeTuple(byteBuffer);
}
use of com.sleepycat.je.DatabaseEntry in project voldemort by voldemort.
the class BdbStorageEngine method getAndLock.
@Override
public KeyLockHandle<byte[]> getAndLock(ByteArray key) {
long startTimeNs = -1;
if (logger.isTraceEnabled())
startTimeNs = System.nanoTime();
StoreUtils.assertValidKey(key);
DatabaseEntry keyEntry = new DatabaseEntry(key.get());
DatabaseEntry valueEntry = new DatabaseEntry();
Transaction transaction = null;
List<Versioned<byte[]>> vals = null;
KeyLockHandle<byte[]> handle;
try {
transaction = environment.beginTransaction(null, null);
// do a get for the existing values
OperationStatus status = getBdbDatabase().get(transaction, keyEntry, valueEntry, LockMode.RMW);
if (OperationStatus.SUCCESS == status) {
vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
} else {
vals = new ArrayList<Versioned<byte[]>>(0);
}
handle = new KeyLockHandle<byte[]>(vals, transaction);
} catch (DatabaseException e) {
this.bdbEnvironmentStats.reportException(e);
// Unless we return out properly from this method, we need to ensure
// the transaction handle is closed on exception..
attemptAbort(transaction);
logger.error("Error in getAndLock for store " + this.getName(), e);
throw new PersistenceFailureException(e);
} finally {
if (logger.isTraceEnabled()) {
logger.trace("Completed getAndLock (" + getName() + ") to key " + key + " (keyRef: " + System.identityHashCode(key) + " in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
}
}
return handle;
}
Aggregations