use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.
the class BlobStoreImplTest method addUnique.
@Test
public void addUnique() throws IOException {
final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
final Path blobDir = temporaryFolder.newFolder("blob").toPath();
final List<Tuple2<byte[], MessageDigest>> testFiles = Arrays.asList(generateTestFile(), generateTestFile(), generateTestFile(), generateTestFile(), generateTestFile());
try (final BlobStore blobStore = newBlobStore(blobDbx, blobDir)) {
blobStore.open();
for (final Tuple2<byte[], MessageDigest> testFile : testFiles) {
addAndVerify(blobStore, testFile);
}
}
// should be 1 entry per unique test file in the blob.dbx, each entry is the digest and then the reference count
final long expectedBlobDbxLen = calculateBlobStoreSize(testFiles.size());
final long actualBlobDbxLen = Files.size(blobDbx);
assertEquals(expectedBlobDbxLen, actualBlobDbxLen);
}
use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.
the class FluentBrokerAPITest method collectionThenCollectionAndDoc.
@Test
public void collectionThenCollectionAndDoc() throws PermissionDeniedException, EXistException, LockException {
final XmldbURI docUri = uri("all-test.xml");
final long collectionCreated = 1234;
final IMocksControl ctrl = createStrictControl();
ctrl.checkOrder(true);
final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
final Collection mockCollection = ctrl.createMock(Collection.class);
final LockedDocument mockLockedDocument = ctrl.createMock(LockedDocument.class);
final DocumentImpl mockDocument = ctrl.createMock(DocumentImpl.class);
expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
expect(mockCollection.getCreated()).andReturn(collectionCreated);
expect(mockCollection.getDocumentWithLock(mockBroker, docUri, READ_LOCK)).andReturn(mockLockedDocument);
expect(mockLockedDocument.getDocument()).andReturn(mockDocument);
expect(mockCollection.getURI()).andReturn(TEST_COLLECTION_URI);
expect(mockDocument.getFileURI()).andReturn(docUri);
// NOTE: checks that Collection lock is release before Document lock
mockCollection.close();
mockLockedDocument.close();
mockBroker.close();
ctrl.replay();
final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
final BiFunction<Collection, DocumentImpl, String> collectionDocOp = (collection, doc) -> collection.getURI().append(doc.getFileURI()).toString();
final Tuple2<Long, String> result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).withDocument(collection -> new Tuple2<>(docUri, READ_LOCK)).execute(collectionDocOp).doAll();
assertEquals(collectionCreated, result._1.longValue());
assertEquals(TEST_COLLECTION_URI.append(docUri), result._2);
ctrl.verify();
}
use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.
the class RpcConnection method summarise.
private Tuple2<java.util.Collection<NodeCount>, java.util.Collection<DoctypeCount>> summarise(final Sequence results) throws XPathException {
final Map<String, NodeCount> nodeCounts = new HashMap<>();
final Map<String, DoctypeCount> doctypeCounts = new HashMap<>();
NodeCount counter;
DoctypeCount doctypeCounter;
for (final SequenceIterator i = results.iterate(); i.hasNext(); ) {
final Item item = i.nextItem();
if (Type.subTypeOf(item.getType(), Type.NODE)) {
final NodeValue nv = (NodeValue) item;
if (nv.getImplementationType() == NodeValue.PERSISTENT_NODE) {
final NodeProxy p = (NodeProxy) nv;
final String docName = p.getOwnerDocument().getURI().toString();
final DocumentType doctype = p.getOwnerDocument().getDoctype();
if (nodeCounts.containsKey(docName)) {
counter = nodeCounts.get(docName);
counter.inc();
} else {
counter = new NodeCount(p.getOwnerDocument());
nodeCounts.put(docName, counter);
}
if (doctype == null) {
continue;
}
if (doctypeCounts.containsKey(doctype.getName())) {
doctypeCounter = doctypeCounts.get(doctype.getName());
doctypeCounter.inc();
} else {
doctypeCounter = new DoctypeCount(doctype);
doctypeCounts.put(doctype.getName(), doctypeCounter);
}
}
}
}
return new Tuple2<>(nodeCounts.values(), doctypeCounts.values());
}
use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.
the class NativeBroker method createCollectionObject.
/**
* NOTE - When this is called there must be a WRITE_LOCK on collectionUri
* and at least a READ_LOCK on parentCollection (if it is not null)
*/
private Collection createCollectionObject(final Txn transaction, @Nullable @EnsureLocked(mode = LockMode.READ_LOCK) final Collection parentCollection, @EnsureLocked(mode = LockMode.WRITE_LOCK, type = LockType.COLLECTION) final XmldbURI collectionUri, final Optional<Tuple2<Permission, Long>> creationAttributes) throws ReadOnlyException, PermissionDeniedException, LockException {
final int collectionId = getNextCollectionId(transaction);
final Collection collection = creationAttributes.map(attrs -> new MutableCollection(this, collectionId, collectionUri, attrs._1, attrs._2)).orElseGet(() -> new MutableCollection(this, collectionId, collectionUri));
// inherit the group to collection if parent-collection is setGid
if (parentCollection != null) {
final Permission parentPermissions = parentCollection.getPermissionsNoLock();
if (parentPermissions.isSetGid()) {
final Permission collectionPermissions = collection.getPermissionsNoLock();
// inherit group
collectionPermissions.setGroupFrom(parentPermissions);
// inherit setGid bit
collectionPermissions.setSetGid(true);
}
}
return collection;
}
use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.
the class NativeBroker method doCopyCollection.
/**
* Copy a collection and all its sub-Collections.
*
* @param transaction The current transaction
* @param documentTrigger The trigger to use for document events
* @param sourceCollection The Collection to copy
* @param destinationCollectionUri The destination Collection URI for the sourceCollection copy
* @param copyCollectionMode false on the first call, true on recursive calls
*
* @return A reference to the Collection, no additional locks are held on the Collection
*
* @throws PermissionDeniedException If the current user does not have appropriate permissions
* @throws LockException If an exception occurs whilst acquiring locks
* @throws IOException If an error occurs whilst copying the Collection on disk
* @throws TriggerException If a CollectionTrigger throws an exception
* @throws EXistException If no more Document IDs are available
*/
private Collection doCopyCollection(final Txn transaction, final DocumentTrigger documentTrigger, @EnsureLocked(mode = LockMode.READ_LOCK) final Collection sourceCollection, @EnsureLocked(mode = LockMode.WRITE_LOCK) final Collection destinationParentCollection, @EnsureLocked(mode = LockMode.WRITE_LOCK, type = LockType.COLLECTION) final XmldbURI destinationCollectionUri, final boolean copyCollectionMode, final PreserveType preserve) throws PermissionDeniedException, IOException, EXistException, TriggerException, LockException {
if (LOG.isDebugEnabled()) {
LOG.debug("Copying collection to '{}'", destinationCollectionUri);
}
// permissions and attributes for the destCollection (if we have to create it)
final Permission createCollectionPerms = PermissionFactory.getDefaultCollectionPermission(getBrokerPool().getSecurityManager());
copyModeAndAcl(sourceCollection.getPermissions(), createCollectionPerms);
final long created;
if (preserveOnCopy(preserve)) {
// only copy the owner and group from the source if we are creating a new collection and we are the DBA
if (getCurrentSubject().hasDbaRole()) {
PermissionFactory.chown(this, createCollectionPerms, Optional.of(sourceCollection.getPermissions().getOwner().getName()), Optional.of(sourceCollection.getPermissions().getGroup().getName()));
}
created = sourceCollection.getCreated();
} else {
created = 0;
}
final Tuple2<Boolean, Collection> destinationCollection = getOrCreateCollectionExplicit(transaction, destinationCollectionUri, Optional.of(new Tuple2<>(createCollectionPerms, created)));
// if we didn't create destCollection but we need to preserve the attributes
if ((!destinationCollection._1) && preserveOnCopy(preserve)) {
copyModeAndAcl(sourceCollection.getPermissions(), destinationCollection._2.getPermissions());
}
// inherit the group to the destinationCollection if parent is setGid
if (destinationParentCollection != null && destinationParentCollection.getPermissions().isSetGid()) {
// inherit group
destinationCollection._2.getPermissions().setGroupFrom(destinationParentCollection.getPermissions());
// inherit setGid bit
destinationCollection._2.getPermissions().setSetGid(true);
}
doCopyCollectionDocuments(transaction, documentTrigger, sourceCollection, destinationCollection._2, preserve);
final XmldbURI sourceCollectionUri = sourceCollection.getURI();
for (final Iterator<XmldbURI> i = sourceCollection.collectionIterator(this); i.hasNext(); ) {
final XmldbURI childName = i.next();
final XmldbURI childUri = sourceCollectionUri.append(childName);
try (final Collection child = getCollection(childUri)) {
// NOTE: we already have a READ lock on child implicitly
if (child == null) {
throw new IOException("Child collection " + childUri + " not found");
} else {
doCopyCollection(transaction, documentTrigger, child, destinationCollection._2, destinationCollection._2.getURI().append(childName), true, preserve);
}
}
}
return destinationCollection._2;
}
Aggregations