DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Erlang: bags

Erlang: bags

Giorgio Sironi user avatar by
Giorgio Sironi
·
Mar. 25, 13 · Interview
Like (0)
Save
Tweet
Share
125.27K Views

Join the DZone community and get the full member experience.

Join For Free

Sets are a data structure whose main selling point is that they contain only one element for each key. Bags relax these semantics and let you save multiple elements; their ETS implementation follow the same API as of sets.

Let's dive into the code

Bags may contain multiple elements that map to the same key (the first, or the n-th field of the tuple representing each element).

When you look up a key, what is returned is not anymore a list with a single tuple, but a list of the matching elements:

bags_contain_multiple_elements_for_each_key_test() ->
  Movies = ets:new(movies, [bag]),
  ets:insert(Movies, {"The day the Earth stood still", 1956}),
  ets:insert(Movies, {"The day the Earth stood still", 2010}),
  [{_, FirstInsertedYear}, {_, SecondInsertedYear}] = ets:lookup(Movies, "The day the Earth stood still"),
  ?assertEqual(1956, FirstInsertedYear),
  ?assertEqual(2010, SecondInsertedYear).

The order is not guaranteed as far as I know, since the internal implementation is an hash map.

Duplicate bags are another variation which allow even perfectly identical elements, that not only map to an equal key but also equal value.

duplicate_bags_contain_multiple_perfectly_equal_elements_for_each_key_test() ->
  Account= ets:new(account, [duplicate_bag]),
  ets:insert(Account, {credit, 200}),
  ets:insert(Account, {debit, 100}),
  ets:insert(Account, {credit, 300}),
  ets:insert(Account, {credit, 200}),
  Credits = ets:lookup(Account, credit),
  ?assertEqual(3, length(Credits)).

In this example, we are storing multiple credit and debit operation over an Account, and since these operations are commonly duplicated we should store all of them. Not only there can be multiple credit tuples, but also multiple {credit, 200} elements (and indeed there are in real bank accounts due to periodical operations such as receiving a salary.)

Keys in a different position than the first element of each tuple let you build bags even with existing data structures:

bags_different_key_positions_are_allowed_test() ->
  Movies = ets:new(movies, [bag, {keypos, 2}]),
  ets:insert(Movies, {"Star Wars", 1977}),
  ets:insert(Movies, {"Terminator", 1984}),
  ets:insert(Movies, {"B Movie", 1977}),
  MoviesOf1977 = ets:lookup(Movies, 1977),
  ?assertEqual(2, length(MoviesOf1977)).

Persistence

It is possible to transform sets and bags into persistent data structures, even by maintaing the current API:

persistent_sets_can_be_created_test() ->
  dets:open_file(movies, [{type, set}]),
  dets:insert(movies, {"Star Wars", 1977}),
  dets:close(movies),
  % in new processes and times...
  dets:open_file(movies, []),
  [{_, Year}] = dets:lookup(movies, "Star Wars"),
  ?assertEqual(1977, Year).

The insert/2 and lookup/2 operations do not change their semantics; however, they now come from the dets package. Some additional operations for opening and closing a DETS structure are introduced.

Bags follow the same API:

persistent_bags_can_be_created_test() ->
  dets:open_file(movies, [{type, bag}]),
  dets:insert(movies, {"Star Wars", 1978}),
  dets:close(movies),
  % in new processes and times...
  dets:open_file(movies, []),
  Result = dets:lookup(movies, "Star Wars"),
  ?assertEqual(1, length(Result)).

Additional, handy configuration can be passed to open_file/2. For example, you can specify a file name for the data structure in order to retrieve it later basing only on that file name.

Conclusions

To avoid reinventing the wheel, it's important to understand which data structures the language gives us for free. Persistence is even more complex from the point of view of durability and consistency of writes, and as such it should always be outsourced to the platform if your application is not a database.

Bag Erlang (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Choose the Right Streaming Database
  • Custom Validators in Quarkus
  • What Is API-First?
  • Solving the Kubernetes Security Puzzle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: