Search in sources :

Example 16 with Hash

use of org.projectnessie.versioned.Hash in project nessie by projectnessie.

the class TxDatabaseAdapter method namedRefs.

public Stream<ReferenceInfo<ByteString>> namedRefs(GetNamedRefsParams params) throws ReferenceNotFoundException {
    Preconditions.checkNotNull(params, "Parameter for GetNamedRefsParams must not be null.");
    Preconditions.checkArgument(namedRefsAnyRetrieves(params), "Must retrieve branches or tags or both.");
    ConnectionWrapper conn = borrowConnection();
    boolean failed = true;
    try {
        Hash defaultBranchHead = namedRefsDefaultBranchHead(conn, params);
        Stream<ReferenceInfo<ByteString>> refs = fetchNamedRefs(conn);
        refs = namedRefsFilterAndEnhance(conn, params, defaultBranchHead, refs);
        failed = false;
        return refs.onClose(conn::close);
    } finally {
        if (failed) {
            conn.close();
        }
    }
}
Also used : DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo)

Example 17 with Hash

use of org.projectnessie.versioned.Hash in project nessie by projectnessie.

the class TxDatabaseAdapter method merge.

@Override
public Hash merge(Hash from, BranchName toBranch, Optional<Hash> expectedHead, Function<ByteString, ByteString> updateCommitMetadata) throws ReferenceNotFoundException, ReferenceConflictException {
    // creates a new commit-tree that is decoupled from other commit-trees.
    try {
        return opLoop("merge", toBranch, false, (conn, currentHead) -> {
            long timeInMicros = commitTimeInMicros();
            Hash toHead = mergeAttempt(conn, timeInMicros, from, toBranch, expectedHead, currentHead, h -> {
            }, h -> {
            }, updateCommitMetadata);
            Hash resultHash = tryMoveNamedReference(conn, toBranch, currentHead, toHead);
            commitRefLog(conn, timeInMicros, toHead, toBranch, RefLogEntry.Operation.MERGE, Collections.singletonList(from));
            return resultHash;
        }, () -> mergeConflictMessage("Conflict", from, toBranch, expectedHead), () -> mergeConflictMessage("Retry-failure", from, toBranch, expectedHead));
    } catch (ReferenceNotFoundException | ReferenceConflictException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) ReferenceRetryFailureException(org.projectnessie.versioned.ReferenceRetryFailureException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLException(java.sql.SQLException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException)

Example 18 with Hash

use of org.projectnessie.versioned.Hash in project nessie by projectnessie.

the class TxDatabaseAdapter method doFetchMultipleFromCommitLog.

@Override
protected List<CommitLogEntry> doFetchMultipleFromCommitLog(ConnectionWrapper c, List<Hash> hashes) {
    String sql = sqlForManyPlaceholders(SqlStatements.SELECT_COMMIT_LOG_MANY, hashes.size());
    try (PreparedStatement ps = c.conn().prepareStatement(sql)) {
        ps.setString(1, config.getRepositoryId());
        for (int i = 0; i < hashes.size(); i++) {
            ps.setString(2 + i, hashes.get(i).asString());
        }
        Map<Hash, CommitLogEntry> result = new HashMap<>(hashes.size() * 2);
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                CommitLogEntry entry = protoToCommitLogEntry(rs.getBytes(1));
                result.put(entry.getHash(), entry);
            }
        }
        return hashes.stream().map(result::get).collect(Collectors.toList());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : ProtoSerialization.protoToCommitLogEntry(org.projectnessie.versioned.persist.serialize.ProtoSerialization.protoToCommitLogEntry) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ByteString(com.google.protobuf.ByteString) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)

Example 19 with Hash

use of org.projectnessie.versioned.Hash in project nessie by projectnessie.

the class TxDatabaseAdapter method doFetchPageFromRefLog.

@Override
protected List<RefLog> doFetchPageFromRefLog(ConnectionWrapper connection, List<Hash> hashes) {
    String sql = sqlForManyPlaceholders(SqlStatements.SELECT_REF_LOG_MANY, hashes.size());
    try (PreparedStatement ps = connection.conn().prepareStatement(sql)) {
        ps.setString(1, config.getRepositoryId());
        for (int i = 0; i < hashes.size(); i++) {
            ps.setString(2 + i, hashes.get(i).asString());
        }
        Map<Hash, RefLog> result = new HashMap<>(hashes.size() * 2);
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                RefLog entry = protoToRefLog(rs.getBytes(1));
                result.put(Objects.requireNonNull(entry).getRefLogId(), entry);
            }
        }
        return hashes.stream().map(result::get).collect(Collectors.toList());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ByteString(com.google.protobuf.ByteString) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) RefLog(org.projectnessie.versioned.persist.adapter.RefLog) ProtoSerialization.protoToRefLog(org.projectnessie.versioned.persist.serialize.ProtoSerialization.protoToRefLog)

Example 20 with Hash

use of org.projectnessie.versioned.Hash in project nessie by projectnessie.

the class TxDatabaseAdapter method fetchNamedRefs.

protected Stream<ReferenceInfo<ByteString>> fetchNamedRefs(ConnectionWrapper conn) {
    return JdbcSelectSpliterator.buildStream(conn.conn(), SqlStatements.SELECT_NAMED_REFERENCES, ps -> ps.setString(1, config.getRepositoryId()), (rs) -> {
        String type = rs.getString(1);
        String ref = rs.getString(2);
        Hash head = Hash.of(rs.getString(3));
        NamedRef namedRef = namedRefFromRow(type, ref);
        if (namedRef != null) {
            return ReferenceInfo.of(head, namedRef);
        }
        return null;
    });
}
Also used : ByteString(com.google.protobuf.ByteString) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) NamedRef(org.projectnessie.versioned.NamedRef)

Aggregations

Hash (org.projectnessie.versioned.Hash)99 BranchName (org.projectnessie.versioned.BranchName)49 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)44 ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)36 Test (org.junit.jupiter.api.Test)33 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)30 ByteString (com.google.protobuf.ByteString)28 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)27 Key (org.projectnessie.versioned.Key)25 CommitLogEntry (org.projectnessie.versioned.persist.adapter.CommitLogEntry)23 ArrayList (java.util.ArrayList)20 RefLogNotFoundException (org.projectnessie.versioned.RefLogNotFoundException)20 Stream (java.util.stream.Stream)19 Collectors (java.util.stream.Collectors)17 TagName (org.projectnessie.versioned.TagName)17 HashMap (java.util.HashMap)16 Optional (java.util.Optional)16 ReferenceAlreadyExistsException (org.projectnessie.versioned.ReferenceAlreadyExistsException)16 ReferenceInfo (org.projectnessie.versioned.ReferenceInfo)16 Collections (java.util.Collections)15