}
@Override
public DocumentImpl getResourceById(final int collectionId, final byte resourceType, final int documentId) throws PermissionDeniedException {
XmldbURI uri = null;
final Lock lock = collectionsDb.getLock();
try {
lock.acquire(Lock.READ_LOCK);
//final VariableByteOutputStream os = new VariableByteOutputStream(8);
//doc.write(os);
//Value key = new CollectionStore.DocumentKey(doc.getCollection().getId(), doc.getResourceType(), doc.getDocId());
//collectionsDb.put(transaction, key, os.data(), true);
//Value collectionKey = new CollectionStore.CollectionKey
//collectionsDb.get(Value.EMPTY_VALUE)
//get the collection uri
String collectionUri = null;
if(collectionId == 0) {
collectionUri = "/db";
} else {
for(final Value collectionDbKey : collectionsDb.getKeys()) {
if(collectionDbKey.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(collectionDbKey.data(), 1, collectionDbKey.data().length));
break;
}
}
}
}
//get the resource uri
final Value key = new CollectionStore.DocumentKey(collectionId, resourceType, documentId);
final VariableByteInput vbi = collectionsDb.getAsStream(key);
vbi.readInt(); //skip doc id
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 " + collectionsDb.getFile().getName());
return null;
} catch(final IOException e) {
LOG.error("IOException while reading resource data", e);
return null;
} finally {
lock.release(Lock.READ_LOCK);
}
return getResource(uri, Permission.READ);
}