0

I have a structure :

struct Track{
    string details;  
    byte32 rc;  
    byte32 sn;  
}   
mapping(byte32 => Track)id;

Here I am able to search using id. Required to make search possible for rc and sn. But id is unique for each entry, but more than one entry can have same rc and sn.I want to list those entries;

2

You may create 2 arrays to store ids. One array for rc and another for sn

bytes32[] rc_ids; bytes32[] sn_ids;


Here is a very good pattern to store data in solidity Are there well-solved and simple storage patterns for Solidity?

Here are some simple and useful patterns in increasing order of utility.

Event logs are omitted for brevity. In practice, it's desirable to emit events for every important state change.

Simple List Using Array

0

Here's a suggestion:

Change the last line to

mapping(byte32 => Track) public trackStructsd;

This will be less confusing overall when you add the indices you need to enumerate things two more ways.

Before we complicate the contract, consider if this is really needed or just the first idea: https://blog.b9lab.com/the-joy-of-minimalism-in-smart-contract-design-2303010c8b09

You can have indices. For example, to enumerate the ids in the mapping:

bytes32[] public trackIdList; // .push(key) as you insert records

You can have a list of rc and a list of sn.

bytes32[] public rc;
bytes32[] public sn;

You can have a lists of the Tracks pointed to by each rc and sn.

mapping(bytes32 => bytes32[]) public rcTrackList; // .push as you go
mapping(bytes32 => bytes32[]) public snTrackList; // .push as you go

That will handle address an append-only case where you don't need to update the rc or sn in a track, ever. Matters get more complicated in the common case that you need to do just that. That is, in effect, deleting a row from a list. You will find there is still no efficient way to approach that. It's doable.

A full pattern to add/modify/delete and enforce referential integrity in a one-to-many relationship: https://medium.com/@robhitchens/enforcing-referential-integrity-in-ethereum-smart-contracts-a9ab1427ff42

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.