4D v16.3Sets |
||||||||||||||||||||||||||
|
4D v16.3
Sets
Sets
Sets offer you a powerful, swift means for manipulating record selections. Besides the ability to create sets, relate them to the current selection, and store, load, and clear sets, 4D offers three standard set operations:
A set is a compact representation of a selection of records. The idea of sets is closely bound to the idea of the current selection. Sets are generally used for the following purposes:
The current selection is a list of references that points to each record that is currently selected. The list exists in memory. Only currently selected records are in the list. A selection doesn’t actually contain the records, but only a list of references to the records. Each reference to a record takes 4 bytes in memory. When you work on a table, you always work with the records in the current selection. When a selection is sorted, only the list of references is rearranged. There is only one current selection for each table inside a process. Like a current selection, a set represents a selection of records. A set does this by using a very compact representation for each record. Each record is represented by one bit (one-eighth of a byte). Operations using sets are very fast, because computers can perform operations on bits very quickly. A set contains one bit for every record in the table, whether or not the record is included in the set. In fact, each bit is equal to 1 or 0, depending on whether or not the record is in the set. Sets are very economical in terms of RAM space. The size of a set, in bytes, is always equal to the total number of records in the table divided by 8. For example, if you create a set for a table containing 10,000 records, the set takes up 1,250 bytes, which is about 1.2K in RAM. There can be many sets for each table. In fact, sets can be saved to disk separately from the database. To change a record belonging to a set, first you must use the set as the current selection, then modify the record or records. A set is never in a sorted order—the records are simply indicated as belonging to the set or not. On the other hand, a named selection is in sorted order, but it requires more memory in most cases. For more information about named selections, see the Named Selections section. A set “remembers” which record was the current record at the time the set was created. The following table compares the concepts of the current selection and of sets:
When you create a set, it belongs to the table from which you created it. Set operations can be performed only between sets belonging to the same table. Sets are independent from the data. This means that after changes are made to a file, a set may no longer be accurate. There are many operations that can cause a set to be inaccurate. For example, if you create a set of all the people from New York City, and then change the data in one of those records to “Boston” the set would not change, because the set is just a representation of a selection of records. Deleting records and replacing them with new ones also changes a set, as well as compacting the data. Sets can be guaranteed to be accurate only as long as the data in the original selection has not been changed. You can have the following three types of sets:
Notes:
The following table indicates the principles concerning the visibility of sets depending on their scope and where they were created: A set can be created inside a transaction. It is possible to create a set of the records created inside a transaction and a set of records created or modified outside of a transaction. When the transaction ends, the set created during the transaction should be cleared, because it may not be an accurate representation of the records, especially if the transaction was canceled. The following example deletes duplicate records from a table which contains information about people. A For...End for loop moves through all the records, comparing the current record to the previous record. If the name, address, and zip code are the same, then the record is added to a set. At the end of the loop, the set is made the current selection and the (old) current selection is deleted: CREATE EMPTY SET([People];"Duplicates") As an alternative to immediately deleting records at the end of the method, you could display them on screen or print them, so that a more detailed comparison can be made. 4D maintains a system set named UserSet, which automatically stores the most recent selection of records highlighted on screen by the user. Thus, you can display a group of records with MODIFY SELECTION or DISPLAY SELECTION, ask the user to select from among them and turn the results of that manual selection into a selection or into a set that you name. 4D Server: Although its name does not begin with the character "$", the UserSet system set is a client set. So, when using INTERSECTION, UNION and DIFFERENCE, make sure you compare UserSet only to client sets. For more information, please refer to the descriptions of these commands as well as to the 4D Server, Sets and Named Selections section of the 4D Server Reference Manual. There is only one UserSet for a process. Each table does not have its own UserSet. UserSet becomes “owned” by a table when a selection of records is displayed for the table. 4D manages the UserSet set for list forms displayed in Design mode or using the MODIFY SELECTION or DISPLAY SELECTION commands. However, this mechanism is not active for subforms. The following method illustrates how you can display records, allow the user to select some of them, and then use UserSet to display the selected records: ` Display all records and allow user to select any number of them. The APPLY TO SELECTION, DELETE SELECTION, ARRAY TO SELECTION and JSON TO SELECTION commands create a set named LockedSet when used in a multi-processing environment.
See also
|
PROPERTIES
Product: 4D
HISTORY
ARTICLE USAGE
4D Language Reference ( 4D v16) |
||||||||||||||||||||||||