Search in sources :

Example 6 with ObjectDatabase

use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.

the class DiffTreeTest method testBoundsFiltering.

@Test
public void testBoundsFiltering() {
    ObjectDatabase db = geogit.getContext().objectDatabase();
    RevTree tree1 = tree(1000, db);
    RevTree tree2 = tree(50, db);
    RevTree root = createRoot(db, tree1, tree2);
    CoordinateReferenceSystem crs = revtype.type().getCoordinateReferenceSystem();
    ReferencedEnvelope filter;
    List<DiffEntry> diffs;
    diffTree.setOldTree(ObjectId.NULL).setNewTree(root.getId());
    filter = new ReferencedEnvelope(50, 51, 50, 51, crs);
    diffTree.setBoundsFilter(filter);
    diffs = ImmutableList.copyOf(diffTree.call());
    assertEquals(2, diffs.size());
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Test(org.junit.Test)

Example 7 with ObjectDatabase

use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.

the class JEObjectDatabase method createDatabase.

protected Database createDatabase() {
    final String databaseName = "ObjectDatabase";
    Environment environment;
    try {
        environment = createEnvironment(readOnly);
    } catch (EnvironmentLockedException e) {
        throw new IllegalStateException("The repository is already open by another process for writing", e);
    }
    if (!environment.getDatabaseNames().contains(databaseName)) {
        if (readOnly) {
            environment.close();
            try {
                environment = createEnvironment(false);
            } catch (EnvironmentLockedException e) {
                throw new IllegalStateException(String.format("Environment open readonly but database %s does not exist.", databaseName));
            }
        }
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        Database openDatabase = environment.openDatabase(null, databaseName, dbConfig);
        openDatabase.close();
        environment.flushLog(true);
        environment.close();
        environment = createEnvironment(readOnly);
    }
    // System.err.println("Opened ObjectDatabase at " + env.getHome()
    // + ". Environment read-only: " + environment.getConfig().getReadOnly()
    // + " database read only: " + this.readOnly);
    Database database;
    try {
        LOGGER.debug("Opening ObjectDatabase at {}", environment.getHome());
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setCacheMode(CacheMode.MAKE_COLD);
        // can result in a slightly smaller db size
        dbConfig.setKeyPrefixing(false);
        dbConfig.setReadOnly(readOnly);
        boolean transactional = environment.getConfig().getTransactional();
        dbConfig.setTransactional(transactional);
        dbConfig.setDeferredWrite(!transactional);
        database = environment.openDatabase(null, databaseName, dbConfig);
    } catch (RuntimeException e) {
        if (environment != null) {
            environment.close();
        }
        throw e;
    }
    this.env = environment;
    return database;
}
Also used : EnvironmentLockedException(com.sleepycat.je.EnvironmentLockedException) AbstractObjectDatabase(org.locationtech.geogig.storage.AbstractObjectDatabase) ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) Database(com.sleepycat.je.Database) Environment(com.sleepycat.je.Environment) DatabaseConfig(com.sleepycat.je.DatabaseConfig)

Example 8 with ObjectDatabase

use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.

the class CommitOp method _call.

/**
     * Executes the commit operation.
     * 
     * @return the commit just applied, or {@code null} if
     *         {@code getProgressListener().isCanceled()}
     * @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
     * @throws NothingToCommitException if there are no staged changes by comparing the index
     *         staging tree and the repository HEAD tree.
     */
@Override
protected RevCommit _call() throws RuntimeException {
    final String committer = resolveCommitter();
    final String committerEmail = resolveCommitterEmail();
    final String author = resolveAuthor();
    final String authorEmail = resolveAuthorEmail();
    final Long authorTime = getAuthorTimeStamp();
    final Long committerTime = getCommitterTimeStamp();
    final Integer authorTimeZoneOffset = getAuthorTimeZoneOffset();
    final Integer committerTimeZoneOffset = getCommitterTimeZoneOffset();
    getProgressListener().started();
    float writeTreeProgress = 99f;
    if (all) {
        writeTreeProgress = 50f;
        AddOp op = command(AddOp.class);
        for (String st : pathFilters) {
            op.addPattern(st);
        }
        op.setUpdateOnly(true).setProgressListener(subProgress(49f)).call();
    }
    if (getProgressListener().isCanceled()) {
        return null;
    }
    final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
    checkState(currHead.isPresent(), "Repository has no HEAD, can't commit");
    final Ref headRef = currHead.get();
    checkState(//
    headRef instanceof SymRef, "HEAD is in a dettached state, cannot commit. Create a branch from it before committing");
    final String currentBranch = ((SymRef) headRef).getTarget();
    final ObjectId currHeadCommitId = headRef.getObjectId();
    Supplier<RevTree> oldRoot = resolveOldRoot();
    if (!currHeadCommitId.isNull()) {
        if (amend) {
            RevCommit headCommit = command(RevObjectParse.class).setObjectId(currHeadCommitId).call(RevCommit.class).get();
            parents.addAll(headCommit.getParentIds());
            if (message == null || message.isEmpty()) {
                message = headCommit.getMessage();
            }
            RevTree commitTree = command(RevObjectParse.class).setObjectId(headCommit.getTreeId()).call(RevTree.class).get();
            oldRoot = Suppliers.ofInstance(commitTree);
        } else {
            parents.add(0, currHeadCommitId);
        }
    } else {
        Preconditions.checkArgument(!amend, "Cannot amend. There is no previous commit to amend");
    }
    // additional operations in case we are committing after a conflicted merge
    final Optional<Ref> mergeHead = command(RefParse.class).setName(Ref.MERGE_HEAD).call();
    if (mergeHead.isPresent()) {
        ObjectId mergeCommitId = mergeHead.get().getObjectId();
        if (!mergeCommitId.isNull()) {
            parents.add(mergeCommitId);
        }
        if (message == null) {
            message = command(ReadMergeCommitMessageOp.class).call();
        }
    }
    ObjectId newTreeId;
    {
        WriteTree2 writeTree = command(WriteTree2.class);
        writeTree.setOldRoot(oldRoot).setProgressListener(subProgress(writeTreeProgress));
        if (!pathFilters.isEmpty()) {
            writeTree.setPathFilter(pathFilters);
        }
        newTreeId = writeTree.call();
    }
    if (getProgressListener().isCanceled()) {
        return null;
    }
    final ObjectId currentRootTreeId = command(ResolveTreeish.class).setTreeish(currHeadCommitId).call().or(RevTree.EMPTY_TREE_ID);
    if (currentRootTreeId.equals(newTreeId)) {
        if (!allowEmpty) {
            throw new NothingToCommitException("Nothing to commit after " + currHeadCommitId);
        }
    }
    final RevCommit commit;
    if (this.commit == null) {
        CommitBuilder cb = new CommitBuilder();
        cb.setAuthor(author);
        cb.setAuthorEmail(authorEmail);
        cb.setCommitter(committer);
        cb.setCommitterEmail(committerEmail);
        cb.setMessage(message);
        cb.setParentIds(parents);
        cb.setTreeId(newTreeId);
        cb.setCommitterTimestamp(committerTime);
        cb.setAuthorTimestamp(authorTime);
        cb.setCommitterTimeZoneOffset(committerTimeZoneOffset);
        cb.setAuthorTimeZoneOffset(authorTimeZoneOffset);
        commit = cb.build();
    } else {
        CommitBuilder cb = new CommitBuilder(this.commit);
        cb.setParentIds(parents);
        cb.setTreeId(newTreeId);
        cb.setCommitterTimestamp(committerTime);
        cb.setCommitterTimeZoneOffset(committerTimeZoneOffset);
        if (message != null) {
            cb.setMessage(message);
        }
        commit = cb.build();
    }
    if (getProgressListener().isCanceled()) {
        return null;
    }
    final ObjectDatabase objectDb = objectDatabase();
    objectDb.put(commit);
    // set the HEAD pointing to the new commit
    final Optional<Ref> branchHead = command(UpdateRef.class).setName(currentBranch).setNewValue(commit.getId()).call();
    checkState(commit.getId().equals(branchHead.get().getObjectId()));
    final Optional<Ref> newHead = command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(currentBranch).call();
    checkState(currentBranch.equals(((SymRef) newHead.get()).getTarget()));
    Optional<ObjectId> treeId = command(ResolveTreeish.class).setTreeish(branchHead.get().getObjectId()).call();
    checkState(treeId.isPresent());
    checkState(newTreeId.equals(treeId.get()));
    getProgressListener().setProgress(100f);
    getProgressListener().complete();
    // TODO: maybe all this "heads cleaning" should be put in an independent operation
    if (mergeHead.isPresent()) {
        command(UpdateRef.class).setDelete(true).setName(Ref.MERGE_HEAD).call();
        command(UpdateRef.class).setDelete(true).setName(Ref.ORIG_HEAD).call();
    }
    final Optional<Ref> cherrypickHead = command(RefParse.class).setName(Ref.CHERRY_PICK_HEAD).call();
    if (cherrypickHead.isPresent()) {
        command(UpdateRef.class).setDelete(true).setName(Ref.CHERRY_PICK_HEAD).call();
        command(UpdateRef.class).setDelete(true).setName(Ref.ORIG_HEAD).call();
    }
    return commit;
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) CommitBuilder(org.locationtech.geogig.api.CommitBuilder) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) SymRef(org.locationtech.geogig.api.SymRef) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) ResolveTreeish(org.locationtech.geogig.api.plumbing.ResolveTreeish) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevTree(org.locationtech.geogig.api.RevTree) WriteTree2(org.locationtech.geogig.api.plumbing.WriteTree2) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 9 with ObjectDatabase

use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.

the class InitOp method callInternal.

private Repository callInternal() {
    final Platform platform = platform();
    final File workingDirectory = platform.pwd();
    final Optional<URL> repoUrl = new ResolveGeogigDir(platform).call();
    final boolean repoExisted = repoUrl.isPresent();
    final File envHome;
    if (repoExisted) {
        // we're at either the repo working dir or a subdirectory of it
        try {
            envHome = new File(repoUrl.get().toURI());
        } catch (URISyntaxException e) {
            throw Throwables.propagate(e);
        }
    } else {
        envHome = new File(workingDirectory, ".geogig");
        if (!envHome.mkdirs()) {
            throw new RuntimeException("Unable to create geogig environment at '" + envHome.getAbsolutePath() + "'");
        }
    }
    Map<String, String> effectiveConfigBuilder = Maps.newTreeMap();
    addDefaults(defaults, effectiveConfigBuilder);
    if (config != null) {
        effectiveConfigBuilder.putAll(config);
    }
    if (filterFile != null) {
        try {
            final String FILTER_FILE = "filter.ini";
            File oldFilterFile = new File(filterFile);
            if (!oldFilterFile.exists()) {
                throw new FileNotFoundException("No filter file found at " + filterFile + ".");
            }
            Optional<URL> envHomeURL = new ResolveGeogigDir(platform).call();
            Preconditions.checkState(envHomeURL.isPresent(), "Not inside a geogig directory");
            final URL url = envHomeURL.get();
            if (!"file".equals(url.getProtocol())) {
                throw new UnsupportedOperationException("Sparse clone works only against file system repositories. " + "Repository location: " + url.toExternalForm());
            }
            File repoDir;
            try {
                repoDir = new File(url.toURI());
            } catch (URISyntaxException e) {
                throw new IllegalStateException("Unable to access directory " + url.toExternalForm(), e);
            }
            File newFilterFile = new File(repoDir, FILTER_FILE);
            Files.copy(oldFilterFile, newFilterFile);
            effectiveConfigBuilder.put("sparse.filter", FILTER_FILE);
        } catch (Exception e) {
            throw new IllegalStateException("Unable to copy filter file at path " + filterFile + " to the new repository.", e);
        }
    }
    try {
        Preconditions.checkState(envHome.toURI().toURL().equals(new ResolveGeogigDir(platform).call().get()));
    } catch (MalformedURLException e) {
        Throwables.propagate(e);
    }
    Repository repository;
    try {
        if (!repoExisted) {
            ConfigDatabase configDB = context.configDatabase();
            try {
                for (Entry<String, String> pair : effectiveConfigBuilder.entrySet()) {
                    String key = pair.getKey();
                    String value = pair.getValue();
                    configDB.put(key, value);
                }
                repository = repository();
                repository.configure();
            } catch (RepositoryConnectionException e) {
                throw new IllegalStateException("Unable to initialize repository for the first time: " + e.getMessage(), e);
            }
        } else {
            repository = repository();
        }
        try {
            repository.open();
            // make sure the repo has the empty tree
            ObjectDatabase objectDatabase = repository.objectDatabase();
            objectDatabase.put(RevTree.EMPTY);
        } catch (RepositoryConnectionException e) {
            throw new IllegalStateException("Error opening repository databases: " + e.getMessage(), e);
        }
        createSampleHooks(envHome);
    } catch (ConfigException e) {
        throw e;
    } catch (RuntimeException e) {
        Throwables.propagateIfInstanceOf(e, IllegalStateException.class);
        throw new IllegalStateException("Can't access repository at '" + envHome.getAbsolutePath() + "'", e);
    }
    if (!repoExisted) {
        try {
            createDefaultRefs();
        } catch (IllegalStateException e) {
            Throwables.propagate(e);
        }
    }
    return repository;
}
Also used : MalformedURLException(java.net.MalformedURLException) Platform(org.locationtech.geogig.api.Platform) ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) FileNotFoundException(java.io.FileNotFoundException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RepositoryConnectionException(org.locationtech.geogig.repository.RepositoryConnectionException) RepositoryConnectionException(org.locationtech.geogig.repository.RepositoryConnectionException) Repository(org.locationtech.geogig.repository.Repository) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) ResolveGeogigDir(org.locationtech.geogig.api.plumbing.ResolveGeogigDir) File(java.io.File)

Example 10 with ObjectDatabase

use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.

the class MutableTreeTest method testBuild.

@Test
@Ignore
public void testBuild() {
    ObjectDatabase origin = new HeapObjectDatabse();
    origin.open();
    ObjectDatabase target = new HeapObjectDatabse();
    target.open();
    RevTree tree = root.build(origin, target);
    Iterator<NodeRef> treeRefs = new DepthTreeIterator("", ObjectId.NULL, tree, target, Strategy.RECURSIVE_TREES_ONLY);
    MutableTree createFromRefs = MutableTree.createFromRefs(root.getNode().getObjectId(), treeRefs);
// TODO finish
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) HeapObjectDatabse(org.locationtech.geogig.storage.memory.HeapObjectDatabse) RevTree(org.locationtech.geogig.api.RevTree) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)27 RevTree (org.locationtech.geogig.api.RevTree)18 ObjectId (org.locationtech.geogig.api.ObjectId)13 RevObject (org.locationtech.geogig.api.RevObject)8 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)8 Test (org.junit.Test)7 NodeRef (org.locationtech.geogig.api.NodeRef)7 RevCommit (org.locationtech.geogig.api.RevCommit)5 RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)5 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)4 HashSet (java.util.HashSet)3 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)3 Node (org.locationtech.geogig.api.Node)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 CommitBuilder (org.locationtech.geogig.api.CommitBuilder)2 ProgressListener (org.locationtech.geogig.api.ProgressListener)2 Ref (org.locationtech.geogig.api.Ref)2 TYPE (org.locationtech.geogig.api.RevObject.TYPE)2