Package org.exist.storage.lock

Examples of org.exist.storage.lock.Lock


     *  Returns the raw data of the next node in document order.
     * @return the raw data of the node
     */
    public Value next() {
        Value nextValue = null;
        final Lock lock = db.getLock();
        try {
            try {
                lock.acquire(Lock.READ_LOCK);
            } catch (final LockException e) {
                LOG.error("Failed to acquire read lock on " + db.getFile().getName());
                //TODO : throw exception here ? -pb
                return null;
            }
            db.setOwnerObject(broker);
            long backLink = 0;
            do {
                final DOMFile.DOMFilePageHeader pageHeader = page.getPageHeader();
                //Next value larger than length of the current page?
                if (offset >= pageHeader.getDataLength()) {
                    //Load next page in chain
                    long nextPage = pageHeader.getNextDataPage();
                    if (nextPage == Paged.Page.NO_PAGE) {
                        SanityCheck.TRACE("Bad link to next page " + page.page.getPageInfo() +
                            "; previous: " + pageHeader.getPreviousDataPage() +
                            "; offset = " + offset + "; lastTupleID = " + lastTupleID);
                        //TODO : throw exception here ? -pb
                        return null;
                    }
                    pageNum = nextPage;
                    page = db.getDOMPage(nextPage);
                    db.addToBuffer(page);
                    offset = 0;
                }
                //Extract the tuple id
                lastTupleID = ByteConversion.byteToShort(page.data, offset);
                offset += DOMFile.LENGTH_TID;
                //Check if this is just a link to a relocated node
                if(ItemId.isLink(lastTupleID)) {
                    //Skip this
                    offset += DOMFile.LENGTH_FORWARD_LOCATION;
                    continue;
                }
                //Read data length
                short valueLength = ByteConversion.byteToShort(page.data, offset);
                offset += DOMFile.LENGTH_DATA_LENGTH;
                if (valueLength < 0) {
                    LOG.error("Got negative length" + valueLength + " at offset " + offset + "!!!");
                    LOG.debug(db.debugPageContents(page));
                    //TODO : throw an exception right now ?
                }
                if (ItemId.isRelocated(lastTupleID)) {
                    // found a relocated node. Read the original address
                    backLink = ByteConversion.byteToLong(page.data, offset);
                    offset += DOMFile.LENGTH_ORIGINAL_LOCATION;
                }
                //Overflow page? load the overflow value
                if (valueLength == DOMFile.OVERFLOW) {
                    valueLength = DOMFile.LENGTH_OVERFLOW_LOCATION;
                    final long overflow = ByteConversion.byteToLong(page.data, offset);
                    offset += DOMFile.LENGTH_OVERFLOW_LOCATION;
                    try {
                        final byte[] odata = db.getOverflowValue(overflow);
                        nextValue = new Value(odata);
                    } catch(final Exception e) {
                        LOG.error("Exception while loading overflow value: " + e.getMessage() +
                            "; originating page: " + page.page.getPageInfo());
                    }
                    // normal node
                } else {
                    try {
                        nextValue = new Value(page.data, offset, valueLength);
                        offset += valueLength;
                    } catch(final Exception e) {
                        LOG.error("Error while deserializing node: " + e.getMessage(), e);
                        LOG.error("Reading from offset: " + offset + "; len = " + valueLength);
                        LOG.debug(db.debugPageContents(page));
                        throw new RuntimeException(e);
                    }
                }
                if (nextValue == null) {
                    LOG.error("illegal node on page " + page.getPageNum() +
                        "; tupleID = " + ItemId.getId(lastTupleID) +
                        "; next = " + page.getPageHeader().getNextDataPage() +
                        "; prev = " + page.getPageHeader().getPreviousDataPage() +
                        "; offset = " + (offset - valueLength) +
                        "; len = " + page.getPageHeader().getDataLength());
                    //TODO : throw exception here ? -pb
                    return null;
                }
                if (ItemId.isRelocated(lastTupleID)) {
                    nextValue.setAddress(backLink);
                } else {
                    nextValue.setAddress(StorageAddress.createPointer((int) pageNum,
                        ItemId.getId(lastTupleID))
                    );
                }
            } while (nextValue == null);
            return nextValue;
        } finally {
            lock.release(Lock.READ_LOCK);
        }
    }
View Full Code Here


        final List<String> collections = new ArrayList<>();

        final Pattern p = Pattern.compile(regexp);
        final Matcher m = p.matcher("");

        final Lock lock = collectionsDb.getLock();
        try {
            lock.acquire(Lock.READ_LOCK);

            //TODO write a regexp lookup for key data in BTree.query
            //IndexQuery idxQuery = new IndexQuery(IndexQuery.REGEXP, regexp);
            //List<Value> keys = collectionsDb.findKeysByCollectionName(idxQuery);
            final List<Value> keys = collectionsDb.getKeys();

            for(final Value key : keys) {

                //TODO restrict keys to just collection uri's

                final String collectionName = new String(key.getData());
                m.reset(collectionName);

                if(m.matches()) {
                    collections.add(collectionName);
                }
            }
        } catch(final UnsupportedEncodingException e) {
            //LOG.error("Unable to encode '" + uri + "' in UTF-8");
            //return null;
        } catch(final LockException e) {
            LOG.warn("Failed to acquire lock on " + collectionsDb.getFile().getName());
            //return null;
        } catch(final TerminatedException | IOException | BTreeException e) {
            LOG.error(e.getMessage(), e);
            //return null;
        } finally {
            lock.release(Lock.READ_LOCK);
        }

        return collections;
    }
View Full Code Here

        }
        return flushed;
    }

    public void freeResourceId(int id) {
        final Lock lock = getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);

            freeResourceIds.push(id);
        } catch (LockException e) {
            LOG.warn("Failed to acquire lock on " + getFile().getName(), e);
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
    }
View Full Code Here

        }
    }

    public int getFreeResourceId() {
        int freeDocId = DocumentImpl.UNKNOWN_DOCUMENT_ID;
        final Lock lock = getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);

            if (!freeResourceIds.isEmpty()) {
                freeDocId = freeResourceIds.pop();
            }
        } catch (final LockException e) {
            LOG.warn("Failed to acquire lock on " + getFile().getName(), e);
            return DocumentImpl.UNKNOWN_DOCUMENT_ID;
            //TODO : rethrow ? -pb
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
        return freeDocId;
    }
View Full Code Here

        }
        return freeDocId;
    }

    public void freeCollectionId(int id) {
        final Lock lock = getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);

            freeCollectionIds.push(id);
        } catch (LockException e) {
            LOG.warn("Failed to acquire lock on " + getFile().getName(), e);
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
    }
View Full Code Here

        }
    }

    public int getFreeCollectionId() {
        int freeCollectionId = Collection.UNKNOWN_COLLECTION_ID;
        final Lock lock = getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);

            if (!freeCollectionIds.isEmpty()) {
                freeCollectionId = freeCollectionIds.pop();
            }
        } catch (final LockException e) {
            LOG.warn("Failed to acquire lock on " + getFile().getName(), e);
            return Collection.UNKNOWN_COLLECTION_ID;
            //TODO : rethrow ? -pb
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
        return freeCollectionId;
    }
View Full Code Here

        Collection collection;
        final CollectionCache collectionsCache = pool.getCollectionsCache();
        synchronized(collectionsCache) {
            collection = collectionsCache.get(uri);
            if(collection == null) {
                final Lock lock = collectionsDb.getLock();
                try {
                    lock.acquire(Lock.READ_LOCK);

                    final Value key = new CollectionStore.CollectionKey(uri.toString());
                    final VariableByteInput is = collectionsDb.getAsStream(key);
                    if(is == null) {
                        LOG.warn("Could not read collection entry for: " + uri);
                        return;
                    }

                    //read the entry details
                    entry.read(is);

                } catch(final UnsupportedEncodingException e) {
                    LOG.error("Unable to encode '" + uri + "' in UTF-8");
                } catch(final LockException e) {
                    LOG.warn("Failed to acquire lock on " + collectionsDb.getFile().getName());
                } catch(final IOException e) {
                    LOG.error(e.getMessage(), e);
                } finally {
                    lock.release(Lock.READ_LOCK);
                }
            } else {

                if(!collection.getURI().equalsInternal(uri)) {
                    LOG.error("The collection received from the cache is not the requested: " + uri +
View Full Code Here

            }
            if (btree == null) {
                System.console().printf("Unkown index: %s\n", id);
                return;
            }
            final Lock lock = btree.getLock();
            try {
                lock.acquire(Lock.WRITE_LOCK);

                System.console().printf("Rebuilding %15s ...", btree.getFile().getName());
                btree.rebuild();
                System.out.println("Done");
            } finally {
                lock.release(Lock.WRITE_LOCK);
            }

        } catch (Exception e) {
            System.console().printf("An exception occurred during repair: %s\n", e.getMessage());
            e.printStackTrace();
View Full Code Here

        Collection collection;
        final CollectionCache collectionsCache = pool.getCollectionsCache();
        synchronized(collectionsCache) {
            collection = collectionsCache.get(uri);
            if(collection == null) {
                final Lock lock = collectionsDb.getLock();
                try {
                    lock.acquire(Lock.READ_LOCK);
                    VariableByteInput is;
                    if(address == BFile.UNKNOWN_ADDRESS) {
                        final Value key = new CollectionStore.CollectionKey(uri.toString());
                        is = collectionsDb.getAsStream(key);
                    } else {
                        is = collectionsDb.getAsStream(address);
                    }
                    if(is == null) {
                        return null;
                    }
                    collection = new Collection(this, uri);
                    collection.read(this, is);
                    //TODO : manage this from within the cache -pb
                    if(!pool.isInitializing()) {
                        collectionsCache.add(collection);
                    }
                    //TODO : rethrow exceptions ? -pb
                } catch(final UnsupportedEncodingException e) {
                    LOG.error("Unable to encode '" + uri + "' in UTF-8");
                    return null;
                } catch(final LockException e) {
                    LOG.warn("Failed to acquire lock on " + collectionsDb.getFile().getName());
                    return null;
                } catch(final IOException e) {
                    LOG.error(e.getMessage(), e);
                    return null;
                } finally {
                    lock.release(Lock.READ_LOCK);
                }
            } else {
                if(!collection.getURI().equalsInternal(uri)) {
                    LOG.error("The collection received from the cache is not the requested: " + uri +
                        "; received: " + collection.getURI());
View Full Code Here

            throw new PermissionDeniedException("Cannot move collection to itself '" + collection.getURI() + "'.");
        }

        final CollectionCache collectionsCache = pool.getCollectionsCache();
        synchronized(collectionsCache) {
            final Lock lock = collectionsDb.getLock();
            try {
                pool.getProcessMonitor().startJob(ProcessMonitor.ACTION_COPY_COLLECTION, collection.getURI());
                lock.acquire(Lock.WRITE_LOCK);

                final XmldbURI parentName = collection.getParentURI();
                final Collection parent = parentName == null ? collection : getCollection(parentName);

                final CollectionTrigger trigger = new CollectionTriggers(this, parent);
                trigger.beforeCopyCollection(this, transaction, collection, dstURI);

                //atomically check all permissions in the tree to ensure a copy operation will succeed before starting copying
                checkPermissionsForCopy(collection, destination.getURI(), newName);

                final DocumentTrigger docTrigger = new DocumentTriggers(this);

                final Collection newCollection = doCopyCollection(transaction, docTrigger, collection, destination, newName);

                trigger.afterCopyCollection(this, transaction, newCollection, srcURI);
            } finally {
                lock.release(Lock.WRITE_LOCK);
                pool.getProcessMonitor().endJob();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.exist.storage.lock.Lock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.