Check if key exist in mapping - solidity

I have a mapping in solidity. How can I check if a key exist in this mapping. Mapping returns the data type default value if I try to access a non existent key.
Is there any graceful way to achieve the same.

There is no way to do it, because if value didn't set, there will be default value (false, 0, etc.). The best way out is to check that the value is different from the default value

There is no other other way to do this than just check if the key in question has a value initialized.
If you need more than that (e.g. to be able to keep track of which keys have values set, count them, list them, etc.) then a fairly common pattern is to maintain an array alongside the mapping, containing the known keys.
That way, when all you want is the value (or to check its existence), you can query the mapping, which is very gas efficient. When you want to do something with the set of known keys, you can interact with the array.

Related

IndexedDB using an index versus a key range?

In indexedDB, if the keys are arrays of integers such as [n,0] through [n,m], for operations that involve getting all the records in which the first element of the array key is n or opening a cursor on the same set of records, is there any advantage to using an index on an additonal property that stores n over using a key range?
Reasons to think an index may not be better include that the browser has to maintain the index for each change to the object store, an additional property has to be added to each record to store already stored data n, and little may be gained since the keys in the index will always point to consecutive records in the object store rather than dispersed throughout.
If the number of different values of n are likely no more than 1,000 and for m no more than 50, is using an index superior to a key range?
Thank you.
I guess the purpose of indexedDB is to have object store locally.
It is not sql that you need to update columns in every object.
since you change the object structure (saying by adding property)
it is true that all the objects in the store must be rewriten as you said...
emm well... another option for you is to update the db with another store
which contain somthing similar to forien key in sql or uniqe key which store the other stored objects extentions... and in it every obj item is also supposed to be same structured.
I think this is the point you start to use onupgradeneeded intansively.

Best datatype for column indicating the enumerated type

I need to add column to a table, which represent the origin of value in another column. I.e. it's an enumerated value of known origins.
What is the best datatype to represent the value? Should I use an integer or string? I realize best design would store both columns in another table, but it's not possible at the moment.
Also, would it be better to represent unknown origin with some fixed value or null?
"I realize best design would store both columns in another table, but it's not possible at the moment."
If you cannot create a lookup table then you need to store the meaning. So choose the data type which fits the meaning. That is, if your enumeration is 'Red, Amber, Green' go for varchar2(5). Use a check constraint to restrict it to valid input.
If you use integer you need to store the translation somewhere else. The meaning of data must reside in the database.
Obviously you need to duplicate the list of values in whatever UI populates the column. This is the technical debt incurred for not creating a lookup table.
"would it be better to represent unknown origin with some fixed value or null?"
The best option would be to have an explicit value of 'unknown' or whatever fits. The next best option is null: nulls are messy and can make queries harder to write, but magic values are worse because they work like real data and so can lead to misleading results.

Neo4J node_auto_indexing and relationship_auto_indexing

I want to know, if the two settings node_auto_indexing and relationship_auto_indexing in the neo4j.properties concerning the ids of nodes and rels?
or creates neo4j automatically an index for the ids of the inserted nodes and rels?
the auto index creates index for all properties defined at the *_keys_indexable line in the neo4j.properties file.
the index then bounds the node ID with the specific property value. thus, searching the index for the the property value will return the node.
since your question is a bit unclear to me, you might want to take a look at official docu:
http://docs.neo4j.org/chunked/milestone/auto-indexing.html
No you shouldn't add your ID to the auto index. There is no use for it, since you can already retrieve nodes by ID, without using auto index.
There are however occassions where the usual ID is not sufficient. For instance, when working with users, you may have a user id of some kind. You'd then store this in a property, and add that property to the auto index. This way, you can search by user id. Underlying, Neo4J matches your custom user ID, with the actual node id.
Important to keep in mind here is that per definition, auto index is not unique. You need to design your application in such a fashion that the property is in fact unique, if you're expecting a single node result.

How to implement using NHibernate UInt64 identifier generator with the option to set its initialize value

Is it possible to use NHibernate for getting unique identifier of type: UInt64.
The initialize value of this property must be unique (for more than 1 DB).
The usage: I need to get this value and increment it by 1.
This action should be a closed transaction.
usually we want to have unique keys in one DB, not for multiple databases, GUIDs are used there as far as I know. Getting the max(int64) out of both DBs and then picking the largests and adding 1 will work but that sounds rather clumsy I think? What do you need this for actually?
Hope this helps,
What about using database identities (or similar) and setting the starting value for database N as N, and the increment by value (how much you add to the current latest key every time you get a new one) equal to the number of databases?
That way each database should keep a distinct set of ids. Only problem would be in case you need to add a new database to the set.

Hibernate and IDs

Is it possible in hibernate to have an entity where some IDs are assigned and some are generated?
For instance:
Some objects have an ID between 1-10000 that are generated outside of the database; while some entities come in with no ID and need an ID generated by the database.
You could use 'assigned' as the Id generation strategy, but you would have to give the entity its id before you saved it to the database. Alternately you could build your own implementation of org.hibernate.id.IdentifierGenerator to provide the Id in the manner you've suggested.
I have to agree w/ Cade Roux though, and doing so seems like it be much more difficult than using built in increment, uuid, or other form of id generation.
I would avoid this and simply have an auxiliary column for the information about the source of the object and a column for the external identifier (assuming the external identifier was an important value you wanted to keep track of).
It's generally a bad idea to use columns for mixed purposes - in this case to infer from the nature of a surrogate key the source of an object.
Use any generator you like, make sure it can start at an offset (when you use a sequence, you can initialize it accordingly).
For all other entities, call setId() before you insert them. Hibernate will only generate an id if the id property is 0. Note that you should first insert objects with ids into the db and then work with them. There is a lot of code in Hibernate which expects the object to be in the DB when id != 0.
Another solution is to use negative ids for entities which come with an id. This will also make sure that there are no collisions when you insert an new object.