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