Java源码示例:org.fusesource.lmdbjni.Entry

示例1
public Count valuesOfKey(final Key key, final long cost, final boolean isEstimateOk)
{
	index.checkOpen();
	if (cost == 0)
		return null;
	else
	{
		Ref<Long> counter = new Ref<Long>() {
		public Long get() {
			Database db = index.keyBucket(key) == 0 ? index.db : index.db2;
			try (Cursor cursor = db.openCursor(index.txn().getDbTransaction())) 
			{
				byte[] keyAsBytes = index.keyConverter.toByteArray(key);
				Entry entry = cursor.get(CursorOp.SET, keyAsBytes);
				if (entry != null)
					return cursor.count();
				else
					return 0l;
			}
			catch (LMDBException ex)
			{
				throw new HGException(ex);
			}
		}};
		return new Count(counter, false);
	}		
}
 
示例2
public Count keysWithValue(final Value value, final long cost, final boolean isEstimateOk)
{
	index.checkOpen();
	if (index instanceof DefaultBiIndexImpl)
		return null;
	if (cost == 0)
		return null;
	else
	{
		final DefaultBiIndexImpl<Key, Value> bindex = (DefaultBiIndexImpl<Key, Value>)index;
		Ref<Long> counter = new Ref<Long>() {
		public Long get() {				
			try (SecondaryCursor cursor = bindex.secondaryDb.openSecondaryCursor(index.txn().getDbTransaction())) 
			{
				byte [] valueAsBytes = bindex.valueConverter.toByteArray(value);
				Entry entry = cursor.get(CursorOp.SET, valueAsBytes);
				if (entry != null)
					return cursor.count();
				else
					return 0l;
			}
			catch (LMDBException ex)
			{
				throw new HGException(ex);
			}
		}};
		return new Count(counter, false);
	}		
	
}
 
示例3
@Override
@SuppressWarnings("unchecked")
public HGRandomAccessResult<HGPersistentHandle> getIncidenceResultSet(HGPersistentHandle handle)
{
	if (handle == null)
		throw new NullPointerException("HGStore.getIncidenceSet called with a null handle.");

	Cursor cursor = null;
	try
	{
		byte[] key = handle.toByteArray();
		Entry entry;
		TransactionLmdbImpl tx = txn();
		cursor = incidence_db.openCursor(tx.getDbTransaction());
		entry = cursor.get(CursorOp.SET, key);

		if (entry == null)
		{
			cursor.close();
			return (HGRandomAccessResult<HGPersistentHandle>) HGSearchResult.EMPTY;
		}
		else
			return new SingleKeyResultSet<HGPersistentHandle>(tx.attachCursor(cursor), new DatabaseEntry(entry.getKey()),
					BAtoHandle.getInstance(handleFactory));
	}
	catch (Throwable ex)
	{
		if (cursor != null)
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
		throw new HGException("Failed to retrieve incidence set for handle " + handle + ": " + ex.toString(), ex);
	}
}
 
示例4
@Override
public long getIncidenceSetCardinality(HGPersistentHandle handle)
{
	if (handle == null)
		throw new NullPointerException("HGStore.getIncidenceSetCardinality called with a null handle.");

	Cursor cursor = null;
	try
	{
		byte[] key = handle.toByteArray();
		cursor = incidence_db.openCursor(txn().getDbTransaction());
		Entry entry = cursor.get(CursorOp.SET, key);
		if (entry == null)
			return 0;
		else
			return cursor.count();
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve incidence set for handle " + handle + ": " + ex.toString(), ex);
	}
	finally
	{
		try
		{
			cursor.close();
		}
		catch (Throwable t)
		{
		}
	}
}
 
示例5
public HGPersistentHandle getLastKeyPrimitive()
{
	Cursor cursor = null;
	try
	{
		cursor = primitive_db.openCursor(txn().getDbTransaction());
		Entry entry = cursor.get(CursorOp.LAST);
		if (entry == null)
			return null;
		else
		{
			DatabaseEntry key = new DatabaseEntry(entry.getKey());
			return linkBinding.entryToObject(key)[0];
		}
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve last key for data db: " + ex.toString(), ex);
	}
	finally
	{
		try
		{
			cursor.close();
		}
		catch (Throwable t)
		{
		}
	}
}
 
示例6
public HGPersistentHandle getLastKeyDataDb()
{
	Cursor cursor = null;
	try
	{
		cursor = data_db.openCursor(txn().getDbTransaction());
		Entry entry = cursor.get(CursorOp.LAST);
		if (entry == null)
			return null;
		else
		{
			DatabaseEntry key = new DatabaseEntry(entry.getKey());
			return linkBinding.entryToObject(key)[0];
		}
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve last key for data db: " + ex.toString(), ex);
	}
	finally
	{
		try
		{
			cursor.close();
		}
		catch (Throwable t)
		{
		}
	}
}
 
示例7
public KeyType findFirstByValue(ValueType value)
{
	if (!isOpen())
		throw new HGException("Attempting to lookup by value index '" + name
				+ "' while it is closed.");
	/*
	 * if (value == null) throw new HGException(
	 * "Attempting to lookup by value index '" + name +
	 * "' with a null value.");
	 */
	byte[] key = valueConverter.toByteArray(value);
	KeyType result = null;
	SecondaryCursor cursor = null;
	try
	{
		cursor = secondaryDb.openSecondaryCursor(txn().getDbTransaction());
		Entry entry = cursor.get(CursorOp.SET, key);
		if (entry != null)
			result = keyConverter.fromByteArray(entry.getKey(), 0,
					entry.getKey().length);
	}
	catch (Exception ex)
	{
		throw new HGException(
				"Failed to lookup index '" + name + "': " + ex.toString(),
				ex);
	}
	finally
	{
		if (cursor != null)
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
	}
	return result;
}
 
示例8
public long countKeys(ValueType value)
{
	byte[] key = valueConverter.toByteArray(value);
	SecondaryCursor cursor = null;
	try
	{
		cursor = secondaryDb.openSecondaryCursor(txn().getDbTransaction());
		Entry entry = cursor.get(CursorOp.SET, key);
		if (entry != null)
			return cursor.count();
		else
			return 0;
	}
	catch (LMDBException ex)
	{
		throw new HGException(ex);
	}
	finally
	{
		if (cursor != null)
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
	}
}
 
示例9
public void addAll(Collection<Map.Entry<String, Sha256Hash> > lst)
{
  Transaction tx = env.createTransaction();
  byte[] b=new byte[1];
  for(Map.Entry<String, Sha256Hash> me : lst)
  {
    String k = me.getKey() +"/" + me.getValue().toString();
    db.put(tx, k.getBytes(), b);
  }
  tx.commit();
}
 
示例10
public Set<Sha256Hash> getSet(String key, int max_results)
{
  String k = key + "/";
  Transaction tx = env.createTransaction(true);

  EntryIterator i = db.seek(tx, k.getBytes());

  Set<Sha256Hash> out = new TreeSet<Sha256Hash>();
  int count = 0;

  while(i.hasNext())
  {
    Entry e = i.next();
    String s = new String(e.getKey());
    if (s.startsWith(k))
    {
      String h = s.substring(k.length());
      out.add(new Sha256Hash(h));
      count++;
      if (count > max_results) throw new DBTooManyResultsException();
    }
  }
  tx.abort();

  return out;

}
 
示例11
public void removeEntry(KeyType keyType, ValueType value)
{
	checkOpen();
	if (keyType == null)
	{
		// System.out.println("Keytype is null for:" + getName());
		return;
	}

	DatabaseEntry keyEntry = new DatabaseEntry(
			keyConverter.toByteArray(keyType));
	DatabaseEntry valueEntry = new DatabaseEntry(
			valueConverter.toByteArray(value));
	Cursor cursor = null;

	try
	{
		switch (keyBucket(keyType))
		{
		case 0:
			cursor = db.openCursor(txn().getDbTransaction());
			break;

		default:
			cursor = db2.openCursor(txn().getDbTransaction());
			break;
		}

		Entry entry = null;

		if (sort_duplicates)
		{
			entry = cursor.get(CursorOp.GET_BOTH, keyEntry.getData(),
					valueEntry.getData());
			if (entry != null)
				cursor.delete();
		}
		else
		{
			OperationStatus status = cursor.get(CursorOp.GET_CURRENT,
					keyEntry, valueEntry);
			if (status == OperationStatus.SUCCESS)
				cursor.delete();
		}
	}
	catch (Exception ex)
	{
		throw new HGException(
				"Failed to lookup index '" + name + "': " + ex.toString(),
				ex);
	}
	finally
	{
		if (cursor != null)
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
	}
}
 
示例12
/**
 * <p>
 * Find the last entry, assuming ordered duplicates, corresponding to the
 * given key.
 * </p>
 * 
 * @param keyType
 *            The key whose last entry is sought.
 * @return The last (i.e. greatest, i.e. maximum) data value for that key or
 *         null if the set of entries for the key is empty.
 */
public ValueType findLast(KeyType keyType)
{
	checkOpen();
	byte[] key = keyConverter.toByteArray(keyType);
	ValueType result = null;
	Cursor cursor = null;
	try
	{
		switch (keyBucket(keyType))
		{
		case 0:
			cursor = db.openCursor(txn().getDbTransaction());
			break;

		default:
			cursor = db2.openCursor(txn().getDbTransaction());
			break;
		}

		Entry entry = cursor.get(CursorOp.LAST, key);
		if (entry != null)
			result = valueConverter.fromByteArray(entry.getValue(), 0,
					entry.getValue().length);
	}
	catch (Exception ex)
	{
		throw new HGException(
				"Failed to lookup index '" + name + "': " + ex.toString(),
				ex);
	}
	finally
	{
		if (cursor != null)
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
	}
	return result;
}
 
示例13
/**
 * Optimized _linear_search_ for the best N documents by cosine similarity.
 * Be warned: This will be slow.
 */
public List<Passage> query(Question question) {
	// Convert the question to a vector.
	float[] query_vector = DenseVectors.mean(
			question.memo(Phrase.simpleTokens)
			.stream().map(DenseVectors::vectorFor)
			.filter(v -> v.isPresent())
			.map(v -> v.get())
			.collect(Collectors.toList()));
	
	// Now look for (almost) that vector!
	// This is a little ugly because we desperately avoid copying.
	byte[][] winners = new byte[LEN][];
	double[] sims = new double[LEN];
	/*try (Transaction tx = wiki_vectors_env.createReadTransaction();
			Database doc_vectors = wiki_vectors_env.openDatabase(tx, "wiki-vectors", 0);
			BufferCursor cursor = doc_vectors.bufferCursor(tx)) {
		cursor.first();
		while (cursor.next()) {
			double this_sim = sim(query_vector, cursor);
			bubble(sims, winners, this_sim, cursor);
		}
	}*/
	try (Transaction tx = wiki_vectors_env.createReadTransaction();
			Database doc_vectors = wiki_vectors_env.openDatabase(tx, "wiki-vectors", 0)) {
		for (Entry e : doc_vectors.iterate(tx).iterable()) {
			double this_sim = DenseVectors.sim(query_vector, KV.asVector(e.getValue()));
			if (Double.isFinite(this_sim))
				bubble(sims, winners, this_sim, e.getKey(), K);
		}
			
	}
	
	// Now get the passages for the top entries.
	List<Passage> passages = new ArrayList<>();
	for (int i=0; i<K; i++) {
		if (winners[i] != null) {
			String id = string(winners[i]);
			passages.add(new Passage("meandv", "", "", id));
			System.out.println("value is : " + id + " sim: " + sims[i]);
		}
	}
	
	/*try{
		Process p = Runtime.getRuntime().exec("python /home/sean/yeshvant/top100vectorSimilarDocs.py " + query );
		BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line = "";
		while((line = in.readLine())!= null)
		{
			String[] sim_id = line.split(" "); 
			passages.add(new Passage("meandv", "", "", sim_id[1]));
			System.out.println("value is : "+sim_id[1]);
		}
	} catch(Exception e) {
		e.printStackTrace();
	}*/
	return fillFromSources(passages);
}