com.sleepycat.bind.tuple.TupleBinding Java Examples

The following examples show how to use com.sleepycat.bind.tuple.TupleBinding. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BdbTripleStoreTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Before
public void makeCollection() throws Exception {
  databaseCreator = new BdbNonPersistentEnvironmentCreator();
  tripleStore = new BdbTripleStore(
    databaseCreator.getDatabase(
      "userId",
      "dataSetId",
      "rdfData",
      true,
      TupleBinding.getPrimitiveBinding(String.class),
      TupleBinding.getPrimitiveBinding(String.class),
      new StringStringIsCleanHandler()
    )
  );
  Thread.sleep(2000); // to make the test work on slow systems
}
 
Example #2
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link LatLng} object with the geolocation of the vector with the given internal id or null
 * if the internal id does not exist. Accesses the BDB store!
 * 
 * @param iid
 *            The internal id of the vector
 * @return The geolocation mapped to the given internal id or null if the internal id does not exist
 */
public LatLng getGeolocation(int iid) {
	if (iid < 0 || iid > loadCounter) {
		System.out.println("Internal id " + iid + " is out of range!");
		return null;
	}
	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(iid, key);
	DatabaseEntry data = new DatabaseEntry();
	if ((iidToGeolocationDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
		TupleInput input = TupleBinding.entryToInput(data);
		double latitude = input.readDouble();
		double longitude = input.readDouble();
		LatLng geolocation = new LatLng(latitude, longitude);
		return geolocation;
	} else {
		System.out.println("Internal id " + iid + " is in range but gelocation was not found.");
		return null;
	}
}
 
Example #3
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * This method is used to set the geolocation of a previously indexed vector. If the geolocation is
 * already set, this method replaces it.
 * 
 * @param iid
 *            The internal id of the vector
 * @param latitude
 * @param longitude
 * @return true if geolocation is successfully set, false otherwise
 */
public boolean setGeolocation(int iid, double latitude, double longitude) {
	if (iid < 0 || iid > loadCounter) {
		System.out.println("Internal id " + iid + " is out of range!");
		return false;
	}
	DatabaseEntry key = new DatabaseEntry();
	DatabaseEntry data = new DatabaseEntry();

	IntegerBinding.intToEntry(iid, key);
	TupleOutput output = new TupleOutput();
	output.writeDouble(latitude);
	output.writeDouble(longitude);
	TupleBinding.outputToEntry(output, data);

	if (iidToGeolocationDB.put(null, key, data) == OperationStatus.SUCCESS) {
		return true;
	} else {
		return false;
	}
}
 
Example #4
Source File: IVFPQ.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * This is a utility method that can be used to dump the contents of the iidToIvfpqDB to a txt file.<br>
 * Currently, only the list id of each item is dumped.
 * 
 * @param dumpFilename
 *            Full path to the file where the dump will be written.
 * @throws Exception
 */
public void dumpIidToIvfpqDB(String dumpFilename) throws Exception {
	DatabaseEntry foundKey = new DatabaseEntry();
	DatabaseEntry foundData = new DatabaseEntry();

	ForwardCursor cursor = iidToIvfpqDB.openCursor(null, null);
	BufferedWriter out = new BufferedWriter(new FileWriter(new File(dumpFilename)));
	while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
		int iid = IntegerBinding.entryToInt(foundKey);
		TupleInput input = TupleBinding.entryToInput(foundData);
		int listId = input.readInt();
		out.write(iid + " " + listId + "\n");
	}
	cursor.close();
	out.close();
}
 
Example #5
Source File: BdbWrapper.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
public List<String> dump(String prefix, int start, int count, LockMode lockMode) {
  EntryBinding<String> binder = TupleBinding.getPrimitiveBinding(String.class);
  DatabaseEntry key = new DatabaseEntry();
  binder.objectToEntry(prefix, key);
  DatabaseEntry value = new DatabaseEntry();

  Cursor cursor = database.openCursor(null, null);
  OperationStatus status = cursor.getSearchKeyRange(key, value, LockMode.READ_UNCOMMITTED);
  List<String> result = new ArrayList<>();
  int index = 0;
  while (status == OperationStatus.SUCCESS && index < (start + count)) {
    if (index >= start) {
      result.add(
        binder.entryToObject(key) + " -> " + binder.entryToObject(value)
      );
    }
    index++;
    status = cursor.getNext(key, value, LockMode.READ_UNCOMMITTED);
  }
  cursor.close();
  return result;
}
 
Example #6
Source File: IVFPQ.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the pq code of the image with the given id.
 * 
 * @param id
 * @return
 * @throws Exception
 */
public byte[] getPQCodeByte(String id) throws Exception {
	int iid = getInternalId(id);
	if (iid == -1) {
		throw new Exception("Id does not exist!");
	}
	if (numProductCentroids > 256) {
		throw new Exception("Call the short variant of the method!");
	}

	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(iid, key);
	DatabaseEntry data = new DatabaseEntry();
	if ((iidToIvfpqDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
		TupleInput input = TupleBinding.entryToInput(data);
		input.readInt(); // skip the list id
		byte[] code = new byte[numSubVectors];
		for (int i = 0; i < numSubVectors; i++) {
			code[i] = input.readByte();
		}
		return code;
	} else {
		throw new Exception("Id does not exist!");
	}
}
 
Example #7
Source File: IVFPQ.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the pq code of the image with the given id.
 * 
 * @param id
 * @return
 * @throws Exception
 */
public short[] getPQCodeShort(String id) throws Exception {
	int iid = getInternalId(id);
	if (iid == -1) {
		throw new Exception("Id does not exist!");
	}
	if (numProductCentroids <= 256) {
		throw new Exception("Call the short variant of the method!");
	}

	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(iid, key);
	DatabaseEntry data = new DatabaseEntry();
	if ((iidToIvfpqDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
		TupleInput input = TupleBinding.entryToInput(data);
		input.readInt(); // skip the list id
		short[] code = new short[numSubVectors];
		for (int i = 0; i < numSubVectors; i++) {
			code[i] = input.readShort();
		}
		return code;
	} else {
		throw new Exception("Id does not exist!");
	}
}
 
Example #8
Source File: IVFPQ.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the inverted list of the image with the given id.
 * 
 * @param id
 * @return
 * @throws Exception
 */
public int getInvertedListId(String id) throws Exception {
	int iid = getInternalId(id);
	if (iid == -1) {
		throw new Exception("Id does not exist!");
	}

	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(iid, key);
	DatabaseEntry data = new DatabaseEntry();
	if ((iidToIvfpqDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
		TupleInput input = TupleBinding.entryToInput(data);
		int listId = input.readInt();
		return listId;
	} else {
		throw new Exception("Id does not exist!");
	}
}
 
Example #9
Source File: PQ.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Appends the persistent index with the given (byte) code.
 * 
 * @param code
 *            The code
 */
private void appendPersistentIndex(byte[] code) {
	// write id and code
	TupleOutput output = new TupleOutput();
	for (int i = 0; i < numSubVectors; i++) {
		output.writeByte(code[i]);
	}
	DatabaseEntry data = new DatabaseEntry();
	TupleBinding.outputToEntry(output, data);
	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(loadCounter, key);
	iidToPqDB.put(null, key, data);
}
 
Example #10
Source File: MigrateTruePatchStoresTaskTest.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  creator = new BdbNonPersistentEnvironmentCreator();
  creator.start();

  databaseCreator = version -> creator.getDatabase(
      "user",
      "dataSet",
      "truePatch" + version,
      true,
      TupleBinding.getPrimitiveBinding(String.class),
      TupleBinding.getPrimitiveBinding(String.class),
      new StringStringIsCleanHandler()
  );

  UpdatedPerPatchStore updatedPerPatchStore = mock(UpdatedPerPatchStore.class);
  when(updatedPerPatchStore.getVersions()).thenReturn(IntStream.of(0, 1, 2).boxed());

  BdbTruePatchStore truePatchStore = new BdbTruePatchStore(databaseCreator, updatedPerPatchStore);

  DataSetRepository dataSetRepository = mock(DataSetRepository.class);
  DataSet dataSet = mock(DataSet.class);
  DataSetMetaData dataSetMetaData = mock(DataSetMetaData.class);

  when(dataSetRepository.getDataSets()).thenReturn(Collections.singletonList(dataSet));
  when(dataSet.getMetadata()).thenReturn(dataSetMetaData);
  when(dataSet.getTruePatchStore()).thenReturn(truePatchStore);
  when(dataSetMetaData.getCombinedId()).thenReturn("user__dataSet");

  instance = new MigrateTruePatchStoresTask(dataSetRepository);
}
 
Example #11
Source File: Linear.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the vector which was assigned the given internal id or null if the internal id does not exist.
 * The vector is taken either from the ram-based (if loadIndexInMemory is true) or from the disk-based
 * index.
 * 
 * @param iid
 *            The internal id of the vector
 * @return The vector with the given internal id or null if the internal id does not exist
 */
public double[] getVector(int iid) {
	if (iid < 0 || iid > loadCounter) {
		System.out.println("Internal id " + iid + " is out of range!");
		return null;
	}
	double[] vector = new double[vectorLength];
	if (loadIndexInMemory) {
		for (int i = 0; i < vectorLength; i++) {
			vector[i] = vectorsList.getQuick(iid * vectorLength + i);
		}
	} else {
		// get the vector from the BDB structure
		DatabaseEntry key = new DatabaseEntry();
		IntegerBinding.intToEntry(iid, key);
		DatabaseEntry foundData = new DatabaseEntry();
		if (iidToVectorDB.get(null, key, foundData, null) == OperationStatus.SUCCESS) {
			TupleInput input = TupleBinding.entryToInput(foundData);
			for (int i = 0; i < vectorLength; i++) {
				vector[i] = input.readDouble();
			}
		} else {
			System.out.println("Internal id " + iid + " is in range but vector was not found..");
			System.out.println("Index is probably corrupted");
			System.exit(0);
			return null;
		}
	}
	return vector;
}
 
Example #12
Source File: Linear.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Appends the persistent index with the given vector.
 * 
 * @param vector
 *            The vector
 */
private void appendPersistentIndex(double[] vector) {
	TupleOutput output = new TupleOutput();
	for (int i = 0; i < vectorLength; i++) {
		output.writeDouble(vector[i]);
	}
	DatabaseEntry data = new DatabaseEntry();
	TupleBinding.outputToEntry(output, data);
	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(loadCounter, key);
	iidToVectorDB.put(null, key, data);
}
 
Example #13
Source File: Linear.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the persistent index in memory.
 * 
 * @throws Exception
 */
private void loadIndexInMemory() throws Exception {
	long start = System.currentTimeMillis();
	System.out.println("Loading persistent index in memory.");

	DatabaseEntry foundKey = new DatabaseEntry();
	DatabaseEntry foundData = new DatabaseEntry();

	ForwardCursor cursor = null;
	if (useDiskOrderedCursor) { // disk ordered cursor
		DiskOrderedCursorConfig docc = new DiskOrderedCursorConfig();
		cursor = iidToVectorDB.openCursor(docc);
	} else {
		cursor = iidToVectorDB.openCursor(null, null);
	}

	int counter = 0;
	while (cursor.getNext(foundKey, foundData, null) == OperationStatus.SUCCESS
			&& counter < maxNumVectors) {
		TupleInput input = TupleBinding.entryToInput(foundData);
		double[] vector = new double[vectorLength];
		for (int i = 0; i < vectorLength; i++) {
			vector[i] = input.readDouble();
		}
		// update ram based index
		vectorsList.add(vector);
		counter++;
		if (counter % 1000 == 0) {
			System.out.println(counter + " vectors loaded in memory!");
		}
	}
	cursor.close();
	long end = System.currentTimeMillis();
	System.out.println(counter + " vectors loaded in " + (end - start) + " ms!");
}
 
Example #14
Source File: PQ.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Appends the persistent index with the given (short) code.
 * 
 * @param code
 *            The code
 */
private void appendPersistentIndex(short[] code) {
	// write id and code
	TupleOutput output = new TupleOutput();
	for (int i = 0; i < numSubVectors; i++) {
		output.writeShort(code[i]);
	}
	DatabaseEntry data = new DatabaseEntry();
	TupleBinding.outputToEntry(output, data);
	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(loadCounter, key);
	iidToPqDB.put(null, key, data);
}
 
Example #15
Source File: UpgradeFrom7To8.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
void storeVirtualHostHierarchyRecord(Database hierarchyDb, Transaction txn, UUID id, UUID virtualHostId)
{
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    UUIDTupleBinding.getInstance().objectToEntry(virtualHostId, value);
    TupleOutput tupleOutput = new TupleOutput();
    tupleOutput.writeLong(id.getMostSignificantBits());
    tupleOutput.writeLong(id.getLeastSignificantBits());
    tupleOutput.writeString("VirtualHost");
    TupleBinding.outputToEntry(tupleOutput, key);
    hierarchyDb.put(txn, key, value);
}
 
Example #16
Source File: IVFPQ.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Appends the persistent index with the given (short) code.
 * 
 * @param code
 *            The code
 */
private void appendPersistentIndex(int listId, short[] code) {
	// write id, listId and code
	TupleOutput output = new TupleOutput();
	output.writeInt(listId);
	for (int i = 0; i < numSubVectors; i++) {
		output.writeShort(code[i]);
	}
	DatabaseEntry data = new DatabaseEntry();
	TupleBinding.outputToEntry(output, data);
	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(loadCounter, key);
	iidToIvfpqDB.put(null, key, data);
}
 
Example #17
Source File: IVFPQ.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Appends the persistent index with the given (byte) code.
 * 
 * @param code
 *            The code
 */
private void appendPersistentIndex(int listId, byte[] code) {
	// write id, listId and code
	TupleOutput output = new TupleOutput();
	output.writeInt(listId);
	for (int i = 0; i < numSubVectors; i++) {
		output.writeByte(code[i]);
	}
	DatabaseEntry data = new DatabaseEntry();
	TupleBinding.outputToEntry(output, data);
	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(loadCounter, key);
	iidToIvfpqDB.put(null, key, data);
}
 
Example #18
Source File: PreparedTransactionBinding.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
public static void objectToEntry(final PreparedTransaction preparedTransaction, final DatabaseEntry value)
{
    TupleOutput tupleOutput = new TupleOutput();
    writeRecords(preparedTransaction.getEnqueues(), tupleOutput);
    writeRecords(preparedTransaction.getDequeues(), tupleOutput);
    TupleBinding.outputToEntry(tupleOutput, value);
}
 
Example #19
Source File: PreparedTransactionBinding.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
public static PreparedTransaction entryToObject(final CachingUUIDFactory uuidFactory, final DatabaseEntry value)
{
    TupleInput input = TupleBinding.entryToInput(value);
    Transaction.EnqueueRecord[] enqueues = readEnqueueRecords(uuidFactory, input);
    Transaction.DequeueRecord[] dequeues = readDequeueRecords(uuidFactory, input);
    return new PreparedTransaction(enqueues, dequeues);
}
 
Example #20
Source File: SchemaGenerationPermutationTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private StoreUpdater createInstance(BdbNonPersistentEnvironmentCreator dataStoreFactory, BdbSchemaStore schema)
  throws DataStoreCreationException, nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.BdbDbCreationException,
  IOException {
  BdbTripleStore quadStore = new BdbTripleStore(dataStoreFactory.getDatabase(
    USER,
    DATA_SET,
    "rdfData",
    true,
    STRING_BINDING,
    STRING_BINDING,
    STRING_IS_CLEAN_HANDLER
  ));
  final BdbTypeNameStore typeNameStore = new BdbTypeNameStore(
    new BdbBackedData(dataStoreFactory.getDatabase(
      USER,
      DATA_SET,
      "typenames",
      false,
      STRING_BINDING,
      STRING_BINDING,
      STRING_IS_CLEAN_HANDLER
    )),
    GRAPH
  );

  final TupleBinding<Integer> integerBinding = TupleBinding.getPrimitiveBinding(Integer.class);
  final UpdatedPerPatchStore updatedPerPatchStore = new UpdatedPerPatchStore(
    dataStoreFactory.getDatabase(
      USER,
      DATA_SET,
      "updatedPerPatch",
      true,
      integerBinding,
      STRING_BINDING,
      new IsCleanHandler<Integer, String>() {
        @Override
        public Integer getKey() {
          return Integer.MAX_VALUE;
        }

        @Override
        public String getValue() {
          return "isClean";
        }
      }
    )
  );
  final BdbTruePatchStore truePatchStore = new BdbTruePatchStore(version ->
      dataStoreFactory.getDatabase(
          USER,
          DATA_SET,
          "truePatch" + version,
          true,
          STRING_BINDING,
          STRING_BINDING,
          STRING_IS_CLEAN_HANDLER
      ), updatedPerPatchStore
  );
  final BdbRmlDataSourceStore rmlDataSourceStore = new BdbRmlDataSourceStore(
    dataStoreFactory.getDatabase(
      USER,
      DATA_SET,
      "rmlSource",
      true,
      STRING_BINDING,
      STRING_BINDING,
      STRING_IS_CLEAN_HANDLER
    ),
    mock(ImportStatus.class)
  );
  VersionStore versionStore = new VersionStore(dataStoreFactory.getDatabase(
    USER,
    DATA_SET,
    "versions",
    false,
    STRING_BINDING,
    integerBinding,
    new IsCleanHandler<String, Integer>() {
      @Override
      public String getKey() {
        return "isClean";
      }

      @Override
      public Integer getValue() {
        return Integer.MAX_VALUE;
      }
    }
  ));

  return new StoreUpdater(
    quadStore,
    typeNameStore,
    truePatchStore,
    updatedPerPatchStore,
    Lists.newArrayList(schema, rmlDataSourceStore),
    versionStore,
    mock(ImportStatus.class)
  );
}
 
Example #21
Source File: SchemaGenerationTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private StoreUpdater createInstance(BdbNonPersistentEnvironmentCreator dataStoreFactory, BdbSchemaStore schema)
  throws DataStoreCreationException, nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.BdbDbCreationException,
  IOException {
  BdbTripleStore quadStore = new BdbTripleStore(dataStoreFactory.getDatabase(
    USER,
    DATA_SET,
    "rdfData",
    true,
    STRING_BINDING,
    STRING_BINDING,
    STRING_IS_CLEAN_HANDLER
  ));
  final BdbTypeNameStore typeNameStore = new BdbTypeNameStore(
    new BdbBackedData(dataStoreFactory.getDatabase(
      USER,
      DATA_SET,
      "typenames",
      false,
      STRING_BINDING,
      STRING_BINDING,
      STRING_IS_CLEAN_HANDLER
    )),
    GRAPH
  );

  final TupleBinding<Integer> integerBinding = TupleBinding.getPrimitiveBinding(Integer.class);
  final UpdatedPerPatchStore updatedPerPatchStore = new UpdatedPerPatchStore(
    dataStoreFactory.getDatabase(
      USER,
      DATA_SET,
      "updatedPerPatch",
      true,
      integerBinding,
      STRING_BINDING,
      new IsCleanHandler<Integer, String>() {
        @Override
        public Integer getKey() {
          return Integer.MAX_VALUE;
        }

        @Override
        public String getValue() {
          return "isClean";
        }
      }
    )
  );
  final BdbTruePatchStore truePatchStore = new BdbTruePatchStore(version ->
      dataStoreFactory.getDatabase(
          USER,
          DATA_SET,
          "truePatch" + version,
          true,
          STRING_BINDING,
          STRING_BINDING,
          STRING_IS_CLEAN_HANDLER
      ), updatedPerPatchStore
  );
  final BdbRmlDataSourceStore rmlDataSourceStore = new BdbRmlDataSourceStore(
    dataStoreFactory.getDatabase(
      USER,
      DATA_SET,
      "rmlSource",
      true,
      STRING_BINDING,
      STRING_BINDING,
      STRING_IS_CLEAN_HANDLER
    ),
    mock(ImportStatus.class)
  );
  VersionStore versionStore = new VersionStore(dataStoreFactory.getDatabase(
    USER,
    DATA_SET,
    "versions",
    false,
    STRING_BINDING,
    integerBinding,
    new IsCleanHandler<String, Integer>() {
      @Override
      public String getKey() {
        return "isClean";
      }

      @Override
      public Integer getValue() {
        return Integer.MAX_VALUE;
      }
    }
  ));

  return new StoreUpdater(
    quadStore,
    typeNameStore,
    truePatchStore,
    updatedPerPatchStore,
    Lists.newArrayList(schema, rmlDataSourceStore),
    versionStore,
    mock(ImportStatus.class)
  );
}
 
Example #22
Source File: BdbRmlDataSourceStoreTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void itWorks() throws Exception {
  BdbNonPersistentEnvironmentCreator dbCreator = new BdbNonPersistentEnvironmentCreator();
  DataSetMetaData dataSetMetadata = new BasicDataSetMetaData(
      "userid",
      "datasetid",
      "http://timbuctoo.huygens.knaw.nl/v5/userid/datasetid",
      "http://timbuctoo.huygens.knaw.nl/v5/userid/datasetid",
      "http://example.org/prefix/", false, false
  );

  final RmlDataSourceStore rmlDataSourceStore = new BdbRmlDataSourceStore(
      dbCreator.getDatabase(
          "userid",
          "datasetid",
          "rmlSource",
          true,
          TupleBinding.getPrimitiveBinding(String.class),
          TupleBinding.getPrimitiveBinding(String.class),
          new StringStringIsCleanHandler()
      ),
      new ImportStatus(new LogList())
  );

  RdfSerializer rdfSerializer = new RmlDataSourceRdfSerializer(rmlDataSourceStore);
  RawUploadRdfSaver rawUploadRdfSaver = new RawUploadRdfSaver(
      dataSetMetadata,
      "fileName",
      APPLICATION_OCTET_STREAM_TYPE,
      rdfSerializer,
      "origFileName",
      Clock.systemUTC()
  );
  final String inputCol1 = rawUploadRdfSaver.addCollection("collection1");
  ImportPropertyDescriptions importPropertyDescriptions = new ImportPropertyDescriptions();
  importPropertyDescriptions.getOrCreate(1).setPropertyName("propName1");
  importPropertyDescriptions.getOrCreate(2).setPropertyName("propName2");
  rawUploadRdfSaver.addPropertyDescriptions(inputCol1, importPropertyDescriptions);
  rawUploadRdfSaver.addEntity(inputCol1, ImmutableMap.of("propName1", "value1", "propName2", "val2"));
  rawUploadRdfSaver.addEntity(inputCol1, ImmutableMap.of("propName1", "entVal1", "propName2", "entVal2"));
  final String inputCol2 = rawUploadRdfSaver.addCollection("collection2");
  ImportPropertyDescriptions importPropertyDescriptions1 = new ImportPropertyDescriptions();
  importPropertyDescriptions1.getOrCreate(1).setPropertyName("prop3");
  importPropertyDescriptions1.getOrCreate(2).setPropertyName("prop4");
  rawUploadRdfSaver.addPropertyDescriptions(inputCol2, importPropertyDescriptions1);
  rawUploadRdfSaver.addEntity(inputCol2, ImmutableMap.of("prop3", "value1", "prop4", "val2"));
  rawUploadRdfSaver.addEntity(inputCol2, ImmutableMap.of("prop3", "entVal1", "prop4", "entVal2"));
  rdfSerializer.close();

  RdfDataSource rdfDataSource = new RdfDataSource(
      rmlDataSourceStore,
      inputCol1,
      new JexlRowFactory(ImmutableMap.of(), new HashMapBasedJoinHandler())
  );
  RdfDataSource rdfDataSource2 = new RdfDataSource(
      rmlDataSourceStore,
      inputCol2,
      new JexlRowFactory(ImmutableMap.of(), new HashMapBasedJoinHandler())
  );

  final List<String> collection1;
  final List<String> collection2;
  try (Stream<Row> stream = rdfDataSource.getRows(new ThrowingErrorHandler())) {
    collection1 = stream
        .map(x -> x.getRawValue("propName1") + ":" + x.getRawValue("propName2"))
        .collect(toList());
  }
  try (Stream<Row> stream = rdfDataSource2.getRows(new ThrowingErrorHandler())) {
    collection2 = stream
        .map(x -> x.getRawValue("prop3") + ":" + x.getRawValue("prop4"))
        .collect(toList());
  }

  assertThat(collection1, contains("value1:val2", "entVal1:entVal2"));
  assertThat(collection2, contains("value1:val2", "entVal1:entVal2"));
  dbCreator.close();
}
 
Example #23
Source File: ChangeFetcherImplTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void showsAdditions() throws Exception {
  UpdatedPerPatchStore updatedPerPatchStore = mock(UpdatedPerPatchStore.class);
  when(updatedPerPatchStore.getVersions()).thenReturn(IntStream.of(0, 1).boxed());

  final BdbNonPersistentEnvironmentCreator databaseCreator = new BdbNonPersistentEnvironmentCreator();
  final BdbTripleStore bdbTripleStore = new BdbTripleStore(databaseCreator.getDatabase(
    "a",
    "b",
    "rdfData",
    true,
    TupleBinding.getPrimitiveBinding(String.class),
    TupleBinding.getPrimitiveBinding(String.class),
    new StringStringIsCleanHandler()
  ));
  final BdbTruePatchStore truePatchStore = new BdbTruePatchStore(version -> databaseCreator.getDatabase(
    "a",
    "b",
    "truePatch" + version,
    true,
    TupleBinding.getPrimitiveBinding(String.class),
    TupleBinding.getPrimitiveBinding(String.class),
    new StringStringIsCleanHandler()
  ), updatedPerPatchStore);

  bdbTripleStore.putQuad("subj", "pred", OUT, "obj", null, null);
  truePatchStore.put("subj", 0, "pred", OUT, true, "obj", null, null);

  ChangeFetcherImpl changeFetcher = new ChangeFetcherImpl(truePatchStore, bdbTripleStore, 0);

  try (Stream<CursorQuad> predicates = changeFetcher.getPredicates("subj", "pred", OUT, false, false, true)) {
    assertThat(predicates.count(), is(1L));
  }
  try (Stream<CursorQuad> predicates = changeFetcher.getPredicates("subj", "pred", OUT, true, true, true)) {
    assertThat(predicates.count(), is(1L));
  }
  try (Stream<CursorQuad> predicates = changeFetcher.getPredicates("subj", "pred", OUT, true, false, true)) {
    assertThat(predicates.count(), is(1L));
  }

  bdbTripleStore.putQuad("subj", "pred", OUT, "obj2", null, null);
  truePatchStore.put("subj", 1, "pred", OUT, true, "obj2", null, null);
  changeFetcher = new ChangeFetcherImpl(truePatchStore, bdbTripleStore, 1);

  try (Stream<CursorQuad> predicates = changeFetcher.getPredicates("subj", "pred", OUT, false, false, true)) {
    assertThat(predicates.count(), is(1L));
  }
  try (Stream<CursorQuad> predicates = changeFetcher.getPredicates("subj", "pred", OUT, true, true, true)) {
    assertThat(predicates.count(), is(2L));
  }
  try (Stream<CursorQuad> predicates = changeFetcher.getPredicates("subj", "pred", OUT, true, false, true)) {
    assertThat(predicates.count(), is(1L));
  }
}