@@ -154,7 +154,9 @@ void print_usage() {
154154 printf (" Appends to a Sqlite database created using -c above\n" );
155155 printf (" with records in CSV format (page_size and col_count have to match)\n\n" );
156156 printf ("test_ulog_sqlite -r <db_name.db> <rowid>\n" );
157- printf (" Searches db_name.db for given row_id using\n" );
157+ printf (" Searches <db_name.db> for given row_id and prints result\n\n" );
158+ printf ("test_ulog_sqlite -b <db_name.db> <col_idx> <value>\n" );
159+ printf (" Searches <db_name.db> and column for given value using\n" );
158160 printf (" binary search and prints result\n\n" );
159161 printf ("test_ulog_sqlite -n\n" );
160162 printf (" Runs pre-defined tests and creates databases (verified manually)\n\n" );
@@ -332,10 +334,126 @@ double read_double(const byte *ptr) {
332334 return ret ;
333335}
334336
337+ void display_row (struct uls_read_context ctx ) {
338+ int col_count = uls_cur_row_col_count (& ctx );
339+ for (int i = 0 ; i < col_count ; i ++ ) {
340+ if (i )
341+ putchar ('|' );
342+ uint32_t col_type ;
343+ const byte * col_val = (const byte * ) uls_read_col_val (& ctx , i , & col_type );
344+ switch (col_type ) {
345+ case 0 :
346+ printf ("null" );
347+ break ;
348+ case 1 :
349+ printf ("%d" , * ((int8_t * )col_val ));
350+ break ;
351+ case 2 :
352+ if (1 ) {
353+ int16_t ival = read_int16 (col_val );
354+ printf ("%d" , ival );
355+ }
356+ break ;
357+ case 4 : {
358+ int32_t ival = read_int32 (col_val );
359+ printf ("%d" , ival );
360+ break ;
361+ }
362+ case 6 : {
363+ int64_t ival = read_int64 (col_val );
364+ printf ("%lld" , ival );
365+ break ;
366+ }
367+ case 7 : {
368+ double dval = read_double (col_val );
369+ printf ("%lf" , dval );
370+ break ;
371+ }
372+ default : {
373+ uint32_t col_len = uls_derive_data_len (col_type );
374+ for (int j = 0 ; j < col_len ; j ++ ) {
375+ if (col_type % 2 )
376+ putchar (col_val [j ]);
377+ else
378+ printf ("x%2x " , col_val [j ]);
379+ }
380+ }
381+ }
382+ }
383+ }
384+
385+ extern void write_uint8 (byte * ptr , uint8_t input );
386+ extern void write_uint16 (byte * ptr , uint16_t input );
387+ extern void write_uint32 (byte * ptr , uint32_t input );
388+ extern void write_uint64 (byte * ptr , uint64_t input );
389+
390+ int resolve_value (char * value , byte * out_val ) {
391+ byte isInt = 1 ;
392+ byte isReal = 1 ;
393+ char * chr = value ;
394+ while (* chr != '\0' ) {
395+ if ((* chr < '0' || * chr > '9' ) && * chr != '-' && * chr != '.' ) {
396+ isInt = 0 ;
397+ isReal = 0 ;
398+ } else {
399+ if (* chr == '.' )
400+ isInt = 0 ;
401+ }
402+ chr ++ ;
403+ }
404+ if (isInt ) {
405+ int64_t ival = (int64_t ) atoll (value );
406+ memcpy (out_val , & ival , sizeof (ival ));
407+ return ULS_TYPE_INT ;
408+ } else
409+ if (isReal ) {
410+ double dval = atof (value );
411+ memcpy (out_val , & dval , sizeof (dval ));
412+ return ULS_TYPE_REAL ;
413+ }
414+ memcpy (out_val , value , strlen (value ));
415+ return ULS_TYPE_TEXT ;
416+ }
417+
418+ int bin_srch_db (int argc , char * argv []) {
419+ int len = strlen (argv [4 ]);
420+ if (len > 71 ) {
421+ printf ("Value too long\n" );
422+ return -1 ;
423+ }
424+ byte buf [72 ];
425+ struct uls_read_context ctx ;
426+ ctx .buf = buf ;
427+ ctx .read_fn = read_fn_rctx ;
428+ fd = open (argv [2 ], O_RDWR | O_SYNC , S_IRUSR | S_IWUSR );
429+ if (fd == -1 ) {
430+ perror ("Error" );
431+ return -2 ;
432+ }
433+ if (uls_read_init (& ctx )) {
434+ printf ("Error during init\n" );
435+ return -3 ;
436+ }
437+ byte page_buf [1 << (ctx .page_size_exp == 1 ? 16 : ctx .page_size_exp )];
438+ ctx .buf = page_buf ;
439+ int col_idx = atol (argv [3 ]);
440+ int val_type = resolve_value (argv [4 ], buf );
441+ if (val_type == ULS_TYPE_INT || val_type == ULS_TYPE_REAL )
442+ len = 8 ;
443+ if (uls_bin_srch_row_by_val (& ctx , col_idx , val_type ,
444+ buf , len , col_idx == -1 ? 1 : 0 )) {
445+ printf ("Not Found\n" );
446+ } else {
447+ display_row (ctx );
448+ }
449+ putchar ('\n' );
450+ return 0 ;
451+ }
452+
335453int read_db (int argc , char * argv []) {
336- byte initial_buf [72 ];
454+ byte buf [72 ];
337455 struct uls_read_context ctx ;
338- ctx .buf = initial_buf ;
456+ ctx .buf = buf ;
339457 ctx .read_fn = read_fn_rctx ;
340458 fd = open (argv [2 ], O_RDWR | O_SYNC , S_IRUSR | S_IWUSR );
341459 if (fd == -1 ) {
@@ -346,57 +464,13 @@ int read_db(int argc, char *argv[]) {
346464 printf ("Error during init\n" );
347465 return -3 ;
348466 }
349- byte actual_buf [1 << (ctx .page_size_exp == 1 ? 16 : ctx .page_size_exp )];
350- ctx .buf = actual_buf ;
467+ byte page_buf [1 << (ctx .page_size_exp == 1 ? 16 : ctx .page_size_exp )];
468+ ctx .buf = page_buf ;
351469 uint32_t rowid = atol (argv [3 ]);
352470 if (uls_srch_row_by_id (& ctx , rowid )) {
353471 printf ("Not Found\n" );
354472 } else {
355- int col_count = uls_cur_row_col_count (& ctx );
356- for (int i = 0 ; i < col_count ; i ++ ) {
357- if (i )
358- putchar ('|' );
359- uint32_t col_type ;
360- const byte * col_val = (const byte * ) uls_read_col_val (& ctx , i , & col_type );
361- switch (col_type ) {
362- case 0 :
363- printf ("null" );
364- break ;
365- case 1 :
366- printf ("%d" , * ((int8_t * )col_val ));
367- break ;
368- case 2 :
369- if (1 ) {
370- int16_t ival = read_int16 (col_val );
371- printf ("%d" , ival );
372- }
373- break ;
374- case 4 : {
375- int32_t ival = read_int32 (col_val );
376- printf ("%d" , ival );
377- break ;
378- }
379- case 6 : {
380- int64_t ival = read_int64 (col_val );
381- printf ("%lld" , ival );
382- break ;
383- }
384- case 7 : {
385- double dval = read_double (col_val );
386- printf ("%lf" , dval );
387- break ;
388- }
389- default : {
390- uint32_t col_len = uls_derive_data_len (col_type );
391- for (int j = 0 ; j < col_len ; j ++ ) {
392- if (col_type % 2 )
393- putchar (col_val [j ]);
394- else
395- printf ("x%2x " , col_val [j ]);
396- }
397- }
398- }
399- }
473+ display_row (ctx );
400474 }
401475 putchar ('\n' );
402476 return 0 ;
@@ -413,6 +487,9 @@ int main(int argc, char *argv[]) {
413487 if (argc == 4 && strcmp (argv [1 ], "-r" ) == 0 ) {
414488 read_db (argc , argv );
415489 } else
490+ if (argc == 5 && strcmp (argv [1 ], "-b" ) == 0 ) {
491+ bin_srch_db (argc , argv );
492+ } else
416493 if (argc == 2 && strcmp (argv [1 ], "-n" ) == 0 ) {
417494 test_basic ("hello.db" );
418495 test_multilevel ("ml.db" );
0 commit comments