Search in sources :

Example 1 with RefLog

use of org.projectnessie.versioned.persist.adapter.RefLog 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 2 with RefLog

use of org.projectnessie.versioned.persist.adapter.RefLog in project nessie by projectnessie.

the class NonTransactionalDatabaseAdapter method writeRefLogEntry.

protected RefLogEntry writeRefLogEntry(NonTransactionalOperationContext ctx, GlobalStatePointer pointer, String refName, RefType refType, Hash commitHash, Operation operation, long timeInMicros, List<Hash> sourceHashes) throws ReferenceConflictException {
    Hash parentHash = Hash.of(pointer.getRefLogId());
    Hash currentRefLogId = randomHash();
    Stream<ByteString> newParents;
    if (pointer.getRefLogParentsInclHeadCount() == 0 || !pointer.getRefLogId().equals(pointer.getRefLogParentsInclHead(0))) {
        // Before Nessie 0.21.0
        newParents = Stream.of(parentHash.asBytes());
        RefLog currentEntry = fetchFromRefLog(ctx, parentHash);
        if (currentEntry != null) {
            newParents = Stream.concat(newParents, currentEntry.getParents().stream().limit(config.getParentsPerRefLogEntry() - 1).map(Hash::asBytes));
        }
    } else {
        // Since Nessie 0.21.0
        newParents = pointer.getRefLogParentsInclHeadList().stream().limit(config.getParentsPerRefLogEntry());
    }
    RefLogEntry.Builder entry = RefLogEntry.newBuilder().setRefLogId(currentRefLogId.asBytes()).setRefName(ByteString.copyFromUtf8(refName)).setRefType(refType).setCommitHash(commitHash.asBytes()).setOperationTime(timeInMicros).setOperation(operation);
    sourceHashes.forEach(hash -> entry.addSourceHashes(hash.asBytes()));
    newParents.forEach(entry::addParents);
    RefLogEntry refLogEntry = entry.build();
    writeRefLog(ctx, refLogEntry);
    return refLogEntry;
}
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) RefLog(org.projectnessie.versioned.persist.adapter.RefLog) RefLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry)

Example 3 with RefLog

use of org.projectnessie.versioned.persist.adapter.RefLog in project nessie by projectnessie.

the class TxDatabaseAdapter method writeRefLogEntry.

private RefLogEntry writeRefLogEntry(ConnectionWrapper connection, NamedRef ref, RefLogHead refLogHead, Hash commitHash, Operation operation, long timeInMicros, List<Hash> sourceHashes) throws ReferenceConflictException {
    ByteString newId = randomHash().asBytes();
    // Before Nessie 0.21.0: only the "head" of the ref-log is maintained, so Nessie has to read
    // the head entry of the ref-log to get the IDs of all the previous parents to fill the parents
    // in the new ref-log-entry.
    // 
    // Since Nessie 0.21.0: the "head" for the ref-log and PLUS the parents of if are persisted,
    // so Nessie no longer need to read the head entries from the ref-log.
    // 
    // The check of the first entry is there to ensure backwards compatibility and also
    // rolling-upgrades work.
    Stream<ByteString> newParents;
    if (refLogHead.getRefLogParentsInclHead().isEmpty() || !refLogHead.getRefLogParentsInclHead().get(0).equals(refLogHead.getRefLogHead())) {
        // Before Nessie 0.21.0
        newParents = Stream.of(refLogHead.getRefLogHead().asBytes());
        RefLog parentEntry = fetchFromRefLog(connection, refLogHead.getRefLogHead());
        if (parentEntry != null) {
            newParents = Stream.concat(newParents, parentEntry.getParents().stream().limit(config.getParentsPerRefLogEntry() - 1).map(Hash::asBytes));
        }
    } else {
        // Since Nessie 0.21.0
        newParents = refLogHead.getRefLogParentsInclHead().stream().map(Hash::asBytes);
    }
    RefLogEntry.RefType refType = ref instanceof TagName ? RefLogEntry.RefType.Tag : RefLogEntry.RefType.Branch;
    RefLogEntry.Builder entry = RefLogEntry.newBuilder().setRefLogId(newId).setRefName(ByteString.copyFromUtf8(ref.getName())).setRefType(refType).setCommitHash(commitHash.asBytes()).setOperationTime(timeInMicros).setOperation(operation);
    sourceHashes.forEach(hash -> entry.addSourceHashes(hash.asBytes()));
    newParents.forEach(entry::addParents);
    RefLogEntry refLogEntry = entry.build();
    writeRefLog(connection, refLogEntry);
    return refLogEntry;
}
Also used : TagName(org.projectnessie.versioned.TagName) 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) RefLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry)

Aggregations

ByteString (com.google.protobuf.ByteString)3 Hash (org.projectnessie.versioned.Hash)3 RefLog (org.projectnessie.versioned.persist.adapter.RefLog)3 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)3 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)3 RefLogEntry (org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry)2 ProtoSerialization.protoToRefLog (org.projectnessie.versioned.persist.serialize.ProtoSerialization.protoToRefLog)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 TagName (org.projectnessie.versioned.TagName)1