4D v18Entity selections |
||||||||||||||
|
4D v18
Entity selections
|
Feature | Unordered entity selection | Ordered entity selection |
Processing speed | very fast | slower |
Size in memory | very small | larger |
Can contain several references to an entity | no | yes |
For optimization reasons, by default 4D ORDA usually creates unordered entity selections, except when you use the orderBy( ) method or use the appropriate options (see below). In this documentation, unless specified, "entity selection" usually refers to an "unordered entity selection".
As mentioned above, by default ORDA creates and handles unordered entity selections as a result of operations such as queries or comparisons like and( ). Ordered entity selections are created only when necessary or when specifically requested using options.
Ordered entity selections are created in the following cases:
Unordered entity selections are created in the following cases:
Note that when an ordered entity selection becomes an unordered entity selection, any repeated entity references are removed.
If you want to transform an ordered entity selection to an unordered one, you can just apply an and( ) operation to it, for example:
//mySel is an ordered entity selection
mySel:=mySel.and(mySel)
//mySel is now an unordered entity selection
4D provides an automatic optimization for ORDA requests that use entity selections or load entities in client/server configurations. This optimization speeds up the execution of your 4D application by reducing drastically the volume of information transmitted over the network.
The following optimization mechanisms are implemented:
The following methods automatically associate the optimization context of the source entity selection to the returned entity selection:
Example
Given the following code:
$sel:=$ds.Employee.query("firstname = ab@")
For each($e;$sel)
$s:=$e.firstname+" "+$e.lastname+" works for "+$e.employer.name // $e.employer refers to Company table
End for each
Thanks to the optimization, this request will only get data from used attributes (firstname, lastname, employer, employer.name) in $sel after a learning phase.
Using the context property
You can increase the benefits of the optimization by using the context property. This property references an optimization context "learned" for an entity selection. It can be passed as parameter to ORDA methods that return new entity selections, so that entity selections directly request used attributes to the server and bypass the learning phase.
A same optimization context property can be passed to unlimited number of entity selections on the same dataclass. All ORDA methods that handle entity selections support the context property (for example dataClass.query( ) or dataClass.all( ) method). Keep in mind, however, that a context is automatically updated when new attributes are used in other parts of the code. Reusing the same context in different codes could result in overloading the context and then, reduce its efficiency.
Note: A similar mechanism is implemented for entities that are loaded, so that only used attributes are requested (see the dataClass.get( ) method).
Example with dataClass.query( ) method:
C_OBJECT($sel1;$sel2;$sel3;$sel4;$querysettings;$querysettings2)
C_COLLECTION($data)
$querysettings:=New object("context";"shortList")
$querysettings2:=New object("context";"longList")
$sel1:=ds.Employee.query("lastname = S@";$querysettings)
$data:=extractData($sel1) // In extractData method an optimization is triggered and associated to context "shortList"
$sel2:=ds.Employee.query("lastname = Sm@";$querysettings)
$data:=extractData($sel2) // In extractData method the optimization associated to context "shortList" is applied
$sel3:=ds.Employee.query("lastname = Smith";$querysettings2)
$data:=extractDetailedData($sel3) // In extractDetailedData method an optimization is triggered and associated to context "longList"
$sel4:=ds.Employee.query("lastname = Brown";$querysettings2)
$data:=extractDetailedData($sel4) // In extractDetailedData method the optimization associated to context "longList" is applied
Entity selection-based list box
Entity selection optimization is automatically applied to entity selection-based list boxes in client/server configurations, when displaying and scrolling a list box content: only the attributes displayed in the list box are requested from the server.
A specific "page mode" context is also provided when loading the current entity through the Current item property expression of the list box (see Collection or entity selection type list boxes). This feature allows you to not overload the list box initial context in this case, especially if the "page" requests additional attributes. Note that only the use of Current item expression will create/use the page context (access through entitySelection[index] will alter the entity selection context).
Subsequent requests to server sent by entity browsing methods will also support this optimization. The following methods automatically associate the optimization context of the source entity to the returned entity:
For example, the following code loads the selected entity and allows browsing in the entity selection. Entities are loaded in a separate context and the listbox initial context is left untouched:
$myEntity:=Form.currentElement //current item expression
//... do something
$myEntity:=$myEntity.next() //loads the next entity using the same context
The entity selection object itself cannot be copied as an object:
$myentitysel:=OB Copy(ds.Employee.all()) //returns null
The entity selection properties are however enumerable:
ARRAY TEXT($prop;0)
OB GET PROPERTY NAMES(ds.Employee.all();$prop)
//$prop contains the names of the entity selection properties
//("length", 00", "01"...)
Product: 4D
Theme: ORDA
Created: 4D v17
4D Design Reference ( 4D v18)