phpredis icon indicating copy to clipboard operation
phpredis copied to clipboard

Got problems on hGetAll() function

Open Jerry-Shaw opened this issue 9 years ago • 25 comments

Hi, all. My first time here.

I've found a problem on the function: hGetAll() of the windows build for php7 from Jan-E, downloaded from Apache Lounge yesterday.

Here are the examples:

$redis->delete('h'); $redis->hSet('h', 'a', 'x'); $redis->hSet('h', 'b', 'y'); $redis->hSet('h', 'c', 'z'); $redis->hSet('h', 'd', 't');

When using $redis->hGetAll('h'), the result should be as follows:

array(4) { ["a"]=> string(1) "x" ["b"]=> string(1) "y" ["c"]=> string(1) "z" ["d"]=> string(1) "t" }

But, I got the a wrong result as follows:

array(7) { ["a"]=> string(1) "x" ["x"]=> string(1) "b" ["b"]=> string(1) "y" ["y"]=> string(1) "c" ["c"]=> string(1) "z" ["z"]=> string(1) "d" ["d"]=> string(1) "t" }

I don't know how to leave a message to Jan-E, but, it leads me here. And thanks.

Jerry-Shaw avatar Jul 07 '15 03:07 Jerry-Shaw

Are you sure about the 'array(4)' in the wrong result? After all, the wrong array has 7 key-value pairs, so that should be 'array(7)'. I get this result with your test script:

array(7) { ["a "]=> string(1) "x" ["x "]=> string(1) "b" ["b "]=> string(1) "y" ["y "]=> string(1) "c" ["c "]=> string(1) "z" ["z "]=> string(1) "d" ["d "]=> string(1) "t" }.

I am using edtechd's sources, so you asked the question at the right place. @edtechd: try this script with some additional commands:

print_r($redis->hGetAll('h'));
print_r($redis->hKeys('h'));
print_r($redis->hVals('h'));

It should return

Array
(
    [a] => x
    [b] => y
    [c] => z
    [d] => t
)
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Array
(
    [0] => x
    [1] => y
    [2] => z
    [3] => t
)

But it does return:

Array
(
    [a ] => x
    [x ] => b
    [b ] => y
    [y ] => c
    [c ] => z
    [z ] => d
    [d ] => t
)
nothing (NULL)
nothing (NULL)

Somewhere in the lot of patches an error was made, most likely in the REDIS_PROCESS_KW_CMD macro or the underlying macro's.

Jan-E avatar Jul 08 '15 04:07 Jan-E

Sorry, Jan-E.

I just copied the content of the result array, forgot to change the 'array(4)' to 'array(7)' in the email copy.

Yep, you got the wrong result now, mine was just like what you've posted.

OK, I made my post corrected.

Jerry-Shaw avatar Jul 08 '15 04:07 Jerry-Shaw

I think that hGetAll works correctly according to the documentation http://redis.io/commands/hgetall It provides the same output as Redis.

And I will check about hKeys and hVals

edtechd avatar Jul 15 '15 21:07 edtechd

If the output does like that, it does make HUGE differences with the latest version in PECL here (http://pecl.php.net/package/redis) for PHP5. The result should not be logical if it gets all the fields and values BUT mapping them from one to another in a way like this.

I've seen the documentation, and test it in redis-cli, it outputs as follows:

Local Redis:2>select 14 OK

Local Redis:4>hset key a x 1

Local Redis:4>hset key b y 1

Local Redis:4>hset key c z 1

Local Redis:4>hset key d t 1

Local Redis:4>hgetall key a x b y c z d t

It gives us a list containing the field names with the values behind each one, but no phenomenon shows that, the other names are associated to the values of the previous fields before. The right format of the result should be like:

Local Redis:4>hgetall key a x

b y

c z

d t

Because it gives us a single list, not an array.

How can we use it if it is like:

Local Redis:4>hgetall key a x

x b

b y

y c

c z

z d

d t

those results of " x -> b, y->c, z->d " are useless at all. We actually don't know every values in the programs, we knows the keys.

By the way, it costs twice memory if it does so.

Just think about it @edtechd & @Jan-E , and test the one from pecl, find out the right type of the result a php programmer should be using.

Thanks, guys.

Jerry-Shaw avatar Jul 16 '15 02:07 Jerry-Shaw

@edtechd My builds do contain your latest repo now. But I am still getting the same problem as in https://github.com/edtechd/phpredis/issues/5#issue-93431556 and https://github.com/edtechd/phpredis/issues/5#issuecomment-119427596

Jan-E avatar Jul 16 '15 10:07 Jan-E

My fork https://github.com/customwebapps/phpredis, based on this code, had an issue with null terminators being in the key (off by 1 errors). I've made some fixes in my fork but not sure if I caught all of them. However my branch is working stably with PHP7b1

neuroscr avatar Jul 21 '15 06:07 neuroscr

@neuroscr Good to see that. Seems like you've fixed the hGetAll function, but by the way, does hMGet work good? I mean, my post issue #10 .

Jerry-Shaw avatar Jul 21 '15 07:07 Jerry-Shaw

Yes, I can confirm hMGet is a function I'm actively using and it is working as expected

neuroscr avatar Jul 21 '15 07:07 neuroscr

Hi, @Jan-E , I'm going to compile the php_redis,dll binary from @neuroscr , I can't wait for it. I'll be keeping tracking this.

Thanks @neuroscr , @Jan-E & @edtechd , & all.

Jerry-Shaw avatar Jul 21 '15 07:07 Jerry-Shaw

T_T , failed to compile it in vs 2015...

Jerry-Shaw avatar Jul 21 '15 11:07 Jerry-Shaw

Could anyone help compiling me a windows binary of php_redis.dll from @neuroscr? I can't get it though, don't know C at all. And thanks a lot.

Jerry-Shaw avatar Jul 21 '15 13:07 Jerry-Shaw

Try applying these two patches: https://github.com/edtechd/phpredis/commit/fd425df67021351adc4b9958a22b13cadfe9211f https://github.com/edtechd/phpredis/commit/355a6aa93ce28e3f721d384011425bc2a3b8b574

Jan-E avatar Jul 21 '15 13:07 Jan-E

OK, got your message, thanks Jan. I'll try it now.

Jerry-Shaw avatar Jul 21 '15 13:07 Jerry-Shaw

And/or maybe these 2: https://github.com/edtechd/phpredis/commit/7c649a69460bae07b2e933b44a8fcbc580515fa8 https://github.com/edtechd/phpredis/commit/16554bb5e7c81bff720adcf26fd0fab0ab9894a2

Jan-E avatar Jul 21 '15 13:07 Jan-E

@Jan-E , almost done, but met an error at the very end of redis_serialize. Still missing patches?

Creating library E:\php\src\phpdev\vc11\x64\src\x64\Release\php7.lib and obje ct E:\php\src\phpdev\vc11\x64\src\x64\Release\php7.exp rc /n /fo E:\php\src\phpdev\vc11\x64\src\x64\Release\php_redis.dll.res / d FILE_DESCRIPTION=""Redis client extension for PHP"" /d FILE_NAME=""php_redi s.dll"" /d URL=""http://pecl.php.net/redis"" /d INTERNAL_NAME=""REDIS extens ion"" /d EXT_FILE_VERSION=2,2,5 /d EXT_VERSION=""2.2.5"" /d THANKS_GUYS=""Th anks to Alfonso Jimenez, Nasreddine Bouafif, Nicolas Favre-Felix, Michael Grunde r"" win32\build\template.rc Microsoft (R) Windows (R) Resource Compiler Version 6.3.9600.17336 Copyright (C) Microsoft Corporation. All rights reserved.

Creating library E:\php\src\phpdev\vc11\x64\src\x64\Release\php_redis.lib and object E:\php\src\phpdev\vc11\x64\src\x64\Release\php_redis.exp library.obj : error LNK2019: unresolved external symbol basic_globals referenced in function redis_serialize E:\php\src\phpdev\vc11\x64\src\x64\Release\php_redis.dll : fatal error LNK1120: 1 unresolved externals NMAKE : fatal error U1077: 'E:\a\VC\BIN\amd64\link.exe' : return code '0x460' Stop.

Jerry-Shaw avatar Jul 21 '15 14:07 Jerry-Shaw

Yes, probably the second two: https://github.com/edtechd/phpredis/issues/5#issuecomment-123309989

Jan-E avatar Jul 21 '15 14:07 Jan-E

Essential part: change every PHPAPI in PHP_REDIS_API

Jan-E avatar Jul 21 '15 14:07 Jan-E

Yes, I have noticed that when I saw the first patches. I am making another try now.

Jerry-Shaw avatar Jul 21 '15 14:07 Jerry-Shaw

It stopped at the very beginning if only using the second two patches.

pecl\redis\redis_commands.c(2509): warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data pecl\redis\redis_commands.c(2693): warning C4267: 'function': conversion from 's ize_t' to 'int', possible loss of data pecl\redis\redis_commands.c(2697): warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data pecl\redis\redis_commands.c(2788): warning C4244: '=': conversion from 'time_t' to 'long', possible loss of data pecl\redis\redis_commands.c(2810): warning C4244: '=': conversion from 'long' to 'short', possible loss of data NMAKE : fatal error U1077: 'E:\a\VC\BIN\amd64\cl.exe' : return code '0x2' Stop.

Jerry-Shaw avatar Jul 21 '15 14:07 Jerry-Shaw

OK, it need this one 355a6aa, the redis.c part. I'll try it again.

Jerry-Shaw avatar Jul 21 '15 14:07 Jerry-Shaw

OK, I got the windows binary now, and have it test.

Still, it has some problem and fixed some issues. hMGet works OK here, which is good.

$redis->hMset('a', array( 'a'=>'x', 'b'=>'y', 'c'=>'z', 'd'=>'t', ));

var_dump($redis->hGetAll('a')); // got confused on the returns (which format is right???) var_dump($redis->hMGet('a', array('a','b'))); // works OK var_dump($redis->hKeys('a')); // works OK, too var_dump($redis->hVals('a')); // still get null

array(7) { ["a"]=> string(1) "x" ["x"]=> string(1) "b" ["b"]=> string(1) "y" ["y"]=> string(1) "c" ["c"]=> string(1) "z" ["z"]=> string(1) "d" ["d"]=> string(1) "t" }

array(2) { ["a"]=> string(1) "x" ["b"]=> string(1) "y" }

array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" }

NULL

Thanks, @Jan-E , @neuroscr , @edtechd , report over, you may need to do a lot, and special thanks to @Jan-E

Jerry-Shaw avatar Jul 21 '15 14:07 Jerry-Shaw

I have a poor method of dealing with hGetAll result now, count all the even number values out into the result, give up all the odd ones. Added few lines and fixed this.

Jerry-Shaw avatar Jul 21 '15 16:07 Jerry-Shaw

Hi, @neuroscr I cloned yours codes of phpredis, and made it possible to be compiled in vs 2015 with the help from @Jan-E . I have made a little changes in library.c of the position you last edited, fixed the hGetAll result format using a bad method (Sorry, don't know C at all). Still the hVals always gets NULL, which I can do nothing about it. If you guys have new ones, I'm happy to test. Cloned url here: https://github.com/Jerry-Shaw/phpredis Thanks

Jerry-Shaw avatar Jul 24 '15 07:07 Jerry-Shaw

Could you test this once again after the changes @laruence has made?

Jan-E avatar Aug 09 '15 13:08 Jan-E

Waiting for his final fix, glad to test.

Jerry-Shaw avatar Aug 09 '15 13:08 Jerry-Shaw