Search in sources :

Example 6 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class AbstractIndexer method removeDocument.

/**
 * This method remove index of lucene a document identified by identifier.
 */
public void removeDocument(final String identifier) {
    try {
        final Directory dir = LuceneUtils.getAppropriateDirectory(getFileDirectory());
        final Term t = new Term("id", identifier);
        final TermQuery query = new TermQuery(t);
        LOGGER.log(Level.FINER, "Term query:{0}", query);
        // look for DOC ID for R-Tree removal
        final NamedEnvelope env = new NamedEnvelope(getTreeCrs(), identifier);
        final TreeElementMapper<NamedEnvelope> mapper = rTree.getTreeElementMapper();
        final int treeID = mapper.getTreeIdentifier(env);
        if (treeID != -1) {
            final NamedEnvelope realEnv = mapper.getObjectFromTreeIdentifier(treeID);
            boolean removed = rTree.remove(realEnv);
            if (!removed) {
                LOGGER.log(Level.WARNING, "unable to remove envelope for:{0}", identifier);
            } else {
                // remove from mapper
                mapper.setTreeIdentifier(null, treeID);
                mapper.flush();
                rTree.flush();
            }
        }
        final IndexWriterConfig config = new IndexWriterConfig(analyzer);
        try (final IndexWriter writer = new IndexWriter(dir, config)) {
            writer.deleteDocuments(query);
            LOGGER.log(Level.FINER, "Metadata: {0} removed from the index", identifier);
            writer.commit();
        }
    } catch (CorruptIndexException ex) {
        LOGGER.log(Level.WARNING, "CorruptIndexException while indexing document: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        LOGGER.log(Level.WARNING, "IOException while indexing document: " + ex.getMessage(), ex);
    } catch (StoreIndexException ex) {
        LOGGER.log(Level.WARNING, "StoreIndexException while indexing document: " + ex.getMessage(), ex);
    }
}
Also used : StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) TermQuery(org.apache.lucene.search.TermQuery) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) IndexWriter(org.apache.lucene.index.IndexWriter) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 7 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class SQLRtreeManager method buildNewTree.

private static Tree buildNewTree(final Path directory) {
    if (Files.exists(directory)) {
        TreeElementMapper treeMapper = null;
        try {
            // creating tree (R-Tree)------------------------------------------------
            final Path treeFile = directory.resolve("tree.bin");
            Files.createFile(treeFile);
            // postgres
            if (PGDataSource.isSetPGDataSource()) {
                if (PGDataSource.isPostgres) {
                    treeMapper = LucenePostgresSQLTreeEltMapper.createTreeEltMapperWithDB(directory);
                    return new PGTreeWrapper(directory, PGDataSource.getDataSource(), treeMapper, DEFAULT_CRS);
                } else {
                    treeMapper = LuceneHSQLTreeEltMapper.createTreeEltMapperWithDB(directory);
                    return new PGTreeWrapper(directory, PGDataSource.getDataSource(), treeMapper, DEFAULT_CRS);
                }
            } else {
                treeMapper = LuceneDerbySQLTreeEltMapper.createTreeEltMapperWithDB(directory);
                return new FileStarRTree(treeFile.toFile().toPath(), 5, DEFAULT_CRS, treeMapper);
            }
        } catch (SQLException | IOException | StoreIndexException ex) {
            if (treeMapper != null) {
                try {
                    treeMapper.close();
                } catch (Exception bis) {
                    ex.addSuppressed(bis);
                }
            }
            LOGGER.log(Level.WARNING, "Unable to create RTree", ex);
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) PGTreeWrapper(org.geotoolkit.index.tree.manager.postgres.PGTreeWrapper) StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) SQLException(java.sql.SQLException) TreeElementMapper(org.geotoolkit.index.tree.TreeElementMapper) IOException(java.io.IOException) FileStarRTree(org.geotoolkit.index.tree.star.FileStarRTree) IOException(java.io.IOException) SQLException(java.sql.SQLException) StoreIndexException(org.geotoolkit.index.tree.StoreIndexException)

Example 8 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class PGTreeWrapper method updateTree.

private void updateTree() throws StoreIndexException {
    try {
        if ((System.currentTimeMillis() - lastUpdate) > (5 * 60 * 1000)) {
            byte[] data = TreeAccessSQLByteArray.getData(directory, source);
            TreeAccess ta = new TreeAccessSQLByteArray(directory, source, data, TreeUtilities.STAR_NUMBER, TreeUtilities.VERSION_NUMBER);
            rTree = new StarRTree<>(ta, mapper);
            lastUpdate = System.currentTimeMillis();
            LOGGER.info("The R-tree has been updated");
        }
    } catch (SQLException | IOException | ClassNotFoundException | NoSuchAlgorithmException ex) {
        throw new StoreIndexException(ex);
    }
}
Also used : StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) SQLException(java.sql.SQLException) TreeAccess(org.geotoolkit.internal.tree.TreeAccess) TreeAccessSQLByteArray(org.geotoolkit.internal.tree.TreeAccessSQLByteArray) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 9 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class StarRTree method insert.

/**
 * {@inheritDoc }.
 */
@Override
public void insert(int identifier, double... coordinates) throws IllegalArgumentException, StoreIndexException {
    try {
        eltCompteur++;
        Node root = getRoot();
        if (root == null || root.isEmpty()) {
            root = createNode(null, IS_LEAF, 0, 0, 0);
            root.addChild(createNode(coordinates, IS_DATA, 1, 0, -identifier));
            setRoot(root);
        } else {
            travelUpBeforeInsertAgain = false;
            final Node newRoot = nodeInsert(root, identifier, coordinates);
            if (newRoot != null) {
                setRoot(newRoot);
                treeAccess.writeNode((Node) newRoot);
            }
            /**
             * Insert again. Property named Tree re-balancing.
             */
            if (travelUpBeforeInsertAgain) {
                travelUpBeforeInsertAgain = false;
                // insert again
                final int siz = listCoords.size();
                assert (siz == listIdentifier.size()) : "getElementAtMore33Percent : nodeInsert : lists should have same size.";
                setIA(false);
                final int nbrElt = getElementsNumber();
                // gere quand root == null
                final int treeIdent = treeIdentifier;
                for (int i = 0; i < siz; i++) {
                    if (!remove(listIdentifier.get(i), listCoords.get(i))) {
                        throw new AssertionError("remove data should succeed during re-inserting event. " + "data identifier : " + listIdentifier.get(i) + " data boundary : " + Arrays.toString(listCoords.get(i)));
                    }
                }
                for (int i = 0; i < siz; i++) {
                    insert(listIdentifier.get(i), listCoords.get(i));
                }
                setIA(true);
                assert nbrElt == getElementsNumber() : "During Insert again element number within tree should not change.";
                treeIdentifier = treeIdent;
            }
        }
    } catch (IOException ex) {
        throw new StoreIndexException(this.getClass().getName() + "Tree.insert(), impossible to add element.", ex);
    }
}
Also used : StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) Node(org.geotoolkit.index.tree.Node) IOException(java.io.IOException)

Example 10 with StoreIndexException

use of org.geotoolkit.index.tree.StoreIndexException in project geotoolkit by Geomatys.

the class FileRtreeManager method get.

public static Tree<NamedEnvelope> get(final Path directory, final Object owner) {
    Tree<NamedEnvelope> tree = CACHED_TREES.get(directory);
    if (tree == null || tree.isClosed()) {
        final Path treeFile = directory.resolve("tree.bin");
        final Path mapperFile = directory.resolve("mapper.bin");
        if (Files.exists(treeFile)) {
            try {
                // ecrire crs dans constructeur
                tree = new FileStarRTree<>(treeFile.toFile().toPath(), new LuceneFileTreeEltMapper(mapperFile.toFile(), DEFAULT_CRS));
            } catch (ClassNotFoundException | IllegalArgumentException | StoreIndexException | IOException ex) {
                LOGGER.log(Level.SEVERE, null, ex);
            }
        } else {
            tree = buildNewTree(directory);
        }
        final List<Object> owners = new ArrayList<>();
        owners.add(owner);
        TREE_OWNERS.put(directory, owners);
        CACHED_TREES.put(directory, tree);
    } else {
        // look if the owner is already registred
        final List<Object> owners = TREE_OWNERS.get(directory);
        if (!owners.contains(owner)) {
            owners.add(owner);
        }
    }
    return tree;
}
Also used : Path(java.nio.file.Path) StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)10 StoreIndexException (org.geotoolkit.index.tree.StoreIndexException)10 SQLException (java.sql.SQLException)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)4 NamedEnvelope (org.geotoolkit.index.tree.manager.NamedEnvelope)3 Path (java.nio.file.Path)2 IndexingException (org.geotoolkit.index.IndexingException)2 TreeElementMapper (org.geotoolkit.index.tree.TreeElementMapper)2 FactoryException (org.opengis.util.FactoryException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Quantity (javax.measure.Quantity)1 Document (org.apache.lucene.document.Document)1 StoredField (org.apache.lucene.document.StoredField)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1 LeafReader (org.apache.lucene.index.LeafReader)1 Term (org.apache.lucene.index.Term)1 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)1