protected void open(final Database db) throws DatabaseException { this.alreadySeen = db; this.value = new DatabaseEntry("".getBytes()); }
/** * Add a dummy 'cap' entry at the given insertion key. Prevents * 'seeks' to queue heads from holding lock on last item of * 'preceding' queue. See: * http://sourceforge.net/tracker/index.php?func=detail&aid=1262665&group_id=73833&atid=539102 * * @param origin key at which to insert the cap */ public void addCap(byte[] origin) { try { pendingUrisDB.put(null, new DatabaseEntry(origin), new DatabaseEntry(new byte[0])); } catch (DatabaseException e) { throw new RuntimeException(e); } }
@Override public void put(String key, Object value) throws FailStoreException { try { String valueString = JSON.toJSONString(value); @SuppressWarnings("unused") OperationStatus status = db.put(null, new DatabaseEntry(key.getBytes("UTF-8")), new DatabaseEntry(valueString.getBytes("UTF-8"))); } catch (Exception e) { throw new FailStoreException(e); } }
@Override public void put(String key, Object value) throws FailStoreException { try { String valueString = JSON.toJSONString(value); @SuppressWarnings("unused") OperationStatus status = db.put(null, new DatabaseEntry(key.getBytes("UTF-8")), new DatabaseEntry(valueString.getBytes("UTF-8"))); } catch (Exception e) { throw new FailStoreException(e); } }
public void addUrlAndDocId(String url, int docId) { synchronized (mutex) { if (docId <= lastDocID) { throw new IllegalArgumentException( "Requested doc id: " + docId + " is not larger than: " + lastDocID); } // Make sure that we have not already assigned a docid for this URL int prevDocid = getDocId(url); if (prevDocid > 0) { if (prevDocid == docId) { return; } throw new IllegalArgumentException("Doc id: " + prevDocid + " is already assigned to URL: " + url); } docIDsDB.put(null, new DatabaseEntry(url.getBytes()), new DatabaseEntry(Util.int2ByteArray(docId))); lastDocID = docId; } }
protected long deleteMatchingFromQueue(final WorkQueueFrontier frontier, final String match) throws IOException { try { final BdbMultipleWorkQueues queues = ((BdbFrontier) frontier) .getWorkQueues(); return queues.deleteMatchingFromQueue(match, classKey, new DatabaseEntry(origin)); } catch (DatabaseException e) { throw new IOException(e); } }
/** * @return the key to the first item in the database * @throws DatabaseException */ protected DatabaseEntry getFirstKey() throws DatabaseException { DatabaseEntry key = new DatabaseEntry(); DatabaseEntry value = new DatabaseEntry(); Cursor cursor = pendingUrisDB.openCursor(null,null); OperationStatus status = cursor.getNext(key,value,null); cursor.close(); if(status == OperationStatus.SUCCESS) { return key; } return null; }
public int getNewDocID(String url) { synchronized (mutex) { try { // Make sure that we have not already assigned a docid for this URL int docID = getDocId(url); if (docID > 0) { return docID; } ++lastDocID; docIDsDB.put(null, new DatabaseEntry(url.getBytes()), new DatabaseEntry(Util.int2ByteArray(lastDocID))); return lastDocID; } catch (RuntimeException e) { if (config.isHaltOnError()) { throw e; } else { logger.error("Exception thrown while getting new DocID", e); return -1; } } } }
public void setValue(String name, long value) { synchronized (mutex) { try { counterValues.put(name, value); if (statisticsDB != null) { Transaction txn = env.beginTransaction(null, null); statisticsDB.put(txn, new DatabaseEntry(name.getBytes()), new DatabaseEntry(Util.long2ByteArray(value))); txn.commit(); } } catch (RuntimeException e) { if (config.isHaltOnError()) { throw e; } else { logger.error("Exception setting value", e); } } } }
@Override public void delete(String key) throws FailStoreException { try { DatabaseEntry delKey = new DatabaseEntry(); delKey.setData(key.getBytes("UTF-8")); @SuppressWarnings("unused") OperationStatus status = db.delete(null, delKey); } catch (Exception e) { throw new FailStoreException(e); } }
@Override public void delete(String key) throws FailStoreException { try { DatabaseEntry delKey = new DatabaseEntry(); delKey.setData(key.getBytes("UTF-8")); @SuppressWarnings("unused") OperationStatus status = db.delete(null, delKey); } catch (Exception e) { throw new FailStoreException(e); } }
public Object call() throws Exception { db.put(null, new DatabaseEntry(Integer.toString(fi * increment + fj).getBytes()), new DatabaseEntry(Integer.toString(fi * increment + fj).getBytes())); return null; } }));
public Object call() throws Exception { int value = rand.nextInt((fi + 1) * increment); return db.get(null, new DatabaseEntry(Integer.toString(value).getBytes()), new DatabaseEntry(Integer.toString(value).getBytes()), null); } }));
protected boolean setRemove(CharSequence uri) { DatabaseEntry key = new DatabaseEntry(); LongBinding.longToEntry(createKey(uri), key); OperationStatus status = null; try { status = alreadySeen.delete(null, key); } catch (DatabaseException e) { logger.severe(e.getMessage()); } if (status == OperationStatus.SUCCESS) { count.decrementAndGet(); return true; // removed } else { return false; // not present } }
public List<WebURL> get(int max) { synchronized (mutex) { List<WebURL> results = new ArrayList<>(max); DatabaseEntry key = new DatabaseEntry(); DatabaseEntry value = new DatabaseEntry(); Transaction txn = beginTransaction(); try (Cursor cursor = openCursor(txn)) { OperationStatus result = cursor.getFirst(key, value, null); int matches = 0; while ((matches < max) && (result == OperationStatus.SUCCESS)) { if (value.getData().length > 0) { results.add(webURLBinding.entryToObject(value)); matches++; } result = cursor.getNext(key, value, null); } } commit(txn); return results; } }
public void delete(int count) { synchronized (mutex) { DatabaseEntry key = new DatabaseEntry(); DatabaseEntry value = new DatabaseEntry(); Transaction txn = beginTransaction(); try (Cursor cursor = openCursor(txn)) { OperationStatus result = cursor.getFirst(key, value, null); int matches = 0; while ((matches < count) && (result == OperationStatus.SUCCESS)) { cursor.delete(); matches++; result = cursor.getNext(key, value, null); } } commit(txn); } }
@Override public StaticBuffer get(StaticBuffer key, StoreTransaction txh) throws BackendException { Transaction tx = getTransaction(txh); try { DatabaseEntry dbkey = key.as(ENTRY_FACTORY); DatabaseEntry data = new DatabaseEntry(); log.trace("db={}, op=get, tx={}", name, txh); OperationStatus status = db.get(tx, dbkey, data, getLockMode(txh)); if (status == OperationStatus.SUCCESS) { return getBuffer(data); } else { return null; } } catch (DatabaseException e) { throw new PermanentBackendException(e); } }
protected static DatabaseEntry getDatabaseEntryKey(WebURL url) { byte[] keyData = new byte[6]; keyData[0] = url.getPriority(); keyData[1] = ((url.getDepth() > Byte.MAX_VALUE) ? Byte.MAX_VALUE : (byte) url.getDepth()); Util.putIntInByteArray(url.getDocid(), keyData, 2); return new DatabaseEntry(keyData); }
public void put(WebURL url) { DatabaseEntry value = new DatabaseEntry(); webURLBinding.objectToEntry(url, value); Transaction txn = beginTransaction(); urlsDB.put(txn, getDatabaseEntryKey(url), value); commit(txn); }
public boolean removeURL(WebURL webUrl) { synchronized (mutex) { DatabaseEntry key = getDatabaseEntryKey(webUrl); DatabaseEntry value = new DatabaseEntry(); Transaction txn = beginTransaction(); try (Cursor cursor = openCursor(txn)) { OperationStatus result = cursor.getSearchKey(key, value, null); if (result == OperationStatus.SUCCESS) { result = cursor.delete(); if (result == OperationStatus.SUCCESS) { return true; } } } finally { commit(txn); } } return false; } }