5858#include "sb_logger.h"
5959
6060#include "sb_ck_pr.h"
61- #include "pthread.h"
61+ #include <ctype.h>
6262
6363TLS sb_rng_state_t sb_rng_state CK_CC_CACHELINE ;
6464
@@ -460,10 +460,16 @@ uint32_t sb_rand_zipfian(uint32_t a, uint32_t b)
460460 sb_rand_zipfian_int (b - a + 1 , zipf_exp , zipf_s , zipf_hIntegralX1 ) - 1 ;
461461}
462462
463+ struct SbKeyVal {
464+ size_t cap ;
465+ size_t klen ;
466+ size_t vlen ;
467+ char * str ; // also key
468+ char * val ; // ptr after tab char
469+ };
470+ static __thread struct SbKeyVal * g_kv = NULL ;
471+
463472static FILE * file = NULL ;
464- static char * str_buf ;
465- static size_t len ;
466- static pthread_mutex_t file_mtx ;
467473
468474int sb_file_init ()
469475{
@@ -475,38 +481,49 @@ int sb_file_init()
475481 log_text (LOG_FATAL , "Invalid filename: %s" , sb_globals .filename );
476482 return 1 ;
477483 }
478- pthread_mutex_init (& file_mtx , NULL );
479484
480- len = 4194304 * sizeof (char );
481- str_buf = (char * )malloc (len );
482485 return 0 ;
483486}
484487
485- void sb_file_str ( char * buf1 , char * buf2 )
488+ struct SbKeyVal * sb_file_str ( )
486489{
487490 assert (file != NULL );
488491
489- pthread_mutex_lock (& file_mtx );
490- ssize_t read = getline (& str_buf , & len , file );
491- if (read != -1 )
492- {
493- int pos = 0 ;
494- while (pos < read ) {
495- if (str_buf [pos ] == '\t' ) {
496- memcpy (buf1 , str_buf , pos );
497- buf1 [pos ] = 0 ;
498- memcpy (buf2 , str_buf + pos + 1 , read - pos - 2 );
499- buf2 [read - pos - 2 ] = 0 ;
500- break ;
492+ if (NULL == g_kv ) {
493+ g_kv = (struct SbKeyVal * )malloc (sizeof (struct SbKeyVal ));
494+ g_kv -> klen = 0 ;
495+ g_kv -> vlen = 0 ;
496+ g_kv -> cap = 4 * 1024 * 1024 ;
497+ g_kv -> str = (char * )malloc (g_kv -> cap );
498+ g_kv -> val = NULL ;
499+ }
500+ while (true) {
501+ ssize_t read = getline (& g_kv -> str , & g_kv -> cap , file );
502+ if (read != -1 ) {
503+ char * str = g_kv -> str ;
504+ while (read > 0 && isspace ((unsigned char )str [read ])) {
505+ str [-- read ] = '\0' ; // trim trailing spaces
506+ }
507+ char * tab = (char * )memchr (str , '\t' , read );
508+ if (NULL == tab ) {
509+ g_kv -> val = str + read ; // empty c string
510+ g_kv -> vlen = 0 ;
511+ g_kv -> klen = read ;
501512 }
502- pos ++ ;
513+ else {
514+ tab [0 ] = '\0' ;
515+ g_kv -> klen = tab - str ;
516+ g_kv -> vlen = read - (tab - str ) - 1 ;
517+ g_kv -> val = tab + 1 ;
518+ }
519+ break ;
503520 }
504-
505- if ( pos >= read ) {
506- memcpy ( buf2 , str_buf , read );
521+ else {
522+ rewind ( file );
523+ fprintf ( stderr , "sb_file_str: read eof, rewind(%s)\n" , sb_globals . filename );
507524 }
508525 }
509- pthread_mutex_unlock ( & file_mtx ) ;
526+ return g_kv ;
510527}
511528
512529void sb_file_done ()
@@ -515,12 +532,6 @@ void sb_file_done()
515532 {
516533 fclose (file );
517534 }
518-
519- if (str_buf )
520- {
521- free (str_buf );
522- }
523- pthread_mutex_destroy (& file_mtx );
524535}
525536
526537
0 commit comments