Пример #1
0
int main (int argc, char *argv[])
{


    trace(argc, argv);

    int opt_err;

    MY_INIT (argv[0]);
    load_defaults ("my", client_groups, &argc, &argv);

    if ((opt_err = handle_options (&argc, &argv, my_opts, get_one_option)))
        exit (opt_err);

    /* solicit password if necessary */
    if (ask_password)
        opt_password = get_tty_password (NULL);

    /* get database name if present on command line */
    if (argc > 0)
    {
        opt_db_name = argv[0];
        --argc;
        ++argv;
    }

    /* initialize client library */
    if (mysql_library_init (0, NULL, NULL))
    {
        print_error (NULL, "mysql_library_init() failed");
        exit (1);
    }

    /* initialize connection handler */
    conn = mysql_init (NULL);
    if (conn == NULL)
    {
        print_error (NULL, "mysql_init() failed (probably out of memory)");
        exit (1);
    }

#ifdef HAVE_OPENSSL
    /* pass SSL information to client library */
    if (opt_use_ssl)
        mysql_ssl_set (conn, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
                       opt_ssl_capath, opt_ssl_cipher);
#if (MYSQL_VERSION_ID >= 50023 && MYSQL_VERSION_ID < 50100) \
    || MYSQL_VERSION_ID >= 50111
    mysql_options (conn,MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
                   (char*)&opt_ssl_verify_server_cert);
#endif
#endif

    /* connect to server */
    if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
                            opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
    {
        print_error (conn, "mysql_real_connect() failed");
        mysql_close (conn);
        exit (1);
    }

    process_prepared_statements (conn);

    /* disconnect from server, terminate client library */
    mysql_close (conn);
    mysql_library_end ();
    exit (0);
}
Пример #2
0
void memtest()
{
#define	TEST_SIZE 1024*1024
#define	TEST_RUN 1024
#define LONG_RUN 100000
#define ma(s) MY_MALLOC(mem_block, (s),__FILE__, __FUNCTION__, \
                                                                __LINE__);
#define mf(p)   MY_FREE(mem_block, (p), __FILE__,  __FUNCTION__, \
                                                                __LINE__);
	char tst_mem[TEST_SIZE];
	struct MY_BLOCK* mem_block;
	char *p0,*p1,*p2,*p3,*p4,*p5,*p6/*,*p7,*p8,*p9*/;
	int i, j, f;
	char *p[TEST_RUN];
	int t;

#ifdef CHANGEABLE_DEBUG_LEVEL
	*debug=7;
#else
	debug=7;
#endif
	log_stderr=1;

	printf("entering test\n");

	mem_block=MY_INIT( tst_mem, TEST_SIZE );

	/* coalescing test w/big fragments */
	p0=ma(8194);
	p1=ma(8194);
	p2=ma(8194);
	MY_STATUS(mem_block);
	mf(p1);
	mf(p0);
	MY_STATUS(mem_block);
	mf(p2);
	MY_STATUS(mem_block);

	/* reuse test w/big fragments */
	p0=ma(8194);
	p1=ma(4196);
	mf(p0);
	p0=ma(8190);
	MY_STATUS(mem_block);
	mf(p1);
	mf(p0);
	MY_STATUS(mem_block);


	exit(0);

	p0=ma(8);
	p1=ma(24);
	p2=ma(32);
	p3=ma(32);
	p4=ma(32);
	p5=ma(1024);
	p6=ma(2048);

//	MY_STATUS(mem_block);

//	*(p0+9)=0;
	mf(p0);
	mf(p2);
	mf(p5);
	mf(p6);

//	MY_STATUS(mem_block);

	mf(p1);
	mf(p4);
	mf(p3);
//	mf(p3);

//	MY_STATUS(mem_block);

	for (i=0;i<TEST_RUN;i++)
		p[i]=ma( random() & 1023 );
//	MY_STATUS(mem_block);
	for (i=0;i<TEST_RUN;i++)
		mf( p[i] );
//	MY_STATUS(mem_block);

	f = 0;
#define GRANULARITY 100
	for (j=0; j<LONG_RUN; j++) {
		for (i=0;i<TEST_RUN;i++) {
			t=random() & 1023;
			if (! (t%24) ) t=(t+4096)*2;
			p[i]=ma( random() & 1023 );
		}
		for (i=TEST_RUN/3;i<2*TEST_RUN/3;i++)
			mf( p[i] );
		for (i=TEST_RUN/3;i<2*TEST_RUN/3;i++) {
			t=random() & 1023;
			if (! (t%24) ) t=(t+4096)*2;
			p[i]=ma( random() & 1023 );
		}
		for (i=0;i<TEST_RUN;i++)
			mf( p[i] );
		if ( GRANULARITY*j/LONG_RUN > f ) {
			f=GRANULARITY*j/LONG_RUN ;
			printf("%d%% done\n", f);
		}
	}
	printf("now I'm really done\n");
	MY_STATUS(mem_block);
	printf("And I'm done with dumping final report too\n");

	exit(0);
}
Пример #3
0
/** one-time initialization for mysql */
int
netsnmp_mysql_init(void)
{
    int not_argc = 0, i;
    char *not_args[] = { NULL };
    char **not_argv = not_args;
    netsnmp_trapd_handler *traph;

    DEBUGMSGTL(("sql:init","called\n"));

    /** negative or 0 interval disables sql logging */
    if (_sql.queue_interval <= 0) {
        DEBUGMSGTL(("sql:init",
                    "mysql not enabled (sqlSaveInterval is <= 0)\n"));
        return 0;
    }

    /** create queue for storing traps til they are written to the db */
    _sql.queue = netsnmp_container_find("fifo");
    if (NULL == _sql.queue) {
        snmp_log(LOG_ERR, "Could not allocate sql buf container\n");
        return -1;
    }

#ifdef HAVE_BROKEN_LIBMYSQLCLIENT
    my_init();
#else
    MY_INIT("snmptrapd");
#endif

    /** load .my.cnf values */
    load_defaults ("my", _sql.groups, &not_argc, &not_argv);
    for(i=0; i < not_argc; ++i) {
        if (NULL == not_argv[i])
            continue;
        if (strncmp(not_argv[i],"--password="******"--host=",7) == 0)
            _sql.host_name = &not_argv[i][7];
        else if (strncmp(not_argv[i],"--user="******"--port=",7) == 0)
            _sql.port_num = atoi(&not_argv[i][7]);
        else if (strncmp(not_argv[i],"--socket=",9) == 0)
            _sql.socket_name = &not_argv[i][9];
        else
            snmp_log(LOG_WARNING, "unknown argument[%d] %s\n", i, not_argv[i]);
    }

    /** init bind structures */
    memset(_tbind, 0x0, sizeof(_tbind));
    memset(_vbind, 0x0, sizeof(_vbind));

    /** trap static bindings */
    _tbind[TBIND_HOST].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_HOST].length = &_tbind[TBIND_HOST].buffer_length;

    _tbind[TBIND_OID].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_OID].length = &_tbind[TBIND_OID].buffer_length;

    _tbind[TBIND_REQID].buffer_type = MYSQL_TYPE_LONG;
    _tbind[TBIND_REQID].is_unsigned = 1;

    _tbind[TBIND_VER].buffer_type = MYSQL_TYPE_SHORT;
    _tbind[TBIND_VER].is_unsigned = 1;

    _tbind[TBIND_TYPE].buffer_type = MYSQL_TYPE_SHORT;
    _tbind[TBIND_TYPE].is_unsigned = 1;

    _tbind[TBIND_DATE].buffer_type = MYSQL_TYPE_DATETIME;

    _tbind[TBIND_USER].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_USER].length = &_tbind[TBIND_USER].buffer_length;

    _tbind[TBIND_TRANSPORT].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_TRANSPORT].length = &_tbind[TBIND_TRANSPORT].buffer_length;

    _tbind[TBIND_SECURITY_MODEL].buffer_type = MYSQL_TYPE_SHORT;
    _tbind[TBIND_SECURITY_MODEL].is_unsigned = 1;

    _tbind[TBIND_v3_MSGID].buffer_type = MYSQL_TYPE_LONG;
    _tbind[TBIND_v3_MSGID].is_unsigned = 1;
    _tbind[TBIND_v3_SECURITY_LEVEL].buffer_type = MYSQL_TYPE_SHORT;
    _tbind[TBIND_v3_SECURITY_LEVEL].is_unsigned = 1;
    _tbind[TBIND_v3_CONTEXT_NAME].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_v3_CONTEXT_ENGINE].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_v3_SECURITY_NAME].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_v3_SECURITY_NAME].length =
        &_tbind[TBIND_v3_SECURITY_NAME].buffer_length;
    _tbind[TBIND_v3_CONTEXT_NAME].length =
        &_tbind[TBIND_v3_CONTEXT_NAME].buffer_length;
    _tbind[TBIND_v3_SECURITY_ENGINE].buffer_type = MYSQL_TYPE_STRING;
    _tbind[TBIND_v3_SECURITY_ENGINE].length =
        &_tbind[TBIND_v3_SECURITY_ENGINE].buffer_length;
    _tbind[TBIND_v3_CONTEXT_ENGINE].length =
        &_tbind[TBIND_v3_CONTEXT_ENGINE].buffer_length;

    _tbind[TBIND_v3_MSGID].is_null =
        _tbind[TBIND_v3_SECURITY_LEVEL].is_null =
        _tbind[TBIND_v3_CONTEXT_NAME].is_null =
        _tbind[TBIND_v3_CONTEXT_ENGINE].is_null =
        _tbind[TBIND_v3_SECURITY_NAME].is_null =
        _tbind[TBIND_v3_SECURITY_ENGINE].is_null = &_no_v3;
    
    /** variable static bindings */
    _vbind[VBIND_ID].buffer_type = MYSQL_TYPE_LONG;
    _vbind[VBIND_ID].is_unsigned = 1;

    _vbind[VBIND_OID].buffer_type = MYSQL_TYPE_STRING;
    _vbind[VBIND_OID].length = &_vbind[VBIND_OID].buffer_length;

    _vbind[VBIND_TYPE].buffer_type = MYSQL_TYPE_SHORT;
    _vbind[VBIND_TYPE].is_unsigned = 1;

#ifdef NETSNMP_MYSQL_TRAP_VALUE_TEXT
    _vbind[VBIND_VAL].buffer_type = MYSQL_TYPE_STRING;
#else
    _vbind[VBIND_VAL].buffer_type = MYSQL_TYPE_BLOB;
#endif
    _vbind[VBIND_VAL].length = &_vbind[VBIND_VAL].buffer_length;

    _sql.conn = mysql_init (NULL);
    if (_sql.conn == NULL) {
        netsnmp_sql_error("mysql_init() failed (out of memory?)");
        return -1;
    }

    /** try to connect; we'll try again later if we fail */
    (void) netsnmp_mysql_connect();

    /** register periodic queue save */
    _sql.alarm_id = snmp_alarm_register(_sql.queue_interval, /* seconds */
                                        1,                   /* repeat */
                                        _sql_process_queue,  /* function */
                                        NULL);               /* client args */

    /** add handler */
    traph = netsnmp_add_global_traphandler(NETSNMPTRAPD_PRE_HANDLER,
                                           mysql_handler);
    if (NULL == traph) {
        snmp_log(LOG_ERR, "Could not allocate sql trap handler\n");
        return -1;
    }
    traph->authtypes = TRAP_AUTH_LOG;

    atexit(netsnmp_mysql_cleanup);
    return 0;
}
Пример #4
0
int main(int argc, char *argv[])
{
  uint i;
  int j,n1,n2,n3,error,k;
  uint write_count,update,dupp_keys,opt_delete,start,length,blob_pos,
       reclength,ant;
  ulong lastpos,range_records,records;
  N_INFO *file;
  N_KEYDEF keyinfo[10];
  N_RECINFO recinfo[10];
  N_ISAMINFO info;
  char *filename,*blob_buffer;
  MY_INIT(argv[0]);

  filename= (char*) "test2.ISM";
  get_options(argc,argv);
  if (! async_io)
    my_disable_async_io=1;

  reclength=STANDAR_LENGTH+60+(use_blob ? 8 : 0);
  blob_pos=STANDAR_LENGTH+60;
  keyinfo[0].seg[0].base.start=0;
  keyinfo[0].seg[0].base.length=6;
  keyinfo[0].seg[0].base.type=HA_KEYTYPE_TEXT;
  keyinfo[0].seg[0].base.flag=(uint8) pack_type;
  keyinfo[0].seg[1].base.type=0;
  keyinfo[0].base.flag = (uint8) (pack_type ? HA_PACK_KEY : 0);
  keyinfo[1].seg[0].base.start=7;
  keyinfo[1].seg[0].base.length=6;
  keyinfo[1].seg[0].base.type=HA_KEYTYPE_BINARY;
  keyinfo[1].seg[0].base.flag=0;
  keyinfo[1].seg[1].base.start=0;			/* Tv}delad nyckel */
  keyinfo[1].seg[1].base.length=6;
  keyinfo[1].seg[1].base.type=HA_KEYTYPE_NUM;
  keyinfo[1].seg[1].base.flag=HA_REVERSE_SORT;
  keyinfo[1].seg[2].base.type=0;
  keyinfo[1].base.flag =0;
  keyinfo[2].seg[0].base.start=12;
  keyinfo[2].seg[0].base.length=8;
  keyinfo[2].seg[0].base.type=HA_KEYTYPE_BINARY;
  keyinfo[2].seg[0].base.flag=HA_REVERSE_SORT;
  keyinfo[2].seg[1].base.type=0;
  keyinfo[2].base.flag =HA_NOSAME;
  keyinfo[3].seg[0].base.start=0;
  keyinfo[3].seg[0].base.length=reclength-(use_blob ? 8 : 0);
  keyinfo[3].seg[0].base.type=HA_KEYTYPE_TEXT;
  keyinfo[3].seg[0].base.flag=(uint8) pack_type;
  keyinfo[3].seg[1].base.type=0;
  keyinfo[3].base.flag = (uint8) (pack_type ? HA_PACK_KEY : 0);
  keyinfo[4].seg[0].base.start=0;
  keyinfo[4].seg[0].base.length=5;
  keyinfo[4].seg[0].base.type=HA_KEYTYPE_TEXT;
  keyinfo[4].seg[0].base.flag=0;
  keyinfo[4].seg[1].base.type=0;
  keyinfo[4].base.flag = (uint8) (pack_type ? HA_PACK_KEY : 0);
  keyinfo[5].seg[0].base.start=0;
  keyinfo[5].seg[0].base.length=4;
  keyinfo[5].seg[0].base.type=HA_KEYTYPE_TEXT;
  keyinfo[5].seg[0].base.flag=(uint8) pack_type;
  keyinfo[5].seg[1].base.type=0;
  keyinfo[5].base.flag = (uint8) (pack_type ? HA_PACK_KEY : 0);

  recinfo[0].base.type=pack_fields ? FIELD_SKIP_PRESPACE : 0;
  recinfo[0].base.length=7;
  recinfo[1].base.type=pack_fields ? FIELD_SKIP_PRESPACE : 0;
  recinfo[1].base.length=5;
  recinfo[2].base.type=pack_fields ? FIELD_SKIP_PRESPACE : 0;
  recinfo[2].base.length=9;
  recinfo[3].base.type=FIELD_NORMAL;
  recinfo[3].base.length=STANDAR_LENGTH-7-5-9-4;
  recinfo[4].base.type=pack_fields ? FIELD_SKIP_ZERO : 0;
  recinfo[4].base.length=4;
  recinfo[5].base.type=pack_fields ? FIELD_SKIP_ENDSPACE : 0;
  recinfo[5].base.length=60;
  if (use_blob)
  {
    recinfo[6].base.type=FIELD_BLOB;
    recinfo[6].base.length=4+sizeof(char*);	/* 4 byte ptr, 4 byte length */
    recinfo[7].base.type= FIELD_LAST;
  }
  else
    recinfo[6].base.type= FIELD_LAST;

  write_count=update=dupp_keys=opt_delete=0;
  blob_buffer=0;

  for (i=999 ; i>0 ; i--) key1[i]=0;
  for (i=4999 ; i>0 ; i--) key3[i]=0;

  printf("- Creating isam-file\n");
  /*  DBUG_PUSH(""); */
  my_delete(filename,MYF(0));		/* Remove old locks under gdb */
  file= 0;
  if (nisam_create(filename,keys,&keyinfo[first_key],&recinfo[0],
		   (ulong) (rec_pointer_size ? (1L << (rec_pointer_size*8))/
			    reclength : 0),100l,0,0,0L))
    goto err;
  if (use_log)
    nisam_log(1);
  if (!(file=nisam_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
    goto err;
  printf("- Writing key:s\n");
  if (key_cacheing)
    init_key_cache(IO_SIZE*16);		/* Use a small cache */
  if (locking)
    nisam_lock_database(file,F_WRLCK);
  if (write_cacheing)
    nisam_extra(file,HA_EXTRA_WRITE_CACHE);

  for (i=0 ; i < recant ; i++)
  {
    n1=rnd(1000); n2=rnd(100); n3=rnd(5000);
    sprintf(record,"%6d:%4d:%8d:Pos: %4d    ",n1,n2,n3,write_count);
    longstore(record+STANDAR_LENGTH-4,(long) i);
    fix_length(record,(uint) STANDAR_LENGTH+rnd(60));
    put_blob_in_record(record+blob_pos,&blob_buffer);
    DBUG_PRINT("test",("record: %d",i));

    if (nisam_write(file,record))
    {
      if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
      {
	printf("Error: %d in write at record: %d\n",my_errno,i);
	goto err;
      }
      if (verbose) printf("   Double key: %d\n",n3);
    }
    else
    {
      if (key3[n3] == 1 && first_key <3 && first_key+keys >= 3)
      {
	printf("Error: Didn't get error when writing second key: '%8d'\n",n3);
	goto err;
      }
      write_count++; key1[n1]++; key3[n3]=1;
    }

    /* Check if we can find key without flushing database */
    if (i == recant/2)
    {
      for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
      if (!j)
	for (j=999 ; j>0 && key1[j] == 0 ; j--) ;
      sprintf(key,"%6d",j);
      if (nisam_rkey(file,read_record,0,key,0,HA_READ_KEY_EXACT))
      {
	printf("Test in loop: Can't find key: \"%s\"\n",key);
	goto err;
      }
    }
  }
  if (testflag==1) goto end;

  if (write_cacheing)
    if (nisam_extra(file,HA_EXTRA_NO_CACHE))
    {
      puts("got error from nisam_extra(HA_EXTRA_NO_CACHE)");
      goto end;
    }

  printf("- Delete\n");
  for (i=0 ; i<recant/10 ; i++)
  {
    for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
    if (j != 0)
    {
      sprintf(key,"%6d",j);
      if (nisam_rkey(file,read_record,0,key,0,HA_READ_KEY_EXACT))
      {
	printf("can't find key1: \"%s\"\n",key);
	goto err;
      }
      if (nisam_delete(file,read_record))
      {
	printf("error: %d; can't delete record: \"%s\"\n", my_errno,read_record);
	goto err;
      }
      opt_delete++;
      key1[atoi(read_record+keyinfo[0].seg[0].base.start)]--;
      key3[atoi(read_record+keyinfo[2].seg[0].base.start)]=0;
    }
  }
  if (testflag==2) goto end;

  printf("- Update\n");
  for (i=0 ; i<recant/10 ; i++)
  {
    n1=rnd(1000); n2=rnd(100); n3=rnd(5000);
    sprintf(record2,"%6d:%4d:%8d:XXX: %4d     ",n1,n2,n3,update);
    longstore(record2+STANDAR_LENGTH-4,(long) i);
    fix_length(record2,(uint) STANDAR_LENGTH+rnd(60));

    for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
    if (j != 0)
    {
      sprintf(key,"%6d",j);
      if (nisam_rkey(file,read_record,0,key,0,HA_READ_KEY_EXACT))
      {
	printf("can't find key1: \"%s\"\n",key);
	goto err;
      }
      if (use_blob)
      {
	if (i & 1)
	  put_blob_in_record(record+blob_pos,&blob_buffer);
	else
	  bmove(record+blob_pos,read_record+blob_pos,8);
      }
      if (nisam_update(file,read_record,record2))
      {
	if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
	{
	  printf("error: %d; can't update:\nFrom: \"%s\"\nTo:   \"%s\"\n",
		 my_errno,read_record,record2);
	  goto err;
	}
	if (verbose)
	  printf("Double key when tried to update:\nFrom: \"%s\"\nTo:   \"%s\"\n",record,record2);
      }
      else
      {
	key1[atoi(read_record+keyinfo[0].seg[0].base.start)]--;
	key3[atoi(read_record+keyinfo[2].seg[0].base.start)]=0;
	key1[n1]++; key3[n3]=1;
	update++;
      }
    }
  }
  if (testflag==3) goto end;

  printf("- Same key: first - next -> last - prev -> first\n");
  DBUG_PRINT("progpos",("first - next -> last - prev -> first"));
  for (i=999, dupp_keys=j=0 ; i>0 ; i--)
  {
    if (key1[i] >dupp_keys) { dupp_keys=key1[i]; j=i; }
  }
  sprintf(key,"%6d",j);
  if (verbose) printf("	 Using key: \"%s\"  Keys: %d\n",key,dupp_keys);
  if (nisam_rkey(file,read_record,0,key,0,HA_READ_KEY_EXACT)) goto err;
  if (nisam_rsame(file,read_record2,-1)) goto err;
  if (memcmp(read_record,read_record2,reclength) != 0)
  {
    printf("nisam_rsame didn't find same record\n");
    goto end;
  }
  nisam_info(file,&info,0);
  if (nisam_rfirst(file,read_record2,0) ||
      nisam_rsame_with_pos(file,read_record2,0,info.recpos) ||
      memcmp(read_record,read_record2,reclength) != 0)
  {
    printf("nisam_rsame_with_pos didn't find same record\n");
    goto end;
  }
  {
    int skr=nisam_rnext(file,read_record2,0);
    if ((skr && my_errno != HA_ERR_END_OF_FILE) ||
	nisam_rprev(file,read_record2,-1) ||
	memcmp(read_record,read_record2,reclength) != 0)
    {
      printf("nisam_rsame_with_pos lost position\n");
      goto end;
    }
  }
  ant=1;
  start=keyinfo[0].seg[0].base.start; length=keyinfo[0].seg[0].base.length;
  while (nisam_rnext(file,read_record2,0) == 0 &&
	 memcmp(read_record2+start,key,length) == 0) ant++;
  if (ant != dupp_keys)
  {
    printf("next: I can only find: %d keys of %d\n",ant,dupp_keys);
    goto end;
  }
  ant=0;
  while (nisam_rprev(file,read_record3,0) == 0 &&
	 bcmp(read_record3+start,key,length) == 0) ant++;
  if (ant != dupp_keys)
  {
    printf("prev: I can only find: %d records of %d\n",ant,dupp_keys);
    goto end;
  }

  printf("- All keys: first - next -> last - prev -> first\n");
  DBUG_PRINT("progpos",("All keys: first - next -> last - prev -> first"));
  ant=1;
  if (nisam_rfirst(file,read_record,0))
  {
    printf("Can't find first record\n");
    goto end;
  }
  while (nisam_rnext(file,read_record3,0) == 0 && ant < write_count+10)
    ant++;
  if (ant != write_count - opt_delete)
  {
    printf("next: I found: %d records of %d\n",ant,write_count - opt_delete);
    goto end;
  }
  if (nisam_rlast(file,read_record2,0) ||
      bcmp(read_record2,read_record3,reclength))
  {
    printf("Can't find last record\n");
    DBUG_DUMP("record2",(byte*) read_record2,reclength);
    DBUG_DUMP("record3",(byte*) read_record3,reclength);
    goto end;
  }
  ant=1;
  while (nisam_rprev(file,read_record3,0) == 0 && ant < write_count+10)
    ant++;
  if (ant != write_count - opt_delete)
  {
    printf("prev: I found: %d records of %d\n",ant,write_count);
    goto end;
  }
  if (bcmp(read_record,read_record3,reclength))
  {
    printf("Can't find first record\n");
    goto end;
  }

  printf("- Test if: Read first - next - prev - prev - next == first\n");
  DBUG_PRINT("progpos",("- Read first - next - prev - prev - next == first"));
  if (nisam_rfirst(file,read_record,0) ||
      nisam_rnext(file,read_record3,0) ||
      nisam_rprev(file,read_record3,0) ||
      nisam_rprev(file,read_record3,0) == 0 ||
      nisam_rnext(file,read_record3,0))
      goto err;
  if (bcmp(read_record,read_record3,reclength) != 0)
     printf("Can't find first record\n");

  printf("- Test if: Read last - prev - next - next - prev == last\n");
  DBUG_PRINT("progpos",("Read last - prev - next - next - prev == last"));
  if (nisam_rlast(file,read_record2,0) ||
      nisam_rprev(file,read_record3,0) ||
      nisam_rnext(file,read_record3,0) ||
      nisam_rnext(file,read_record3,0) == 0 ||
      nisam_rprev(file,read_record3,0))
      goto err;
  if (bcmp(read_record2,read_record3,reclength))
     printf("Can't find last record\n");

  puts("- Test read key-part");
  strmov(key2,key);
  for(i=strlen(key2) ; i-- > 1 ;)
  {
    key2[i]=0;
    if (nisam_rkey(file,read_record,0,key2,(uint) i,HA_READ_KEY_EXACT)) goto err;
    if (bcmp(read_record+start,key,(uint) i))
    {
      puts("Didn't find right record");
      goto end;
    }
  }
  if (dupp_keys > 2)
  {
    printf("- Read key (first) - next - delete - next -> last\n");
    DBUG_PRINT("progpos",("first - next - delete - next -> last"));
    if (nisam_rkey(file,read_record,0,key,0,HA_READ_KEY_EXACT)) goto err;
    if (nisam_rnext(file,read_record3,0)) goto err;
    if (nisam_delete(file,read_record3)) goto err;
    opt_delete++;
    ant=1;
    while (nisam_rnext(file,read_record3,0) == 0 &&
	   bcmp(read_record3+start,key,length) == 0) ant++;
    if (ant != dupp_keys-1)
    {
      printf("next: I can only find: %d keys of %d\n",ant,dupp_keys-1);
      goto end;
    }
  }
  if (dupp_keys>4)
  {
    printf("- Read last of key - prev - delete - prev -> first\n");
    DBUG_PRINT("progpos",("last - prev - delete - prev -> first"));
    if (nisam_rprev(file,read_record3,0)) goto err;
    if (nisam_rprev(file,read_record3,0)) goto err;
    if (nisam_delete(file,read_record3)) goto err;
    opt_delete++;
    ant=1;
    while (nisam_rprev(file,read_record3,0) == 0 &&
	   bcmp(read_record3+start,key,length) == 0) ant++;
    if (ant != dupp_keys-2)
    {
      printf("next: I can only find: %d keys of %d\n",ant,dupp_keys-2);
      goto end;
    }
  }
  if (dupp_keys > 6)
  {
    printf("- Read first - delete - next -> last\n");
    DBUG_PRINT("progpos",("first - delete - next -> last"));
    if (nisam_rkey(file,read_record3,0,key,0,HA_READ_KEY_EXACT)) goto err;
    if (nisam_delete(file,read_record3)) goto err;
    opt_delete++;
    ant=1;
    if (nisam_rnext(file,read_record,0))
      goto err;					/* Skall finnas poster */
    while (nisam_rnext(file,read_record3,0) == 0 &&
	   bcmp(read_record3+start,key,length) == 0) ant++;
    if (ant != dupp_keys-3)
    {
      printf("next: I can only find: %d keys of %d\n",ant,dupp_keys-3);
      goto end;
    }

    printf("- Read last - delete - prev -> first\n");
    DBUG_PRINT("progpos",("last - delete - prev -> first"));
    if (nisam_rprev(file,read_record3,0)) goto err;
    if (nisam_delete(file,read_record3)) goto err;
    opt_delete++;
    ant=0;
    while (nisam_rprev(file,read_record3,0) == 0 &&
	   bcmp(read_record3+start,key,length) == 0) ant++;
    if (ant != dupp_keys-4)
    {
      printf("next: I can only find: %d keys of %d\n",ant,dupp_keys-4);
      goto end;
    }
  }

  puts("- Test if: Read rrnd - same");
  DBUG_PRINT("progpos",("Read rrnd - same"));
  for (i=0 ; i < write_count ; i++)
  {
    if (nisam_rrnd(file,read_record,i == 0 ? 0L : NI_POS_ERROR) == 0)
      break;
  }
  if (i == write_count)
    goto err;

  bmove(read_record2,read_record,reclength);
  for (i=2 ; i-- > 0 ;)
  {
    if (nisam_rsame(file,read_record2,(int) i)) goto err;
    if (bcmp(read_record,read_record2,reclength) != 0)
    {
      printf("is_rsame didn't find same record\n");
      goto end;
    }
  }
  puts("- Test nisam_records_in_range");
  nisam_info(file,&info,HA_STATUS_VARIABLE);
  for (i=0 ; i < info.keys ; i++)
  {
    if (nisam_rfirst(file,read_record,(int) i) ||
	nisam_rlast(file,read_record2,(int) i))
      goto err;
    copy_key(file,(uint) i,(uchar*) read_record,(uchar*) key);
    copy_key(file,(uint) i,(uchar*) read_record2,(uchar*) key2);
    range_records=nisam_records_in_range(file,(int) i,key,0,HA_READ_KEY_EXACT,
				      key2,0,HA_READ_AFTER_KEY);
    if (range_records < info.records*8/10 ||
	range_records > info.records*12/10)
    {
      printf("ni_records_range returned %lu; Should be about %lu\n",
	     range_records,info.records);
      goto end;
    }
    if (verbose)
    {
      printf("ni_records_range returned %ld;  Exact is %ld  (diff: %4.2g %%)\n",
	     range_records,info.records,
	     labs((long) range_records - (long) info.records)*100.0/
	     info.records);

    }
  }
  for (i=0 ; i < 5 ; i++)
  {
    for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
    for (k=rnd(1000) ; k>0 && key1[k] == 0 ; k--) ;
    if (j != 0 && k != 0)
    {
      if (j > k)
	swap(int,j,k);
      sprintf(key,"%6d",j);
      sprintf(key2,"%6d",k);
      range_records=nisam_records_in_range(file,0,key,0,HA_READ_AFTER_KEY,
					key2,0,HA_READ_BEFORE_KEY);
      records=0;
      for (j++ ; j < k ; j++)
	records+=key1[j];
      if ((long) range_records < (long) records*7/10-2 ||
	  (long) range_records > (long) records*13/10+2)
      {
	printf("ni_records_range returned %ld; Should be about %ld\n",
	       range_records,records);
	goto end;
      }
      if (verbose && records)
      {
	printf("ni_records_range returned %ld;  Exact is %ld  (diff: %4.2g %%)\n",
	       range_records,records,
	       labs((long) range_records-(long) records)*100.0/records);

      }
    }
    }

  printf("- nisam_info\n");
  nisam_info(file,&info,0);
  if (info.records != write_count-opt_delete || info.deleted > opt_delete + update
      || info.keys != keys)
  {
    puts("Wrong info from nisam_info");
    printf("Got: records: %ld  opt_delete: %ld  i_keys: %d\n",
	   info.records,info.deleted,info.keys);
  }
  if (verbose)
  {
    char buff[80];
    get_date(buff,3,info.create_time);
    printf("info: Created %s\n",buff);
    get_date(buff,3,info.isamchk_time);
    printf("info: checked %s\n",buff);
    get_date(buff,3,info.update_time);
    printf("info: Modified %s\n",buff);
  }

  nisam_panic(HA_PANIC_WRITE);
  nisam_panic(HA_PANIC_READ);
  if (nisam_is_changed(file))
    puts("Warning: nisam_is_changed reported that datafile was changed");

  printf("- nisam_extra(CACHE) + nisam_rrnd.... + nisam_extra(NO_CACHE)\n");
  if (nisam_extra(file,HA_EXTRA_RESET) || nisam_extra(file,HA_EXTRA_CACHE))
  {
    if (locking || (!use_blob && !pack_fields))
    {
      puts("got error from nisam_extra(HA_EXTRA_CACHE)");
      goto end;
    }
  }
  ant=0;
  while ((error=nisam_rrnd(file,record,NI_POS_ERROR)) >= 0 &&
	 ant < write_count + 10)
	ant+= error ? 0 : 1;
  if (ant != write_count-opt_delete)
  {
    printf("rrnd with cache: I can only find: %d records of %d\n",
	   ant,write_count-opt_delete);
    goto end;
  }
  if (nisam_extra(file,HA_EXTRA_NO_CACHE))
  {
    puts("got error from nisam_extra(HA_EXTRA_NO_CACHE)");
    goto end;
  }

  if (testflag == 4) goto end;

  printf("- Removing keys\n");
  lastpos = NI_POS_ERROR;
  /* DBUG_POP(); */
  nisam_extra(file,HA_EXTRA_RESET);
  while ((error=nisam_rrnd(file,read_record,NI_POS_ERROR)) >=0)
  {
    nisam_info(file,&info,1);
    if (lastpos >= info.recpos && lastpos != NI_POS_ERROR)
    {
      printf("nisam_rrnd didn't advance filepointer; old: %ld, new: %ld\n",
	     lastpos,info.recpos);
      goto err;
    }
    lastpos=info.recpos;
    if (error == 0)
    {
      if (nisam_rsame(file,read_record,-1))
      {
	printf("can't find record %lx\n",info.recpos);
	goto err;
      }
      if (use_blob)
      {
	ulong blob_length,pos;
	uchar *ptr;
	longget(blob_length,read_record+blob_pos+4);
	ptr=(uchar*) blob_length;
	longget(blob_length,read_record+blob_pos);
	for (pos=0 ; pos < blob_length ; pos++)
	{
	  if (ptr[pos] != (uchar) (blob_length+pos))
	  {
	    printf("found blob with wrong info at %ld\n",lastpos);
	    use_blob=0;
	    break;
	  }
	}
      }
      if (nisam_delete(file,read_record))
      {
	printf("can't delete record: %s\n",read_record);
	goto err;
      }
      opt_delete++;
    }
  }
  if (my_errno != HA_ERR_END_OF_FILE && my_errno != HA_ERR_RECORD_DELETED)
    printf("error: %d from nisam_rrnd\n",my_errno);
  if (write_count != opt_delete)
  {
    printf("Deleted only %d of %d records\n",write_count,opt_delete);
    goto err;
  }
end:
  if (nisam_close(file))
    goto err;
  nisam_panic(HA_PANIC_CLOSE);			/* Should close log */
  printf("\nFollowing test have been made:\n");
  printf("Write records: %d\nUpdate records: %d\nSame-key-read: %d\nDelete records: %d\n", write_count,update,dupp_keys,opt_delete);
  if (rec_pointer_size)
    printf("Record pointer size: %d\n",rec_pointer_size);
  if (key_cacheing)
    puts("Key cacheing used");
  if (write_cacheing)
    puts("Write cacheing used");
  if (async_io && locking)
    puts("Asyncron io with locking used");
  else if (locking)
    puts("Locking used");
  if (use_blob)
    puts("blobs used");
  end_key_cache();
  if (blob_buffer)
    my_free(blob_buffer,MYF(0));
  my_end(MY_CHECK_ERROR | MY_GIVE_INFO);
  return(0);
err:
  printf("got error: %d when using NISAM-database\n",my_errno);
  if (file)
    VOID(nisam_close(file));
  return(1);
} /* main */
int main(int argc __attribute__((unused)), char *argv[])
{
  uint pagen;
  int rc= 1;
  uchar long_tr_id[6];
  PAGECACHE pagecache;
  LSN first_lsn;
  TRANSLOG_HEADER_BUFFER rec;
  LEX_CUSTRING parts[TRANSLOG_INTERNAL_PARTS + 1];
  translog_size_t len;

  MY_INIT(argv[0]);

  plan(1);

  bzero(&pagecache, sizeof(pagecache));
  maria_data_root= create_tmpdir(argv[0]);
  if (maria_log_remove(0))
    exit(1);

  bzero(long_tr_id, 6);
#ifndef DBUG_OFF
#if defined(__WIN__)
  default_dbug_option= "d:t:i:O,\\ma_test_loghandler.trace";
#else
  default_dbug_option= "d:t:i:o,/tmp/ma_test_loghandler.trace";
#endif
  if (argc > 1)
  {
    DBUG_SET(default_dbug_option);
    DBUG_SET_INITIAL(default_dbug_option);
  }
#endif

  if (ma_control_file_open(TRUE, TRUE))
  {
    fprintf(stderr, "Can't init control file (%d)\n", errno);
    exit(1);
  }
  if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
                             PCACHE_PAGE, 0)) == 0)
  {
    fprintf(stderr, "Got error: init_pagecache() (errno: %d)\n", errno);
    exit(1);
  }
  if (translog_init_with_table(maria_data_root, LOG_FILE_SIZE, 50112, 0, &pagecache,
                               LOG_FLAGS, 0, &translog_example_table_init,
                               0))
  {
    fprintf(stderr, "Can't init loghandler (%d)\n", errno);
    exit(1);
  }
  /* Suppressing of automatic record writing */
  dummy_transaction_object.first_undo_lsn|= TRANSACTION_LOGGED_LONG_ID;

  int4store(long_tr_id, 0);
  long_tr_id[5]= 0xff;
  parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
  parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
  if (translog_write_record(&first_lsn,
                            LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
                            &dummy_transaction_object, NULL, 6,
                            TRANSLOG_INTERNAL_PARTS + 1,
                            parts, NULL, NULL))
  {
    fprintf(stderr, "Can't write record #%lu\n", (ulong) 0);
    translog_destroy();
    exit(1);
  }

  len= translog_read_record_header(first_lsn, &rec);
  if (len == 0)
  {
    fprintf(stderr, "translog_read_record_header failed (%d)\n", errno);
    goto err;
  }
  if (rec.type !=LOGREC_FIXED_RECORD_0LSN_EXAMPLE || rec.short_trid != 0 ||
      rec.record_length != 6 || uint4korr(rec.header) != 0 ||
      ((uchar)rec.header[4]) != 0 || ((uchar)rec.header[5]) != 0xFF ||
      first_lsn != rec.lsn)
  {
    fprintf(stderr, "Incorrect LOGREC_FIXED_RECORD_0LSN_EXAMPLE "
            "data read(0)\n"
            "type: %u (%d)  strid: %u (%d)  len: %u (%d)  i: %u (%d), "
            "4: %u (%d)  5: %u (%d)  "
            "lsn(%lu,0x%lx) (%d)\n",
            (uint) rec.type, (rec.type !=LOGREC_FIXED_RECORD_0LSN_EXAMPLE),
            (uint) rec.short_trid, (rec.short_trid != 0),
            (uint) rec.record_length, (rec.record_length != 6),
            (uint) uint4korr(rec.header), (uint4korr(rec.header) != 0),
            (uint) rec.header[4], (((uchar)rec.header[4]) != 0),
            (uint) rec.header[5], (((uchar)rec.header[5]) != 0xFF),
            LSN_IN_PARTS(rec.lsn), (first_lsn != rec.lsn));
    goto err;
  }

  ok(1, "read OK");
  rc= 0;

err:
  translog_destroy();
  end_pagecache(&pagecache, 1);
  ma_control_file_end();
  if (maria_log_remove(maria_data_root))
    exit(1);

  exit(rc);
}
Пример #6
0
int
main(int argc, char**	argv)
{
  char*	server_key = 0,	*server_cert = 0;
  char*	client_key = 0,	*client_cert = 0;
  char*	ca_file = 0,	*ca_path = 0;
  char*	cipher=0;
  int	child_pid,sv[2];
  my_bool unused;
  struct st_VioSSLFd* ssl_acceptor= 0;
  struct st_VioSSLFd* ssl_connector= 0;
  Vio* client_vio=0, *server_vio=0;
  enum enum_ssl_init_error ssl_init_error;
  unsigned long ssl_error;

  MY_INIT(argv[0]);
  DBUG_PROCESS(argv[0]);
  DBUG_PUSH(default_dbug_option);

  if (argc<5)
  {
    print_usage();
    return 1;
  }

  server_key = argv[1];
  server_cert = argv[2];
  client_key = argv[3];
  client_cert = argv[4];
  if (argc>5)
    ca_file = argv[5];
  if (argc>6)
    ca_path = argv[6];
  printf("Server key/cert : %s/%s\n", server_key, server_cert);
  printf("Client key/cert : %s/%s\n", client_key, client_cert);
  if (ca_file!=0)
    printf("CAfile          : %s\n", ca_file);
  if (ca_path!=0)
    printf("CApath          : %s\n", ca_path);


  if (socketpair(PF_UNIX, SOCK_STREAM, IPPROTO_IP, sv)==-1)
    fatal_error("socketpair");

  ssl_acceptor = new_VioSSLAcceptorFd(server_key, server_cert, ca_file,
				      ca_path, cipher);
  ssl_connector = new_VioSSLConnectorFd(client_key, client_cert, ca_file,
					ca_path, cipher, &ssl_init_error);

  client_vio = (struct st_vio*)my_malloc(sizeof(struct st_vio),MYF(0));
  client_vio->sd = sv[0];
  client_vio->vioblocking(client_vio, 0, &unused);
  sslconnect(ssl_connector,client_vio,60L,&ssl_error);
  server_vio = (struct st_vio*)my_malloc(sizeof(struct st_vio),MYF(0));
  server_vio->sd = sv[1];
  server_vio->vioblocking(client_vio, 0, &unused);
  sslaccept(ssl_acceptor,server_vio,60L, &ssl_error);

  printf("Socketpair: %d , %d\n", client_vio->sd, server_vio->sd);

  child_pid = fork();
  if (child_pid==-1) {
    my_free(ssl_acceptor);
    my_free(ssl_connector);
    fatal_error("fork");
  }
  if (child_pid==0)
  {
    /* child, therefore, client */
    char	xbuf[100];
    int	r = vio_read(client_vio,xbuf, sizeof(xbuf));
    if (r<=0) {
      my_free(ssl_acceptor);
      my_free(ssl_connector);
      fatal_error("client:SSL_read");
    }
    xbuf[r] = 0;
    printf("client:got %s\n", xbuf);
    my_free(client_vio);
    my_free(ssl_acceptor);
    my_free(ssl_connector);
  }
  else
  {
    const char*	s = "Huhuhuh";
    int		r = vio_write(server_vio,(uchar*)s, strlen(s));
    if (r<=0) {
      my_free(ssl_acceptor);
      my_free(ssl_connector);
      fatal_error("server:SSL_write");
    }
    my_free(server_vio);
    my_free(ssl_acceptor);
    my_free(ssl_connector);
  }
  return 0;
}
Пример #7
0
int
main (int argc, char *argv[])
{
int opt_err;

  MY_INIT (argv[0]);
  load_defaults ("my", client_groups, &argc, &argv);

  if ((opt_err = handle_options (&argc, &argv, my_opts, get_one_option)))
    exit (opt_err);

  /* solicit password if necessary */
  if (ask_password)
    opt_password = get_tty_password (NULL);

  /* get database name if present on command line */
  if (argc > 0)
  {
    opt_db_name = argv[0];
    --argc; ++argv;
  }

  /* initialize client library */
  if (mysql_library_init (0, NULL, NULL))
  {
    print_error (NULL, "mysql_library_init() failed");
    exit (1);
  }

/* #@ _SET_UP_SSL_ */
  /* initialize connection handler */
  conn = mysql_init (NULL);
  if (conn == NULL)
  {
    print_error (NULL, "mysql_init() failed (probably out of memory)");
    exit (1);
  }

#ifdef HAVE_OPENSSL
  /* pass SSL information to client library */
  if (opt_use_ssl)
    mysql_ssl_set (conn, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
                   opt_ssl_capath, opt_ssl_cipher);
  mysql_options (conn,MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
                 (char*)&opt_ssl_verify_server_cert);
#endif

  /* connect to server */
  if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
      opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
  {
    print_error (conn, "mysql_real_connect() failed");
    mysql_close (conn);
    exit (1);
  }
/* #@ _SET_UP_SSL_ */

  printf ("Testing connection...\n");
  process_statement (conn, "SHOW STATUS LIKE 'Ssl_cipher'");
  printf ("If Ssl_cipher is non-blank, the connection is secure.\n");

  while (1)
  {
    char  buf[10000];

    fprintf (stderr, "query> ");                  /* print prompt */
    if (fgets (buf, sizeof (buf), stdin) == NULL) /* read statement */
      break;
    if (strcmp (buf, "quit\n") == 0 || strcmp (buf, "\\q\n") == 0)
      break;
    process_statement (conn, buf);                /* execute it */
  }

  /* disconnect from server, terminate client library */
  mysql_close (conn);
  mysql_library_end ();
  exit (0);
}
Пример #8
0
int main(int argc __attribute__((unused)), char **argv)
{
  uint i;
  char *big_string= (char *)malloc(1024*1024);

  MY_INIT(argv[0]);
  plan(68);

  if (!big_string)
    exit(1);
  for (i= 0; i < 1024*1024; i++)
    big_string[i]= ('0' + (i % 10));
  test_value_single_null();
  test_value_single_uint(0, "0");
  test_value_single_uint(0xffffffffffffffffULL, "0xffffffffffffffff");
  test_value_single_uint(0xaaaaaaaaaaaaaaaaULL, "0xaaaaaaaaaaaaaaaa");
  test_value_single_uint(0x5555555555555555ULL, "0x5555555555555555");
  test_value_single_uint(27652, "27652");
  test_value_single_sint(0, "0");
  test_value_single_sint(1, "1");
  test_value_single_sint(-1, "-1");
  test_value_single_sint(0x7fffffffffffffffLL, "0x7fffffffffffffff");
  test_value_single_sint(0xaaaaaaaaaaaaaaaaLL, "0xaaaaaaaaaaaaaaaa");
  test_value_single_sint(0x5555555555555555LL, "0x5555555555555555");
  test_value_single_sint(0x8000000000000000LL, "0x8000000000000000");
  test_value_single_double(0.0, "0.0");
  test_value_single_double(1.0, "1.0");
  test_value_single_double(-1.0, "-1.0");
  test_value_single_double(1.0e100, "1.0e100");
  test_value_single_double(1.0e-100, "1.0e-100");
  test_value_single_double(9999999999999999999999999999999999999.0,
                           "9999999999999999999999999999999999999.0");
  test_value_single_double(-9999999999999999999999999999999999999.0,
                           "-9999999999999999999999999999999999999.0");
  test_value_single_decimal("0");
  test_value_single_decimal("1");
  test_value_single_decimal("-1");
  test_value_single_decimal("9999999999999999999999999999999");
  test_value_single_decimal("-9999999999999999999999999999999");
  test_value_single_decimal("0.9999999999999999999999999999999");
  test_value_single_decimal("-0.9999999999999999999999999999999");
  test_value_single_string("", 0, charset_list[0]);
  test_value_single_string("", 1, charset_list[0]);
  test_value_single_string("1234567890", 11, charset_list[0]);
  test_value_single_string("nulls\0\0\0\0\0", 10, charset_list[0]);
  sprintf(big_string, "%x", 0x7a);
  test_value_single_string(big_string, 0x7a, charset_list[0]);
  sprintf(big_string, "%x", 0x80);
  test_value_single_string(big_string, 0x80, charset_list[0]);
  sprintf(big_string, "%x", 0x7ffa);
  test_value_single_string(big_string, 0x7ffa, charset_list[0]);
  sprintf(big_string, "%x", 0x8000);
  test_value_single_string(big_string, 0x8000, charset_list[0]);
  sprintf(big_string, "%x", 1024*1024);
  test_value_single_string(big_string, 1024*1024, charset_list[0]);
  test_value_single_date(0, 0, 0, "zero date");
  test_value_single_date(9999, 12, 31, "max date");
  test_value_single_date(2011, 3, 26, "some date");
  test_value_single_time(0, 0, 0, 0, 0, "zero time");
  test_value_single_time(1, 23, 59, 59, 999999, "min time");
  test_value_single_time(0, 23, 59, 59, 999999, "max time");
  test_value_single_time(0, 21, 36, 20, 28, "some time");
  test_value_single_datetime(0, 0, 0, 0, 0, 0, 0, 0, "zero datetime");
  test_value_single_datetime(1, 9999, 12, 31, 23, 59, 59, 999999,
                             "min datetime");
  test_value_single_datetime(0, 9999, 12, 31, 23, 59, 59, 999999,
                             "max datetime");
  test_value_single_datetime(0, 2011, 3, 26, 21, 53, 12, 3445,
                             "some datetime");
  {
    uint column_numbers[]= {100,1,2,3,4,5,6,7,8};
    test_value_multi(0, 0, 0.0, "0",
                     "", 0, charset_list[0],
                     0, 0, 0,
                     0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0,
                     column_numbers,
                     "zero data");
  }
  {
    uint column_numbers[]= {10,1,12,37,4,57,6,76,87};
    test_value_multi(0xffffffffffffffffULL, 0x7fffffffffffffffLL,
                     99999999.999e120, "9999999999999999999999999999999",
                     big_string, 1024*1024, charset_list[0],
                     9999, 12, 31,
                     0, 23, 59, 59, 999999,
                     0, 9999, 12, 31, 23, 59, 59, 999999,
                     column_numbers,
                     "much data");
  }
  free(big_string);
  {
    uint column_numbers[]= {101,12,122,37,24,572,16,726,77};
    test_value_multi(37878, -3344,
                     2873.3874, "92743.238984789898",
                     "string", 6, charset_list[0],
                     2011, 3, 26,
                     1, 23, 23, 20, 333,
                     0, 2011, 3, 26, 23, 23, 53, 334,
                     column_numbers,
                     "zero data");
  }
  test_value_multi_same_num();
  {
    uint column_numbers[]= {1,2,3,4,5,6,7,2, 3, 4};
    uint column_values[]=  {1,2,3,4,5,6,7,0,30,40};
    my_bool null_values[]= {0,0,0,0,0,0,0,1, 0, 0};

    test_update_multi(column_numbers, column_values, null_values, 7, 10);
  }
  {
    uint column_numbers[]= {4,3,2,1, 1,2,3,4};
    uint column_values[]=  {4,3,2,1, 0,0,0,0};
    my_bool null_values[]= {0,0,0,0, 1,1,1,1};

    test_update_multi(column_numbers, column_values, null_values, 4, 8);
  }
  {
    uint column_numbers[]= {4,3,2,1, 4,3,2,1};
    uint column_values[]=  {4,3,2,1, 0,0,0,0};
    my_bool null_values[]= {0,0,0,0, 1,1,1,1};

    test_update_multi(column_numbers, column_values, null_values, 4, 8);
  }
  test_empty_string();
  {
    uint column_numbers[]= {1, 2, 3};
    uint column_values[]=  {1, 2, 3};
    uint update_numbers[]= {4, 3, 2, 1};
    uint update_values[]=  {40,30, 0,10};
    my_bool update_nulls[]={0, 0, 1, 0};
    uint result_numbers[]= {1, 3, 4};
    uint result_values[]=  {10,30,40};
    test_update_many(column_numbers, column_values, 3,
                     update_numbers, update_values, update_nulls, 4,
                     result_numbers, result_values, 3);
  }
  test_mdev_4994();
  test_mdev_4995();
  test_mdev_9773();

  my_end(0);
  return exit_status();
}
int main(int argc __attribute__((unused)), char *argv[])
{
  ulong i;
  uchar long_tr_id[6];
  PAGECACHE pagecache;
  LSN lsn;
  LEX_CUSTRING parts[TRANSLOG_INTERNAL_PARTS + 1];
  uchar *long_buffer= malloc(LONG_BUFFER_SIZE);

  MY_INIT(argv[0]);

  plan(2);

  bzero(&pagecache, sizeof(pagecache));
  bzero(long_buffer, LONG_BUFFER_SIZE);
  maria_data_root= create_tmpdir(argv[0]);
  if (maria_log_remove(0))
    exit(1);

  bzero(long_tr_id, 6);
#ifndef DBUG_OFF
#if defined(__WIN__)
  default_dbug_option= "d:t:i:O,\\ma_test_loghandler.trace";
#else
  default_dbug_option= "d:t:i:o,/tmp/ma_test_loghandler.trace";
#endif
  if (argc > 1)
  {
    DBUG_SET(default_dbug_option);
    DBUG_SET_INITIAL(default_dbug_option);
  }
#endif

  if (ma_control_file_open(TRUE, TRUE))
  {
    fprintf(stderr, "Can't init control file (%d)\n", errno);
    exit(1);
  }
  if (init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
                     PCACHE_PAGE, 0, 0) == 0)
  {
    fprintf(stderr, "Got error: init_pagecache() (errno: %d)\n", errno);
    exit(1);
  }
  if (translog_init_with_table(maria_data_root, LOG_FILE_SIZE, 50112, 0, &pagecache,
                               LOG_FLAGS, 0, &translog_example_table_init,
                               0))
  {
    fprintf(stderr, "Can't init loghandler (%d)\n", errno);
    exit(1);
  }
  /* Suppressing of automatic record writing */
  dummy_transaction_object.first_undo_lsn|= TRANSACTION_LOGGED_LONG_ID;

  /* write more then 1 file */
  int4store(long_tr_id, 0);
  parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
  parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
  if (translog_write_record(&lsn,
                            LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
                            &dummy_transaction_object, NULL, 6,
                            TRANSLOG_INTERNAL_PARTS + 1,
                            parts, NULL, NULL))
  {
    fprintf(stderr, "Can't write record #0\n");
    translog_destroy();
    exit(1);
  }

  for(i= 0; i < LOG_FILE_SIZE/6 && LSN_FILE_NO(lsn) == 1; i++)
  {
    if (translog_write_record(&lsn,
                              LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
                              &dummy_transaction_object, NULL, 6,
                              TRANSLOG_INTERNAL_PARTS + 1,
                              parts, NULL, NULL))
    {
      fprintf(stderr, "Can't write record #0\n");
      translog_destroy();
      exit(1);
    }
  }

  translog_destroy();
  end_pagecache(&pagecache, 1);
  ma_control_file_end();

  {
    char file_name[FN_REFLEN];
    for (i= 1; i <= 2; i++)
    {
      translog_filename_by_fileno(i, file_name);
      if (my_access(file_name, W_OK))
      {
        fprintf(stderr, "No file '%s'\n", file_name);
        exit(1);
      }
      if (my_delete(file_name, MYF(MY_WME)) != 0)
      {
        fprintf(stderr, "Error %d during removing file'%s'\n",
                errno, file_name);
        exit(1);
      }
    }
  }

  if (ma_control_file_open(TRUE, TRUE))
  {
    fprintf(stderr, "Can't init control file (%d)\n", errno);
    exit(1);
  }
  if (init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
                     PCACHE_PAGE, 0, 0) == 0)
  {
    fprintf(stderr, "Got error: init_pagecache() (errno: %d)\n", errno);
    exit(1);
  }
  if (translog_init_with_table(maria_data_root, LOG_FILE_SIZE, 50112, 0, &pagecache,
                               LOG_FLAGS, 0, &translog_example_table_init,
                               1))
  {
    fprintf(stderr, "Can't init loghandler (%d)\n", errno);
    exit(1);
  }
  /* Suppressing of automatic record writing */
  dummy_transaction_object.first_undo_lsn|= TRANSACTION_LOGGED_LONG_ID;

  ok(1, "Log init OK");

  int4store(long_tr_id, 0);
  parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
  parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
  if (translog_write_record(&lsn,
                            LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
                            &dummy_transaction_object, NULL, 6,
                            TRANSLOG_INTERNAL_PARTS + 1,
                            parts, NULL, NULL))
  {
    fprintf(stderr, "Can't write record #0\n");
    translog_destroy();
    exit(1);
  }

  translog_destroy();
  end_pagecache(&pagecache, 1);
  ma_control_file_end();

  if (!translog_is_file(3))
  {
    fprintf(stderr, "No file #3\n");
    exit(1);
  }

  ok(1, "New log is OK");

  if (maria_log_remove(maria_data_root))
    exit(1);
  exit(0);
}
Пример #10
0
int main(int argc, char **argv)
{
  LSN lsn;
  char **default_argv;
  uint warnings_count;
  MY_INIT(argv[0]);

  maria_data_root= (char *)".";
  load_defaults("my", load_default_groups, &argc, &argv);
  default_argv= argv;
  get_options(&argc, &argv);

  maria_in_recovery= TRUE;

  if (maria_init())
  {
    fprintf(stderr, "Can't init Aria engine (%d)\n", errno);
    goto err;
  }
  maria_block_size= 0;                          /* Use block size from file */
  /* we don't want to create a control file, it MUST exist */
  if (ma_control_file_open(FALSE, TRUE))
  {
    fprintf(stderr, "Can't open control file (%d)\n", errno);
    goto err;
  }
  if (last_logno == FILENO_IMPOSSIBLE)
  {
    fprintf(stderr, "Can't find any log\n");
    goto err;
  }
  if (init_pagecache(maria_pagecache, opt_page_buffer_size, 0, 0,
                     maria_block_size, MY_WME) == 0)
  {
    fprintf(stderr, "Got error in init_pagecache() (errno: %d)\n", errno);
    goto err;
  }
  /*
    If log handler does not find the "last_logno" log it will return error,
    which is good.
    But if it finds a log and this log was crashed, it will create a new log,
    which is useless. TODO: start log handler in read-only mode.
  */
  if (init_pagecache(maria_log_pagecache, opt_translog_buffer_size,
                     0, 0, TRANSLOG_PAGE_SIZE, MY_WME) == 0 ||
      translog_init(maria_data_root, TRANSLOG_FILE_SIZE,
                    0, 0, maria_log_pagecache, TRANSLOG_DEFAULT_FLAGS,
                    opt_display_only))
  {
    fprintf(stderr, "Can't init loghandler (%d)\n", errno);
    goto err;
  }

  if (opt_display_only)
    printf("You are using --display-only, NOTHING will be written to disk\n");

  lsn= translog_first_lsn_in_log();
  if (lsn == LSN_ERROR)
  {
    fprintf(stderr, "Opening transaction log failed\n");
    goto end;
  }
  if (lsn == LSN_IMPOSSIBLE)
  {
     fprintf(stdout, "The transaction log is empty\n");
  }
  if (opt_start_from_checkpoint && !opt_start_from_lsn &&
      last_checkpoint_lsn != LSN_IMPOSSIBLE)
  {
    lsn= LSN_IMPOSSIBLE;             /* LSN set in maria_apply_log() */
    fprintf(stdout, "Starting from checkpoint (%lu,0x%lx)\n",
            LSN_IN_PARTS(last_checkpoint_lsn));
  }
  else
    fprintf(stdout, "The transaction log starts from lsn (%lu,0x%lx)\n",
            LSN_IN_PARTS(lsn));

  if (opt_start_from_lsn)
  {
    if (opt_start_from_lsn < (ulonglong) lsn)
    {
      fprintf(stderr, "start_from_lsn is too small. Aborting\n");
      maria_end();
      goto err;
    }
    lsn= (LSN) opt_start_from_lsn;
    fprintf(stdout, "Starting reading log from lsn (%lu,0x%lx)\n",
            LSN_IN_PARTS(lsn));
  }

  if (opt_end_lsn != LSN_IMPOSSIBLE)
  {
    /* We can't apply undo if we use end_lsn */
    opt_apply_undo= 0;
  }

  fprintf(stdout, "TRACE of the last aria_read_log\n");
  if (maria_apply_log(lsn, opt_end_lsn, opt_apply ?  MARIA_LOG_APPLY :
                      (opt_check ? MARIA_LOG_CHECK :
                       MARIA_LOG_DISPLAY_HEADER), opt_silent ? NULL : stdout,
                      opt_apply_undo, FALSE, FALSE, &warnings_count))
    goto err;
  if (warnings_count == 0)
    fprintf(stdout, "%s: SUCCESS\n", my_progname_short);
  else
    fprintf(stdout, "%s: DOUBTFUL (%u warnings, check previous output)\n",
            my_progname_short, warnings_count);

end:
  maria_end();
  free_tmpdir(&maria_chk_tmpdir);
  free_defaults(default_argv);
  my_end(0);
  exit(0);
  return 0;				/* No compiler warning */

err:
  /* don't touch anything more, in case we hit a bug */
  fprintf(stderr, "%s: FAILED\n", my_progname_short);
  free_tmpdir(&maria_chk_tmpdir);
  free_defaults(default_argv);
  exit(1);
}
Пример #11
0
int main(int argc, char *argv[])
{
  unsigned int ret;
  azio_stream reader_handle;

  MY_INIT(argv[0]);
  get_options(&argc, &argv);

  if (argc < 1)
  {
    printf("No file specified. \n");
    return 0;
  }

  if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY|O_BINARY)))
  {
    printf("Could not open Archive file\n");
    return 0;
  }

  if (opt_autoincrement)
  {
    azio_stream writer_handle;

    if (new_auto_increment_value)
    {
      if (reader_handle.auto_increment >= new_auto_increment_value)
      {
        printf("Value is lower then current value\n");
        goto end;
      }
    }
    else
    {
      new_auto_increment_value= reader_handle.auto_increment + 1;
    }

    if (!(ret= azopen(&writer_handle, argv[0], O_CREAT|O_RDWR|O_BINARY)))
    {
      printf("Could not open file for update: %s\n", argv[0]);
      goto end;
    }

    writer_handle.auto_increment= new_auto_increment_value;

    azclose(&writer_handle);
    azflush(&reader_handle, Z_SYNC_FLUSH);
  }

  printf("Version %u\n", reader_handle.version);
  if (reader_handle.version > 2)
  {
    printf("\tMinor version %u\n", reader_handle.minor_version);
    printf("\tStart position %llu\n", (unsigned long long)reader_handle.start);
    printf("\tBlock size %u\n", reader_handle.block_size);
    printf("\tRows %llu\n", reader_handle.rows);
    printf("\tAutoincrement %llu\n", reader_handle.auto_increment);
    printf("\tCheck Point %llu\n", reader_handle.check_point);
    printf("\tForced Flushes %llu\n", reader_handle.forced_flushes);
    printf("\tLongest Row %u\n", reader_handle.longest_row);
    printf("\tShortest Row %u\n", reader_handle.shortest_row);
    printf("\tState %s\n", ( reader_handle.dirty ? "dirty" : "clean"));
    printf("\tFRM stored at %u\n", reader_handle.frm_start_pos);
    printf("\tComment stored at %u\n", reader_handle.comment_start_pos);
    printf("\tData starts at %u\n", (unsigned int)reader_handle.start);
    if (reader_handle.frm_start_pos)
      printf("\tFRM length %u\n", reader_handle.frm_length);
    if (reader_handle.comment_start_pos)
    {
      char *comment =
        (char *) malloc(sizeof(char) * reader_handle.comment_length);
      azread_comment(&reader_handle, comment);
      printf("\tComment length %u\n\t\t%.*s\n", reader_handle.comment_length, 
             reader_handle.comment_length, comment);
      free(comment);
    }
  }
  else
  {
    goto end;
  }

  printf("\n");

  if (opt_check)
  {
    uchar size_buffer[ARCHIVE_ROW_HEADER_SIZE];
    int error;
    unsigned int x;
    unsigned int read;
    unsigned int row_len;
    unsigned long long row_count= 0;
    char buffer;

    while ((read= azread(&reader_handle, (uchar *)size_buffer, 
                        ARCHIVE_ROW_HEADER_SIZE, &error)))
    {
      if (error == Z_STREAM_ERROR ||  (read && read < ARCHIVE_ROW_HEADER_SIZE))
      {
        printf("Table is damaged\n");
        goto end;
      }

      /* If we read nothing we are at the end of the file */
      if (read == 0 || read != ARCHIVE_ROW_HEADER_SIZE)
        break;

      row_len=  uint4korr(size_buffer);
      row_count++;

      if (row_len > reader_handle.longest_row)
      {
        printf("Table is damaged, row %llu is invalid\n", 
               row_count);
        goto end;
      }


      for (read= x= 0; x < row_len ; x++) 
      {
        read+= (unsigned int)azread(&reader_handle, &buffer, sizeof(char), &error); 
        if (!read)
          break;
      }


      if (row_len != read)
      {
        printf("Row length did not match row (at %llu). %u != %u \n", 
               row_count, row_len, read);
        goto end;
      }
    }

    if (0)
    {
      printf("Table is damaged\n");
      goto end;
    }
    else
    {
      printf("Found %llu rows\n", row_count);
    }
  }

  if (opt_backup)
  {
    uchar size_buffer[ARCHIVE_ROW_HEADER_SIZE];
    int error;
    unsigned int read;
    unsigned int row_len;
    unsigned long long row_count= 0;
    char *buffer;

    azio_stream writer_handle;

    buffer= (char *)malloc(reader_handle.longest_row);
    if (buffer == NULL)
    {
      printf("Could not allocate memory for row %llu\n", row_count);
      goto end;
    }


    if (!(ret= azopen(&writer_handle, argv[1], O_CREAT|O_RDWR|O_BINARY)))
    {
      printf("Could not open file for backup: %s\n", argv[1]);
      goto end;
    }

    writer_handle.auto_increment= reader_handle.auto_increment;
    if (reader_handle.frm_length)
    {
      char *ptr;
      ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
      azread_frm(&reader_handle, ptr);
      azwrite_frm(&writer_handle, ptr, reader_handle.frm_length);
      my_free(ptr, MYF(0));
    }

    if (reader_handle.comment_length)
    {
      char *ptr;
      ptr= (char *)my_malloc(sizeof(char) * reader_handle.comment_length, MYF(0));
      azread_comment(&reader_handle, ptr);
      azwrite_comment(&writer_handle, ptr, reader_handle.comment_length);
      my_free(ptr, MYF(0));
    }

    while ((read= azread(&reader_handle, (uchar *)size_buffer, 
                        ARCHIVE_ROW_HEADER_SIZE, &error)))
    {
      if (error == Z_STREAM_ERROR ||  (read && read < ARCHIVE_ROW_HEADER_SIZE))
      {
        printf("Table is damaged\n");
        goto end;
      }

      /* If we read nothing we are at the end of the file */
      if (read == 0 || read != ARCHIVE_ROW_HEADER_SIZE)
        break;

      row_len=  uint4korr(size_buffer);

      row_count++;

      memcpy(buffer, size_buffer, ARCHIVE_ROW_HEADER_SIZE);

      read= (unsigned int)azread(&reader_handle, buffer + ARCHIVE_ROW_HEADER_SIZE, 
                                 row_len, &error); 

      DBUG_ASSERT(read == row_len);

      azwrite(&writer_handle, buffer, row_len + ARCHIVE_ROW_HEADER_SIZE);


      if (row_len != read)
      {
        printf("Row length did not match row (at %llu). %u != %u \n", 
               row_count, row_len, read);
        goto end;
      }

      if (reader_handle.rows == writer_handle.rows)
        break;
    }

    free(buffer);

    azclose(&writer_handle);
  }

  if (opt_extract_frm)
  {
    File frm_file;
    char *ptr;
    frm_file= my_open(argv[1], O_CREAT|O_RDWR|O_BINARY, MYF(0));
    ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
    azread_frm(&reader_handle, ptr);
    my_write(frm_file, (uchar*) ptr, reader_handle.frm_length, MYF(0));
    my_close(frm_file, MYF(0));
    my_free(ptr, MYF(0));
  }

end:
  printf("\n");
  azclose(&reader_handle);

  return 0;
}
Пример #12
0
int main(int argc, char *argv[])
{
  unsigned int ret;
  char comment_str[10];

  int error;
  unsigned int x;
  int written_rows= 0;
  azio_stream writer_handle, reader_handle;
  char buffer[BUFFER_LEN];

  int4store(test_string, 1024);
  memcpy(test_string+sizeof(unsigned int), TEST_STRING_INIT, 1024);

  unlink(TEST_FILENAME);

  if (argc > 1)
    return 0;

  MY_INIT(argv[0]);

  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_CREAT|O_RDWR|O_BINARY)))
  {
    printf("Could not create test file\n");
    return 0;
  }

  azwrite_comment(&writer_handle, (char *)COMMENT_STRING, 
                  (unsigned int)strlen(COMMENT_STRING));
  azread_comment(&writer_handle, comment_str);
  assert(!memcmp(COMMENT_STRING, comment_str,
                strlen(COMMENT_STRING)));

  azwrite_frm(&writer_handle, (char *)FRM_STRING, 
                  (unsigned int)strlen(FRM_STRING));
  azread_frm(&writer_handle, comment_str);
  assert(!memcmp(FRM_STRING, comment_str,
                strlen(FRM_STRING)));


  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
  {
    printf("Could not open test file\n");
    return 0;
  }

  assert(reader_handle.rows == 0);
  assert(reader_handle.auto_increment == 0);
  assert(reader_handle.check_point == 0);
  assert(reader_handle.forced_flushes == 0);
  assert(reader_handle.dirty == AZ_STATE_DIRTY);

  for (x= 0; x < TEST_LOOP_NUM; x++)
  {
    ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
    assert(ret == BUFFER_LEN);
    written_rows++;
  }
  azflush(&writer_handle,  Z_SYNC_FLUSH);

  azread_comment(&writer_handle, comment_str);
  assert(!memcmp(COMMENT_STRING, comment_str,
                strlen(COMMENT_STRING)));

  /* Lets test that our internal stats are good */
  assert(writer_handle.rows == TEST_LOOP_NUM);

  /* Reader needs to be flushed to make sure it is up to date */
  azflush(&reader_handle,  Z_SYNC_FLUSH);
  assert(reader_handle.rows == TEST_LOOP_NUM);
  assert(reader_handle.auto_increment == 0);
  assert(reader_handle.check_point == 96);
  assert(reader_handle.forced_flushes == 1);
  assert(reader_handle.comment_length == 10);
  assert(reader_handle.dirty == AZ_STATE_SAVED);

  writer_handle.auto_increment= 4;
  azflush(&writer_handle, Z_SYNC_FLUSH);
  assert(writer_handle.rows == TEST_LOOP_NUM);
  assert(writer_handle.auto_increment == 4);
  assert(writer_handle.check_point == 96);
  assert(writer_handle.forced_flushes == 2);
  assert(writer_handle.dirty == AZ_STATE_SAVED);

  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
  {
    printf("Could not open test file\n");
    return 0;
  }

  /* Read the original data */
  for (x= 0; x < writer_handle.rows; x++)
  {
    ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
    assert(!error);
    assert(ret == BUFFER_LEN);
    assert(!memcmp(buffer, test_string, ret));
  }
  assert(writer_handle.rows == TEST_LOOP_NUM);

  /* Test here for falling off the planet */

  /* Final Write before closing */
  ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
  assert(ret == BUFFER_LEN);

  /* We don't use FINISH, but I want to have it tested */
  azflush(&writer_handle,  Z_FINISH);

  assert(writer_handle.rows == TEST_LOOP_NUM+1);

  /* Read final write */
  azrewind(&reader_handle);
  for (x= 0; x < writer_handle.rows; x++)
  {
    ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
    assert(ret == BUFFER_LEN);
    assert(!error);
    assert(!memcmp(buffer, test_string, ret));
  }


  azclose(&writer_handle);

  /* Rewind and full test */
  azrewind(&reader_handle);
  for (x= 0; x < writer_handle.rows; x++)
  {
    ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
    assert(ret == BUFFER_LEN);
    assert(!error);
    assert(!memcmp(buffer, test_string, ret));
  }

  printf("Finished reading\n");

  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_RDWR|O_BINARY)))
  {
    printf("Could not open file (%s) for appending\n", TEST_FILENAME);
    return 0;
  }
  ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
  assert(ret == BUFFER_LEN);
  azflush(&writer_handle,  Z_SYNC_FLUSH);

  /* Rewind and full test */
  azrewind(&reader_handle);
  for (x= 0; x < writer_handle.rows; x++)
  {
    ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
    assert(!error);
    assert(ret == BUFFER_LEN);
    assert(!memcmp(buffer, test_string, ret));
  }

  /* Reader needs to be flushed to make sure it is up to date */
  azflush(&reader_handle,  Z_SYNC_FLUSH);
  assert(reader_handle.rows == 102);
  assert(reader_handle.auto_increment == 4);
  assert(reader_handle.check_point == 1290);
  assert(reader_handle.forced_flushes == 4);
  assert(reader_handle.dirty == AZ_STATE_SAVED);

  azflush(&writer_handle, Z_SYNC_FLUSH);
  assert(writer_handle.rows == reader_handle.rows);
  assert(writer_handle.auto_increment == reader_handle.auto_increment);
  assert(writer_handle.check_point == reader_handle.check_point);
  /* This is +1 because  we do a flush right before we read */
  assert(writer_handle.forced_flushes == reader_handle.forced_flushes + 1);
  assert(writer_handle.dirty == reader_handle.dirty);

  azclose(&writer_handle);
  azclose(&reader_handle);
  unlink(TEST_FILENAME);

  /* Start size tests */
  printf("About to run 2/4/8 gig tests now, you may want to hit CTRL-C\n");
  size_test(TWOGIG, 2088992L);
  size_test(FOURGIG, 4177984L);
  size_test(EIGHTGIG, 8355968L);

  return 0;
}
Пример #13
0
int main(int argc, char **argv)
{
    char self_name[FN_REFLEN];

    MY_INIT(argv[0]);
#ifdef __NETWARE__
    setscreenmode(SCR_AUTOCLOSE_ON_EXIT);
#endif

#if __WIN__
    if (GetModuleFileName(NULL, self_name, FN_REFLEN) == 0)
#endif
    {
        strncpy(self_name, argv[0], FN_REFLEN);
    }

    if (init_dynamic_string(&ds_args, "", 512, 256))
        die("Out of memory");

    load_defaults("my", load_default_groups, &argc, &argv);
    defaults_argv= argv; /* Must be freed by 'free_defaults' */

    if (handle_options(&argc, &argv, my_long_options, get_one_option))
        die(NULL);

    if (tty_password)
    {
        opt_password= get_tty_password(NullS);
        /* add password to defaults file */
        dynstr_append_os_quoted(&ds_args, "--password="******" ");
    }
    /* add user to defaults file */
    dynstr_append_os_quoted(&ds_args, "--user="******" ");

    /* Find mysql */
    find_tool(mysql_path, IF_WIN("mysql.exe", "mysql"), self_name);

    /* Find mysqlcheck */
    find_tool(mysqlcheck_path, IF_WIN("mysqlcheck.exe", "mysqlcheck"), self_name);

    /*
      Read the mysql_upgrade_info file to check if mysql_upgrade
      already has been run for this installation of MySQL
    */
    if (!opt_force && upgrade_already_done())
    {
        printf("This installation of MySQL is already upgraded to %s, "
               "use --force if you still need to run mysql_upgrade\n",
               MYSQL_SERVER_VERSION);
        die(NULL);
    }

    /*
      Run "mysqlcheck" and "mysql_fix_privilege_tables.sql"
    */
    if (run_mysqlcheck_upgrade() ||
            run_sql_fix_privilege_tables())
    {
        /*
          The upgrade failed to complete in some way or another,
          significant error message should have been printed to the screen
        */
        die("Upgrade failed" );
    }
    verbose("OK");

    /* Create a file indicating upgrade has been performed */
    create_mysql_upgrade_info_file();

    free_used_memory();
    my_end(MY_CHECK_ERROR);
    exit(0);
}
Пример #14
0
int main(int argc, char **argv)
{
  char self_name[FN_REFLEN];

  MY_INIT(argv[0]);

#if __WIN__
  if (GetModuleFileName(NULL, self_name, FN_REFLEN) == 0)
#endif
  {
    strncpy(self_name, argv[0], FN_REFLEN);
  }

  if (init_dynamic_string(&ds_args, "", 512, 256) ||
      init_dynamic_string(&conn_args, "", 512, 256))
    die("Out of memory");

  my_getopt_use_args_separator= TRUE;
  if (load_defaults("my", load_default_groups, &argc, &argv))
    die(NULL);
  my_getopt_use_args_separator= FALSE;
  defaults_argv= argv; /* Must be freed by 'free_defaults' */

  if (handle_options(&argc, &argv, my_long_options, get_one_option))
    die(NULL);
  if (debug_info_flag)
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
  if (debug_check_flag)
    my_end_arg= MY_CHECK_ERROR;

  if (tty_password)
  {
    opt_password= get_tty_password(NullS);
    /* add password to defaults file */
    dynstr_append_os_quoted(&ds_args, "--password="******" ");
  }
  /* add user to defaults file */
  dynstr_append_os_quoted(&ds_args, "--user="******" ");

  /* Find mysql */
  find_tool(mysql_path, IF_WIN("mysql.exe", "mysql"), self_name);

  if (!opt_systables_only)
  {
    /* Find mysqlcheck */
    find_tool(mysqlcheck_path, IF_WIN("mysqlcheck.exe", "mysqlcheck"), self_name);
  }
  else
  {
    printf("The --upgrade-system-tables option was used, databases won't be touched.\n");
  }

  /*
    Read the mysql_upgrade_info file to check if mysql_upgrade
    already has been run for this installation of MySQL
  */
  if (!opt_force && upgrade_already_done())
  {
    printf("This installation of MySQL is already upgraded to %s, "
           "use --force if you still need to run mysql_upgrade\n",
           MYSQL_SERVER_VERSION);
    die(NULL);
  }

  if (opt_version_check && check_version_match())
    die("Upgrade failed");

  /*
    Run "mysqlcheck" and "mysql_fix_privilege_tables.sql"
    First run mysqlcheck on the system database.
    Then do the upgrade.
    And then run mysqlcheck on all tables.
  */
  if ((!opt_systables_only &&
       (run_mysqlcheck_mysql_db_fixnames() || run_mysqlcheck_mysql_db_upgrade())) ||
      run_sql_fix_privilege_tables() ||
      (!opt_systables_only &&
      (run_mysqlcheck_fixnames() || run_mysqlcheck_upgrade())))
  {
    /*
      The upgrade failed to complete in some way or another,
      significant error message should have been printed to the screen
    */
    die("Upgrade failed" );
  }
  verbose("OK");

  /* Create a file indicating upgrade has been performed */
  create_mysql_upgrade_info_file();

  free_used_memory();
  my_end(my_end_arg);
  exit(0);
}
int
main(int argc, char **argv)
{
	gcry_error_t 		gcry_error;
	File filein = 0;
	File fileout = 0;

	MY_INIT(argv[0]);

	if (get_options(&argc, &argv)) {
		goto err;
	}

	/* Acording to gcrypt docs (and my testing), setting up the threading
	   callbacks must be done first, so, lets give it a shot */
	gcry_error = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
	if (gcry_error) {
		msg("%s: unable to set libgcrypt thread cbs - "
		    "%s : %s\n", my_progname,
		    gcry_strsource(gcry_error),
		    gcry_strerror(gcry_error));
		return 1;
	}

	/* Version check should be the very first call because it
	makes sure that important subsystems are intialized. */
	if (!gcry_control(GCRYCTL_ANY_INITIALIZATION_P)) {
		const char	*gcrypt_version;
		gcrypt_version = gcry_check_version(NULL);
		/* No other library has already initialized libgcrypt. */
		if (!gcrypt_version) {
			msg("%s: failed to initialize libgcrypt\n",
			    my_progname);
			return 1;
		} else if (opt_verbose) {
			msg("%s: using gcrypt %s\n", my_progname,
			    gcrypt_version);
		}
	}
	gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
	gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);

	/* Determine the algorithm */
	encrypt_algo = encrypt_algos[opt_encrypt_algo];

	/* Set up the iv length */
	encrypt_iv_len = gcry_cipher_get_algo_blklen(encrypt_algo);

	/* Now set up the key */
	if (opt_encrypt_key == NULL && opt_encrypt_key_file == NULL) {
		msg("%s: no encryption key or key file specified.\n",
		    my_progname);
		return 1;
	} else if (opt_encrypt_key && opt_encrypt_key_file) {
		msg("%s: both encryption key and key file specified.\n",
		    my_progname);
		return 1;
	} else if (opt_encrypt_key_file) {
		if (!xb_crypt_read_key_file(opt_encrypt_key_file,
					    &opt_encrypt_key,
					    &encrypt_key_len)) {
			msg("%s: unable to read encryption key file \"%s\".\n",
			    opt_encrypt_key_file, my_progname);
			return 1;
		}
	} else {
		encrypt_key_len = strlen(opt_encrypt_key);
	}

	if (opt_input_file) {
		MY_STAT 	mystat;

		if (opt_verbose)
			msg("%s: input file \"%s\".\n", my_progname,
			    opt_input_file);

		if (my_stat(opt_input_file, &mystat, MYF(MY_WME)) == NULL) {
			goto err;
		}
		if (!MY_S_ISREG(mystat.st_mode)) {
			msg("%s: \"%s\" is not a regular file, exiting.\n",
			    my_progname, opt_input_file);
			goto err;
		}
		if ((filein = my_open(opt_input_file, O_RDONLY, MYF(MY_WME)))
		     < 0) {
			msg("%s: failed to open \"%s\".\n", my_progname,
			     opt_input_file);
			goto err;
		}
	} else {
		if (opt_verbose)
			msg("%s: input from standard input.\n", my_progname);
		filein = fileno(stdin);
	}

	if (opt_output_file) {
		if (opt_verbose)
			msg("%s: output file \"%s\".\n", my_progname,
			    opt_output_file);

		if ((fileout = my_create(opt_output_file, 0,
					 O_WRONLY|O_BINARY|O_EXCL|O_NOFOLLOW,
					 MYF(MY_WME))) < 0) {
			msg("%s: failed to create output file \"%s\".\n",
			    my_progname, opt_output_file);
			goto err;
		}
	} else {
		if (opt_verbose)
			msg("%s: output to standard output.\n", my_progname);
		fileout = fileno(stdout);
	}

	if (opt_run_mode == RUN_MODE_DECRYPT
	    && mode_decrypt(filein, fileout)) {
		goto err;
	} else if (opt_run_mode == RUN_MODE_ENCRYPT
		   && mode_encrypt(filein, fileout)) {
		goto err;
	}

	if (opt_input_file && filein) {
		my_close(filein, MYF(MY_WME));
	}
	if (opt_output_file && fileout) {
		my_close(fileout, MYF(MY_WME));
	}

	my_cleanup_options(my_long_options);

	my_end(0);

	return EXIT_SUCCESS;
err:
	if (opt_input_file && filein) {
		my_close(filein, MYF(MY_WME));
	}
	if (opt_output_file && fileout) {
		my_close(fileout, MYF(MY_WME));
	}

	my_cleanup_options(my_long_options);

	my_end(0);

	exit(EXIT_FAILURE);

}
Пример #16
0
int main(int argc, char **argv)
{
  int error;
  my_bool first_argument_uses_wildcards=0;
  char *wild;
  MYSQL mysql;
  MY_INIT(argv[0]);
  sf_leaking_memory=1; /* don't report memory leaks on early exits */
  if (load_defaults("my",load_default_groups,&argc,&argv))
    exit(1);

  get_options(&argc,&argv);

  sf_leaking_memory=0; /* from now on we cleanup properly */
  wild=0;
  if (argc)
  {
    char *pos= argv[argc-1], *to;
    for (to= pos ; *pos ; pos++, to++)
    {
      switch (*pos) {
      case '*':
	*pos= '%';
	first_argument_uses_wildcards= 1;
	break;
      case '?':
	*pos= '_';
	first_argument_uses_wildcards= 1;
	break;
      case '%':
      case '_':
	first_argument_uses_wildcards= 1;
	break;
      case '\\':
	pos++;
      default: break;
      }
      *to= *pos;
    }    
    *to= *pos; /* just to copy a '\0'  if '\\' was used */
  }
  if (first_argument_uses_wildcards)
    wild= argv[--argc];
  else if (argc == 3)			/* We only want one field */
    wild= argv[--argc];

  if (argc > 2)
  {
    fprintf(stderr,"%s: Too many arguments\n",my_progname);
    exit(1);
  }
  mysql_init(&mysql);
  if (opt_compress)
    mysql_options(&mysql,MYSQL_OPT_COMPRESS,NullS);
#ifdef HAVE_OPENSSL
  if (opt_use_ssl)
    mysql_ssl_set(&mysql, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
		  opt_ssl_capath, opt_ssl_cipher);
  mysql_options(&mysql,MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
                (char*)&opt_ssl_verify_server_cert);
#endif
  if (opt_protocol)
    mysql_options(&mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
#ifdef HAVE_SMEM
  if (shared_memory_base_name)
    mysql_options(&mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
#endif
  mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset);

  if (opt_plugin_dir && *opt_plugin_dir)
    mysql_options(&mysql, MYSQL_PLUGIN_DIR, opt_plugin_dir);

  if (opt_default_auth && *opt_default_auth)
    mysql_options(&mysql, MYSQL_DEFAULT_AUTH, opt_default_auth);

  if (!(mysql_real_connect(&mysql,host,user,opt_password,
			   (first_argument_uses_wildcards) ? "" :
                           argv[0],opt_mysql_port,opt_mysql_unix_port,
			   0)))
  {
    fprintf(stderr,"%s: %s\n",my_progname,mysql_error(&mysql));
    exit(1);
  }
  mysql.reconnect= 1;

  switch (argc) {
  case 0:  error=list_dbs(&mysql,wild); break;
  case 1:
    if (opt_status)
      error=list_table_status(&mysql,argv[0],wild);
    else
      error=list_tables(&mysql,argv[0],wild);
    break;
  default:
    if (opt_status && ! wild)
      error=list_table_status(&mysql,argv[0],argv[1]);
    else
      error=list_fields(&mysql,argv[0],argv[1],wild);
    break;
  }
  mysql_close(&mysql);			/* Close & free connection */
  my_free(opt_password);
#ifdef HAVE_SMEM
  my_free(shared_memory_base_name);
#endif
  my_end(my_end_arg);
  exit(error ? 1 : 0);
  return 0;				/* No compiler warnings */
}
Пример #17
0
int
main (int argc, char *argv[])
{
int        opt_err;
MYSQL      *conn;   /* pointer to connection handler */
MYSQL_STMT *stmt;   /* pointer to statement handler */

  MY_INIT (argv[0]);
  load_defaults ("my", client_groups, &argc, &argv);

  if ((opt_err = handle_options (&argc, &argv, my_opts, get_one_option)))
    exit (opt_err);

  /* solicit password if necessary */
  if (ask_password)
    opt_password = get_tty_password (NULL);

  /* get database name if present on command line */
  if (argc > 0)
  {
    opt_db_name = argv[0];
    --argc; ++argv;
  }

  /* initialize client library */
  if (mysql_library_init (0, NULL, NULL))
  {
    print_error (NULL, "mysql_library_init() failed");
    exit (1);
  }

  /* initialize connection handler */
  conn = mysql_init (NULL);
  if (conn == NULL)
  {
    print_error (NULL, "mysql_init() failed (probably out of memory)");
    exit (1);
  }

#ifdef HAVE_OPENSSL
  /* pass SSL information to client library */
  if (opt_use_ssl)
    mysql_ssl_set (conn, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
                   opt_ssl_capath, opt_ssl_cipher);
  mysql_options (conn,MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
                 (char*)&opt_ssl_verify_server_cert);
#endif

  /* connect to server */
  if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
      opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
  {
    print_error (conn, "mysql_real_connect() failed");
    mysql_close (conn);
    exit (1);
  }

/* #@ _VERIFY_SERVER_VERSION_ */
  if (mysql_get_server_version (conn) < 50503)
  {
    print_error (NULL, "Prepared CALL requires MySQL 5.5.3 or higher");
    mysql_close (conn);
    exit (1);
  }
/* #@ _VERIFY_SERVER_VERSION_ */

  /* initialize statement handler, execute prepared CALL, close handler */
/* #@ _CALL_PROCEDURE_ */
  stmt = mysql_stmt_init (conn);
  if (!stmt)
    print_error (NULL, "Could not initialize statement handler");
  else
  {
    if (exec_prepared_call (stmt) == 0)
      process_call_result (conn, stmt);
    mysql_stmt_close (stmt);
  }
/* #@ _CALL_PROCEDURE_ */

  /* disconnect from server, terminate client library */
  mysql_close (conn);
  mysql_library_end ();
  exit (0);
}
Пример #18
0
int
main (int argc, char *argv[])
{
int opt_err;

  MY_INIT (argv[0]);
  load_defaults ("my", client_groups, &argc, &argv);

  if ((opt_err = handle_options (&argc, &argv, my_opts, get_one_option)))
    exit (opt_err);

  /* solicit password if necessary */
  if (ask_password)
    opt_password = get_tty_password (NULL);

  /* get database name if present on command line */
  if (argc > 0)
  {
    opt_db_name = argv[0];
    --argc; ++argv;
  }

  /* initialize client library */
  if (mysql_library_init (0, NULL, NULL))
  {
    print_error (NULL, "mysql_library_init() failed");
    exit (1);
  }

  /* initialize connection handler */
  conn = mysql_init (NULL);
  if (conn == NULL)
  {
    print_error (NULL, "mysql_init() failed (probably out of memory)");
    exit (1);
  }

  if (opt_protocol)
    mysql_options (conn, MYSQL_OPT_PROTOCOL, (char*)&opt_protocol);
#ifdef HAVE_SMEM
  if (opt_shared_memory_base_name)
    mysql_options (conn, MYSQL_SHARED_MEMORY_BASE_NAME,
            opt_shared_memory_base_name);
#endif

  /* connect to server */
  if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
      opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
  {
    print_error (conn, "mysql_real_connect() failed");
    mysql_close (conn);
    exit (1);
  }

  while (1)
  {
    char  buf[10000];

    fprintf (stderr, "query> ");          /* print prompt */
    if (fgets (buf, sizeof (buf), stdin) == NULL) /* read statement */
      break;
    if (strcmp (buf, "quit\n") == 0 || strcmp (buf, "\\q\n") == 0)
      break;
    process_statement (conn, buf);          /* execute it */
  }

  /* disconnect from server, terminate client library */
  mysql_close (conn);
  mysql_library_end ();
  exit (0);
}
Пример #19
0
int main(int argc, char *argv[])
{
  MI_INFO *file;
  int i,j;

  MY_INIT(argv[0]);
  get_options(argc,argv);
  bzero((char*)recinfo,sizeof(recinfo));

  /* First define 2 columns */
  recinfo[0].type=FIELD_SKIP_ENDSPACE;
  recinfo[0].length=docid_length;
  recinfo[1].type=FIELD_BLOB;
  recinfo[1].length= 4+portable_sizeof_char_ptr;

  /* Define a key over the first column */
  keyinfo[0].seg=keyseg;
  keyinfo[0].keysegs=1;
  keyinfo[0].block_length= 0;                   /* Default block length */
  keyinfo[0].seg[0].type= HA_KEYTYPE_TEXT;
  keyinfo[0].seg[0].flag= HA_BLOB_PART;
  keyinfo[0].seg[0].start=recinfo[0].length;
  keyinfo[0].seg[0].length=key_length;
  keyinfo[0].seg[0].null_bit=0;
  keyinfo[0].seg[0].null_pos=0;
  keyinfo[0].seg[0].bit_start=4;
  keyinfo[0].seg[0].language=MY_CHARSET_CURRENT;
  keyinfo[0].flag = HA_FULLTEXT;

  if (!silent)
    printf("- Creating isam-file\n");
  if (mi_create(filename,1,keyinfo,2,recinfo,0,NULL,(MI_CREATE_INFO*) 0,0))
    goto err;
  if (!(file=mi_open(filename,2,0)))
    goto err;
  if (!silent)
    printf("Initializing stopwords\n");
  ft_init_stopwords(stopwordlist);

  if (!silent)
    printf("- Writing key:s\n");

  my_errno=0;
  i=0;
  while (create_record(record,df))
  {
    error=mi_write(file,record);
    if (error)
      printf("I= %2d  mi_write: %d  errno: %d\n",i,error,my_errno);
    i++;
  }
  fclose(df);

  if (mi_close(file)) goto err;
  if (!silent)
    printf("- Reopening file\n");
  if (!(file=mi_open(filename,2,0))) goto err;
  if (!silent)
    printf("- Reading rows with key\n");
  for (i=1;create_record(record,qf);i++)
  {
    FT_DOCLIST *result;
    double w;
    int t, err;

    result=ft_nlq_init_search(file,0,blob_record,(uint) strlen(blob_record),1);
    if (!result)
    {
      printf("Query %d failed with errno %3d\n",i,my_errno);
      goto err;
    }
    if (!silent)
      printf("Query %d. Found: %d.\n",i,result->ndocs);
    for (j=0;(err=ft_nlq_read_next(result, read_record))==0;j++)
    {
      t=uint2korr(read_record);
      w=ft_nlq_get_relevance(result);
      printf("%d %.*s %f\n",i,t,read_record+2,w);
    }
    if (err != HA_ERR_END_OF_FILE)
    {
      printf("ft_read_next %d failed with errno %3d\n",j,my_errno);
      goto err;
    }
    ft_nlq_close_search(result);
  }

  if (mi_close(file)) goto err;
  my_end(MY_CHECK_ERROR);

  return (0);

 err:
  printf("got error: %3d when using myisam-database\n",my_errno);
  return 1;			/* skip warning */

}
Пример #20
0
int
main(void)
{
  int i, cmp;
  size_t j, k, l, dst_len, needed_length;
  MY_INIT("base64-t");

  plan(BASE64_LOOP_COUNT * BASE64_ROWS);

  for (i= 0; i < BASE64_LOOP_COUNT; i++)
  {
    /* Create source data */
    const size_t src_len= rand() % 1000 + 1;

    char * src= (char *) malloc(src_len);
    char * s= src;
    char * str;
    char * dst;

    for (j= 0; j<src_len; j++)
    {
      char c= rand();
      *s++= c;
    }

    /* Encode */
    needed_length= base64_needed_encoded_length(src_len);
    str= (char *) malloc(needed_length);
    for (k= 0; k < needed_length; k++)
      str[k]= 0xff; /* Fill memory to check correct NUL termination */
    ok(base64_encode(src, src_len, str) == 0,
       "base64_encode: size %d", i);
    ok(needed_length == strlen(str) + 1,
       "base64_needed_encoded_length: size %d", i);

    /* Decode */
    dst= (char *) malloc(base64_needed_decoded_length(strlen(str)));
    dst_len= base64_decode(str, strlen(str), dst, NULL);
    ok(dst_len == src_len, "Comparing lengths");

    cmp= memcmp(src, dst, src_len);
    ok(cmp == 0, "Comparing encode-decode result");
    if (cmp != 0)
    {
      char buf[80];
      diag("       --------- src ---------   --------- dst ---------");
      for (k= 0; k<src_len; k+=8)
      {
        sprintf(buf, "%.4x   ", (uint) k);
        for (l=0; l<8 && k+l<src_len; l++)
        {
          unsigned char c= src[k+l];
          sprintf(buf, "%.2x ", (unsigned)c);
        }

        sprintf(buf, "  ");

        for (l=0; l<8 && k+l<dst_len; l++)
        {
          unsigned char c= dst[k+l];
          sprintf(buf, "%.2x ", (unsigned)c);
        }
        diag("%s", buf);
      }
      diag("src length: %.8x, dst length: %.8x\n",
           (uint) src_len, (uint) dst_len);
    }
  }
  return exit_status();
}
Пример #21
0
int main(int argc, char *argv[])
{
  register uint i,j;
  uint ant,n1,n2,n3;
  uint write_count,update,opt_delete,check2,dupp_keys,found_key;
  int error;
  ulong pos;
  unsigned long key_check;
  uchar record[128],record2[128],record3[128],key[10];
  const char *filename,*filename2;
  HP_INFO *file,*file2;
  HP_SHARE *tmp_share;
  HP_KEYDEF keyinfo[MAX_KEYS];
  HA_KEYSEG keyseg[MAX_KEYS*5];
  HEAP_PTR UNINIT_VAR(position);
  HP_CREATE_INFO hp_create_info;
  CHARSET_INFO *cs= &my_charset_latin1;
  my_bool unused;
  MY_INIT(argv[0]);		/* init my_sys library & pthreads */

  filename= "test2";
  filename2= "test2_2";
  file=file2=0;
  get_options(argc,argv);

  bzero(&hp_create_info, sizeof(hp_create_info));
  hp_create_info.max_table_size= 2*1024L*1024L;
  hp_create_info.keys= keys;
  hp_create_info.keydef= keyinfo;
  hp_create_info.reclength= reclength;
  hp_create_info.max_records= (ulong) flag*100000L;
  hp_create_info.min_records= (ulong) recant/2;

  write_count=update=opt_delete=0;
  key_check=0;

  keyinfo[0].seg=keyseg;
  keyinfo[0].keysegs=1;
  keyinfo[0].flag= 0;
  keyinfo[0].algorithm= HA_KEY_ALG_HASH;
  keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
  keyinfo[0].seg[0].start=0;
  keyinfo[0].seg[0].length=6;
  keyinfo[0].seg[0].null_bit=0;
  keyinfo[0].seg[0].charset=cs;
  keyinfo[1].seg=keyseg+1;
  keyinfo[1].keysegs=2;
  keyinfo[1].flag=0;
  keyinfo[1].algorithm= HA_KEY_ALG_HASH;
  keyinfo[1].seg[0].type=HA_KEYTYPE_BINARY;
  keyinfo[1].seg[0].start=7;
  keyinfo[1].seg[0].length=6;
  keyinfo[1].seg[0].null_bit=0;
  keyinfo[1].seg[0].charset=cs;
  keyinfo[1].seg[1].type=HA_KEYTYPE_TEXT;
  keyinfo[1].seg[1].start=0;			/* key in two parts */
  keyinfo[1].seg[1].length=6;
  keyinfo[1].seg[1].null_bit=0;
  keyinfo[1].seg[1].charset=cs;
  keyinfo[2].seg=keyseg+3;
  keyinfo[2].keysegs=1;
  keyinfo[2].flag=HA_NOSAME;
  keyinfo[2].algorithm= HA_KEY_ALG_HASH;
  keyinfo[2].seg[0].type=HA_KEYTYPE_BINARY;
  keyinfo[2].seg[0].start=12;
  keyinfo[2].seg[0].length=8;
  keyinfo[2].seg[0].null_bit=0;
  keyinfo[2].seg[0].charset=cs;
  keyinfo[3].seg=keyseg+4;
  keyinfo[3].keysegs=1;
  keyinfo[3].flag=HA_NOSAME;
  keyinfo[3].algorithm= HA_KEY_ALG_HASH;
  keyinfo[3].seg[0].type=HA_KEYTYPE_BINARY;
  keyinfo[3].seg[0].start=37;
  keyinfo[3].seg[0].length=1;
  keyinfo[3].seg[0].null_bit=1;
  keyinfo[3].seg[0].null_pos=38;
  keyinfo[3].seg[0].charset=cs;

  bzero((char*) key1,sizeof(key1));
  bzero((char*) key3,sizeof(key3));

  printf("- Creating heap-file\n");
  if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
      !(file= heap_open(filename, 2)))
    goto err;
  signal(SIGINT,endprog);

  printf("- Writing records:s\n");
  strmov((char*) record,"          ..... key");

  for (i=0 ; i < recant ; i++)
  {
    n1=rnd(1000); n2=rnd(100); n3=rnd(MY_MIN(recant*5,MAX_RECORDS));
    make_record(record,n1,n2,n3,"Pos",write_count);

    if (heap_write(file,record))
    {
      if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
      {
	printf("Error: %d in write at record: %d\n",my_errno,i);
	goto err;
      }
      if (verbose) printf("   Double key: %d\n",n3);
    }
    else
    {
      if (key3[n3] == 1)
      {
	printf("Error: Didn't get error when writing second key: '%8d'\n",n3);
	goto err;
      }
      write_count++; key1[n1]++; key3[n3]=1;
      key_check+=n1;
    }
    if (testflag == 1 && heap_check_heap(file,0))
    {
      puts("Heap keys crashed");
      goto err;
    }
  }
  if (testflag == 1)
    goto end;
  if (heap_check_heap(file,0))
  {
    puts("Heap keys crashed");
    goto err;
  }

  printf("- Delete\n");
  for (i=0 ; i < write_count/10 ; i++)
  {
    for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
    if (j != 0)
    {
      sprintf((char*) key,"%6d",j);
      if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
      {
	printf("can't find key1: \"%s\"\n",(char*) key);
	goto err;
      }
      if (heap_delete(file,record))
      {
	printf("error: %d; can't delete record: \"%s\"\n", my_errno,(char*) record);
	goto err;
      }
      opt_delete++;
      key1[atoi((char*) record+keyinfo[0].seg[0].start)]--;
      key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0;
      key_check-=atoi((char*) record);
      if (testflag == 2 && heap_check_heap(file,0))
      {
	puts("Heap keys crashed");
	goto err;
      }
    }
    else
      puts("Warning: Skipping delete test because no dupplicate keys");
  }
  if (testflag==2) goto end;
  if (heap_check_heap(file,0))
  {
    puts("Heap keys crashed");
    goto err;
  }

  printf("- Update\n");
  for (i=0 ; i < write_count/10 ; i++)
  {
    n1=rnd(1000); n2=rnd(100); n3=rnd(MY_MIN(recant*2,MAX_RECORDS));
    make_record(record2, n1, n2, n3, "XXX", update);
    if (rnd(2) == 1)
    {
      if (heap_scan_init(file))
	goto err;
      j=rnd(write_count-opt_delete);
      while ((error=heap_scan(file,record) == HA_ERR_RECORD_DELETED) ||
	     (!error && j))
      {
	if (!error)
	  j--;
      }
      if (error)
	goto err;
    }
    else
    {
      for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
      if (!key1[j])
	continue;
      sprintf((char*) key,"%6d",j);
      if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
      {
	printf("can't find key1: \"%s\"\n",(char*) key);
	goto err;
      }
    }
    if (heap_update(file,record,record2))
    {
      if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
      {
	printf("error: %d; can't update:\nFrom: \"%s\"\nTo:   \"%s\"\n",
	       my_errno,(char*) record, (char*) record2);
	goto err;
      }
      if (verbose)
	printf("Double key when tried to update:\nFrom: \"%s\"\nTo:   \"%s\"\n",
               (char*) record, (char*) record2);
    }
    else
    {
      key1[atoi((char*) record+keyinfo[0].seg[0].start)]--;
      key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0;
      key1[n1]++; key3[n3]=1;
      update++;
      key_check=key_check-atoi((char*) record)+n1;
    }
    if (testflag == 3 && heap_check_heap(file,0))
    {
      puts("Heap keys crashed");
      goto err;
    }
  }
  if (testflag == 3) goto end;
  if (heap_check_heap(file,0))
  {
    puts("Heap keys crashed");
    goto err;
  }

  for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
  {
    if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; }
    sprintf((char*) key,"%6d",found_key);
  }

  if (dupp_keys > 3)
  {
    if (!silent)
      printf("- Read first key - next - delete - next -> last\n");
    DBUG_PRINT("progpos",("first - next - delete - next -> last"));

    if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
      goto err;
    if (heap_rnext(file,record3)) goto err;
    if (heap_delete(file,record3)) goto err;
    key_check-=atoi((char*) record3);
    key1[atoi((char*) record+keyinfo[0].seg[0].start)]--;
    key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0;
    opt_delete++;
    ant=2;
    while ((error=heap_rnext(file,record3)) == 0 ||
	   error == HA_ERR_RECORD_DELETED)
      if (! error)
	ant++;
    if (ant != dupp_keys)
    {
      printf("next: I can only find: %d records of %d\n",
	     ant,dupp_keys);
      goto end;
    }
    dupp_keys--;
    if (heap_check_heap(file,0))
    {
      puts("Heap keys crashed");
      goto err;
    }

    if (!silent)
      printf("- Read last key - delete - prev - prev - opt_delete - prev -> first\n");

    if (heap_rprev(file,record))
      goto err;
    if (heap_delete(file,record3)) goto err;
    key_check-=atoi((char*) record3);
    key1[atoi((char*) record+keyinfo[0].seg[0].start)]--;
    key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0;
    opt_delete++;
    if (heap_rprev(file,record3) || heap_rprev(file,record3))
      goto err;
    if (heap_delete(file,record3)) goto err;
    key_check-=atoi((char*) record3);
    key1[atoi((char*) record+keyinfo[0].seg[0].start)]--;
    key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0;
    opt_delete++;
    ant=3;
    while ((error=heap_rprev(file,record3)) == 0 ||
	   error == HA_ERR_RECORD_DELETED)
    {
      if (! error)
	ant++;
    }
    if (ant != dupp_keys)
    {
      printf("next: I can only find: %d records of %d\n",
	     ant,dupp_keys);
      goto end;
    }
    dupp_keys-=2;
    if (heap_check_heap(file,0))
    {
      puts("Heap keys crashed");
      goto err;
    }
  }
  else
    puts("Warning: Not enough duplicated keys:  Skipping delete key check");

  if (!silent)
    printf("- Read (first) - next - delete - next -> last\n");
  DBUG_PRINT("progpos",("first - next - delete - next -> last"));

  if (heap_scan_init(file))
    goto err;
  while ((error=heap_scan(file,record3) == HA_ERR_RECORD_DELETED)) ;
  if (error)
    goto err;
  if (heap_delete(file,record3)) goto err;
  key_check-=atoi((char*) record3);
  opt_delete++;
  key1[atoi((char*) record+keyinfo[0].seg[0].start)]--;
  key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0;
  ant=0;
  while ((error=heap_scan(file,record3)) == 0 ||
	 error == HA_ERR_RECORD_DELETED)
    if (! error)
      ant++;
  if (ant != write_count-opt_delete)
  {
    printf("next: Found: %d records of %d\n",ant,write_count-opt_delete);
    goto end;
  }
  if (heap_check_heap(file,0))
  {
    puts("Heap keys crashed");
    goto err;
  }

  puts("- Test if: Read rrnd - same - rkey - same");
  DBUG_PRINT("progpos",("Read rrnd - same"));
  pos=rnd(write_count-opt_delete-5)+5;
  heap_scan_init(file);
  i=5;
  while ((error=heap_scan(file,record)) == HA_ERR_RECORD_DELETED ||
	 (error == 0 && pos))
  {
    if (!error)
      pos--;
    if (i-- == 0)
    {
      bmove(record3,record,reclength);
      position=heap_position(file);
    }
  }
  if (error)
    goto err;
  bmove(record2,record,reclength);
  if (heap_rsame(file,record,-1) || heap_rsame(file,record2,2))
    goto err;
  if (memcmp(record2,record,reclength))
  {
    puts("heap_rsame didn't find right record");
    goto end;
  }

  puts("- Test of read through position");
  if (heap_rrnd(file,record,position))
    goto err;
  if (memcmp(record3,record,reclength))
  {
    puts("heap_frnd didn't find right record");
    goto end;
  }

  printf("- heap_info\n");
  {
    HEAPINFO info;
    heap_info(file,&info,0);
    /* We have to test with opt_delete +1 as this may be the case if the last
       inserted row was a duplicate key */
    if (info.records != write_count-opt_delete ||
	(info.deleted != opt_delete && info.deleted != opt_delete+1))
    {
      puts("Wrong info from heap_info");
      printf("Got: records: %ld(%d)  deleted: %ld(%d)\n",
	     info.records,write_count-opt_delete,info.deleted,opt_delete);
    }
  }

#ifdef OLD_HEAP_VERSION
  {
    uint check;
    printf("- Read through all records with rnd\n");
    if (heap_extra(file,HA_EXTRA_RESET) || heap_extra(file,HA_EXTRA_CACHE))
    {
      puts("got error from heap_extra");
      goto end;
    }
    ant=check=0;
    while ((error=heap_rrnd(file,record,(ulong) -1)) != HA_ERR_END_OF_FILE &&
	   ant < write_count + 10)
    {
      if (!error)
      {
	ant++;
	check+=calc_check(record,reclength);
      }
    }
    if (ant != write_count-opt_delete)
    {
      printf("rrnd: I can only find: %d records of %d\n", ant,
	     write_count-opt_delete);
      goto end;
    }
    if (heap_extra(file,HA_EXTRA_NO_CACHE))
    {
      puts("got error from heap_extra(HA_EXTRA_NO_CACHE)");
      goto end;
    }
  }
#endif

  printf("- Read through all records with scan\n");
  if (heap_reset(file) || heap_extra(file,HA_EXTRA_CACHE))
  {
    puts("got error from heap_extra");
    goto end;
  }
  ant=check2=0;
  heap_scan_init(file);
  while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE &&
	 ant < write_count + 10)
  {
    if (!error)
    {
      ant++;
      check2+=calc_check(record,reclength);
    }
  }
  if (ant != write_count-opt_delete)
  {
    printf("scan: I can only find: %d records of %d\n", ant,
	   write_count-opt_delete);
    goto end;
  }
#ifdef OLD_HEAP_VERSION
  if (check != check2)
  {
    puts("scan: Checksum didn't match reading with rrnd");
    goto end;
  }
#endif


  if (heap_extra(file,HA_EXTRA_NO_CACHE))
  {
    puts("got error from heap_extra(HA_EXTRA_NO_CACHE)");
    goto end;
  }

  for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
  {
    if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; }
    sprintf((char*) key,"%6d",found_key);
  }
  printf("- Read through all keys with first-next-last-prev\n");
  ant=0;
  for (error=heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT);
      ! error ;
       error=heap_rnext(file,record))
    ant++;
  if (ant != dupp_keys)
  {
    printf("first-next: I can only find: %d records of %d\n", ant,
	   dupp_keys);
    goto end;
  }

  ant=0;
  for (error=heap_rprev(file,record) ;
      ! error ;
      error=heap_rprev(file,record))
  {
    ant++;
    check2+=calc_check(record,reclength);
  }
  if (ant != dupp_keys)
  {
    printf("last-prev: I can only find: %d records of %d\n", ant,
	   dupp_keys);
    goto end;
  }

  if (testflag == 4) goto end;

  printf("- Reading through all rows through keys\n");
  if (!(file2=heap_open(filename, 2)))
    goto err;
  if (heap_scan_init(file))
    goto err;
  while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
  {
    if (error == 0)
    {
      if (heap_rkey(file2,record2,2,record+keyinfo[2].seg[0].start,8,
		    HA_READ_KEY_EXACT))
      {
	printf("can't find key3: \"%.8s\"\n",
	       record+keyinfo[2].seg[0].start);
	goto err;
      }
    }
  }
  heap_close(file2);

  printf("- Creating output heap-file 2\n");
  hp_create_info.keys= 1;
  hp_create_info.max_records= 0;
  hp_create_info.min_records= 0;
  if (heap_create(filename2, &hp_create_info, &tmp_share, &unused) ||
      !(file2= heap_open_from_share_and_register(tmp_share, 2)))
    goto err;

  printf("- Copying and removing records\n");
  if (heap_scan_init(file))
    goto err;
  while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
  {
    if (error == 0)
    {
      if (heap_write(file2,record))
	goto err;
      key_check-=atoi((char*) record);
      write_count++;
      if (heap_delete(file,record))
	goto err;
      opt_delete++;
    }
    pos++;
  }
  printf("- Checking heap tables\n");
  if (heap_check_heap(file,1) || heap_check_heap(file2,1))
  {
    puts("Heap keys crashed");
    goto err;
  }

  if (my_errno != HA_ERR_END_OF_FILE)
    printf("error: %d from heap_rrnd\n",my_errno);
  if (key_check)
    printf("error: Some read got wrong: check is %ld\n",(long) key_check);

end:
  printf("\nFollowing test have been made:\n");
  printf("Write records: %d\nUpdate records: %d\nDelete records: %d\n", write_count,update,opt_delete);
  heap_clear(file);
  if (heap_close(file) || (file2 && heap_close(file2)))
    goto err;
  heap_delete_table(filename2);
  hp_panic(HA_PANIC_CLOSE);
  my_end(MY_GIVE_INFO);
  return(0);
err:
  printf("Got error: %d when using heap-database\n",my_errno);
  (void) heap_close(file);
  return(1);
} /* main */
int main(int argc __attribute__((unused)), char *argv[])
{
  ulong i;
  uint pagen;
  uchar long_tr_id[6];
  PAGECACHE pagecache;
  LSN lsn;
  LEX_CUSTRING parts[TRANSLOG_INTERNAL_PARTS + 1];
  uchar *long_buffer= malloc(LONG_BUFFER_SIZE);

  MY_INIT(argv[0]);

  plan(4);

  bzero(&pagecache, sizeof(pagecache));
  bzero(long_buffer, LONG_BUFFER_SIZE);
  maria_data_root= create_tmpdir(argv[0]);
  if (maria_log_remove(0))
    exit(1);

  bzero(long_tr_id, 6);
#ifndef DBUG_OFF
#if defined(__WIN__)
  default_dbug_option= "d:t:i:O,\\ma_test_loghandler.trace";
#else
  default_dbug_option= "d:t:i:o,/tmp/ma_test_loghandler.trace";
#endif
  if (argc > 1)
  {
    DBUG_SET(default_dbug_option);
    DBUG_SET_INITIAL(default_dbug_option);
  }
#endif

  if (ma_control_file_open(TRUE, TRUE))
  {
    fprintf(stderr, "Can't init control file (%d)\n", errno);
    exit(1);
  }
  if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
                             PCACHE_PAGE, 0, 0)) == 0)
  {
    fprintf(stderr, "Got error: init_pagecache() (errno: %d)\n", errno);
    exit(1);
  }
  if (translog_init_with_table(maria_data_root, LOG_FILE_SIZE, 50112, 0, &pagecache,
                               LOG_FLAGS, 0, &translog_example_table_init,
                               0))
  {
    fprintf(stderr, "Can't init loghandler (%d)\n", errno);
    exit(1);
  }
  /* Suppressing of automatic record writing */
  dummy_transaction_object.first_undo_lsn|= TRANSACTION_LOGGED_LONG_ID;

  /* write more then 1 file */
  int4store(long_tr_id, 0);
  parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
  parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
  if (translog_write_record(&lsn,
                            LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
                            &dummy_transaction_object, NULL, 6,
                            TRANSLOG_INTERNAL_PARTS + 1,
                            parts, NULL, NULL))
  {
    fprintf(stderr, "Can't write record #%lu\n", (ulong) 0);
    translog_destroy();
    exit(1);
  }

  translog_purge(lsn);
  if (!translog_is_file(1))
  {
    fprintf(stderr, "First file was removed after first record\n");
    translog_destroy();
    exit(1);
  }
  ok(1, "First is not removed");

  for(i= 0; i < LOG_FILE_SIZE/6 && LSN_FILE_NO(lsn) == 1; i++)
  {
    if (translog_write_record(&lsn,
                              LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
                              &dummy_transaction_object, NULL, 6,
                              TRANSLOG_INTERNAL_PARTS + 1,
                              parts, NULL, NULL))
    {
      fprintf(stderr, "Can't write record #%lu\n", (ulong) 0);
      translog_destroy();
      exit(1);
    }
  }

  translog_purge(lsn);
  if (translog_is_file(1))
  {
    fprintf(stderr, "First file was not removed.\n");
    translog_destroy();
    exit(1);
  }

  ok(1, "First file is removed");

  parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_buffer;
  parts[TRANSLOG_INTERNAL_PARTS + 0].length= LONG_BUFFER_SIZE;
  if (translog_write_record(&lsn,
			    LOGREC_VARIABLE_RECORD_0LSN_EXAMPLE,
			    &dummy_transaction_object, NULL, LONG_BUFFER_SIZE,
			    TRANSLOG_INTERNAL_PARTS + 1, parts, NULL, NULL))
  {
    fprintf(stderr, "Can't write variable record\n");
    translog_destroy();
    exit(1);
  }

  translog_purge(lsn);
  if (!translog_is_file(2) || !translog_is_file(3))
  {
    fprintf(stderr, "Second file (%d) or third file (%d) is not present.\n",
	    translog_is_file(2), translog_is_file(3));
    translog_destroy();
    exit(1);
  }

  ok(1, "Second and third files are not removed");

  int4store(long_tr_id, 0);
  parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
  parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
  if (translog_write_record(&lsn,
                            LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
                            &dummy_transaction_object, NULL, 6,
                            TRANSLOG_INTERNAL_PARTS + 1,
                            parts, NULL, NULL))
  {
    fprintf(stderr, "Can't write last record\n");
    translog_destroy();
    exit(1);
  }

  translog_purge(lsn);
  if (translog_is_file(2))
  {
    fprintf(stderr, "Second file is not removed\n");
    translog_destroy();
    exit(1);
  }

  ok(1, "Second file is removed");

  translog_destroy();
  end_pagecache(&pagecache, 1);
  ma_control_file_end();
  if (maria_log_remove(maria_data_root))
    exit(1);

  my_uuid_end();
  my_free_open_file_info();
  my_end(0);

  exit(0);
}
Пример #23
0
int main(int argc, char **argv)
{
  int i,j,error,deleted;
  HP_INFO *file;
  uchar record[128],key[32];
  const char *filename;
  HP_KEYDEF keyinfo[10];
  HA_KEYSEG keyseg[4];
  HP_CREATE_INFO hp_create_info;
  HP_SHARE *tmp_share;
  my_bool unused;
  MY_INIT(argv[0]);

  filename= "test1";
  get_options(argc,argv);

  memset(&hp_create_info, 0, sizeof(hp_create_info));
  hp_create_info.max_table_size= 1024L*1024L;
  hp_create_info.keys= 1;
  hp_create_info.keydef= keyinfo;
  hp_create_info.reclength= 30;
  hp_create_info.max_records= (ulong) flag*100000L;
  hp_create_info.min_records= 10UL;

  keyinfo[0].keysegs=1;
  keyinfo[0].seg=keyseg;
  keyinfo[0].algorithm= HA_KEY_ALG_HASH;
  keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
  keyinfo[0].seg[0].start=1;
  keyinfo[0].seg[0].length=6;
  keyinfo[0].seg[0].charset= &my_charset_latin1;
  keyinfo[0].seg[0].null_bit= 0;
  keyinfo[0].flag = HA_NOSAME;

  deleted=0;
  memset(flags, 0, sizeof(flags));

  printf("- Creating heap-file\n");
  if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
      !(file= heap_open(filename, 2)))
    goto err;
  printf("- Writing records:s\n");
  my_stpcpy((char*) record,"          ..... key           ");

  for (i=49 ; i>=1 ; i-=2 )
  {
    j=i%25 +1;
    sprintf((char*) key,"%6d",j);
    memmove(record + 1, key, 6);
    error=heap_write(file,record);
    if (heap_check_heap(file,0))
    {
      puts("Heap keys crashed");
      goto err;
    }
    flags[j]=1;
    if (verbose || error) printf("J= %2d  heap_write: %d  my_errno: %d\n",
       j,error,my_errno);
  }
  if (heap_close(file))
    goto err;
  printf("- Reopening file\n");
  if (!(file=heap_open(filename, 2)))
    goto err;

  printf("- Removing records\n");
  for (i=1 ; i<=10 ; i++)
  {
    if (i == remove_ant) { (void) heap_close(file); return (0) ; }
    sprintf((char*) key,"%6d",(j=(int) ((rand() & 32767)/32767.*25)));
    if ((error = heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT)))
    {
      if (verbose || (flags[j] == 1 ||
		      (error && my_errno != HA_ERR_KEY_NOT_FOUND)))
	printf("key: %s  rkey:   %3d  my_errno: %3d\n",(char*) key,error,my_errno);
    }
    else
    {
      error=heap_delete(file,record);
      if (error || verbose)
	printf("key: %s  delete: %d  my_errno: %d\n",(char*) key,error,my_errno);
      flags[j]=0;
      if (! error)
	deleted++;
    }
    if (heap_check_heap(file,0))
    {
      puts("Heap keys crashed");
      goto err;
    }
  }

  printf("- Reading records with key\n");
  for (i=1 ; i<=25 ; i++)
  {
    sprintf((char*) key,"%6d",i);
    memmove(record + 1, key, 6);
    my_errno=0;
    error=heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT);
    if (verbose ||
	(error == 0 && flags[i] != 1) ||
	(error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
    {
      printf("key: %s  rkey: %3d  my_errno: %3d  record: %s\n",
             (char*) key,error,my_errno,record+1);
    }
  }

  if (heap_close(file) || hp_panic(HA_PANIC_CLOSE))
    goto err;
  my_end(MY_GIVE_INFO);
  return(0);
err:
  printf("got error: %d when using heap-database\n",my_errno);
  return(1);
} /* main */
Пример #24
0
int main(int argc,char **argv)
{
  int status,wait_ret;
  uint i=0;
  MI_KEYDEF keyinfo[10];
  MI_COLUMNDEF recinfo[10];
  HA_KEYSEG keyseg[10][2];
  MY_INIT(argv[0]);
  get_options(argc,argv);

  bzero((char*) keyinfo,sizeof(keyinfo));
  bzero((char*) recinfo,sizeof(recinfo));
  bzero((char*) keyseg,sizeof(keyseg));
  keyinfo[0].seg= &keyseg[0][0];
  keyinfo[0].seg[0].start=0;
  keyinfo[0].seg[0].length=8;
  keyinfo[0].seg[0].type=HA_KEYTYPE_TEXT;
  keyinfo[0].seg[0].flag=HA_SPACE_PACK;
  keyinfo[0].key_alg=HA_KEY_ALG_BTREE;
  keyinfo[0].keysegs=1;
  keyinfo[0].flag = (uint8) HA_PACK_KEY;
  keyinfo[0].block_length= 0;                   /* Default block length */
  keyinfo[1].seg= &keyseg[1][0];
  keyinfo[1].seg[0].start=8;
  keyinfo[1].seg[0].length=4;		/* Long is always 4 in myisam */
  keyinfo[1].seg[0].type=HA_KEYTYPE_LONG_INT;
  keyinfo[1].seg[0].flag=0;
  keyinfo[1].key_alg=HA_KEY_ALG_BTREE;
  keyinfo[1].keysegs=1;
  keyinfo[1].flag =HA_NOSAME;
  keyinfo[1].block_length= 0;                   /* Default block length */

  recinfo[0].type=0;
  recinfo[0].length=sizeof(record.id);
  recinfo[1].type=0;
  recinfo[1].length=sizeof(record.nr);
  recinfo[2].type=0;
  recinfo[2].length=sizeof(record.text);

  puts("- Creating myisam-file");
  my_delete(filename,MYF(0));		/* Remove old locks under gdb */
  if (mi_create(filename,2,&keyinfo[0],2,&recinfo[0],0,(MI_UNIQUEDEF*) 0,
		(MI_CREATE_INFO*) 0,0))
    exit(1);

  rnd_init(0);
  printf("- Starting %d processes\n",forks); fflush(stdout);
  for (i=0 ; i < forks; i++)
  {
    if (!fork())
    {
      start_test(i+1);
      sleep(1);
      return 0;
    }
    (void) rnd(1);
  }

  for (i=0 ; i < forks ; i++)
    while ((wait_ret=wait(&status)) && wait_ret == -1);
  return 0;
}
Пример #25
0
int
main(int argc __attribute__((unused)), char** argv)
{
	char	server_key[] = "../SSL/server-key.pem",
		server_cert[] = "../SSL/server-cert.pem";
	char	ca_file[] = "../SSL/cacert.pem",
		*ca_path = 0,
		*cipher = 0;
	struct	st_VioSSLFd*	ssl_acceptor;
	pthread_t	th;
	TH_ARGS		th_args;


	struct sockaddr_in sa_serv;
	struct sockaddr_in sa_cli;
	int listen_sd;
	int err;
        size_socket client_len;
	int	reuseaddr = 1; /* better testing, uh? */

	MY_INIT(argv[0]);
        DBUG_PROCESS(argv[0]);
        DBUG_PUSH(default_dbug_option);

	printf("Server key/cert : %s/%s\n", server_key, server_cert);
	if (ca_file!=0)

		printf("CAfile          : %s\n", ca_file);
	if (ca_path!=0)
		printf("CApath          : %s\n", ca_path);

        th_args.ssl_acceptor = ssl_acceptor = new_VioSSLAcceptorFd(server_key, server_cert, ca_file, ca_path,cipher);

	/* ----------------------------------------------- */
	/* Prepare TCP socket for receiving connections */

	listen_sd = socket (AF_INET, SOCK_STREAM, 0);
	setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(&reuseaddr));

	memset (&sa_serv, '\0', sizeof(sa_serv));
	sa_serv.sin_family      = AF_INET;
	sa_serv.sin_addr.s_addr = INADDR_ANY;
	sa_serv.sin_port        = htons (1111);          /* Server Port number */

	err = bind(listen_sd, (struct sockaddr*) &sa_serv,
	     sizeof (sa_serv));                  

	/* Receive a TCP connection. */

	err = listen (listen_sd, 5); 
	client_len = sizeof(sa_cli);
	th_args.sd = accept (listen_sd, (struct sockaddr*) &sa_cli, &client_len);
	close (listen_sd);

	printf ("Connection from %lx, port %x\n",
		  (long)sa_cli.sin_addr.s_addr, sa_cli.sin_port);

	/* ----------------------------------------------- */
	/* TCP connection is ready. Do server side SSL. */

	err = pthread_create(&th, NULL, client_thread, (void*)&th_args);
	DBUG_PRINT("info", ("pthread_create: %d", err));
	pthread_join(th, NULL);

#if 0
	if (err<=0) {
		my_free(ssl_acceptor);
		fatal_error("server:SSL_write");
	}
#endif /* 0 */

	my_free(ssl_acceptor);
	return 0;
}
Пример #26
0
int main(int argc,char *argv[])
{
  int error=0, subkeys;
  uint keylen, keylen2=0, inx, doc_cnt=0;
  float weight= 1.0;
  double gws, min_gws=0, avg_gws=0;
  MI_INFO *info;
  char buf[MAX_LEN], buf2[MAX_LEN], buf_maxlen[MAX_LEN], buf_min_gws[MAX_LEN];
  ulong total=0, maxlen=0, uniq=0, max_doc_cnt=0;
  struct { MI_INFO *info; } aio0, *aio=&aio0; /* for GWS_IN_USE */

  MY_INIT(argv[0]);
  if ((error= handle_options(&argc, &argv, my_long_options, get_one_option)))
    exit(error);
  if (count || dump)
    verbose=0;
  if (!count && !dump && !lstats && !query)
    stats=1;

  if (verbose)
    setbuf(stdout,NULL);

  if (argc < 2)
    usage();

  {
    char *end;
    inx= (uint) strtoll(argv[1], &end, 10);
    if (*end)
      usage();
  }

  init_key_cache(dflt_key_cache,MI_KEY_BLOCK_LENGTH,USE_BUFFER_INIT, 0, 0);

  if (!(info=mi_open(argv[0], O_RDONLY,
                     HA_OPEN_ABORT_IF_LOCKED|HA_OPEN_FROM_SQL_LAYER)))
  {
    error=my_errno;
    goto err;
  }

  *buf2=0;
  aio->info=info;

  if ((inx >= info->s->base.keys) ||
      !(info->s->keyinfo[inx].flag & HA_FULLTEXT))
  {
    printf("Key %d in table %s is not a FULLTEXT key\n", inx, info->filename);
    goto err;
  }

  mi_lock_database(info, F_EXTRA_LCK);

  info->lastpos= HA_OFFSET_ERROR;
  info->update|= HA_STATE_PREV_FOUND;

  while (!(error=mi_rnext(info,NULL,inx)))
  {
    keylen=*(info->lastkey);

    subkeys=ft_sintXkorr(info->lastkey+keylen+1);
    if (subkeys >= 0)
      ft_floatXget(weight, info->lastkey+keylen+1);

#ifdef HAVE_SNPRINTF
    snprintf(buf,MAX_LEN,"%.*s",(int) keylen,info->lastkey+1);
#else
    sprintf(buf,"%.*s",(int) keylen,info->lastkey+1);
#endif
    my_casedn_str(default_charset_info,buf);
    total++;
    lengths[keylen]++;

    if (count || stats)
    {
      if (strcmp(buf, buf2))
      {
        if (*buf2)
        {
          uniq++;
          avg_gws+=gws=GWS_IN_USE;
          if (count)
            printf("%9u %20.7f %s\n",doc_cnt,gws,buf2);
          if (maxlen<keylen2)
          {
            maxlen=keylen2;
            strmov(buf_maxlen, buf2);
          }
          if (max_doc_cnt < doc_cnt)
          {
            max_doc_cnt=doc_cnt;
            strmov(buf_min_gws, buf2);
            min_gws=gws;
          }
        }
        strmov(buf2, buf);
        keylen2=keylen;
        doc_cnt=0;
      }
      doc_cnt+= (subkeys >= 0 ? 1 : -subkeys);
    }
    if (dump)
    {
      if (subkeys>=0)
        printf("%9lx %20.7f %s\n", (long) info->lastpos,weight,buf);
      else
        printf("%9lx => %17d %s\n",(long) info->lastpos,-subkeys,buf);
    }
    if (verbose && (total%HOW_OFTEN_TO_WRITE)==0)
      printf("%10ld\r",total);
  }
  mi_lock_database(info, F_UNLCK);

  if (count || stats)
  {
    if (*buf2)
    {
      uniq++;
      avg_gws+=gws=GWS_IN_USE;
      if (count)
        printf("%9u %20.7f %s\n",doc_cnt,gws,buf2);
      if (maxlen<keylen2)
      {
        maxlen=keylen2;
        strmov(buf_maxlen, buf2);
      }
      if (max_doc_cnt < doc_cnt)
      {
        max_doc_cnt=doc_cnt;
        strmov(buf_min_gws, buf2);
        min_gws=gws;
      }
    }
  }

  if (stats)
  {
    count=0;
    for (inx=0;inx<256;inx++)
    {
      count+=lengths[inx];
      if ((ulong) count >= total/2)
        break;
    }
    printf("Total rows: %lu\nTotal words: %lu\n"
           "Unique words: %lu\nLongest word: %lu chars (%s)\n"
           "Median length: %u\n"
           "Average global weight: %f\n"
           "Most common word: %lu times, weight: %f (%s)\n",
           (long) info->state->records, total, uniq, maxlen, buf_maxlen,
           inx, avg_gws/uniq, max_doc_cnt, min_gws, buf_min_gws);
  }
  if (lstats)
  {
    count=0;
    for (inx=0; inx<256; inx++)
    {
      count+=lengths[inx];
      if (count && lengths[inx])
        printf("%3u: %10lu %5.2f%% %20lu %4.1f%%\n", inx,
               (ulong) lengths[inx],100.0*lengths[inx]/total,(ulong) count,
               100.0*count/total);
    }
  }

err:
  if (error && error != HA_ERR_END_OF_FILE)
    printf("got error %d\n",my_errno);
  if (info)
    mi_close(info);
  return 0;
}
Пример #27
0
int main(int argc,char **argv)
{
  int status,wait_ret;
  uint i=0;
  MARIA_KEYDEF keyinfo[10];
  MARIA_COLUMNDEF recinfo[10];
  HA_KEYSEG keyseg[10][2];
  MY_INIT(argv[0]);
  get_options(argc,argv);

  fprintf(stderr, "WARNING! this program is to test 'external locking'"
          " (when several processes share a table through file locking)"
          " which is not supported by Maria at all; expect errors."
          " We may soon remove this program.\n");
  maria_init();
  bzero((char*) keyinfo,sizeof(keyinfo));
  bzero((char*) recinfo,sizeof(recinfo));
  bzero((char*) keyseg,sizeof(keyseg));
  keyinfo[0].seg= &keyseg[0][0];
  keyinfo[0].seg[0].start=0;
  keyinfo[0].seg[0].length=8;
  keyinfo[0].seg[0].type=HA_KEYTYPE_TEXT;
  keyinfo[0].seg[0].flag=HA_SPACE_PACK;
  keyinfo[0].key_alg=HA_KEY_ALG_BTREE;
  keyinfo[0].keysegs=1;
  keyinfo[0].flag = (uint8) HA_PACK_KEY;
  keyinfo[0].block_length= 0;                   /* Default block length */
  keyinfo[1].seg= &keyseg[1][0];
  keyinfo[1].seg[0].start=8;
  keyinfo[1].seg[0].length=4;		/* Long is always 4 in maria */
  keyinfo[1].seg[0].type=HA_KEYTYPE_LONG_INT;
  keyinfo[1].seg[0].flag=0;
  keyinfo[1].key_alg=HA_KEY_ALG_BTREE;
  keyinfo[1].keysegs=1;
  keyinfo[1].flag =HA_NOSAME;
  keyinfo[1].block_length= 0;                   /* Default block length */

  recinfo[0].type=0;
  recinfo[0].length=sizeof(record.id);
  recinfo[1].type=0;
  recinfo[1].length=sizeof(record.nr);
  recinfo[2].type=0;
  recinfo[2].length=sizeof(record.text);

  puts("- Creating maria-file");
  my_delete(filename,MYF(0));		/* Remove old locks under gdb */
  if (maria_create(filename,BLOCK_RECORD, 2, &keyinfo[0],2,&recinfo[0],0,
                   (MARIA_UNIQUEDEF*) 0, (MARIA_CREATE_INFO*) 0,0))
    exit(1);

  rnd_init(0);
  printf("- Starting %d processes\n",forks); fflush(stdout);
  for (i=0 ; i < forks; i++)
  {
    if (!fork())
    {
      start_test(i+1);
      sleep(1);
      return 0;
    }
    rnd(1);
  }

  for (i=0 ; i < forks ; i++)
    while ((wait_ret=wait(&status)) && wait_ret == -1);
  maria_end();
  return 0;
}