Forum BigDB How to get all contents of a table?

Discussion and help relating to the PlayerIO database solution, BigDB.

How to get all contents of a table?

Postby Tsuken » June 10th, 2010, 8:33 pm

How do you get all data from a table? So without requiring a key or index?

In SQL it would be:
SELECT * FROM TABLE X

The only functions I see which could return an array are:
loadKeys (requires multiple keys)
loadKeysOrCreate (requires multiple keys)
loadRange (requires an index)

setting the index or key to null doesn't seem to work.

The only work-around I can think of is to make an index for a property that is set to the same value for all objects in the table.
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby Henrik » June 10th, 2010, 10:15 pm

You can use the LoadRange method, set the path to null, and set both start and end to null. This query will return all objects in the table that exist in the index. Note however that there is still a limit to the amount of objects you get per query so if you really want all objects you have to do several calls until you know you've fetched all of them. To ensure all objects exist in the index, they would all have to have a value in whatever property is in the index.

I'm curious what your use-case is though? What are you doing in your game where you need to load all objects in a table?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How to get all contents of a table?

Postby Tsuken » June 10th, 2010, 10:45 pm

cool, that works fine, although I had to create an index for a random property.

As for the reason, let's say for example that I'm making a racing game. I make a Car table in which I store all possible cars in the game together with their properties:

car1 = (
name: "Brand X"
acceleration: 2.1
brakes: 0.9
)

I could then give the player a choice which car he wants to drive, so I could list all cars for him. If I want to add more cars to the game, I could then simply add them to the database without having to change any of the game code.

Further in development I might plan to have hidden cars, so I wouldn't want to send all cars, but at the moment I'm still developing it, so I just want to see everything.
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby Tsuken » June 11th, 2010, 9:48 am

I don't like how limited player.Send() is. It can't send an object nor an array of objects, or am I missing something? I know it can send multiple parameters, but it's annoying to have to manually deconstruct objects from the database server-side and send them to flash as parameters. Also, if you want to send an array of objects, you're going to have a lot of separate calls, rather than just one.
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby fox1980 » June 11th, 2010, 10:14 am

Have you tried serializing your objects into bytearrays ? It works for me, as long as you don't use very complex objects it should work for you too.
fox1980
 
Posts: 206
Joined: April 1st, 2010, 10:39 pm

Re: How to get all contents of a table?

Postby Tsuken » June 11th, 2010, 10:16 am

Also, can you add System.Reflection.PropertyInfo to the whitelist so I can write my own function for sending any database object as parameters?
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby Tsuken » June 11th, 2010, 10:17 am

fox1980 wrote:Have you tried serializing your objects into bytearrays ? It works for me, as long as you don't use very complex objects it should work for you too.


hehe no I haven't tried that. To be honest, I never used bytearrays before. I'll look into it, thanks :)
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby Tsuken » June 11th, 2010, 10:31 am

I'm using the following code to serialize:

Code: Select all
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public byte[] getByteArrayWithObject(Object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, o);
            return ms.ToArray();
        }


But it gives me these errors in the console when I start debugging:

Game Errors:
========================
(dll: \.NET Libraries\Test Server Project\bin\Debug\MyGame.dll)
MyGame.GameCode.getByteArrayWithObject(...) has varible of the non-allowed type: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
MyGame.GameCode.getByteArrayWithObject(...) uses the non-allowed method: System.Void System.Runtime.Serialization.Formatters.Binary.BinaryFormatter::.ctor()
MyGame.GameCode.getByteArrayWithObject(...) uses the non-allowed method: System.Void System.Runtime.Serialization.Formatters.Binary.BinaryFormatter::Serialize(System.IO.Stream,System.Object)

Does that mean certain functions need to be added to the whitelist?
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby fox1980 » June 11th, 2010, 10:42 am

Code: Select all
byte[] serialized = (byte[])your_object;


This is all you need. You can then send the serialized object from the server and in AS3 read it with bytearray.readobject.
fox1980
 
Posts: 206
Joined: April 1st, 2010, 10:39 pm

Re: How to get all contents of a table?

Postby Tsuken » June 11th, 2010, 1:39 pm

Thanks fox1980, but I get the error:
Cannot convert type 'PlayerIO.GameLibrary.DatabaseObject' to 'byte[]'

I tried making a custom class, saved in a separate .cs file, for example:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServersideGameCode
{
    class Car
    {
        public String id;
        public String name;
    }
}

But I get the same error:
Cannot convert type 'ServersideGameCode.Car' to 'byte[]'

Sorry but I'm not used to using C# :)
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby Henrik » June 11th, 2010, 2:11 pm

Tsuken wrote:I could then give the player a choice which car he wants to drive, so I could list all cars for him. If I want to add more cars to the game, I could then simply add them to the database without having to change any of the game code.

Further in development I might plan to have hidden cars, so I wouldn't want to send all cars, but at the moment I'm still developing it, so I just want to see everything.

Make one table, put all cars in it, and one extra object that looks like this:

Code: Select all
"cars" = {
    visible = [id, id, id, id, ...],
    hidden = [id, id, ...]
}

So first you load the list of cars, and then as the user pages through it, you can load the actual car objects a few at a time, because you then know their keys.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How to get all contents of a table?

Postby fox1980 » June 11th, 2010, 2:17 pm

I'm not very proficient with c# either, i wrote that from memory so it might be wrong. I'd have to take a look at my code, and i'm at work atm. I had the same problem as you and i found out an example through google. I'll see if i can post a working example later tonight.
fox1980
 
Posts: 206
Joined: April 1st, 2010, 10:39 pm

Re: How to get all contents of a table?

Postby Henrik » June 11th, 2010, 2:19 pm

Tsuken wrote:I don't like how limited player.Send() is. It can't send an object nor an array of objects, or am I missing something? I know it can send multiple parameters, but it's annoying to have to manually deconstruct objects from the database server-side and send them to flash as parameters. Also, if you want to send an array of objects, you're going to have a lot of separate calls, rather than just one.


You can't deserialize .Net objects in AS3 or vice-versa, they're completely different. You have to make a message and add each property as a primitive type, and then read it on the other side, reconstructing the object.

But in the case of BigDB objects, why not just send over the key, and load the object at the other side?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How to get all contents of a table?

Postby Tsuken » June 14th, 2010, 6:47 pm

Can you add System.Reflection.PropertyInfo to the whitelist or could that be used in an unsafe way?
Tsuken
 
Posts: 41
Joined: January 26th, 2010, 7:54 pm

Re: How to get all contents of a table?

Postby Oliver » June 15th, 2010, 1:19 pm

We won't white list anything under the Reflection whitespace; Reflection is slow, and there is a lot of unsafe things you can do with it.

- Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am


Return to BigDB



cron