Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 1 | package eu.mulk.jgvariant.ostree; |
| 2 | |
| 3 | import eu.mulk.jgvariant.core.Decoder; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 4 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 5 | import java.nio.ByteOrder; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 6 | import java.util.ArrayList; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 7 | import java.util.List; |
| 8 | |
| 9 | /** |
| 10 | * An entry in a {@link DeltaSuperblock}. |
| 11 | * |
| 12 | * <p>Reference: {@code ostree-repo-static-delta-private.h#OSTREE_STATIC_DELTA_META_ENTRY_FORMAT} |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 13 | * |
| 14 | * @param version the version corresponding to the version of {@link DeltaPartPayload}; always 0. |
| 15 | * @param checksum the checksum of the {@link DeltaPartPayload}. |
| 16 | * @param compressedSize the total compressed size of the delta. |
| 17 | * @param uncompressedSize the total uncompressed size of the files generated by the delta. |
| 18 | * @param objects a list of objects generated by the delta payload. |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 19 | */ |
| 20 | public record DeltaMetaEntry( |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 21 | int version, |
| 22 | Checksum checksum, |
| 23 | long compressedSize, |
| 24 | long uncompressedSize, |
| 25 | List<DeltaObject> objects) { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 26 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 27 | /** |
| 28 | * A reference to an object generated by a {@link DeltaPartPayload}. |
| 29 | * |
| 30 | * @param objectType the file type. |
| 31 | * @param checksum the checksum of the resulting file. |
| 32 | */ |
| 33 | public record DeltaObject(ObjectType objectType, Checksum checksum) { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 34 | |
| 35 | private static final Decoder<DeltaObject> DECODER = |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 36 | Decoder.ofStructure( |
| 37 | DeltaObject.class, Decoder.ofByte().map(ObjectType::valueOf), Checksum.decoder()); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 38 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Acquires a {@link Decoder} for the enclosing type. |
| 41 | * |
| 42 | * @return a possibly shared {@link Decoder}. |
| 43 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 44 | public static Decoder<DeltaObject> decoder() { |
| 45 | return DECODER; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private static final Decoder<DeltaMetaEntry> DECODER = |
| 50 | Decoder.ofStructure( |
| 51 | DeltaMetaEntry.class, |
Matthias Andreas Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame^] | 52 | Decoder.ofInt(), |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 53 | Checksum.decoder(), |
Matthias Andreas Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame^] | 54 | Decoder.ofLong(), |
| 55 | Decoder.ofLong(), |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 56 | Decoder.ofByteArray().map(DeltaMetaEntry::parseObjectList)); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 57 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 58 | private static List<DeltaObject> parseObjectList(byte[] bytes) { |
| 59 | var byteBuffer = ByteBuffer.wrap(bytes); |
| 60 | List<DeltaObject> objects = new ArrayList<>(); |
| 61 | |
| 62 | while (byteBuffer.hasRemaining()) { |
| 63 | var type = ObjectType.valueOf(byteBuffer.get()); |
| 64 | var checksum = Checksum.readFrom(byteBuffer); |
| 65 | objects.add(new DeltaObject(type, checksum)); |
| 66 | } |
| 67 | |
| 68 | return objects; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Acquires a {@link Decoder} for the enclosing type. |
| 73 | * |
Matthias Andreas Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame^] | 74 | * <p><strong>Note:</strong> This decoder has an unspecified {@link ByteOrder}. |
| 75 | * |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 76 | * @return a possibly shared {@link Decoder}. |
| 77 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 78 | public static Decoder<DeltaMetaEntry> decoder() { |
| 79 | return DECODER; |
| 80 | } |
Matthias Andreas Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame^] | 81 | |
| 82 | DeltaMetaEntry byteSwapped() { |
| 83 | // FIXME |
| 84 | return new DeltaMetaEntry(version, checksum, compressedSize, uncompressedSize, objects); |
| 85 | } |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 86 | } |