Json value search

Nov 21, 2014 at 12:08 PM
Hi,

I apologize in advance for my english and because I'm new to DBreeze.
Well, I inserted my Dbreeze table values with serialized Json and i need to filter the tables.
For example:
{
    "type": "simple",
    "name": "test 1"
}

{
    "type": "complex",
    "name": "test 2"
}
So i need to get only the simple one from the database.

Can anyone help me?

Thank you!
Coordinator
Nov 21, 2014 at 12:53 PM
DBreeze can search only by Keys in a Key/Value table.

If you want to filter by type and by name, you will have to create 2 tables.
In first key must be "type" and second key must be "name".

Please, read carefully documentation, specially paragraph about secondary indexes.
Nov 21, 2014 at 7:22 PM
Edited Nov 21, 2014 at 7:24 PM
hhblaze wrote:
DBreeze can search only by Keys in a Key/Value table.

If you want to filter by type and by name, you will have to create 2 tables.
In first key must be "type" and second key must be "name".

Please, read carefully documentation, specially paragraph about secondary indexes.
Thank you for your reply hhblaze!

That was just an example. On my specific case, there will be many other fields (sorry for the lack of details).
So I'm thinking of creating another table with the code of the element(key) and his type(value) and use it to create a list, then finally get the elements from the main table.

Is this a good approach?
Coordinator
Nov 22, 2014 at 10:53 AM
Edited Nov 22, 2014 at 3:54 PM
You will have to read docu to understand following terms:

Writing without C# editor, so sorry for syntax mistakes.

Code example is here Code example
//Setup Dbreeze
//Bind protobuf to DBreeze

public class Customer
{
   public long id=0;
   public string ExternalId="";
   public string Name="";
}

//Inserting 1 entity. For bulk inserts, all operations must be done in one transaction...for speed can be added tran.OverwriteIsNotAllowed = true
public void InsertCustomer(Customer cst)    
{
    //Inserting Customer
    using(var tran = Engine.GetTransaction())
    {
        tran.SynchronizeTable("C"); //not necessary for one table
      
        //Getting nested table for inserting customer's main entity
        //EntityTable with the byte[] key type and heterogenous values, where we store different information
        //This nested Table will have structure <long,Customer> (Customer is protobuff serialized value)
        var ct = tran.InsertTable<byte[]>("C",new byte[] {1},0);    //Use tran.SelectTable if you are not going to insert into this nested table in this transaction
        //Getting nested table to insert ExternalId as key and value reference to main entity table
        var et= tran.InsertTable<byte[]>("C",new byte[] {2},0);
        //Getting new Id for the customer 
        
        if(cst.id == 0) 
        {
          //Getting new ID for the customer entity
          cst.id = tran.Select<byte[],long>("C",new byte[]{3}).Value + 1 ;
          //Under Key new byte[]{3} we store monotonically grown identity (id) for the Customer entity. 
          //We could use Max from ct, in case if we are not going to delete entities from the table, and skip this extra storage place
          tran.Insert<byte[],long>("C",new byte[]{3},cst.id);
        }
        else
        {
           //Such entity already exists, May be here we want to clean secondary index
           tran.ValueLazyLoading = false;
           var oldCustomer = ct.Select<long, Customer>(cst.id,true).Value; //Note using read visibility scope (just to show existance of the concept)
          if(!oldCustomer.ExternalId.Equals(cst.ExternalId, StringOption.IgnoreCase))
          {
            //removing from secondary index old ExternalId
            et.RemoveKey<string>(oldCustomer.ExternalId);
          }
        }
        //Inserting, Updating Customer entity (Value is protobuffed)
        ct.Insert<long,Customer>(cst.id,cst);
      
        //Inserting into secondary index (key is ExternalId for fast search)
        //We will be able quickly search Customer also by External ID
        et.Insert<string,long>(cst.ExternalId.ToLower(),cst.id);
      
        //Also can be used technique SelectDirect by pointer (it's interesting for speed optimization only)
      
        tran.Commit();
    }
}
Marked as answer by juniordluk on 11/24/2014 at 9:11 AM
Nov 24, 2014 at 5:18 PM
hhblaze wrote:
You will have to read docu to understand following terms:

Writing without C# editor, so sorry for syntax mistakes.

Code example is here Code example
[...]
Thank you hhblaze!

My question was solved.