开发者

Client Server Many To Many Design Model (Many Users - Many Applications) in C++

I need to create a mapping records of users and applications (many to many model) in c++. One users can have many applications connected to it, and vice versa, one application can have many users connected to it.

I have 2 design model, as follow:

First Design

unordered_map <string, unordered_set<string> > OneToManyMapping;
OneToManyMapping userAppMappings; // mapping records of 1 user to all application that it connects to.
OneToManyMapping appUserMappings; // mapping records of 1 application to all users that connects to it.

So every time a user is connected to new applications, we do not have to create a new record, but just insert the new application id to the unordered_set element of the userAppMappings. And same case for the appUserMappings. (every time a new user connects to it, we just insert the new user Id to the unordered_set element of the appUserMappings.

Second Design

unordered_multimap <string, string > ManyToManyMapping;
ManyToManyMappings userAppMappings; // mapping records of many user to many applications.
ManyToManyMappings appUserMappings; // mapping records of many applications to all users.

Every time a user is connected to a new application, we have to create new record on the userAppMappings. Same case with the appUserMappings.

Which design would be the most efficient if I want it to support the following operations:

Insertion,

Deletion,

Access (get list of all applications, or list of all users, or 开发者_如何学运维get list of all applications which is connected to a user, or get list of all users connected to an application ),

Delete all applications connected to a user, delete all users connected to an application, etc?

What will be the pro and cons if I use the first design or the second design? Is mapping the ID of the user and application a good approach, or is it better to map the complete User object and Application object?

Is there any other better design? Please advise.


Your second design will not work as many to many mapping, unordered_map key should be unique, that is you will not be able to store different records for the same user/application. What you are asking is multimap container, see http://en.wikipedia.org/wiki/Multimap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜