Search in sources :

Example 1 with VariableByteInput

use of org.exist.storage.io.VariableByteInput in project exist by eXist-db.

the class UnixStylePermissionTest method writeRead_roundtrip.

@Test
public void writeRead_roundtrip() throws IOException {
    final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
    final int ownerId = new Random().nextInt();
    final int mode = 0700;
    final int ownerGroupId = new Random().nextInt();
    final VariableByteOutputStream mockOstream = EasyMock.createMock(VariableByteOutputStream.class);
    final VariableByteInput mockIstream = EasyMock.createMock(VariableByteInput.class);
    final TestableUnixStylePermission permission = new TestableUnixStylePermission(mockSecurityManager, ownerId, ownerGroupId, mode);
    final long permissionVector = permission.getVector_testable();
    // expectations
    mockOstream.writeLong(permissionVector);
    expect(mockIstream.readLong()).andReturn(permissionVector);
    replay(mockSecurityManager, mockOstream, mockIstream);
    permission.write(mockOstream);
    permission.read(mockIstream);
    verify(mockSecurityManager, mockOstream, mockIstream);
    assertEquals(permissionVector, permission.getVector_testable());
}
Also used : VariableByteInput(org.exist.storage.io.VariableByteInput) Random(java.util.Random) VariableByteOutputStream(org.exist.storage.io.VariableByteOutputStream) Test(org.junit.Test)

Example 2 with VariableByteInput

use of org.exist.storage.io.VariableByteInput in project exist by eXist-db.

the class SymbolTableTest method readLegacyFormat.

@Test
public void readLegacyFormat() throws IOException, BrokerPoolServiceException {
    final SymbolTable symbolTable = createSymbolTable(createTempDir());
    VariableByteInput mockIs = createMock(VariableByteInput.class);
    /* readLegacy expectations */
    // max and nsMax
    expect(mockIs.readShort()).andReturn((short) 1);
    expect(mockIs.readShort()).andReturn((short) 1);
    // localnames
    expect(mockIs.readInt()).andReturn(1);
    expect(mockIs.readUTF()).andReturn("local-name");
    expect(mockIs.readShort()).andReturn((short) 67);
    // namespaces
    expect(mockIs.readInt()).andReturn(1);
    expect(mockIs.readUTF()).andReturn("http://some/or/other");
    expect(mockIs.readShort()).andReturn((short) 77);
    // default mappings
    expect(mockIs.readInt()).andReturn(1);
    expect(mockIs.readUTF()).andReturn("mapping");
    expect(mockIs.readShort()).andReturn((short) 87);
    // mimetypes
    expect(mockIs.readInt()).andReturn(1);
    expect(mockIs.readUTF()).andReturn("some/other");
    expect(mockIs.readInt()).andReturn(97);
    // replay
    replay(mockIs);
    // action
    symbolTable.readLegacy(mockIs);
    // verify
    verify(mockIs);
    symbolTable.close();
}
Also used : VariableByteInput(org.exist.storage.io.VariableByteInput) Test(org.junit.Test)

Example 3 with VariableByteInput

use of org.exist.storage.io.VariableByteInput in project exist by eXist-db.

the class NativeBroker method getResourceById.

@Override
public DocumentImpl getResourceById(final int collectionId, final byte resourceType, final int documentId) throws PermissionDeniedException {
    XmldbURI uri;
    try (final ManagedLock<ReentrantLock> collectionsDbLock = lockManager.acquireBtreeReadLock(collectionsDb.getLockName())) {
        // get the collection uri
        String collectionUri = null;
        if (collectionId == FIRST_COLLECTION_ID) {
            collectionUri = "/db";
        } else {
            for (final Value collectionDbKey : collectionsDb.getKeys()) {
                final byte[] data = collectionDbKey.data();
                if (data[0] == CollectionStore.KEY_TYPE_COLLECTION) {
                    // Value collectionDbValue = collectionsDb.get(collectionDbKey);
                    final VariableByteInput vbi = collectionsDb.getAsStream(collectionDbKey);
                    final int id = vbi.readInt();
                    // check if the collection id matches (first 4 bytes)
                    if (collectionId == id) {
                        collectionUri = new String(Arrays.copyOfRange(data, 1, data.length));
                        break;
                    }
                }
            }
        }
        // get the resource uri
        final Value key = new CollectionStore.DocumentKey(collectionId, resourceType, documentId);
        final VariableByteInput vbi = collectionsDb.getAsStream(key);
        // skip doc id
        vbi.readInt();
        final String resourceUri = vbi.readUTF();
        // get the resource
        uri = XmldbURI.createInternal(collectionUri + "/" + resourceUri);
    } catch (final TerminatedException te) {
        LOG.error("Query Terminated", te);
        return null;
    } catch (final BTreeException bte) {
        LOG.error("Problem reading btree", bte);
        return null;
    } catch (final LockException e) {
        LOG.error("Failed to acquire lock on {}", FileUtils.fileName(collectionsDb.getFile()));
        return null;
    } catch (final IOException e) {
        LOG.error("IOException while reading resource data", e);
        return null;
    }
    return getResource(uri, Permission.READ);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) VariableByteInput(org.exist.storage.io.VariableByteInput) XmldbURI(org.exist.xmldb.XmldbURI) TerminatedException(org.exist.xquery.TerminatedException)

Example 4 with VariableByteInput

use of org.exist.storage.io.VariableByteInput in project exist by eXist-db.

the class NativeBroker method loadCollection.

/**
 * Loads a Collection from disk
 *
 * @param collectionUri The URI of the Collection to load
 *
 * @return The Collection object loaded from disk, or null if the record does not exist on disk
 */
@Nullable
@EnsureLocked(mode = LockMode.READ_LOCK, type = LockType.COLLECTION)
private Collection loadCollection(@EnsureLocked(mode = LockMode.READ_LOCK, type = LockType.COLLECTION) final XmldbURI collectionUri) throws PermissionDeniedException, LockException, IOException {
    try (final ManagedLock<ReentrantLock> collectionsDbLock = lockManager.acquireBtreeReadLock(collectionsDb.getLockName())) {
        final Value key = new CollectionStore.CollectionKey(collectionUri.toString());
        final VariableByteInput is = collectionsDb.getAsStream(key);
        return is == null ? null : MutableCollection.load(this, collectionUri, is);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) VariableByteInput(org.exist.storage.io.VariableByteInput) Nullable(javax.annotation.Nullable)

Example 5 with VariableByteInput

use of org.exist.storage.io.VariableByteInput in project exist by eXist-db.

the class NativeBroker method readCollectionEntry.

@Override
public void readCollectionEntry(final SubCollectionEntry entry) throws IOException, LockException {
    final XmldbURI uri = prepend(entry.getUri().toCollectionPathURI());
    final CollectionCache collectionsCache = pool.getCollectionsCache();
    final Collection collection = collectionsCache.getIfPresent(uri);
    if (collection == null) {
        try (final ManagedLock<ReentrantLock> collectionsDbLock = lockManager.acquireBtreeReadLock(collectionsDb.getLockName())) {
            final Value key = new CollectionStore.CollectionKey(uri.toString());
            final VariableByteInput is = collectionsDb.getAsStream(key);
            if (is == null) {
                throw new IOException("Could not find collection entry for: " + uri);
            }
            // read the entry details
            entry.read(is);
        }
    } else {
        if (!collection.getURI().equalsInternal(uri)) {
            throw new IOException(String.format("readCollectionEntry: The Collection received from the cache: %s is not the requested: %s", collection.getURI(), uri));
        }
        entry.read(collection);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) VariableByteInput(org.exist.storage.io.VariableByteInput) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

VariableByteInput (org.exist.storage.io.VariableByteInput)8 ReentrantLock (java.util.concurrent.locks.ReentrantLock)4 VariableByteOutputStream (org.exist.storage.io.VariableByteOutputStream)3 Test (org.junit.Test)3 XmldbURI (org.exist.xmldb.XmldbURI)2 IOException (java.io.IOException)1 Random (java.util.Random)1 Nullable (javax.annotation.Nullable)1 EXistException (org.exist.EXistException)1 Collection (org.exist.collections.Collection)1 NodeId (org.exist.numbering.NodeId)1 OccurrenceList (org.exist.storage.OccurrenceList)1 VariableByteInputStream (org.exist.storage.io.VariableByteInputStream)1 TerminatedException (org.exist.xquery.TerminatedException)1