| 1 | /** |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0. |
| 4 | */ |
| 5 | |
| 6 | #ifndef AWS_S3_PARALLEL_INPUT_STREAM_H |
| 7 | #define AWS_S3_PARALLEL_INPUT_STREAM_H |
| 8 | |
| 9 | #include <aws/s3/s3.h> |
| 10 | |
| 11 | #include <aws/common/ref_count.h> |
| 12 | |
| 13 | AWS_PUSH_SANE_WARNING_LEVEL |
| 14 | |
| 15 | struct aws_byte_buf; |
| 16 | struct aws_future_bool; |
| 17 | struct aws_future_void; |
| 18 | struct aws_input_stream; |
| 19 | struct aws_s3_meta_request; |
| 20 | struct aws_s3_request; |
| 21 | |
| 22 | struct aws_event_loop_group; |
| 23 | |
| 24 | /** |
| 25 | * This should be private, but keep it public for providing your own implementation. |
| 26 | */ |
| 27 | struct aws_parallel_input_stream { |
| 28 | const struct aws_parallel_input_stream_vtable *vtable; |
| 29 | struct aws_allocator *alloc; |
| 30 | struct aws_ref_count ref_count; |
| 31 | struct aws_future_void *shutdown_future; |
| 32 | |
| 33 | void *impl; |
| 34 | }; |
| 35 | |
| 36 | struct aws_parallel_input_stream_vtable { |
| 37 | /** |
| 38 | * Destroy the stream, its refcount has reached 0. |
| 39 | */ |
| 40 | void (*destroy)(struct aws_parallel_input_stream *stream); |
| 41 | |
| 42 | /** |
| 43 | * Read from the offset until fill the dest, or EOF reached. |
| 44 | * It's thread safe to be called from multiple threads without waiting for other read to complete |
| 45 | * |
| 46 | * @param stream The stream to read from |
| 47 | * @param offset The offset in the stream from beginning to start reading |
| 48 | * @param max_length The maximum number of bytes to read |
| 49 | * @param dest The output buffer read to |
| 50 | * @return a future, which will contain an error code if something went wrong, |
| 51 | * or a result bool indicating whether EOF has been reached. |
| 52 | */ |
| 53 | struct aws_future_bool *( |
| 54 | *read)(struct aws_parallel_input_stream *stream, uint64_t offset, size_t max_length, struct aws_byte_buf *dest); |
| 55 | |
| 56 | /** |
| 57 | * Get the length of the stream. |
| 58 | * |
| 59 | * @param stream The stream to get length from |
| 60 | * @param out_length The output length |
| 61 | * @return AWS_OP_SUCCESS if success, otherwise AWS_OP_ERR |
| 62 | */ |
| 63 | int (*get_length)(struct aws_parallel_input_stream *stream, int64_t *out_length); |
| 64 | }; |
| 65 | |
| 66 | AWS_EXTERN_C_BEGIN |
| 67 | |
| 68 | /** |
| 69 | * Initialize aws_parallel_input_stream "base class" |
| 70 | */ |
| 71 | AWS_S3_API |
| 72 | void aws_parallel_input_stream_init_base( |
| 73 | struct aws_parallel_input_stream *stream, |
| 74 | struct aws_allocator *alloc, |
| 75 | const struct aws_parallel_input_stream_vtable *vtable, |
| 76 | void *impl); |
| 77 | |
| 78 | /** |
| 79 | * Increment reference count. |
| 80 | * You may pass in NULL (has no effect). |
| 81 | * Returns whatever pointer was passed in. |
| 82 | */ |
| 83 | AWS_S3_API |
| 84 | struct aws_parallel_input_stream *aws_parallel_input_stream_acquire(struct aws_parallel_input_stream *stream); |
| 85 | |
| 86 | /** |
| 87 | * Decrement reference count. |
| 88 | * You may pass in NULL (has no effect). |
| 89 | * Always returns NULL. |
| 90 | */ |
| 91 | AWS_S3_API |
| 92 | struct aws_parallel_input_stream *aws_parallel_input_stream_release(struct aws_parallel_input_stream *stream); |
| 93 | |
| 94 | /** |
| 95 | * Read from the offset until fill the dest, or EOF reached. |
| 96 | * It's thread safe to be called from multiple threads without waiting for other read to complete |
| 97 | * |
| 98 | * @param stream The stream to read from |
| 99 | * @param offset The offset in the stream from beginning to start reading |
| 100 | * @param max_length The maximum number of bytes to read |
| 101 | * @param dest The output buffer read to |
| 102 | * @return a future, which will contain an error code if something went wrong, |
| 103 | * or a result bool indicating whether EOF has been reached. |
| 104 | */ |
| 105 | AWS_S3_API |
| 106 | struct aws_future_bool *aws_parallel_input_stream_read( |
| 107 | struct aws_parallel_input_stream *stream, |
| 108 | uint64_t offset, |
| 109 | size_t max_length, |
| 110 | struct aws_byte_buf *dest); |
| 111 | |
| 112 | /** |
| 113 | * Get the total length of the parallel input stream. |
| 114 | * |
| 115 | * @param stream |
| 116 | * @param out_length |
| 117 | * @return AWS_S3_API |
| 118 | */ |
| 119 | AWS_S3_API |
| 120 | int aws_parallel_input_stream_get_length(struct aws_parallel_input_stream *stream, int64_t *out_length); |
| 121 | |
| 122 | /** |
| 123 | * Creates a new parallel input stream that reads from a file. |
| 124 | * This stream uses an event loop group to perform file I/O operations asynchronously. |
| 125 | * |
| 126 | * Notes for direct_io_read: |
| 127 | * - checking `aws_file_path_read_from_offset_direct_io` for detail |
| 128 | * - If `aws_file_path_read_from_offset_direct_io` failed, fallback to reading via cache with warnings, |
| 129 | * instead of fail read. |
| 130 | * - If alignment required, it's callers' responsibility to align with the page size. |
| 131 | * |
| 132 | * @param allocator The allocator to use for memory allocation |
| 133 | * @param file_name The name of the file to read from |
| 134 | * @param reading_elg The event loop group to use for file I/O operations |
| 135 | * @param direct_io_read Whether to use direct I/O for reading the file. |
| 136 | * |
| 137 | * @return A new parallel input stream that reads from the specified file |
| 138 | */ |
| 139 | AWS_S3_API |
| 140 | struct aws_parallel_input_stream *aws_parallel_input_stream_new_from_file( |
| 141 | struct aws_allocator *allocator, |
| 142 | struct aws_byte_cursor file_name, |
| 143 | struct aws_event_loop_group *reading_elg, |
| 144 | bool direct_io_read); |
| 145 | |
| 146 | /** |
| 147 | * Get the shutdown future from the parallel input stream. |
| 148 | * The future will be completed when every refcount on the stream has been released. |
| 149 | * And all the resource has been released. |
| 150 | * Don't hold any refcount of the stream while waiting on the future, otherwise, deadlock can happen. |
| 151 | * You need to release the future after using it. |
| 152 | */ |
| 153 | AWS_S3_API |
| 154 | struct aws_future_void *aws_parallel_input_stream_get_shutdown_future(struct aws_parallel_input_stream *stream); |
| 155 | |
| 156 | AWS_EXTERN_C_END |
| 157 | AWS_POP_SANE_WARNING_LEVEL |
| 158 | |
| 159 | #endif /* AWS_S3_PARALLEL_INPUT_STREAM_H */ |