how to push a struct data into redis?

497 views
Skip to first unread message

oscar

unread,
Nov 2, 2010, 1:01:54 PM11/2/10
to Redis DB
Hi all, I want to push a struct data into redis. But it seems that
redis don't support push a struct data into binary, is there any way
to push a struct data into redis?

Demis Bellot

unread,
Nov 2, 2010, 1:14:34 PM11/2/10
to redi...@googlegroups.com
I think the norm is to serialize to json and treat like a string.

- Demis



--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.


Jonathan Leibiusky

unread,
Nov 2, 2010, 1:38:13 PM11/2/10
to redi...@googlegroups.com
Or you could use Ohm (if you are using ruby) http://github.com/soveran/ohm
or JOhm (if you are in java) http://github.com/xetorthio/johm

Demis Bellot

unread,
Nov 2, 2010, 1:43:01 PM11/2/10
to redi...@googlegroups.com
Yeah I should've also mentioned that as well :) 

If you're using .NET/MONO you can use the typed interface on ServiceStack's C# RedisClient which will give you strongly-typed generic interface that does this for you:


The JSON Serializer used is super fast so its still a win-win!

- Demis

oscar

unread,
Nov 3, 2010, 4:55:27 AM11/3/10
to Redis DB
thank you all,
I'm working in C, and in my application I can't use JSON to deal with
the struct data, and it's hard to convert struct to JSON.
So push a binary data into list is the best way for me now.
Any help is appreciate.

On Nov 3, 1:43 am, Demis Bellot <demis.bel...@gmail.com> wrote:
> Yeah I should've also mentioned that as well :)
>
> If you're using .NET/MONO you can use the typed interface on ServiceStack's
> C# RedisClient which will give you strongly-typed generic interface that
> does this for you:
>
> http://code.google.com/p/servicestack/wiki/ServiceStackRedishttp://code.google.com/p/servicestack/wiki/IRedisTypedClient
>
> The JSON Serializer used is super fast so its still a win-win!http://www.servicestack.net/mythz_blog/?p=344
>
> - Demis
>
> On Tue, Nov 2, 2010 at 5:38 PM, Jonathan Leibiusky <ionat...@gmail.com>wrote:
>
>
>
> > Or you could use Ohm (if you are using ruby)http://github.com/soveran/ohm
> > or JOhm (if you are in java)http://github.com/xetorthio/johm
>
> > On Tue, Nov 2, 2010 at 2:14 PM, Demis Bellot <demis.bel...@gmail.com>wrote:
>
> >> I think the norm is to serialize to json and treat like a string.
>
> >> - Demis
>
> >> On Tue, Nov 2, 2010 at 5:01 PM, oscar <liyuan...@gmail.com> wrote:
>
> >>> Hi all, I want to push a struct data into redis. But it seems that
> >>> redis don't support push a struct data into binary, is there any way
> >>> to push a struct data into redis?
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "Redis DB" group.
> >>> To post to this group, send email to redi...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> redis-db+u...@googlegroups.com<redis-db%2Bunsubscribe@googlegroups.c­om>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/redis-db?hl=en.
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "Redis DB" group.
> >> To post to this group, send email to redi...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> redis-db+u...@googlegroups.com<redis-db%2Bunsubscribe@googlegroups.c­om>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/redis-db?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "Redis DB" group.
> > To post to this group, send email to redi...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > redis-db+u...@googlegroups.com<redis-db%2Bunsubscribe@googlegroups.c­om>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/redis-db?hl=en.- Hide quoted text -
>
> - Show quoted text -

Pieter Noordhuis

unread,
Nov 3, 2010, 5:28:51 AM11/3/10
to redi...@googlegroups.com
It's trivial: just memcpy the struct into a buffer (of size sizeof(yourstruct)+1) and terminate it with a NULL. Because the Redis protocol is binary-safe you can use anything for arguments. It depends on the client you use what you need to do, but something similar should work when the client supports the recent Redis protocol. This only works if all your clients share the same architecture, otherwise this will break very hard.

Cheers,
Pieter

To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.

oscar

unread,
Nov 3, 2010, 6:53:12 AM11/3/10
to Redis DB
Pieter Noordhuis, thank you very much.
My struct is like this:
Struct test
{
char a[100];
char b[100];
}
There is a NULL in array a. Is it safe to copy to a buf with memcpy?
Is the hiredis or credis support this method?

On Nov 3, 5:28 pm, Pieter Noordhuis <pcnoordh...@gmail.com> wrote:
> It's trivial: just memcpy the struct into a buffer (of size
> sizeof(yourstruct)+1) and terminate it with a NULL. Because the Redis
> protocol is binary-safe you can use anything for arguments. It depends on
> the client you use what you need to do, but something similar should work
> when the client supports the recent Redis protocol. This only works if all
> your clients share the same architecture, otherwise this will break very
> hard.
>
> Cheers,
> Pieter
>
>
>
> On Wed, Nov 3, 2010 at 9:55 AM, oscar <liyuan...@gmail.com> wrote:
> > thank you all,
> > I'm working in C, and in my application I can't use JSON to deal with
> > the struct data, and it's hard to convert struct to JSON.
> > So push a binary data into list is the best way for me now.
> > Any help is appreciate.
>
> > On Nov 3, 1:43 am, Demis Bellot <demis.bel...@gmail.com> wrote:
> > > Yeah I should've also mentioned that as well :)
>
> > > If you're using .NET/MONO you can use the typed interface on
> > ServiceStack's
> > > C# RedisClient which will give you strongly-typed generic interface that
> > > does this for you:
>
> >http://code.google.com/p/servicestack/wiki/ServiceStackRedishttp://co...
>
> > > The JSON Serializer used is super fast so its still a win-win!
> >http://www.servicestack.net/mythz_blog/?p=344
>
> > > - Demis
>
> > > On Tue, Nov 2, 2010 at 5:38 PM, Jonathan Leibiusky <ionat...@gmail.com
> > >wrote:
>
> > > > Or you could use Ohm (if you are using ruby)
> >http://github.com/soveran/ohm
> > > > or JOhm (if you are in java)http://github.com/xetorthio/johm
>
> > > > On Tue, Nov 2, 2010 at 2:14 PM, Demis Bellot <demis.bel...@gmail.com
> > >wrote:
>
> > > >> I think the norm is to serialize to json and treat like a string.
>
> > > >> - Demis
>
> > > >> On Tue, Nov 2, 2010 at 5:01 PM, oscar <liyuan...@gmail.com> wrote:
>
> > > >>> Hi all, I want to push a struct data into redis. But it seems that
> > > >>> redis don't support push a struct data into binary, is there any way
> > > >>> to push a struct data into redis?
>
> > > >>> --
> > > >>> You received this message because you are subscribed to the Google
> > Groups
> > > >>> "Redis DB" group.
> > > >>> To post to this group, send email to redi...@googlegroups.com.
> > > >>> To unsubscribe from this group, send email to
> > > >>> redis-db+u...@googlegroups.com<redis-db%2Bunsubscribe@googlegroups.c­om>
> > <redis-db%2Bunsubscr...@googlegroups.c­om>
> > > >>> .
> > > >>> For more options, visit this group at
> > > >>>http://groups.google.com/group/redis-db?hl=en.
>
> > > >>  --
> > > >> You received this message because you are subscribed to the Google
> > Groups
> > > >> "Redis DB" group.
> > > >> To post to this group, send email to redi...@googlegroups.com.
> > > >> To unsubscribe from this group, send email to
> > > >> redis-db+u...@googlegroups.com<redis-db%2Bunsubscribe@googlegroups.c­om>
> > <redis-db%2Bunsubscr...@googlegroups.c­om>
> > > >> .
> > > >> For more options, visit this group at
> > > >>http://groups.google.com/group/redis-db?hl=en.
>
> > > >  --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "Redis DB" group.
> > > > To post to this group, send email to redi...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > redis-db+u...@googlegroups.com<redis-db%2Bunsubscribe@googlegroups.c­om>
> > <redis-db%2Bunsubscr...@googlegroups.c­om>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/redis-db?hl=en.-Hide quoted text -

paulino huerta

unread,
Nov 4, 2010, 12:10:39 PM11/4/10
to redi...@googlegroups.com
Hi oscar

I saw that the client library "credis" to get the amount of bytes to push into the server uses the strlen function, this means you can't apply the method indicated by Pieter, IMHO :)

--Paulino

2010/11/3 oscar <liyu...@gmail.com>
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.

Andy Lawman

unread,
Nov 4, 2010, 12:37:36 PM11/4/10
to redi...@googlegroups.com
If so, and JSON isn't an option, then try:
 
char buffer[sizeof(struct test) * 2 + 1];
struct test *pt = &my_test;
char *pc = (char *) ((void *) pt);

int i;
for (i = 0; i < sizeof(struct test); i++ )
{
    sprintf(buffer + i, "%2.2x", *pc++);
}

*(buffer + 2*sizeof(struct test)) = '\0';



to convert each byte in to the printable hexadecimal equivalent ie 'c' => "63" or 0x63 => 0x3633. Or more importantly '\0' => "00" or 0x00 to 0x3030. There is considerable scope for optimisation here eg: sprintf(buffer + i, "%2.2x%2.2x%2.2x%2.2x", *pc++, *pc++, *pc++, *pc++); etc especially if you know the length of the fields in struct. And in C, you do.

Use scanf() to reverse the conversion.

Andy.





IMPORTANT - CONFIDENTIALITY NOTICE - This e-mail is intended only for the use of the addressee/s above.  It may contain information which is privileged, confidential or otherwise protected from disclosure under applicable laws.  If the reader of this transmission is not the intended recipient, you are hereby notified that any dissemination, printing, distribution, copying, disclosure or the taking of any action in reliance on the contents of this information is strictly prohibited.  If you have received this transmission in error, please immediately notify us by reply e-mail or using the address below and delete the message and any attachments from your system.

Amadeus Services Ltd, World Business Centre 3, 1208 Newall Road, Hounslow, Middlesex, TW6 2TA, Registered number 4040059

Mike Shaver

unread,
Nov 4, 2010, 12:41:40 PM11/4/10
to redi...@googlegroups.com
On Thu, Nov 4, 2010 at 9:37 AM, Andy Lawman <ALa...@amadeus.com> wrote:
> to convert each byte in to the printable hexadecimal equivalent

can't redis handle raw binary data, if that's what you're going to do?

Mike

Salvatore Sanfilippo

unread,
Nov 4, 2010, 12:45:57 PM11/4/10
to redi...@googlegroups.com

Exactly, the whole thread is more or less a non-issue ;)

It's just a matter of figuring how, from C, you want to
serialize-deserialize data, if you want cross-platform portability.
Otherwise as Pieter suggested just copy the whole struct as a binary blob.

It's the same C code needed to copy the structure into a file.

Cheers,
Salvatore

> Mike


>
> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To post to this group, send email to redi...@googlegroups.com.
> To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.
>
>

--
Salvatore 'antirez' Sanfilippo
http://invece.org

"We are what we repeatedly do. Excellence, therefore, is not an act,
but a habit." -- Aristotele

Andy Lawman

unread,
Nov 4, 2010, 12:56:00 PM11/4/10
to redi...@googlegroups.com
redis can, but the client he's using can't judging by the earlier post about strlen(). This looks for 0x00 and assumes that it terminates the string.

Andy.



From:        Mike Shaver <mike....@gmail.com>
To:        redi...@googlegroups.com
Date:        04/11/2010 16:42
Subject:        Re: how to push a struct data into redis?
Sent by:        redi...@googlegroups.com




--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/redis-db?hl=en.

Andy Lawman

unread,
Nov 4, 2010, 1:08:27 PM11/4/10
to redi...@googlegroups.com
+1 now I've browsed the credis code.

Andy.


From:        paulino huerta <paulin...@gmail.com>

Salvatore Sanfilippo

unread,
Nov 4, 2010, 1:19:06 PM11/4/10
to redi...@googlegroups.com
On Thu, Nov 4, 2010 at 6:08 PM, Andy Lawman <ALa...@amadeus.com> wrote:
> +1 now I've browsed the credis code.
>
> Andy.

Shameless plug: we have an official C client for Redis, hiredis, and
Pieter did some impressive work in making it a serious thing with
support for the whole command set, pipelining, event-driven model, and
so forth.

Cheers,
Salvatore

--

Reply all
Reply to author
Forward
0 new messages