When the solution matters

Tips...

Registration Documentation Knowledgebase Seminars / Training Partner Programs 4D Partner Central

Step 12) Sets

Prerequisites

You should be familiar with the following concepts before reading this section:

Introduction

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, 4th Dimension offers three standard set operations:

Sets and the Current Selection

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, and then modify the record or records. The name of an interprocess set must be unique in the database.

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 4D Language Reference, pages 691 - 700.

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:

Comparison Current Selection Sets
Number per table 1 0 to many
Sortable Yes No
Can be saved on disk No Yes
RAM per record(in bytes)
Number of selected records * 4
Total number of records/8
Combinable No Yes
Contains current record Yes Yes, as of the time the set was created

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. Sets can be guaranteed to be accurate only as long as the data in the original selection has not been changed.

Process and Interprocess Sets

You can have the following three types of sets:

Sets and Transactions

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 UserSet System Set

4th Dimension 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.

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.

The following method illustrates how you can display records, allow the user to select some, and then use UserSet to display the selected records:

` Display all records and allow user to select any number of them.
` Then display this selection by using UserSet to change the current selection.
OUTPUT FORM ([People]; "Display") ` Set the output layout
ALL RECORDS ([People]) ` Select all people
ALERT ("Press Ctrl or Command and Click to select the people required.")
DISPLAY SELECTION ([People]) ` Display the people
USE SET ("UserSet") ` Use the people that were selected
ALERT ("You chose the following people.")
DISPLAY SELECTION ([People]) ` Display the selected people

The LockedSet System Set

The commands APPLY TO SELECTION, ARRAY TO SELECTION and DELETE SELECTION create a set named LockedSet when used in a multi-processing environment. LockedSet indicates which records were locked during the execution of the command.

For more info on sets, see the descriptions of the following commands: ADD TO SET, CLEAR SET, COPY SET, CREATE EMPTY SET, CREATE SET, DIFFERENCE, INTERSECTION, Is in set, LOAD SET, Records in set, REMOVE FROM SET, SAVE SET, UNION, USE SET. (Webmaster – please create links to the corresponding pages in the online version of the Language Reference).

Learning Resources

Commands and Topics for Sets
General description of sets, plus a detailed description of each of the set commands.

Technical Note: On sets
A more detailed discussion about sets and how they’re used.

Technical Note: Manipulating Selections with Sets
Discussion about using sets to manipulate record selections.

4D Server and Sets
Discussion of sets and their use on in a multiuser environment using 4D Server.

Technical Note: Sending Sets
Tech note which describes a technique for sending a set from one client machine to another without first creating a document on disk.

Technical Note: Sets in 4D v6
Discussion of local sets, new to 4D v6.


International | Company | Contact 4D | Site Map | Privacy Policy | © 4D, Inc. 1995-2008 | Change font size: [A] [A] [A] | Print this page | 4D RSS Feeds