Search in sources :

Example 1 with ObjectSerializingFactory

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

the class HttpMappedRemoteRepo method pushSparseCommit.

/**
     * Pushes a sparse commit to a remote repository and updates all mappings.
     * 
     * @param commitId the commit to push
     */
@Override
protected void pushSparseCommit(ObjectId commitId) {
    Repository from = localRepository;
    Optional<RevObject> object = from.command(RevObjectParse.class).setObjectId(commitId).call();
    if (object.isPresent() && object.get().getType().equals(TYPE.COMMIT)) {
        RevCommit commit = (RevCommit) object.get();
        ObjectId parent = ObjectId.NULL;
        List<ObjectId> newParents = new LinkedList<ObjectId>();
        for (int i = 0; i < commit.getParentIds().size(); i++) {
            ObjectId parentId = commit.getParentIds().get(i);
            if (i != 0) {
                Optional<ObjectId> commonAncestor = from.command(FindCommonAncestor.class).setLeftId(commit.getParentIds().get(0)).setRightId(parentId).call();
                if (commonAncestor.isPresent()) {
                    if (from.command(CheckSparsePath.class).setStart(parentId).setEnd(commonAncestor.get()).call()) {
                        // This should be the base commit to preserve changes that were filtered
                        // out.
                        newParents.add(0, from.graphDatabase().getMapping(parentId));
                        continue;
                    }
                }
            }
            newParents.add(from.graphDatabase().getMapping(parentId));
        }
        if (newParents.size() > 0) {
            parent = from.graphDatabase().getMapping(newParents.get(0));
        }
        Iterator<DiffEntry> diffIter = from.command(DiffOp.class).setNewVersion(commitId).setOldVersion(parent).setReportTrees(true).call();
        // connect and send packed changes
        final URL resourceURL;
        try {
            resourceURL = new URL(repositoryURL.toString() + "/repo/applychanges");
        } catch (MalformedURLException e) {
            throw Throwables.propagate(e);
        }
        final HttpURLConnection connection;
        final OutputStream out;
        try {
            connection = (HttpURLConnection) resourceURL.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            out = connection.getOutputStream();
            // pack the commit object
            final ObjectSerializingFactory factory = DataStreamSerializationFactoryV1.INSTANCE;
            final ObjectWriter<RevCommit> commitWriter = factory.createObjectWriter(TYPE.COMMIT);
            commitWriter.write(commit, out);
            // write the new parents
            out.write(newParents.size());
            for (ObjectId parentId : newParents) {
                out.write(parentId.getRawValue());
            }
            // pack the changes
            BinaryPackedChanges changes = new BinaryPackedChanges(from);
            changes.write(out, diffIter);
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
        final InputStream in;
        try {
            in = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            String line = rd.readLine();
            if (line != null) {
                ObjectId remoteCommitId = ObjectId.valueOf(line);
                from.graphDatabase().map(commit.getId(), remoteCommitId);
                from.graphDatabase().map(remoteCommitId, commit.getId());
            }
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
}
Also used : ObjectSerializingFactory(org.locationtech.geogig.storage.ObjectSerializingFactory) MalformedURLException(java.net.MalformedURLException) OutputStream(java.io.OutputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) CheckSparsePath(org.locationtech.geogig.api.plumbing.CheckSparsePath) InputStreamReader(java.io.InputStreamReader) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) InputStream(java.io.InputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Repository(org.locationtech.geogig.repository.Repository) FindCommonAncestor(org.locationtech.geogig.api.plumbing.FindCommonAncestor) BufferedReader(java.io.BufferedReader)

Example 2 with ObjectSerializingFactory

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

the class HttpRemoteRepo method sendPackedObjects.

private void sendPackedObjects(final List<ObjectId> toSend, final Set<ObjectId> roots, Deduplicator deduplicator, final ProgressListener progress) {
    Set<ObjectId> sent = new HashSet<ObjectId>();
    while (!toSend.isEmpty()) {
        try {
            BinaryPackedObjects.Callback callback = new BinaryPackedObjects.Callback() {

                @Override
                public void callback(Supplier<RevObject> supplier) {
                    RevObject object = supplier.get();
                    progress.setProgress(progress.getProgress() + 1);
                    if (object instanceof RevCommit) {
                        RevCommit commit = (RevCommit) object;
                        toSend.remove(commit.getId());
                        roots.removeAll(commit.getParentIds());
                        roots.add(commit.getId());
                    }
                }
            };
            ObjectDatabase database = localRepository.objectDatabase();
            BinaryPackedObjects packer = new BinaryPackedObjects(database);
            ImmutableList<ObjectId> have = ImmutableList.copyOf(roots);
            final boolean traverseCommits = false;
            Stopwatch sw = Stopwatch.createStarted();
            ObjectSerializingFactory serializer = DataStreamSerializationFactoryV1.INSTANCE;
            SendObjectsConnectionFactory outFactory;
            ObjectFunnel objectFunnel;
            outFactory = new SendObjectsConnectionFactory(repositoryURL);
            int pushBytesLimit = parsePushLimit();
            objectFunnel = ObjectFunnels.newFunnel(outFactory, serializer, pushBytesLimit);
            final long writtenObjectsCount = packer.write(objectFunnel, toSend, have, sent, callback, traverseCommits, deduplicator);
            objectFunnel.close();
            sw.stop();
            long compressedSize = outFactory.compressedSize;
            long uncompressedSize = outFactory.uncompressedSize;
            LOGGER.info(String.format("HttpRemoteRepo: Written %,d objects." + " Time to process: %s." + " Compressed size: %,d bytes. Uncompressed size: %,d bytes.", writtenObjectsCount, sw, compressedSize, uncompressedSize));
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }
}
Also used : ObjectSerializingFactory(org.locationtech.geogig.storage.ObjectSerializingFactory) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) Supplier(com.google.common.base.Supplier) HashSet(java.util.HashSet) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 3 with ObjectSerializingFactory

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

the class Cat method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(paths.size() < 2, "Only one refspec allowed");
    checkParameter(!paths.isEmpty(), "A refspec must be specified");
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    String path = paths.get(0);
    Optional<RevObject> obj = geogig.command(RevObjectParse.class).setRefSpec(path).call();
    checkParameter(obj.isPresent(), "refspec did not resolve to any object.");
    if (binary) {
        ObjectSerializingFactory factory = DataStreamSerializationFactoryV1.INSTANCE;
        ObjectWriter<RevObject> writer = factory.createObjectWriter(obj.get().getType());
        writer.write(obj.get(), System.out);
    } else {
        CharSequence s = geogig.command(CatObject.class).setObject(Suppliers.ofInstance(obj.get())).call();
        console.println(s);
    }
}
Also used : ObjectSerializingFactory(org.locationtech.geogig.storage.ObjectSerializingFactory) ConsoleReader(jline.console.ConsoleReader) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Aggregations

RevObject (org.locationtech.geogig.api.RevObject)3 ObjectSerializingFactory (org.locationtech.geogig.storage.ObjectSerializingFactory)3 IOException (java.io.IOException)2 ObjectId (org.locationtech.geogig.api.ObjectId)2 RevCommit (org.locationtech.geogig.api.RevCommit)2 Stopwatch (com.google.common.base.Stopwatch)1 Supplier (com.google.common.base.Supplier)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ConsoleReader (jline.console.ConsoleReader)1 GeoGIG (org.locationtech.geogig.api.GeoGIG)1 CheckSparsePath (org.locationtech.geogig.api.plumbing.CheckSparsePath)1 FindCommonAncestor (org.locationtech.geogig.api.plumbing.FindCommonAncestor)1