Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Efficient way to store text with only 0s and 1s

    I need to store a huge string made of only 0s and 1s.
    Ex: 0000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000 0000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000 0000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000 00000000000000000000000010000000000
    ...

    Is there another alternative than using BLOB/MEDIUMBLOB to do so? I don`t need any regular letter or numbers.

    Thanks

    EDIT: Storing as a string doesn`t work because it converts my 0s and 1s into their equivalent in base 2...
    Last edited by IdkWhatsRc; 06-19-2013 at 05:26 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Quote Originally Posted by IdkWhatsRc View Post
    Storing as a string doesn`t work because it converts my 0s and 1s into their equivalent in base 2...
    WHAT????

    Storing as a string converts them into *CHARACTERS* "1" and "0" and that has nothing whatsoever to do with *ANY* numeric base.

    Storing them as BITs (e.g., in a BINARY field) does indeed convert to base 2. But so what? Why does that bother you?

    Not to ask a dumbassed question: What in the world is this *FOR*? I'm betting there's a better solution, all around.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    WHAT????

    Storing as a string converts them into *CHARACTERS* "1" and "0" and that has nothing whatsoever to do with *ANY* numeric base.

    Storing them as BITs (e.g., in a BINARY field) does indeed convert to base 2. But so what? Why does that bother you?

    Not to ask a dumbassed question: What in the world is this *FOR*? I'm betting there's a better solution, all around.
    The list of 0s and 1s represents a map grid. 0 = can move, 1 = can't walk.
    I want to be able to do something like:
    if(grid[player.position] == 1)
    { collision() }

    Problem with BINARY is that the maximum size in MySQL is too small to store all my grid.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    What about VARBINARY? That can be up to 64KB in size.

    But I don't see what is wrong with VARCHAR or even TEXT.

    So what if the data is stored as a string?

    So you read that string back into JS and instead of
    Code:
    if(grid[player.position] == 1)
    you just use
    Code:
    if( grid.charAt(player.position) == "1")
    Or, for that matter, for all browsers except some ancient ones, you *can* treat a string as an array of characters, so you would just use
    Code:
    if(grid[player.position] == "1")
    Or, as a minor hack, you could do:
    Code:
    var grid = ...string you get from the database ...
    grid = grid.split(""); // converts the string into an array!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    IdkWhatsRc (06-20-2013)

  • #5
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Okay. Solution found. I'm using COMPRESS() and UNCOMPRESS() built-in functions of MySql.

    Code:
    client.query('select uncompress(grid) from map where id=3',function(err, results) { 	
    	console.log(   results[0]['uncompress(grid)'].toString()[player.position]  ); //0 or 1
    });
    I'm converting my 9216 of length string of 0s and 1s into a 629 of length compressed blob.
    Last edited by IdkWhatsRc; 06-20-2013 at 01:01 AM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Why do you think that is "efficient"???

    It takes *TIME* to compress and uncompress.

    Unless your DB table is huge, who cares about saving a lousy 8600 bytes or so per record????

    Say you had 10,000 people playing the game at the same time, you'd be saving 86,000,000 bytes of disk space. Under 100 megabytes. On a disk that probably has hundreds of *GIGABYTES* of storage. Whoopee.

    Most of the time, trading disk space for cpu time is a *good* tradeoff.

    Besides, you will *STILL* be sending all 9216 bytes of data back and forth from JavaScript to the server. And *THAT* will be a bigger performance hit than anything. If you wanted real efficiency, you'd do the compress and uncompress in the JavaScript code.

    Which would be pretty trivial to do.

    Heck, you could do an 8-to-1 compression just by using the bits in a string of bytes instead of using entire bytes.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Why do you think that is "efficient"???

    It takes *TIME* to compress and uncompress.

    Unless your DB table is huge, who cares about saving a lousy 8600 bytes or so per record????

    Say you had 10,000 people playing the game at the same time, you'd be saving 86,000,000 bytes of disk space. Under 100 megabytes. On a disk that probably has hundreds of *GIGABYTES* of storage. Whoopee.

    Most of the time, trading disk space for cpu time is a *good* tradeoff.

    Besides, you will *STILL* be sending all 9216 bytes of data back and forth from JavaScript to the server. And *THAT* will be a bigger performance hit than anything. If you wanted real efficiency, you'd do the compress and uncompress in the JavaScript code.

    Which would be pretty trivial to do.

    Heck, you could do an 8-to-1 compression just by using the bits in a string of bytes instead of using entire bytes.
    Actually, I'm only 'loading' the maps once for all the players when the server is initialized. All the players are using the same maps collision grid.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    My head hurts. So then why does the map need to be in a database table?

    Or will you have several possible maps?

    And why would you only use an on/off indicator? Why wouldn't you do something like
    Code:
    var map = [
        ["ffffwwwwCCCC"],
        ["ffwwwgggCppC"],
        ...
    ];
    where each letter is used to indicate what is there on the map? (e.g., f==forest, w==water, C==castle, g==grass, p==path).

    And make it a 2D array, instead of just 1D, so the you can easily reference it via map[playerYposition][playerXposition]??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    IdkWhatsRc (06-20-2013)

  • #9
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    My head hurts. So then why does the map need to be in a database table?

    Or will you have several possible maps?

    And why would you only use an on/off indicator? Why wouldn't you do something like
    Code:
    var map = [
        ["ffffwwwwCCCC"],
        ["ffwwwgggCppC"],
        ...
    ];
    where each letter is used to indicate what is there on the map? (e.g., f==forest, w==water, C==castle, g==grass, p==path).

    And make it a 2D array, instead of just 1D, so the you can easily reference it via map[playerYposition][playerXposition]??
    That's what I was doing before when my game was only solo. When I think about it, I guess you're right, I probably don't need to put my grid in my DB if I'm going to copy it only once when the game starts. I don't really know why I really wanted it in a DB in the first place...

    Thanks.

    EDIT: If you're wondering, here is my game (solo) with its source code. http://www.youtube.com/watch?v=8EaAiazT2K4
    Last edited by IdkWhatsRc; 06-20-2013 at 03:18 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •