Line data Source code
1 : #ifndef HEADER_fd_src_discof_restore_utils_fd_snapshot_parser_h 2 : #define HEADER_fd_src_discof_restore_utils_fd_snapshot_parser_h 3 : 4 : #include "../../../flamenco/types/fd_types.h" 5 : #include "fd_ssmanifest_parser.h" 6 : #include "fd_ssmsg.h" 7 : 8 0 : #define SNAP_STATE_IGNORE ((uchar)0) /* ignore file content */ 9 0 : #define SNAP_STATE_TAR ((uchar)1) /* reading tar header (buffered) */ 10 0 : #define SNAP_STATE_MANIFEST ((uchar)2) /* reading manifest (zero copy) */ 11 0 : #define SNAP_STATE_ACCOUNT_HDR ((uchar)3) /* reading account hdr (buffered) */ 12 0 : #define SNAP_STATE_ACCOUNT_DATA ((uchar)4) /* reading account data (zero copy) */ 13 : #define SNAP_STATE_DONE ((uchar)5) /* expect no more data */ 14 : 15 0 : #define SNAP_FLAG_FAILED 1 16 0 : #define SNAP_FLAG_DONE 2 17 : 18 : struct fd_snapshot_parser; 19 : typedef struct fd_snapshot_parser fd_snapshot_parser_t; 20 : 21 : typedef void 22 : (* fd_snapshot_parser_process_manifest_fn_t)( void * _ctx, 23 : ulong manifest_sz ); 24 : 25 : typedef void 26 : (* fd_snapshot_process_acc_hdr_fn_t)( void * _ctx, 27 : fd_solana_account_hdr_t const * hdr ); 28 : 29 : typedef void 30 : (* fd_snapshot_process_acc_data_fn_t)( void * _ctx, 31 : uchar const * buf, 32 : ulong data_sz ); 33 : 34 : struct fd_snapshot_parser_metrics { 35 : ulong accounts_files_processed; 36 : ulong accounts_files_total; 37 : ulong accounts_processed; 38 : }; 39 : 40 : typedef struct fd_snapshot_parser_metrics fd_snapshot_parser_metrics_t; 41 : 42 : struct fd_snapshot_parser { 43 : uchar state; 44 : uchar flags; 45 : uchar manifest_done; 46 : uchar processing_accv; 47 : 48 : /* Frame buffer */ 49 : 50 : uchar * buf; 51 : ulong buf_ctr; /* number of bytes allocated in buffer */ 52 : ulong buf_sz; /* target buffer size (buf_ctr<buf_sz implies incomplete read) */ 53 : ulong buf_max; /* byte capacity of buffer */ 54 : 55 : /* Manifest dcache buffer */ 56 : uchar * manifest_buf; 57 : ulong manifest_bufsz; 58 : 59 : /* Tar parser */ 60 : ulong goff; /* current position in stream */ 61 : ulong tar_file_rem; /* number of stream bytes in current TAR file */ 62 : 63 : /* Snapshot file parser */ 64 : ulong accv_slot; /* account vec slot */ 65 : ulong accv_id; /* account vec index */ 66 : ulong accv_sz; /* account vec size */ 67 : ulong accv_key_max; /* max account vec count */ 68 : 69 : /* Account defrag */ 70 : ulong acc_sz; 71 : ulong acc_rem; /* acc bytes pending write */ 72 : ulong acc_pad; /* padding size at end of account */ 73 : 74 : /* Account processing callbacks */ 75 : fd_snapshot_parser_process_manifest_fn_t manifest_cb; 76 : fd_snapshot_process_acc_hdr_fn_t acc_hdr_cb; 77 : fd_snapshot_process_acc_data_fn_t acc_data_cb; 78 : void * cb_arg; 79 : 80 : fd_ssmanifest_parser_t * manifest_parser; 81 : 82 : /* Metrics */ 83 : fd_snapshot_parser_metrics_t metrics; 84 : }; 85 : typedef struct fd_snapshot_parser fd_snapshot_parser_t; 86 : 87 : FD_FN_CONST static inline ulong 88 0 : fd_snapshot_parser_align( void ) { 89 0 : return 128UL; 90 0 : } 91 : 92 : FD_FN_CONST ulong 93 : fd_snapshot_parser_footprint( ulong max_acc_vecs ); 94 : 95 : static inline void 96 0 : fd_snapshot_parser_reset_tar( fd_snapshot_parser_t * self ) { 97 0 : self->state = SNAP_STATE_TAR; 98 0 : self->buf_ctr = 0UL; 99 0 : self->buf_sz = 0UL; 100 0 : self->processing_accv = 0; 101 0 : self->tar_file_rem = 0UL; 102 0 : } 103 : 104 : /* Reset the snapshot parser on a new stream. As part of stream 105 : decoding, the manifest is written to an output buffer so that the 106 : caller can send it to interested parties. The location to place the 107 : manifest should be given in the manifest_buf and manifest_bufsz 108 : parameters. */ 109 : 110 : static inline void 111 : fd_snapshot_parser_reset( fd_snapshot_parser_t * self, 112 : uchar * manifest_buf, 113 0 : ulong manifest_bufsz ) { 114 0 : self->flags = 0UL; 115 0 : fd_snapshot_parser_reset_tar( self ); 116 0 : fd_ssmanifest_parser_init( self->manifest_parser, (fd_snapshot_manifest_t*)manifest_buf ); 117 0 : self->manifest_done = 0; 118 0 : self->metrics.accounts_files_processed = 0UL; 119 0 : self->metrics.accounts_files_total = 0UL; 120 0 : self->metrics.accounts_processed = 0UL; 121 0 : self->processing_accv = 0; 122 0 : self->goff = 0UL; 123 0 : self->accv_slot = 0UL; 124 0 : self->accv_id = 0UL; 125 : 126 0 : self->manifest_buf = manifest_buf; 127 0 : self->manifest_bufsz = manifest_bufsz; 128 0 : } 129 : 130 : fd_snapshot_parser_t * 131 : fd_snapshot_parser_new( void * mem, 132 : void * cb_arg, 133 : ulong seed, 134 : ulong max_acc_vecs, 135 : fd_snapshot_parser_process_manifest_fn_t manifest_cb, 136 : fd_snapshot_process_acc_hdr_fn_t acc_hdr_cb, 137 : fd_snapshot_process_acc_data_fn_t acc_data_cb ); 138 : 139 : static inline void 140 0 : fd_snapshot_parser_close( fd_snapshot_parser_t * self ) { 141 0 : self->flags = SNAP_FLAG_DONE; 142 0 : } 143 : 144 : static inline fd_snapshot_parser_metrics_t 145 0 : fd_snapshot_parser_get_metrics( fd_snapshot_parser_t * self ) { 146 0 : return self->metrics; 147 0 : } 148 : 149 : uchar const * 150 : fd_snapshot_parser_process_chunk( fd_snapshot_parser_t * self, 151 : uchar const * buf, 152 : ulong bufsz ); 153 : 154 : #endif /* HEADER_fd_src_discof_restore_utils_fd_snapshot_parser_h */