Skip to main content

Storage Method, continued

This section of Store() creates several indexes for the stored answers. Indexes are also globals, created using Set, but their structure is different. You'll use ^PersonI for the indexes. These three statements from the method may look strange to you at first, because they store their data in the subscripts. Two of the statements have nothing but the empty string ("") for the value on the right side of the equals sign! But it makes sense if you think of the tree that these statements build.

    set ^PersonI("Name", last, first, id) = ""  // index last and first name
    set ^PersonI("Phone", phone) = id           // index the UNIQUE phone
    set ^PersonI("DOB", intdob, id) = ""        // index the internal DOB
Copy code to clipboard

Each last name, first name, and ID combination, for example, is stored in the subscripts. Since the last and first names will automatically be sorted alphabetically, this will create an index of the database, by last name, and then by first name, with similar indexes for phone numbers and dates of birth. ^PersonI will be a tree with three large branches, one for each index. Note that the line that builds the phone index is different (ID isn't stored in the subscripts) to help enforce uniqueness among the phone numbers.

These lines use Set $Bit to create a bitmap index of the ID numbers. The bitmap index will have one or more "chunks" of 64000 bits. Persons with IDs from 1-63999 have their bits in chunk 1, persons 64000-127999 will be in chunk 2, and so on. You'll see how to use these in Part 3.

    set chunk = (id\64000) + 1, position = (id#64000) + 1
    set $bit(^PersonI("Bitmap-ID", chunk), position) = 1
Copy code to clipboard

Once you complete the method, you'll run it and start adding data to your global. As you do this, you might find it helpful to draw the tree for each global on a piece of paper, to see how it looks. Whether or not you do that, once you add data, use the Management Portal. Click System Explorer > Globals and click View to look at your two globals (^PersonD and ^PersonI).

VS Code - ObjectScript

Class ObjectScript.DataEntry4
{

/// store the data
ClassMethod Store(answers as %String)
{
    read !, "Store? (y/n): ", yn  // ask if user wants to store
    // only go on if user says yes
    if ((yn '= "y") && (yn '= "Y")) {
        write "...not stored."
        quit
    }

    set id = $increment(^PersonD)  // use $increment to generate a new ID
    set ^PersonD(id) = answers  // store the answers
   
    set $listbuild(name, phone, intdob) = answers
    // split name into last and first for storage in index
    set last = $piece(name, ",", 1), first = $piece(name, ",", 2)

    /* the next three statements store data in subscripts.
       because of the automatic sorting of subscripts,
       this has the effect of building 3 indexes: name, phone, and DOB */
    set ^PersonI("Name", last, first, id) = ""  // index last and first name
    set ^PersonI("Phone", phone) = id           // index the UNIQUE phone
    set ^PersonI("DOB", intdob, id) = ""        // index the internal DOB

    /* these statements turn the id into a "chunk #" and a "position #"
       and set a bit into the bitmap index */
    set chunk = (id\64000) + 1, position = (id#64000) + 1
    set $bit(^PersonI("Bitmap-ID", chunk), position) = 1
    write "...stored"    
}
}
Copy code to clipboard
Feedback