Package org.exist.storage.lock

Examples of org.exist.storage.lock.Lock


    protected void removeQNamesForDoc(DocumentImpl doc) {
        final byte[] fromKey = computeDocKey(doc.getDocId());
        final byte[] toKey = computeDocKey(doc.getDocId() + 1);
        final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
        final Lock lock = index.btree.getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);
            index.btree.remove(query, null);
        } catch (final LockException e) {
            NativeStructuralIndex.LOG.warn("Failed to lock structural index: " + e.getMessage(), e);
        } catch (final Exception e) {
            NativeStructuralIndex.LOG.warn("Exception caught while reading structural index for document " +
                doc.getURI() + ": " + e.getMessage(), e);
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
    }
View Full Code Here


        if (index.btree == null)
            {return qnames;}
        final byte[] fromKey = computeDocKey(doc.getDocId());
        final byte[] toKey = computeDocKey(doc.getDocId() + 1);
        final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
        final Lock lock = index.btree.getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);
            index.btree.query(query, new BTreeCallback() {
                public boolean indexInfo(Value value, long pointer) throws TerminatedException {
                    final QName qname = readQName(value.getData());
                    qnames.add(qname);
                    return true;
                }
            });
        } catch (final LockException e) {
            NativeStructuralIndex.LOG.warn("Failed to lock structural index: " + e.getMessage(), e);
        } catch (final Exception e) {
            NativeStructuralIndex.LOG.warn("Exception caught while reading structural index for document " +
                doc.getURI() + ": " + e.getMessage(), e);
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
        return qnames;
    }
View Full Code Here

                }
                final byte[] fromKey = computeKey(qname.getNameType(), qname, doc.getDocId());
                final byte[] toKey = computeKey(qname.getNameType(), qname, doc.getDocId() + 1);
                final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));

                final Lock lock = index.btree.getLock();
                try {
                    lock.acquire(Lock.READ_LOCK);
                    index.btree.query(query, new BTreeCallback() {
                        public boolean indexInfo(Value value, long pointer) throws TerminatedException {
                            Occurrences oc = occurrences.get(name);
                            if (oc == null) {
                                oc = new Occurrences(name);
                                occurrences.put(name, oc);
                                oc.addDocument(doc);
                                oc.addOccurrences(1);
                            } else {
                                oc.addOccurrences(1);
                                oc.addDocument(doc);
                            }
                            return true;
                        }
                    });
                } catch (final LockException e) {
                    NativeStructuralIndex.LOG.warn("Failed to lock structural index: " + e.getMessage(), e);
                } catch (final Exception e) {
                    NativeStructuralIndex.LOG.warn("Exception caught while reading structural index for document " +
                            doc.getURI() + ": " + e.getMessage(), e);
                } finally {
                    lock.release(Lock.READ_LOCK);
                }
            }
        }
        final Occurrences[] result = new Occurrences[occurrences.size()];
        int i = 0;
View Full Code Here

    private void processPending() {
        if (pending.size() == 0)
            {return;}

        try {
            final Lock lock = index.btree.getLock();
            for (final Map.Entry<QName,List<NodeProxy>> entry: pending.entrySet()) {
                final QName qname = entry.getKey();
                try {
                    lock.acquire(Lock.WRITE_LOCK);
                    final List<NodeProxy> nodes = entry.getValue();
                    for (final NodeProxy proxy : nodes) {
                        final NodeId nodeId = proxy.getNodeId();
                        final byte[] key = computeKey(qname.getNameType(), qname, document.getDocId(), nodeId);
                        index.btree.addValue(new Value(key), computeValue(proxy));
                    }
                    final Value docKey = new Value(computeDocKey(qname.getNameType(), document.getDocId(), qname));
                    if (index.btree.findValue(docKey) == -1) {
                        index.btree.addValue(docKey, 0);
                    }
                } catch (final LockException e) {
                    NativeStructuralIndex.LOG.warn("Failed to lock structural index: " + e.getMessage(), e);
                // } catch (ReadOnlyException e) {
                //    NativeStructuralIndex.LOG.warn("Read-only error: " + e.getMessage(), e);
                } catch (final Exception e) {
                    NativeStructuralIndex.LOG.warn("Exception caught while writing to structural index: " + e.getMessage(), e);
                } finally {
                    lock.release(Lock.WRITE_LOCK);
                }
            }
        } finally {
            pending.clear();
        }
View Full Code Here

    /* (non-Javadoc)
     * @see org.exist.storage.ContentLoadingObserver#sync()
     */
    public void sync() {
        final Lock lock = dbTokens.getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);
            dbTokens.flush();
        } catch (final LockException e) {
            LOG.warn("Failed to acquire lock for '" + dbTokens.getFile().getName() + "'", e);
            //TODO : throw an exception ? -pb
        } catch (final DBException e) {
            LOG.error(e.getMessage(), e);
            //TODO : throw an exception ? -pb
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
    }
View Full Code Here

    /* Drop all index entries for the given collection.
     * @see org.exist.storage.ContentLoadingObserver#dropIndex(org.exist.collections.Collection)
     */
    public void dropIndex(Collection collection) {
        final Lock lock = dbTokens.getLock();
        try {
            lock.acquire(Lock.WRITE_LOCK);
            // remove generic index
            Value value = new WordRef(collection.getId());
            dbTokens.removeAll(null, new IndexQuery(IndexQuery.TRUNC_RIGHT, value));
            // remove QName index
            value = new QNameWordRef(collection.getId());
            dbTokens.removeAll(null, new IndexQuery(IndexQuery.TRUNC_RIGHT, value));
        } catch (final LockException e) {
            LOG.warn("Failed to acquire lock for '" + dbTokens.getFile().getName() + "'", e);
            //TODO : throw exception ? -pb
        } catch (final BTreeException e) {
            LOG.error(e.getMessage(), e);
            //TODO : throw exception ? -pb
        } catch (final IOException e) {
            LOG.error(e.getMessage(), e);
            //TODO: throw exception ? -pb
        } finally {
            lock.release(Lock.WRITE_LOCK);
        }
    }
View Full Code Here

            if (qname == null)
                {key = new WordRef(collectionId, token);}
            else {
                key = new QNameWordRef(collectionId, qname, token, broker.getBrokerPool().getSymbols());
            }
            final Lock lock = dbTokens.getLock();
            try {
                lock.acquire(Lock.READ_LOCK);
                final VariableByteInput is = dbTokens.getAsStream(key);
                //Does the token already has data in the index ?
                if (is == null)
                    {continue;}
                while (is.available() > 0) {
                    final int storedDocId = is.readInt();
                    final int storedSection = is.readByte();
                    final int gidsCount = is.readInt();
                    //Read (variable) length of node IDs + frequency + offsets      
                    final int length = is.readFixedInt();
                    final DocumentImpl storedDocument = docs.getDoc(storedDocId);
                    //Exit if the document is not concerned
                    if (storedDocument == null) {
                        is.skipBytes(length);
                        continue;
                    }
                    //Process the nodes
                    NodeId previous = null;
                    for (int m = 0; m < gidsCount; m++) {
                        NodeId nodeId = broker.getBrokerPool().getNodeFactory().createFromStream(previous, is);
                        previous = nodeId;
                        final int freq = is.readInt();
                        NodeProxy storedNode;
                        switch (storedSection) {
                        case ATTRIBUTE_SECTION :
                            storedNode = new NodeProxy(storedDocument, nodeId, Node.ATTRIBUTE_NODE);
                            break;
                        case TEXT_SECTION :
                            storedNode = new NodeProxy(storedDocument, nodeId, Node.TEXT_NODE);
                            break;
                        case QNAME_SECTION :
                            storedNode = new NodeProxy(storedDocument, nodeId,
                                qname.getNameType() == ElementValue.ATTRIBUTE ?
                                    Node.ATTRIBUTE_NODE : Node.ELEMENT_NODE);
                            break;
                        default :
                            throw new IllegalArgumentException("Invalid section type in '" +
                                dbTokens.getFile().getName() + "'");
                        }
                        // if a context set is specified, we can directly check if the
                        // matching text node is a descendant of one of the nodes
                        // in the context set.
                        if (contextSet != null) {
                            NodeProxy parent;
                            switch(storedSection) {
                            case ATTRIBUTE_SECTION :
                                if (contextSet instanceof VirtualNodeSet) {
                                    parent = contextSet.parentWithChild(storedNode,
                                        false, true, NodeProxy.UNKNOWN_NODE_LEVEL);
                                    if (parent != null && !parent.getNodeId().equals(storedNode.getNodeId()))
                                        {parent = null;}
                                } else
                                    {parent = contextSet.get(storedNode);}
                                break;
                            case QNAME_SECTION:
                            case TEXT_SECTION :
                                parent = contextSet.parentWithChild(storedNode,
                                    false, true, NodeProxy.UNKNOWN_NODE_LEVEL);
                                break;
                            default :
                                throw new IllegalArgumentException("Invalid section type in '" + dbTokens.getFile().getName() + "'");
                            }
                            if (parent != null) {
                                final Match match = new FTMatch(-1, nodeId, token, freq);
                                readOccurrences(freq, is, match, token.length());
                                if (axis == NodeSet.ANCESTOR) {
                                    parent.addMatch(match);
                                    final int sizeHint = contextSet.getSizeHint(storedDocument);
                                    result.add(parent, sizeHint);
                                } else {
                                    storedNode.addMatch(match);
                                    final int sizeHint = contextSet.getSizeHint(storedDocument);
                                    result.add(storedNode, sizeHint);
                                }
                            } else {
                                is.skip(freq);
                            }
                        //Otherwise, we add all text nodes without check
                        } else {
                            final Match match = new FTMatch(-1, nodeId, token, freq);
                            readOccurrences(freq, is, match, token.length());
                            storedNode.addMatch(match);
                            result.add(storedNode, Constants.NO_SIZE_HINT);
                        }
                        context.proceed();
                    }
                }
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '" + dbTokens.getFile().getName() + "'", e);
                //TODO : throw exception ? -pb
            } catch (final IOException e) {
                LOG.error(e.getMessage() + " in '" +
                    dbTokens.getFile().getName() + "'", e);
                //TODO : throw exception ? -pb
            } finally {
                lock.release(Lock.READ_LOCK);
            }
        }
        return result;
    }
View Full Code Here

        if (LOG.isTraceEnabled() && qname != null)
            {LOG.trace("Index lookup by QName: " + qname);}
        final NodeSet result = new NewArrayNodeSet();
        final SearchCallback cb = new SearchCallback(context, matcher, result,
            contextSet, axis, docs, qname);
        final Lock lock = dbTokens.getLock();   
        for (final Iterator<Collection> iter = docs.getCollectionIterator(); iter.hasNext();) {
            final int collectionId = ((Collection) iter.next()).getId();
            //Compute a key for the token
            Value value;
            if (startTerm != null && startTerm.length() > 0) {
                //TODO : case conversion should be handled by the tokenizer -pb
                if (qname == null) {
                    value = new WordRef(collectionId, startTerm.toString().toLowerCase());
                } else {
                    value = new QNameWordRef(collectionId, qname, startTerm.toString().toLowerCase(),
                            broker.getBrokerPool().getSymbols());
                }
            } else {
                if (qname == null) {
                    value = new WordRef(collectionId);
                } else {
                    value = new QNameWordRef(collectionId, qname, broker.getBrokerPool().getSymbols());
                }
            }
            final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, value);
            try {
                lock.acquire(Lock.READ_LOCK);
                dbTokens.query(query, cb);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '" + dbTokens.getFile().getName() + "'", e);
                //TODO : throw exception ? -pb
            } catch (final BTreeException e) {
                LOG.error(e.getMessage(), e);
                //TODO : throw exception ? -pb
            } catch (final IOException e) {
                LOG.error(e.getMessage(), e);
              //TODO : throw exception ? -pb
            } finally {
                lock.release(Lock.READ_LOCK);
            }
        }
        return result;
    }
View Full Code Here

        return result;
    }

    public String[] getIndexTerms(DocumentSet docs, TermMatcher matcher) {
        final IndexCallback cb = new IndexCallback(null, matcher);
        final Lock lock = dbTokens.getLock();
        for (final Iterator<Collection> iter = docs.getCollectionIterator(); iter.hasNext();) {
            final int collectionId = ((Collection) iter.next()).getId();
            //Compute a key for the token
            final Value value = new WordRef(collectionId);
            final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, value);
            try {
                lock.acquire(Lock.READ_LOCK);
                dbTokens.query(query, cb);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '" + dbTokens.getFile().getName() + "'", e);
                //TODO : throw exception ? -pb
            } catch (final IOException e) {
                LOG.error(e.getMessage(), e);
                //TODO : throw exception ? -pb
            } catch (final BTreeException e) {
                LOG.error(e.getMessage(), e);
                //TODO: throw exception ? -pb
            } catch (final TerminatedException e) {
                LOG.warn(e.getMessage(), e);
                //TODO : throw exception ? -pb
            } finally {
                lock.release(Lock.READ_LOCK);
            }
        }
        return cb.getMatches();
    }
View Full Code Here

    }

    public Occurrences[] scanIndexTerms(DocumentSet docs, NodeSet contextSet,
            String start, String end) throws PermissionDeniedException {
        final IndexScanCallback cb = new IndexScanCallback(docs, contextSet, false);
        final Lock lock = dbTokens.getLock();
        for (final Iterator<Collection> i = docs.getCollectionIterator(); i.hasNext();) {
            final int collectionId = (i.next()).getId();
            final IndexQuery query;
            if (start == null) {
                final Value startRef = new WordRef(collectionId);
                query = new IndexQuery(IndexQuery.TRUNC_RIGHT, startRef);
            } else if (end == null) {
                final Value startRef = new WordRef(collectionId, start.toLowerCase());
                query = new IndexQuery(IndexQuery.TRUNC_RIGHT, startRef);
            } else {
                final Value startRef = new WordRef(collectionId,  start.toLowerCase());
                final Value endRef = new WordRef(collectionId, end.toLowerCase());
                query = new IndexQuery(IndexQuery.BW, startRef, endRef);
            }
            try {
                lock.acquire(Lock.READ_LOCK);
                dbTokens.query(query, cb);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '" + dbTokens.getFile().getName() + "'", e);
                //TODO : throw exception ? -pb
            } catch (final IOException e) {
                LOG.error(e.getMessage(), e);
                //TODO : throw exception ? -pb
            } catch (final BTreeException e) {
                LOG.error(e.getMessage(), e);
                //TODO : throw exception ? -pb
            } catch (final TerminatedException e) {
                LOG.warn(e.getMessage(), e);
                //TODO : throw exception ? -pb
            } finally {
                lock.release(Lock.READ_LOCK);
            }
        }
        final Occurrences[] result = new Occurrences[cb.map.size()];
        return (Occurrences[]) cb.map.values().toArray(result);
    }
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.