Search in sources :

Example 1 with Tuple2

use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.

the class BlobStoreImpl method add.

@Override
public Tuple2<BlobId, Long> add(final Txn transaction, final InputStream is) throws IOException {
    if (state.get() != State.OPEN) {
        throw new IOException("Blob Store is not open!");
    }
    // stage the BLOB file
    final Tuple3<Path, Long, MessageDigest> staged = stage(is);
    final BlobVacuum.RequestDeleteStagedBlobFile requestDeleteStagedBlobFile = new BlobVacuum.RequestDeleteStagedBlobFile(stagingDir, staged._1.getFileName().toString());
    // register a callback to cleanup the staged BLOB file ONLY after commit+checkpoint
    final JournalManager journalManager = database.getJournalManager().orElse(null);
    if (journalManager != null) {
        final DeleteStagedBlobFile cleanupStagedBlob = new DeleteStagedBlobFile(vacuumQueue, requestDeleteStagedBlobFile);
        journalManager.listen(cleanupStagedBlob);
        transaction.registerListener(cleanupStagedBlob);
    }
    final BlobId blobId = new BlobId(staged._3.getValue());
    // if the blob entry does not exist, we exclusively compute it as STAGED.
    BlobReference blobReference = references.computeIfAbsent(blobId, k -> new BlobReference(STAGED));
    try {
        while (true) {
            if (blobReference.count.compareAndSet(STAGED, PROMOTING)) {
                // write journal entries to the WAL
                if (journalManager != null) {
                    try {
                        journalManager.journal(new StoreBlobFileLoggable(transaction.getId(), blobId, staged._1.getFileName().toString()));
                        journalManager.journal(new UpdateBlobRefCountLoggable(transaction.getId(), blobId, 0, 1));
                        // force WAL entries to disk!
                        journalManager.flush(true, true);
                    } catch (final JournalException e) {
                        references.remove(blobId);
                        throw new IOException(e);
                    }
                }
                // promote the staged blob
                promote(staged);
                if (journalManager == null) {
                    // no journal (or recovery)... so go ahead and schedule cleanup of the staged blob file
                    enqueueVacuum(vacuumQueue, requestDeleteStagedBlobFile);
                }
                // schedule disk persist of the new value
                persistQueue.put(Tuple(blobId, blobReference, 1));
                // update memory with the new value
                blobReference.count.set(1);
                // done!
                return Tuple(blobId, staged._2);
            }
            final int count = blobReference.count.get();
            // guard against a concurrent #add or #remove
            if (count == PROMOTING || count == UPDATING_COUNT) {
                // spin whilst another thread promotes the blob, or updates the reference count
                // sleep a small time to save CPU
                Thread.sleep(10);
                continue;
            }
            // i.e. wait for the deletion of the blob to complete, and then we can add the blob again
            if (count == DELETING) {
                blobReference = references.computeIfAbsent(blobId, k -> new BlobReference(STAGED));
                // loop again
                continue;
            }
            // only increment the blob reference if the blob is active!
            if (count >= 0 && blobReference.count.compareAndSet(count, UPDATING_COUNT)) {
                // NOTE: we are the only thread that can be in this branch for the blobId
                final int newCount = count + 1;
                // write journal entries to the WAL
                if (journalManager != null) {
                    try {
                        journalManager.journal(new UpdateBlobRefCountLoggable(transaction.getId(), blobId, count, newCount));
                        // force WAL entries to disk!
                        journalManager.flush(true, true);
                    } catch (final JournalException e) {
                        // restore the state of the blobReference first!
                        blobReference.count.set(count);
                        throw new IOException(e);
                    }
                }
                // persist the new value
                persistQueue.put(Tuple(blobId, blobReference, newCount));
                // update memory with the new value, and release other spinning threads
                blobReference.count.set(newCount);
                // done!
                return Tuple(blobId, staged._2);
            }
        }
    } catch (final InterruptedException e) {
        // thrown by persistQueue.put or Thread.sleep
        Thread.currentThread().interrupt();
        throw new IOException(e);
    }
}
Also used : Path(java.nio.file.Path) Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) Txn(org.exist.storage.txn.Txn) JournalException(org.exist.storage.journal.JournalException) RawDataBackup(org.exist.backup.RawDataBackup) ByteBuffer(java.nio.ByteBuffer) FileUtils(org.exist.util.FileUtils) Tuple(com.evolvedbinary.j8fu.tuple.Tuple.Tuple) ThreadUtils.nameInstanceThread(org.exist.util.ThreadUtils.nameInstanceThread) DigestInputStream(org.exist.util.crypto.digest.DigestInputStream) StreamableDigest(org.exist.util.crypto.digest.StreamableDigest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DigestType(org.exist.util.crypto.digest.DigestType) LOG_UPDATE_BLOB_REF_COUNT(org.exist.storage.blob.BlobLoggable.LOG_UPDATE_BLOB_REF_COUNT) Path(java.nio.file.Path) java.util.concurrent(java.util.concurrent) StandardOpenOption(java.nio.file.StandardOpenOption) FileNotFoundException(java.io.FileNotFoundException) SeekableByteChannel(java.nio.channels.SeekableByteChannel) Logger(org.apache.logging.log4j.Logger) TxnListener(org.exist.storage.txn.TxnListener) LogEntryTypes(org.exist.storage.journal.LogEntryTypes) java.util(java.util) Try(com.evolvedbinary.j8fu.Try) ThreadSafe(net.jcip.annotations.ThreadSafe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) FilterInputStream(java.io.FilterInputStream) LOG_STORE_BLOB_FILE(org.exist.storage.blob.BlobLoggable.LOG_STORE_BLOB_FILE) ThreadUtils.newInstanceSubThreadGroup(org.exist.util.ThreadUtils.newInstanceSubThreadGroup) LogException(org.exist.storage.journal.LogException) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) Nullable(javax.annotation.Nullable) OutputStream(java.io.OutputStream) Database(org.exist.Database) ATOMIC_MOVE(java.nio.file.StandardCopyOption.ATOMIC_MOVE) CountingInputStream(org.apache.commons.io.input.CountingInputStream) Files(java.nio.file.Files) HexEncoder.bytesToHex(org.exist.util.HexEncoder.bytesToHex) JournalManager(org.exist.storage.journal.JournalManager) IOException(java.io.IOException) BlobReference(org.exist.storage.blob.BlobStoreImpl.BlobReference) UUIDGenerator(org.exist.util.UUIDGenerator) MessageDigest(org.exist.util.crypto.digest.MessageDigest) FileUtils.fileName(org.exist.util.FileUtils.fileName) TaggedTryUnchecked(com.evolvedbinary.j8fu.Try.TaggedTryUnchecked) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) BlobReference(org.exist.storage.blob.BlobStoreImpl.BlobReference) JournalException(org.exist.storage.journal.JournalException) JournalManager(org.exist.storage.journal.JournalManager) IOException(java.io.IOException) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Example 2 with Tuple2

use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.

the class LocalCollectionManagementService method createCollection.

@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
    final XmldbURI collName = resolve(name);
    withDb((broker, transaction) -> {
        try {
            final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
            try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collName)) {
                broker.saveCollection(transaction, coll);
            }
            return null;
        } catch (final LockException | TriggerException e) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
    return new LocalCollection(user, brokerPool, collection, collName);
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) PreserveType(org.exist.storage.DBBroker.PreserveType) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) Optional(java.util.Optional) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) ErrorCodes(org.xmldb.api.base.ErrorCodes) Lock(org.exist.storage.lock.Lock) Nullable(javax.annotation.Nullable) Collection(org.xmldb.api.base.Collection) LockException(org.exist.util.LockException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) XMLDBException(org.xmldb.api.base.XMLDBException) TriggerException(org.exist.collections.triggers.TriggerException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 3 with Tuple2

use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.

the class CollectionRemovalTest method initDB.

@Before
public void initDB() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final int worldReadable = 0744;
        final int worldForbidden = 0700;
        /*
             * Creates 3 collections: /db/test, /db/test/test2, /db/test/test2/test3 and /db/test/test2/test4,
             * and stores one document into each.
             * Collection /db/test/test2/test3 is only readable by the owner (i.e. admin user).
             */
        final List<Tuple2<XmldbURI, Integer>> collectionUriAndModes = Arrays.asList(Tuple(TestConstants.TEST_COLLECTION_URI2, worldReadable), Tuple(TestConstants.TEST_COLLECTION_URI3, worldForbidden), Tuple(TestConstants.TEST_COLLECTION_URI2.append("test4"), worldReadable));
        // creat collections
        for (final Tuple2<XmldbURI, Integer> collectionUriAndMode : collectionUriAndModes) {
            final XmldbURI collectionUri = collectionUriAndMode._1;
            final int mode = collectionUriAndMode._2;
            // create collection
            final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
            assertNotNull(collection);
            final Permission perms = collection.getPermissions();
            perms.setMode(mode);
            broker.saveCollection(transaction, collection);
            // store document
            broker.storeDocument(transaction, XmldbURI.create("document.xml"), new StringInputSource(DATA), MimeType.XML_TYPE, collection);
        }
        transact.commit(transaction);
    }
}
Also used : Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) Permission(org.exist.security.Permission) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 4 with Tuple2

use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.

the class AbstractXMLReaderSecurityTest method createTempSecretFile.

/**
 * @return A tuple whose first item is the secret, and the second which is the path to a temporary file containing the secret
 */
protected Tuple2<String, Path> createTempSecretFile() throws IOException {
    final Path file = Files.createTempFile("exist.XMLReaderSecurityTest", "topsecret");
    final String randomSecret = generateRandomString(SECRET_LENGTH);
    return new Tuple2<>(randomSecret, Files.write(file, randomSecret.getBytes(UTF_8)));
}
Also used : Path(java.nio.file.Path) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2)

Example 5 with Tuple2

use of com.evolvedbinary.j8fu.tuple.Tuple2 in project exist by eXist-db.

the class AbstractTestRunner method executeQuery.

protected static Sequence executeQuery(final BrokerPool brokerPool, final Source query, final List<Function<XQueryContext, Tuple2<String, Object>>> externalVariableBindings) throws EXistException, PermissionDeniedException, XPathException, IOException, DatabaseConfigurationException {
    final SecurityManager securityManager = requireNonNull(brokerPool.getSecurityManager(), "securityManager is null");
    try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
        final XQueryPool queryPool = brokerPool.getXQueryPool();
        CompiledXQuery compiledQuery = queryPool.borrowCompiledXQuery(broker, query);
        try {
            XQueryContext context;
            if (compiledQuery == null) {
                context = new XQueryContext(broker.getBrokerPool());
            } else {
                context = compiledQuery.getContext();
                context.prepareForReuse();
            }
            // setup misc. context
            context.setBaseURI(new AnyURIValue("/db"));
            if (query instanceof FileSource) {
                final Path queryPath = Paths.get(((FileSource) query).getPath().toAbsolutePath().toString());
                if (Files.isDirectory(queryPath)) {
                    context.setModuleLoadPath(queryPath.toString());
                } else {
                    context.setModuleLoadPath(queryPath.getParent().toString());
                }
            }
            // declare variables for the query
            for (final Function<XQueryContext, Tuple2<String, Object>> externalVariableBinding : externalVariableBindings) {
                final Tuple2<String, Object> nameValue = externalVariableBinding.apply(context);
                context.declareVariable(nameValue._1, nameValue._2);
            }
            final XQuery xqueryService = brokerPool.getXQueryService();
            // compile or update the context
            if (compiledQuery == null) {
                compiledQuery = xqueryService.compile(context, query);
            } else {
                compiledQuery.getContext().updateContext(context);
                context.getWatchDog().reset();
            }
            return xqueryService.execute(broker, compiledQuery, null);
        } finally {
            queryPool.returnCompiledXQuery(query, compiledQuery);
        }
    }
}
Also used : Path(java.nio.file.Path) SecurityManager(org.exist.security.SecurityManager) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) FileSource(org.exist.source.FileSource) XQueryContext(org.exist.xquery.XQueryContext) XQueryPool(org.exist.storage.XQueryPool) DBBroker(org.exist.storage.DBBroker) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2)

Aggregations

Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)17 XmldbURI (org.exist.xmldb.XmldbURI)10 Function (java.util.function.Function)8 EXistException (org.exist.EXistException)8 Collection (org.exist.collections.Collection)8 Test (org.junit.Test)8 Tuple3 (com.evolvedbinary.j8fu.tuple.Tuple3)7 DocumentImpl (org.exist.dom.persistent.DocumentImpl)7 LockedDocument (org.exist.dom.persistent.LockedDocument)7 Lock (org.exist.storage.lock.Lock)7 LockException (org.exist.util.LockException)7 Path (java.nio.file.Path)6 BiFunction (java.util.function.BiFunction)6 LockMode (org.exist.storage.lock.Lock.LockMode)6 EasyMock.createStrictControl (org.easymock.EasyMock.createStrictControl)5 EasyMock.expect (org.easymock.EasyMock.expect)5 IMocksControl (org.easymock.IMocksControl)5 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 FluentBrokerAPI.uri (org.exist.storage.FluentBrokerAPI.uri)5 Assert.assertEquals (org.junit.Assert.assertEquals)5