Search in sources :

Example 1 with NodeIdFactory

use of org.exist.numbering.NodeIdFactory in project exist by eXist-db.

the class DOMFileRecoverTest method add.

@Test
public void add() throws EXistException, ReadOnlyException, TerminatedException, IOException, BTreeException {
    BrokerPool.FORCE_CORRUPTION = false;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final NodeIdFactory idFact = pool.getNodeFactory();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        // Add some random data and force db corruption
        broker.flush();
        final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        domDb.setOwnerObject(this);
        final TransactionManager mgr = pool.getTransactionManager();
        long firstToRemove = -1;
        try (final Txn txn = mgr.beginTransaction()) {
            // put 1000 values into the btree
            for (int i = 1; i <= 10000; i++) {
                byte[] data = ("Value" + i).getBytes();
                NodeId id = idFact.createInstance(i);
                long addr = domDb.put(txn, new NativeBroker.NodeRef(500, id), data);
                // TODO : test addr ?
                if (i == 1)
                    firstToRemove = addr;
            }
            domDb.closeDocument();
            // remove all
            NativeBroker.NodeRef ref = new NativeBroker.NodeRef(500);
            assertNotNull(ref);
            IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);
            assertNotNull(idx);
            domDb.remove(txn, idx, null);
            domDb.removeAll(txn, firstToRemove);
            // put some more
            for (int i = 1; i <= 10000; i++) {
                byte[] data = ("Value" + i).getBytes();
                @SuppressWarnings("unused") long addr = domDb.put(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)), data);
            // TODO : test addr ?
            }
            domDb.closeDocument();
            mgr.commit(txn);
        }
        try (final Txn txn = mgr.beginTransaction()) {
            // put 1000 new values into the btree
            for (int i = 1; i <= 1000; i++) {
                byte[] data = ("Value" + i).getBytes();
                long addr = domDb.put(txn, new NativeBroker.NodeRef(501, idFact.createInstance(i)), data);
                // TODO : test addr ?
                if (i == 1)
                    firstToRemove = addr;
            }
            domDb.closeDocument();
            mgr.commit(txn);
        }
        // the following transaction is not committed and will be rolled back during recovery
        try (final Txn txn = mgr.beginTransaction()) {
            for (int i = 1; i <= 200; i++) {
                domDb.remove(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)));
            }
            final IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, new NativeBroker.NodeRef(501));
            domDb.remove(txn, idx, null);
            domDb.removeAll(txn, firstToRemove);
            // Don't commit...
            mgr.commit(txn);
        }
        pool.getJournalManager().get().flush(true, false);
        Writer writer = new StringWriter();
        domDb.dump(writer);
    }
}
Also used : IndexQuery(org.exist.storage.btree.IndexQuery) DOMFile(org.exist.storage.dom.DOMFile) Txn(org.exist.storage.txn.Txn) NodeIdFactory(org.exist.numbering.NodeIdFactory) StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) NodeId(org.exist.numbering.NodeId) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 2 with NodeIdFactory

use of org.exist.numbering.NodeIdFactory in project exist by eXist-db.

the class BTreeRecoverTest method add.

private void add(final BrokerPool pool) throws EXistException, IOException, BTreeException, TerminatedException {
    // Add some random data and force db corruption
    final TransactionManager mgr = pool.getTransactionManager();
    final NodeIdFactory idFact = pool.getNodeFactory();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        broker.flush();
        final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        domDb.setOwnerObject(this);
        try (final Txn txn = mgr.beginTransaction()) {
            // put 1000 values into the btree
            for (int i = 1; i < 1001; i++) {
                final NodeId id = idFact.createInstance(i);
                domDb.addValue(txn, new NativeBroker.NodeRef(500, id), i);
            }
            final IndexQuery idx = new IndexQuery(IndexQuery.GT, new NativeBroker.NodeRef(500, idFact.createInstance(800)));
            domDb.remove(txn, idx, null);
            mgr.commit(txn);
        }
        // start a dirty, uncommitted transaction. This will be rolled back by the recovery.
        final Txn txn = mgr.beginTransaction();
        for (int i = 801; i < 2001; i++) {
            domDb.addValue(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)), i);
        }
        for (int i = 101; i < 301; i++) {
            domDb.addValue(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)), i * 3);
        }
        final IndexQuery idx = new IndexQuery(IndexQuery.GT, new NativeBroker.NodeRef(500, idFact.createInstance(600)));
        domDb.remove(txn, idx, null);
        // DO NOT COMMIT THE TRANSACTION!
        pool.getJournalManager().get().flush(true, false);
    }
}
Also used : NodeIdFactory(org.exist.numbering.NodeIdFactory) IndexQuery(org.exist.storage.btree.IndexQuery) TransactionManager(org.exist.storage.txn.TransactionManager) NodeId(org.exist.numbering.NodeId) DOMFile(org.exist.storage.dom.DOMFile) Txn(org.exist.storage.txn.Txn)

Example 3 with NodeIdFactory

use of org.exist.numbering.NodeIdFactory in project exist by eXist-db.

the class BTreeRecoverTest method get.

private void get(final BrokerPool pool) throws EXistException, TerminatedException, IOException, BTreeException {
    final NodeIdFactory idFact = pool.getNodeFactory();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        domDb.setOwnerObject(this);
        final IndexQuery query = new IndexQuery(IndexQuery.GEQ, new NativeBroker.NodeRef(500, idFact.createInstance(1)));
        domDb.query(query, new IndexCallback());
        assertEquals(count, 800);
    }
}
Also used : NodeIdFactory(org.exist.numbering.NodeIdFactory) IndexQuery(org.exist.storage.btree.IndexQuery) DOMFile(org.exist.storage.dom.DOMFile)

Example 4 with NodeIdFactory

use of org.exist.numbering.NodeIdFactory in project exist by eXist-db.

the class DocumentImpl method computeNodeIds.

private void computeNodeIds() throws EXistException {
    if (nodeId[0] != null) {
        return;
    }
    final NodeIdFactory nodeFactory = getDatabase().getNodeFactory();
    nodeId[0] = nodeFactory.documentNodeId();
    if (size == 1) {
        return;
    }
    NodeId nextId = nodeFactory.createInstance();
    NodeImpl next = (NodeImpl) getFirstChild();
    while (next != null) {
        computeNodeIds(nextId, next.nodeNumber);
        next = (NodeImpl) next.getNextSibling();
        nextId = nextId.nextSibling();
    }
}
Also used : NodeIdFactory(org.exist.numbering.NodeIdFactory) NodeId(org.exist.numbering.NodeId)

Aggregations

NodeIdFactory (org.exist.numbering.NodeIdFactory)4 NodeId (org.exist.numbering.NodeId)3 IndexQuery (org.exist.storage.btree.IndexQuery)3 DOMFile (org.exist.storage.dom.DOMFile)3 TransactionManager (org.exist.storage.txn.TransactionManager)2 Txn (org.exist.storage.txn.Txn)2 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Test (org.junit.Test)1