简体   繁体   中英

How to store list of associated entities in LevelDB

Say I have an entity Client which has many entities Product. I am figuring out the best way to store these information in LevelDB.

class Client {

     String clientId;
     String clientName;
     List<Product> products;
}

class Product {

     String productId;
     Client client;
     String productName;
}

I have thought about marshaling these data to xml/json and persisting to LevelDB, yet it takes effort and might not be a good approach. Can you suggest me other solutions?

I would recommend you to seperate this business model into 3 types of data(like 3 tables in RMDB).

One is Client entity, serialize the entity to json, store it using client ID as key(with prefix "client_"), like "client_1", "client_2", the relation products is not stored in Client entity. Sample data: client_1 => {clientId: 1, clientName: "name"}

Second is Product entity, change client property to clientId. Serialize and store just like you do to Client. Sample data: product_1 => {productId: 1, productName: "book"}

The third is the Relation , using client Id as key(with prefix "client_product_"), like "client_product_1", "client_product_2", etc. Implode all client Ids of its products into a string. Sample data: client_product_1 => 1,2,3,4,5

However, you may want to use some database that is designed to store "relation"(lists, sets, hashmaps), try SSDB( https://github.com/ideawu/ssdb ), SSDB is a network wrapper of LevelDB, and is suitable for storing colletion data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM