Skip to main content

Accessing Global Arrays with Node.js

The Native SDK for Node.js provides mechanisms for working with global arrays. The following topics are covered here:

Note:
Creating a Database Connection in Node.js

The examples in this chapter assume that an Iris object named irisjs already exists and is connected to the server. The following code was used to create and connect it:

const IRISNative = require('intersystems-iris')

// Open a connection to the server and create an Iris object
  let connectionInfo = {
    host: '127.0.0.1',
    port: 51773,
    ns: 'USER',
    user: '_SYSTEM',
    pwd: 'SYS'
  };
const conn = IRISNative.createConnection(connectionInfo);
const irisjs = conn.createIris();

For more information, see the Quick Reference entries for createConnection() and createIris().

Introduction to Global Arrays

A global array, like all sparse arrays, is a tree structure rather than a sequential list. The basic concept behind global arrays can be illustrated by analogy to a file structure. Each directory in the tree is uniquely identified by a path composed of a root directory identifier followed by a series of subdirectory identifiers, and any directory may or may not contain data.

Global arrays work the same way: each node in the tree is uniquely identified by a node address composed of a global name identifier and a series of subscript identifiers, and a node may or may not contain a value. For example, here is a global array consisting of six nodes, two of which contain values:

   root -->|--> foo --> SubFoo='A'
           |--> bar --> lowbar --> UnderBar=123

Values could be stored in the other possible node addresses (for example, root or root->bar), but no resources are wasted if those node addresses are valueless. Unlike a directory structure, all nodes in a global array must have either a value or a subnode with a value. In InterSystems ObjectScript globals notation, the two nodes with values would be:

   root('foo','SubFoo')
   root('bar','lowbar','UnderBar')

In this notation, the global name (root) is followed by a comma-delimited subscript list in parentheses. Together, they specify the entire node address of the node.

This global array could be created by two calls to the Native SDK set() method. The first argument is the value to be assigned, and the rest of the arguments specify the node address:

  irisjs.set('A', 'root', 'foo', 'SubFoo');
  irisjs.set(123, 'root', 'bar', 'lowbar', 'UnderBar');

Global array root is does not exist until the first call assigns value 'A' to node root('foo','SubFoo'). Nodes can be created in any order, and with any set of subscripts. The same global array would be created if we reversed the order of these two calls. The valueless nodes are created automatically, and will be deleted automatically when no longer needed.

The Native SDK code to create this array is demonstrated in the following example. An IRISConnection object establishes a connection to the server. The connection will be used by an instance of class Iris named irisjs. Native SDK methods are then used to create a global array, read the resulting persistent values from the database, and delete the global array.

The NativeDemo Program
// Import the Native SDK module
const IRISNative = require('intersystems-iris')

// Open a connection to the server and create an Iris object
  let connectionInfo = {
    host: '127.0.0.1',
    port: 51773,
    ns: 'USER',
    user: '_SYSTEM',
    pwd: 'SYS'
  };
const conn = IRISNative.createConnection(connectionInfo);
const irisjs = conn.createIris();

// Create a global array in the USER namespace on the server
  irisjs.set('A', 'root', 'foo', 'SubFoo');
  irisjs.set(123, 'root', 'bar', 'lowbar', 'UnderBar');

// Read the values from the database and print them
  let subfoo = irisjs.get('root', 'foo', 'SubFoo')
  let underbar = irisjs.get('root', 'bar', 'lowbar', 'UnderBar')
  console.log('Created two values: ');
  console.log('   root("foo","SubFoo")=' + subfoo);
  console.log('   root("bar","lowbar","UnderBar")=' + underbar);

// Delete the global array and terminate
  irisjs.kill('root'); // delete entire global array
  conn.close();

NativeDemo prints the following lines:

  Created two values:
     root("foo","SubFoo")="A"
     root("bar","lowbar","UnderBar")=123

In this example, method createConnection() defines a Connection object named conn that provides a connection to the database associated with the USER namespace. Native SDK methods perform the following actions:

  • Connection.createIRIS() creates a new instance of Iris named irisjs, which will access the database through server connection conn.

  • Iris.set() creates new persistent nodes in database namespace USER.

  • Iris.get() queries the database and returns the values of the specified nodes.

  • Iris.kill() deletes the specified root node and all of its subnodes from the database.

The next chapter provides detailed explanations and examples for all of these methods.

Glossary of Global Array Terms

See the previous section for an overview of the concepts listed here. Examples in this glossary will refer to the global array structure listed below. The Legs global array has ten nodes and three node levels. Seven of the ten nodes contain values:

  Legs                       // root node, valueless, 3 child nodes
    fish = 0                 // level 1 node, value=0
    mammal                   // level 1 node, valueless
      human = 2              // level 2 node, value=2
      dog = 4                // level 2 node, value=4
    bug                      // level 1 node, valueless, 3 child nodes
      insect = 6             // level 2 node, value=6
      spider = 8             // level 2 node, value=8
      millipede = Diplopoda  // level 2 node, value='Diplopoda', 1 child node
        centipede = 100      // level 3 node, value=100

Child node

The nodes immediately under a given parent node. The address of a child node is specified by adding exactly one subscript to the end of the parent subscript list. For example, parent node Legs('mammal') has child nodes Legs('mammal','human') and Legs('mammal','dog').

Global name

The identifier for the root node is also the name of the entire global array. For example, root node identifier Legs is the global name of global array Legs. Unlike subscripts, global names can only consist of letters, numbers, and periods (see Global Naming Rules).

Node

An element of a global array, uniquely identified by a namespace consisting of a global name and an arbitrary number of subscript identifiers. A node must either contain a value, have child nodes, or both.

Node level

The number of subscripts in the node address. A ‘level 2 node’ is just another way of saying ‘a node with two subscripts’. For example, Legs('mammal','dog') is a level 2 node. It is two levels under root node Legs and one level under Legs('mammal').

Node address

The complete namespace of a node, consisting of the global name and all subscripts. For example, node address Legs('fish') consists of root node identifier Legs plus a list containing one subscript, 'fish'. Depending on context, Legs (with no subscript list) can refer to either the root node address or the entire global array.

Root node

The unsubscripted node at the base of the global array tree. The identifier for a root node is its global name with no subscripts.

Subnode

All descendants of a given node are referred to as subnodes of that node. For example, node Legs('bug') has four different subnodes on two levels. All nine subscripted nodes are subnodes of root node Legs.

Subscript / Subscript list

All nodes under the root node are addressed by specifying the global name and a list of one or more subscript identifiers. Subscripts can be strings or numbers. The global name plus the subscript list is the node address.

Target address

Many Native SDK methods require you to specify a valid node address that does not necessarily point to an existing node. For example, the set() method takes a value argument and a target address, and stores the value at that address. If no node exists at the target address, a new node is created at that address.

Value

A node can contain a value of any supported type. A node with no child nodes must contain a value; a node that has child nodes can be valueless.

Valueless node

A node must either contain data, have child nodes, or both. A node that has child nodes but does not contain data is called a valueless node. Valueless nodes only exist as pointers to lower level nodes.

Global Naming Rules

Global names and subscripts obey the following rules:

  • The length of a node address (totaling the length of the global name and all subscripts) can be up to 511 characters. (Some typed characters may count as more than one encoded character for this limit. For more information, see “Maximum Length of a Global Reference” in Using Globals).

  • A global name can include letters, numbers, and periods ('.'), and can have a length of up to 31 significant characters. It must begin with a letter, and must not end with a period.

  • A subscript can be a string, an integer, or a number. String subscripts are case-sensitive, and can contain any character (including non-printing characters). Subscript length is restricted only by the maximum length of a node address.

Fundamental Node Operations

This section demonstrates how to use the set(), get(), and kill() methods to create, access, and delete nodes. These methods have the following signatures:

  set (value, globalName, subscripts)
  get (globalName, subscripts)
  kill (globalName, subscripts)

  • value is the item to be stored.

  • globalName can only include letters, numbers, and periods ('.'), must begin with a letter, and cannot end with a period.

  • subscripts can be strings, integers, or floating point numbers. A string subscript is case-sensitive and can include non-printing characters.

All of the examples in this section assume that a connected instance of class Iris named irisjs already exists (see “Creating a Connection in Node.js”).

Setting and changing node values

Iris.set() takes a value argument and stores the value at the specified address.

If no node exists at that address, a new one is created.

The set() method can assign values of any supported datatype. In the following example, the first call to set() creates a new node at subnode address myGlobal('A') and sets the value of the node to string 'first'. The second call changes the value of the subnode, replacing it with integer 1.

  irisjs.set('first', 'myGlobal', 'A');     // create node myGlobal('A') = 'first'
  irisjs.set(1, 'myGlobal', 'A');   // change value of myGlobal('A') to 1.

set() is a polymorphic accessor that can create and change values of any supported datatype, as demonstrated in this example.

Retrieving node values with get()

Iris.get() takes globalname and *subscripts arguments and returns the value stored at the specified node address, or None if there is no value at that address.

  irisjs.set(23,'myGlobal','A');
  value_of_A = irisjs.get('myGlobal','A')

The get() method returns an untyped value. To return a specific datatype, use one of the IRIS.get() typecast methods. The following methods are available: getBoolean(), getBytes(), getDecimal() getFloat() getInteger(), getString(), getIRISList(), and getObject().

Deleting a node or group of nodes

Iris.kill() — deletes the specified node and all of its subnodes. The entire global array will be deleted if the root node is deleted or if all nodes with values are deleted.

Global array myGlobal initially contains the following nodes:

   myGlobal = <valueless node>
     myGlobal('A') = 0
       myGlobal('A',1) = 0
       myGlobal('A',2) = 0
     myGlobal('B') = <valueless node>
       myGlobal('B',1) = 0

This example will delete the global array by calling kill() on two of its subnodes. The first call will delete node myGlobal('A') and both of its subnodes:

  irisjs.kill('myGlobal','A')    // also kills myGlobal('A',1) and myGlobal('A',2)

The second call deletes the last remaining subnode with a value, killing the entire global array:

  irisjs.kill('myGlobal','B',1)  // deletes last value in global array myGlobal
  • The parent node, myGlobal('B'), is deleted because it is valueless and now has no subnodes.

  • Root node myGlobal is valueless and now has no subnodes, so the entire global array is deleted from the database.

Managing Global Arrays

The Native SDK provides ways to iterate over part or all of a global array. The following topics describe the various iteration methods:

Iterating Over a Set of Child Nodes

Child nodes are sets of nodes immediately under the same parent node. Any child node address can be defined by appending one subscript to the subscript list of the parent. For example, the following global array has four child nodes under parent node heroes('dogs'):

The heroes global array

This global array uses the names of several heroic dogs (plus a reckless boy and a pioneering sheep) as subscripts. The values are birth years.

heroes                               // root node, no value, 2 child nodes
  heroes('dogs')                            // level 1, no value, 4 child nodes
    heroes('dogs','Balto') = 1919              // level 2, value=1919
    heroes('dogs','Hachiko') = 1923            // level 2, value=1923
    heroes('dogs','Lassie') = 1940             // level 2, value=1940, 1 child
      heroes('dogs','Lassie','Timmy') = 1954      // level 3, value=1954
    heroes('dogs','Whitefang') = 1906          // level 2, value=1906
  heroes('sheep')                           // level 1, no value, 1 child
    heroes('sheep','Dolly') = 1996             // level 2, value=1996

The following methods are used to create an iterator, define the direction of iteration, and set the starting point of the search:

  • Iris.iterator() returns an instance of Iterator for the child nodes of the specified target node.

  • Iterator.reversed() — toggles direction of iteration between forward and reverse collation order.

  • Iterator.startFrom() sets the iterator's starting position to the specified subscript. The subscript is an arbitrary starting point, and does not have to address an existing node.

Read child node values in reverse order

The following code iterates over child nodes of heroes('dogs') in reverse collation order, starting with subscript V:

// Iterate in reverse, seeking nodes lower than heroes('dogs','V') in collation order
  let iterDogs = irisjs.iterator('heroes','dogs').reversed().startFrom('V');

  let output = '\nDog birth years: ';
  for ([key,value] of iterDogs) {
    output += key + ':' + value + '  ';
  };
  console.log(output);

This code prints the following output:

  Dog birth years: Lassie:1940  Hachiko:1923  Balto:1919

In this example, two subnodes of heroes('dogs') are ignored:

  • Child node heroes('dogs','Whitefang') will not be found because it is outside of the search range (Whitefang is higher than V in collation order).

  • Level 3 node heroes('dogs','Lassie','Timmy') will not be found because it is a child of Lassie, not dogs.

See the last section in this chapter (“Testing for Child Nodes and Node Values”) for a discussion of how to iterate over multiple node levels.

Note:
Collation Order

The order in which nodes are retrieved depends on the collation order of the subscripts. When a node is created, it is automatically stored it in the collation order specified by the storage definition. In this example, the child nodes of heroes('dogs') would be stored in the order shown (Balto, Hachiko, Lassie, Whitefang) regardless of the order in which they were created. For more information, see “Collation of Global Nodes” in Using Globals.

Iteration with next()

The Native SDK also supports the standard next() and return type iterator methods:

  • Iterator.next() — positions the iterator at the next child node (if one exists) and returns an array containing properties done and value. The done property will be false if there are no more nodes. When an iterator is created, it defaults to the entries() return type.

  • Iterator.entries() — sets return type to an array containing both the key (the top level subscript of the child node) and the node value. For example, the returned value for node heroes(,'dogs','Balto') would be ['Balto',1919].

  • Iterator.keys() — sets return type to return only the key (the top level subscript).

  • Iterator.values() — sets return type to return only the node value.

In the following example, each call to the next() method sets variable iter to an array containing the current values for the iterator done and value properties. Since the keys() method was called when the iterator was created, the value property will contain only the key (top level subscript) for the current child node of heroes('dogs').

Use next() to list the subscripts under node heroes('dogs')
// Get a list of child subscripts under node heroes('dogs')
  let iterDogs = irisjs.iterator('heroes','dogs').keys();
  let output = "\nSubscripts under node heroes('dogs'): ";

  let iter = iterDogs.next();
  while (!iter.done) {
    output += iter.value + ' ';
    iter = iterDogs.next();
  }
  console.log(output);

This code prints the following output:

  Subscripts under node heroes('dogs'): Balto Hachiko Lassie Whitefang

Testing for Child Nodes and Node Values

In the previous examples, the scope of the search is restricted to child nodes of heroes('dogs'). The iterator fails to find two values in global array heroes because they are under different parents:

  • Level 3 node heroes('dogs','Lassie','Timmy') will not be found because it is a child of Lassie, not dogs.

  • Level 2 node heroes('sheep','Dolly') is not found because it is a child of sheep, not dogs.

To search the entire global array, we need to find all of the nodes that have child nodes, and create an iterator for each set of child nodes. The isDefined() method provides the necessary information:

  • Iris.isDefined() — can be used to determine if a node has a value, a subnode, or both. It returns one of the following values:

    • 0 — the specified node does not exist

    • 1 — the node exists and has a value

    • 10 — the node is valueless but has a child node

    • 11 — the node has both a value and a child node

    The returned value can be used to determine several useful boolean values:

       let exists = (irisjs.isDefined(root,subscripts) > 0);      // returns 1, 10, or 11
       let hasValue = (irisjs.isDefined(root,subscripts)%10 > 0); // returns 1 or 11
       let hasChild = (irisjs.isDefined(root,subscripts) > 9);    // returns 10 or 11
    
    

The following example consists of two methods:

  • findAllHeroes() iterates over child nodes of the current node, and calls testNode() for each node. Whenever testNode() indicates that the current node has child nodes, findAllHeroes() creates a new iterator for the next level of child nodes.

  • testNode() will be called for each node in the heroes global array. It calls isDefined() on the current node, and returns a boolean value indicating whether the node has child nodes. It also prints node information for each node.

Method findAllHeroes()

This example processes a known structure, and traverses the various levels with simple nested calls. In the less common case where a structure has an arbitrary number of levels, a recursive algorithm could be used.

  function findAllHeroes() {
    const root = 'heroes';
    console.log('List all subnodes of root node '+root+':\n'+root)
    let iterRoot = irisjs.iterator(root);
    let hasChild = false;

    // Iterate over children of root node heroes
    for ([sub1,value] of iterRoot) {
      hasChild = testNode(value,root,sub1);

      // Iterate over children of heroes(sub1)
      if (hasChild) {
        let iterOne = irisjs.iterator(root,sub1);
        for ([sub2,value] of iterOne) {
          hasChild = testNode(value,root,sub1,sub2);

          // Iterate over children of heroes(sub1,sub2)
          if (hasChild) {
            let iterTwo = irisjs.iterator(root,sub1,sub2);
            for ([sub3,value] of iterTwo) {
              testNode(value,root,sub1,sub2,sub3); //no child nodes below level 3
            }
          } //end level 2
        }
      } //end level 1
    } // end main loop
  } // end findAllHeroes()

Method testNode()
  function testNode(value, root, ...subs) {

  // Test for values and child nodes
    let state = irisjs.isDefined(root,...subs);
    let hasValue = (state%10 > 0); // has value if state is 1 or 11
    let hasChild = (state > 9);    // has child if state is 10 or 11

  // format the node address output string
    let subList = Array.from(subs);
    let level = subList.length-1;
    let indent = '  ' + String('      ').slice(0,(level*2));
    let address = indent + root+'(' + subList.join() + ')';

  // Add node value to string and note special cases
    if (hasValue) { // ignore valueless nodes
      address += ' = ' + value;
      for (name of ['Timmy','Dolly']) {
        if (name == subList[level]) {
          address += ' (not a dog!)'
        }
      }
    }
    console.log(address);
    return hasChild;
  }
}

This method will write the following lines:

List all subnodes of root node heroes:
heroes
  heroes(dogs)
    heroes(dogs,Balto) = 1919
    heroes(dogs,Hachiko) = 1923
    heroes(dogs,Lassie) = 1940
      heroes(dogs,Lassie,Timmy) = 1954  (not a dog!)
    heroes(dogs,Whitefang) = 1906
  heroes(sheep)
    heroes(sheep,Dolly) = 1996  (not a dog!)

The output of testNodes() includes some nodes that were not found in previous examples because they are not child nodes of heroes('dogs'):

  • heroes('dogs','Lassie','Timmy') is a child of Lassie, not dogs.

  • heroes('sheep','Dolly') is a child of sheep, not dogs.

FeedbackOpens in a new tab