Search in sources :

Example 51 with DataRecord

use of org.apache.jackrabbit.core.data.DataRecord in project jackrabbit-oak by apache.

the class AbstractDataStoreTest method doDeleteAllOlderThan.

/**
     * Asserts that {@link DataStore#deleteAllOlderThan(long)} only deleted
     * records older than argument passed.
     */
protected void doDeleteAllOlderThan() throws Exception {
    Random random = randomGen;
    byte[] data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec1 = ds.addRecord(new ByteArrayInputStream(data));
    data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec2 = ds.addRecord(new ByteArrayInputStream(data));
    // sleep for some time to ensure that async upload completes in backend.
    sleep(10000);
    long updateTime = System.currentTimeMillis();
    ds.updateModifiedDateOnAccess(updateTime);
    // sleep to workaround System.currentTimeMillis granularity.
    sleep(3000);
    data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec3 = ds.addRecord(new ByteArrayInputStream(data));
    data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec4 = ds.addRecord(new ByteArrayInputStream(data));
    rec1 = ds.getRecord(rec1.getIdentifier());
    ds.clearInUse();
    Assert.assertEquals("only rec2 should be deleted", 1, ds.deleteAllOlderThan(updateTime));
    assertNull("rec2 should be null", ds.getRecordIfStored(rec2.getIdentifier()));
    Iterator<DataIdentifier> itr = ds.getAllIdentifiers();
    List<DataIdentifier> list = new ArrayList<DataIdentifier>();
    list.add(rec1.getIdentifier());
    list.add(rec3.getIdentifier());
    list.add(rec4.getIdentifier());
    while (itr.hasNext()) {
        assertTrue("record found on list", list.remove(itr.next()));
    }
    Assert.assertEquals("touched records found", 0, list.size());
    Assert.assertEquals("rec1 touched", true, rec1.getLastModified() > updateTime);
    Assert.assertEquals("rec3 touched", true, rec3.getLastModified() > updateTime);
    Assert.assertEquals("rec4 touched", true, rec4.getLastModified() > updateTime);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) DataRecord(org.apache.jackrabbit.core.data.DataRecord)

Example 52 with DataRecord

use of org.apache.jackrabbit.core.data.DataRecord in project jackrabbit-oak by apache.

the class SharedDataStoreUtilsTest method testAddMetadata.

@Test
public void testAddMetadata() throws Exception {
    File rootFolder = folder.newFolder();
    dataStore = getBlobStore(rootFolder);
    String repoId = UUID.randomUUID().toString();
    Set<String> refs = Sets.newHashSet("1_1", "1_2");
    File f = folder.newFile();
    FileIOUtils.writeStrings(refs.iterator(), f, false);
    dataStore.addMetadataRecord(new FileInputStream(f), SharedStoreRecordType.REFERENCES.getNameFromId(repoId));
    DataRecord rec = dataStore.getMetadataRecord(SharedStoreRecordType.REFERENCES.getNameFromId(repoId));
    Set<String> refsReturned = FileIOUtils.readStringsAsSet(rec.getStream(), false);
    Assert.assertEquals(refs, refsReturned);
    dataStore.deleteAllMetadataRecords(SharedStoreRecordType.REFERENCES.getType());
    dataStore.addMetadataRecord(f, SharedStoreRecordType.REFERENCES.getNameFromId(repoId));
    rec = dataStore.getMetadataRecord(SharedStoreRecordType.REFERENCES.getNameFromId(repoId));
    refsReturned = FileIOUtils.readStringsAsSet(rec.getStream(), false);
    Assert.assertEquals(refs, refsReturned);
    assertEquals(SharedStoreRecordType.REFERENCES.getIdFromName(rec.getIdentifier().toString()), repoId);
    dataStore.deleteAllMetadataRecords(SharedStoreRecordType.REFERENCES.getType());
}
Also used : DataRecord(org.apache.jackrabbit.core.data.DataRecord) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 53 with DataRecord

use of org.apache.jackrabbit.core.data.DataRecord in project jackrabbit-oak by apache.

the class SharedDataStoreUtilsTest method testStreamFromGetAllRecords.

@Test
public void testStreamFromGetAllRecords() throws Exception {
    File rootFolder = folder.newFolder();
    dataStore = getBlobStore(rootFolder);
    int number = 10;
    Set<DataRecord> added = newHashSet();
    for (int i = 0; i < number; i++) {
        added.add(dataStore.addRecord(randomStream(i, 16516)));
    }
    Set<DataRecord> retrieved = newHashSet((dataStore.getAllRecords()));
    assertRecords(added, retrieved);
}
Also used : DataRecord(org.apache.jackrabbit.core.data.DataRecord) File(java.io.File) Test(org.junit.Test)

Example 54 with DataRecord

use of org.apache.jackrabbit.core.data.DataRecord in project jackrabbit-oak by apache.

the class DataStoreBlobStoreTest method testGetAllChunks.

@Test
public void testGetAllChunks() throws Exception {
    DataIdentifier d10 = new DataIdentifier("d-10");
    DataIdentifier d20 = new DataIdentifier("d-20");
    DataIdentifier d30 = new DataIdentifier("d-30");
    List<DataIdentifier> dis = ImmutableList.of(d10, d20, d30);
    List<DataRecord> recs = Lists.newArrayList(Iterables.transform(dis, new Function<DataIdentifier, DataRecord>() {

        @Nullable
        @Override
        public DataRecord apply(@Nullable DataIdentifier input) {
            return new TimeDataRecord(input);
        }
    }));
    OakFileDataStore mockedDS = mock(OakFileDataStore.class);
    when(mockedDS.getAllRecords()).thenReturn(recs.iterator());
    when(mockedDS.getRecord(new DataIdentifier("d-10"))).thenReturn(new TimeDataRecord(d10));
    when(mockedDS.getRecord(new DataIdentifier("d-20"))).thenReturn(new TimeDataRecord(d20));
    when(mockedDS.getRecord(new DataIdentifier("d-30"))).thenReturn(new TimeDataRecord(d30));
    DataStoreBlobStore ds = new DataStoreBlobStore(mockedDS);
    Iterator<String> chunks = ds.getAllChunkIds(25);
    Set<String> expected = Sets.newHashSet("d-10", "d-20");
    assertEquals(expected, Sets.newHashSet(chunks));
}
Also used : Function(com.google.common.base.Function) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) DataRecord(org.apache.jackrabbit.core.data.DataRecord) Nullable(javax.annotation.Nullable) AbstractBlobStoreTest(org.apache.jackrabbit.oak.spi.blob.AbstractBlobStoreTest) Test(org.junit.Test)

Example 55 with DataRecord

use of org.apache.jackrabbit.core.data.DataRecord in project jackrabbit-oak by apache.

the class DataStoreBlobStoreTest method testExternalBinary.

@Test
public void testExternalBinary() throws DataStoreException, IOException {
    int maxInlineSize = 300;
    int actualSize = maxInlineSize + 10;
    byte[] data = new byte[actualSize];
    new Random().nextBytes(data);
    DataIdentifier testDI = new DataIdentifier("test");
    DataRecord testDR = new ByteArrayDataRecord(data, testDI, "testReference");
    DataStore mockedDS = mock(DataStore.class);
    when(mockedDS.getMinRecordLength()).thenReturn(maxInlineSize);
    when(mockedDS.getRecord(testDI)).thenReturn(testDR);
    when(mockedDS.getRecordIfStored(testDI)).thenReturn(testDR);
    when(mockedDS.addRecord(any(InputStream.class))).thenReturn(testDR);
    DataStoreBlobStore ds = new DataStoreBlobStore(mockedDS);
    DataRecord dr = ds.addRecord(new ByteArrayInputStream(data));
    assertFalse(InMemoryDataRecord.isInstance(dr.getIdentifier().toString()));
    assertEquals(testDI, dr.getIdentifier());
    assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(data), dr.getStream()));
    assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(data), new BlobStoreInputStream(ds, dr.getIdentifier().toString(), 0)));
    assertEquals(dr, ds.getRecordIfStored(dr.getIdentifier()));
    assertEquals(dr, ds.getRecord(dr.getIdentifier()));
    //        assertTrue(ds.getInputStream(dr.getIdentifier().toString()) instanceof BufferedInputStream);
    assertEquals(actualSize, ds.getBlobLength(dr.getIdentifier().toString()));
    assertEquals(testDI.toString(), BlobId.of(ds.writeBlob(new ByteArrayInputStream(data))).blobId);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobStoreInputStream(org.apache.jackrabbit.oak.spi.blob.BlobStoreInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DataStore(org.apache.jackrabbit.core.data.DataStore) BlobStoreInputStream(org.apache.jackrabbit.oak.spi.blob.BlobStoreInputStream) DataRecord(org.apache.jackrabbit.core.data.DataRecord) AbstractBlobStoreTest(org.apache.jackrabbit.oak.spi.blob.AbstractBlobStoreTest) Test(org.junit.Test)

Aggregations

DataRecord (org.apache.jackrabbit.core.data.DataRecord)76 Test (org.junit.Test)43 ByteArrayInputStream (java.io.ByteArrayInputStream)34 DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)30 File (java.io.File)24 Hex.encodeHexString (org.apache.commons.codec.binary.Hex.encodeHexString)19 FileInputStream (java.io.FileInputStream)16 Random (java.util.Random)12 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)6 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)6 FileDataStore (org.apache.jackrabbit.core.data.FileDataStore)6 IOException (java.io.IOException)4 AbstractBlobStoreTest (org.apache.jackrabbit.oak.spi.blob.AbstractBlobStoreTest)4 Function (com.google.common.base.Function)3 SequenceInputStream (java.io.SequenceInputStream)3 RepositoryException (javax.jcr.RepositoryException)3 DataStore (org.apache.jackrabbit.core.data.DataStore)3 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)2 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)2