EOSIO / eos

An open source smart contract platform
https://developers.eos.io/manuals/eos
MIT License
11.27k stars 3.6k forks source link

String Index #2244

Closed gleehokie closed 5 years ago

brianjohnson5972 commented 5 years ago

This is a duplicate of #1623 (and there is no information here either)

dominic-healid commented 5 years ago

Any work around for this? need to add an index with a string type for the searching of texts.

arhag commented 5 years ago

Any work around for this? need to add an index with a string type for the searching of texts.

Don't expect native (i.e. intrinsic) support for a string index in EOSIO persistent storage anytime soon.

I strongly suggest you find an alternative design for your smart contract. For example, if you only care about lookup of exact string matches, then you can take a SHA256 hash of the string and store a 256-bit secondary index of that hash digest.

If you need to do actual lexicographical sorting of arbitrary length strings, then:

dominic-healid commented 5 years ago

@arhag Thank you for your response.

I'm trying to follow your suggestion but I'm confuse to what exact data type should I use for this? You mentioned 256-bit, can you provide a sample?

arhag commented 5 years ago

@dominic-healid:

I don't know of a very good existing example specific to what you are trying to do.

But you can first take a look at how eosio::setabi action in the reference system contract handles taking a SHA256 hash of the ABI byte string and storing the digest in a table:

https://github.com/EOSIO/eosio.contracts/blob/v1.5.1/eosio.system/include/eosio.system/native.hpp#L71-L77

https://github.com/EOSIO/eosio.contracts/blob/v1.5.1/eosio.system/src/eosio.system.cpp#L269-L282

However, there are two important things to note about that example.

First, it is using the old capi_checksum256 C struct rather than the new eosio::checksum256 C++ type that was recently added in eosio.cdt. I recommend using the eosio::checksum256 type in the table rather than capi_checksum256, and you can generate the eosio::checksum256 from the SHA256 hash of arbitrary data (like your string) using the C++ eosio::sha256 function.

Second, it doesn't use the SHA256 digest as a 256-bit secondary key in the multi_index table, which you would need to do if you want to do lookups by the digest.

You can see an example of using a 256-bit secondary key in the old identity contract:

https://github.com/EOSIO/eos/blob/v1.4.4/contracts/identity/common.hpp#L23-L45

https://github.com/EOSIO/eos/blob/v1.4.4/contracts/identity/common.hpp#L64-L66

Except you would want to use the eosio::checksum256 rather than the now deprecated eosio::key256.