Learning
Community
Open Exchange
Global Masters
InterSystems IRIS Data Platform 2019.3 / Application Development / Persisting .NET Objects with InterSystems XEP / Using XEP Event Persistence
Previous section   Next section

Using XEP Event Persistence

XEP provides extremely rapid storage and retrieval of .NET structured data. It provides ways to control schema generation for optimal mapping of complex data structures, but schemas for simpler data structures can often be generated and used without modification.
The following topics are discussed in this chapter:
Note:
Why Persistent Event?
The term persistent event originally referred to data acquired from real time events. The current implementation of XEP is designed for much more than simple event processing, so XEP “persistent events” are actually database objects that XEP has persisted from any source.

Introduction to Event Persistence

A persistent event is an InterSystems IRIS database object that holds a persistent copy of the data fields in a .NET object. By default, XEP stores each event as a standard %Persistent object. Storage is automatically configured so that the data will be accessible to InterSystems IRIS by other means, such as objects, SQL, or direct global access.
Before a persistent event can be created and stored, XEP must analyze the corresponding .NET class and import a schema, which defines how the data structure of a .NET object is projected to a persistent object in the database. A schema can use either of the following two object projection models:
  • The default model is the flat schema, where all referenced objects are serialized and stored as part of the imported class, and all fields inherited from superclasses are stored as if they were native fields of the imported class. This is the fastest and most efficient model, but does not preserve any information about the original .NET class structure.
  • If structural information must be preserved, the full schema model may be used. This preserves the full .NET inheritance structure by creating a one-to-one relationship between .NET source classes and InterSystems IRIS projected classes, but may impose a slight performance penalty.
See “Schema Import Models” for a detailed discussion of both models, and “Schema Mapping Rules” for detailed information about how various datatypes are projected.
When importing a schema, XEP acquires basic information by analyzing the .NET class. You can supply additional information that allows XEP to generate indexes (see “Using IdKeys”) and override the default rules for importing fields (see “Using Attributes” and “Implementing an InterfaceResolver”).
Fields of a persistent event can be simple numeric types and their associated System types, strings, objects (projected as embedded/serial objects), enumerations, and types derived from collection classes. These types can also be contained in arrays, nested collections, and collections of arrays.
Once a schema has been imported, XEP can be used to store, query, update and delete data at very high rates. Stored events are immediately available for querying, or for full object or global access. The EventPersister, Event, and EventQuery<T> classes provide the main features of the XEP API. They are used in the following sequence:
The following section describes two very short applications that demonstrate all of these features.

Simple Applications to Store and Query Persistent Events

This section describes two very simple applications that use XEP to create and access persistent events:
  • The StoreEvents program — opens a connection to an InterSystems IRIS database, creates a schema for the events to be stored, uses an instance of Event to store the array of objects as persistent events, then closes the connection and terminates.
  • The QueryEvents program — opens a new connection accessing the same namespace as StoreEvents, creates an instance of EventQuery<T> to read and delete the previously stored events, then closes the connection and terminates.
Note:
It is assumed that these applications have exclusive use of the system, and run in two consecutive processes.
Both programs use instances of xep.samples.SingleStringSample, which is one of the classes defined in the XEP sample applications (see “XEP Sample Applications” for details).

The StoreEvents Program

In StoreEvents, a new instance of EventPersister is created and connected to a specific InterSystems IRIS namespace. A schema is imported for the SingleStringSample class, and the test database is initialized by deleting all existing events from the extent of the class. An instance of Event is created and used to store an array of SingleStringSample objects as persistent events. The connection is then terminated. The new events will persist in the InterSystems IRIS database, and will be accessed by the QueryEvents program (described in the next section).
The StoreEvents Program: Creating a schema and storing events
using System;
using InterSystems.XEP;
using xep.samples; // compiled XEPTest.csproj

public class StoreEvents {
  private static String className = "xep.samples.SingleStringSample";
  private static SingleStringSample[] eventItems = SingleStringSample.generateSampleData(12);

  public static void Main(String[] args) {
    for (int i=0; i < eventItems.Length; i++) {
      eventItems[i].name = "String event " + i;
    }
    try {
      Console.WriteLine("Connecting and importing schema for " + className);
      EventPersister myPersister = PersisterFactory.CreatePersister();
      myPersister.Connect("127.0.0.1",51773,"User", "_SYSTEM", "SYS");
      try { // delete any existing SingleStringSample events, then import new ones
        myPersister.DeleteExtent(className);
        myPersister.ImportSchema(className);
      }
      catch (XEPException e) { Console.WriteLine("import failed:\n" + e); }
      Event newEvent = myPersister.GetEvent(className);
      long[] itemIDs = newEvent.Store(eventItems);  // store array of events
      Console.WriteLine("Stored " + itemIDs.Length + " of "
        + eventItems.Length + " objects. Closing connection...");
      newEvent.Close();
      myPersister.Close();
    }
    catch (XEPException e) { Console.WriteLine("Event storage failed:\n" + e); }
  } // end Main()
} // end class StoreEvents
Before StoreEvents.Main() is called, the xep.samples.SingleStringSample.generateSampleData() method is called to generate sample data array eventItems (see “XEP Sample Applications” for information on sample classes).
In this example, XEP methods perform the following actions:
All of these methods are discussed in detail later in this chapter. See “Creating and Connecting an EventPersister” for information on opening, testing, and closing a connection. See “Importing a Schema” for details about schema creation. See “Storing and Modifying Events” for details about using the Event class and deleting an extent.

The QueryEvents Program

This example assumes that QueryEvents runs immediately after the StoreEvents process terminates (see “The StoreEvents Program”). QueryEvents establishes a new database connection that accesses the same namespace as StoreEvents. An instance of EventQuery<T> is created to iterate through the previously stored events, print their data, and delete them.
The QueryEvents Program: Fetching and processing persistent events
using System;
using InterSystems.XEP;
using SingleStringSample = xep.samples.SingleStringSample; // compiled XEPTest.csproj

public class QueryEvents {
  public static void Main(String[] args) {
    EventPersister myPersister = null;
    EventQuery<SingleStringSample> myQuery = null;
    try {
// Open a connection, then set up and execute an SQL query
      Console.WriteLine("Connecting to query SingleStringSample events");
      myPersister = PersisterFactory.CreatePersister();
      myPersister.Connect("127.0.0.1",51773,"User","_SYSTEM","SYS");
      try {
        Event newEvent = myPersister.GetEvent("xep.samples.SingleStringSample");
        String sql = "SELECT * FROM xep_samples.SingleStringSample WHERE %ID BETWEEN 3 AND ?";
        myQuery = newEvent.CreateQuery<SingleStringSample>(sql);
        newEvent.Close();
        myQuery.AddParameter(12);  // assign value 12 to SQL parameter
        myQuery.Execute();
      }
      catch (XEPException e) {Console.WriteLine("createQuery failed:\n" + e);}

// Iterate through the returned data set, printing and deleting each event
      SingleStringSample currentEvent;
      currentEvent = myQuery.GetNext(); // get first item
      while (currentEvent != null) {
        Console.WriteLine("Retrieved " + currentEvent.name);
        myQuery.DeleteCurrent();
        currentEvent = myQuery.GetNext(); // get next item
      }
      myQuery.Close();
      myPersister.Close();
    }
    catch (XEPException e) {Console.WriteLine("QueryEvents failed:\n" + e);}
  } // end Main()
}  // end class QueryEvents
In this example, XEP methods perform the following actions:
All of these methods are discussed in detail later in this chapter. See “Creating and Connecting an EventPersister” for information on opening, testing, and closing a connection. See “Using Queries” for details about creating and using an instance of EventQuery<T>.

Creating and Connecting an EventPersister

The EventPersister class is the main entry point for the XEP API. It provides the methods for connecting to the database, importing schemas, handling transactions, and creating instances of Event to access events in the database.
An instance of EventPersister is created and destroyed by the following methods:
The following method is used to create a connection:
  • EventPersister.Connect() — takes String arguments for namespace, username, password, and establishes a connection to the specified InterSystems IRIS namespace.
The following example establishes a connection:
Creating and Connecting an EventPersister: Creating a connection
// Open a connection
  string host = "127.0.0.1";
  int port = 51773;
  string namespace = "USER";
  string username = "_SYSTEM";
  string password = "SYS";
  EventPersister myPersister = PersisterFactory.CreatePersister();
  myPersister.Connect(host, port, namespace,username,password);
  // perform event processing here . . .
  myPersister.Close();
The EventPersister.Connect() method establishes a connection to the specified port and namespace of the host machine. If no connection exists in the current process, a new connection is created. If a connection already exists, the method returns a reference to the existing connection object.
If the server address is 127.0.0.1 or localhost, the connection will default to a local shared memory connection, which is faster than the standard TCP/IP connection (see “Shared Memory Connections” in Using the InterSystems Managed Provider for .NET).
Note:
Always call close() to release resources
Always call Close() on an instance of EventPersister before it goes out of scope to ensure that all locks, licenses, and other resources associated with the connection are released.

Importing a Schema

Before an instance of a .NET class can be stored as a persistent event, a schema must be imported for the class. The schema defines the database structure in which the event will be stored. XEP provides two different schema import models: flat schema and full schema. The main difference between these models is the way in which .NET objects are projected to InterSystems IRIS events. A flat schema is the optimal choice if performance is essential and the event schema is fairly simple. A full schema offers a richer set of features for more complex schemas, but may have an impact on performance. See “Schema Customization and Mapping” for a detailed discussion of schema models and related subjects.
The following methods are used to analyze a .NET class and import a schema of the desired type:
  • EventPersister.ImportSchema() — imports a flat schema. Takes an argument specifying a .dll file name, a fully qualified class name, or an array of class names, and imports all classes and any dependencies found in the specified locations. Returns a String array containing the names of all successfully imported classes.
  • EventPersister.ImportSchemaFull() — imports a full schema. Takes the same arguments and returns the same class list as ImportSchema(). A class imported by this method must declare a user-generated IdKey (see “Using IdKeys”).
  • Event.IsEvent() — a static Event method that takes a .NET object or class name of any type as an argument, tests to see if the specified object can be projected as a valid XEP event (see “Requirements for Imported Classes”), and throws an appropriate error if it is not valid.
The import methods are identical except for the schema model used. The following example imports a simple test class and its dependent class:
Importing a Schema: Importing a class and its dependencies
The following classes from namespace test are to be imported:
namespace test {
  public class MainClass {
    public MainClass() {}
    public String  myString;
    public test.Address  myAddress;
  }

  public class Address {
    public String  street;
    public String  city;
    public String  state;
  }
}
The following code uses ImportSchema() to import the main class, test.MainClass, after calling IsEvent() to make sure it can be projected. Dependent class test.Address is also imported automatically when test.MainClass is imported:
  try {
    Event.IsEvent("test.MainClass"); // throw an exception if class is not projectable
    myPersister.ImportSchema("test.MainClass");
  }
  catch (XEPException e) {Console.WriteLine("Import failed:\n" + e);}
In this example, instances of dependent class test.Address will be serialized and embedded in the same InterSystems IRIS object as other fields of test.MainClass. If ImportSchemaFull() had been used instead, stored instances of test.MainClass would contain references to instances of test.Address stored in a separate InterSystems IRIS class extent.

Storing and Modifying Events

Once the schema for a class has been imported (see “Importing a Schema”), an instance of Event can be created to store and access events of that class. The Event class provides methods to store, update, or delete persistent events, create queries on the class extent, and control index updating. This section discusses the following topics:

Creating and Storing Events

Instances of the Event class are created and destroyed by the following methods:
  • EventPersister.GetEvent() — takes a className String argument and returns an instance of Event that can store and access events of the specified class. Optionally takes an indexMode argument that specifies the default way to update index entries (see “Controlling Index Updating” for details).
    Note:
    Target Class
    An instance of Event can only store, access, or query events of the class specified by the className argument in the call to GetEvent(). In this chapter, class className is referred to as the target class. In the examples, the target class is always SingleStringSample.
  • Event.Close() — closes the Event instance and releases the native code resources associated with it.
The following Event method stores .NET objects of the target class as persistent events:
  • Store() — adds one or more instances of the target class to the database. Takes either an event or an array of events as an argument, and returns a long database ID (or 0 if the database id could not be returned) for each stored event.
    Important:
    When an event is stored, it is not tested in any way, and it will never change or overwrite existing data. Each event is appended to the extent at the highest possible speed, or silently ignored if an event with the specified key already exists in the database.
The following example creates an instance of Event with SingleStringSample as the target class, and uses it to project an array of .NET SingleStringSample objects as persistent events. The example assumes that myPersister has already been created and connected, and that a schema has been imported for the SingleStringSample class. See “Simple Applications to Store and Query Persistent Events” for an example of how this is done. See “XEP Sample Applications” for information on SingleStringSample and the sample programs that define and use it.
Storing and Modifying Events: Storing an array of objects
  SingleStringSample[] eventItems = SingleStringSample.generateSampleData(12);
  try {
    Event newEvent = myPersister.GetEvent("xep.samples.SingleStringSample");
    long[] itemIdList = newEvent.Store(eventItems);  // store all events
    int itemCount = 0;
    for (int i=0; i < itemIdList.Length; i++) {
      if (itemIdList[i]>0) itemCount++;
    }
    Console.WriteLine("Stored " + itemCount + " of " + eventItems.Length + " events");
    newEvent.Close();
  }
  catch (XEPException e) { Console.WriteLine("Event storage failed:\n" + e); }
  • The generateSampleData() method of SingleStringSample generates twelve SingleStringSample objects and stores them in an array named eventItems.
  • The EventPersister.GetEvent() method creates an Event instance named newEvent with SingleStringSample as the target class.
  • The Event.Store() method is called to project each object in the eventItems array as a persistent event in the database.
    The method returns an array named itemIdList, which contains a long object ID for each successfully stored event, or 0 for an object that could not be stored. Variable itemCount is incremented once for each ID greater than 0 in itemIdList, and the total is printed.
  • When the loop terminates, the Event.Close() method is called to releaseassociated resources.
Note:
Always call Close() to release resources
Always call Close() on an instance of EventPersister before it goes out of scope to ensure that all locks, licenses, and other resources associated with the connection are released.

Accessing Stored Events

Once a persistent event has been stored, an Event instance of that target class provides the following methods for reading, updating, deleting the event:
  • DeleteObject() — takes a database object ID or IdKey as an argument and deletes the specified event from the database.
  • GetObject() — takes a database object ID or IdKey as an argument and returns the specified event.
  • UpdateObject() — takes a database object ID or IdKey and an Object of the target class as arguments, and updates the specified event.
If the target class uses a standard object ID, it is specified as a long value (as returned by the Store() method described in the previous section). If the target class uses an IdKey, it is specified as an array of Object where each item in the array is a value for one of the fields that make up the IdKey (see “Using IdKeys”).
In the following example, array itemIdList contains a list of object ID values for some previously stored SingleStringSample events. The example arbitrarily updates the first six persistent events in the list and deletes the rest.
Note:
See “Creating and Storing Events” for the example that created the itemIdList array. This example also assumes that an EventPersister instance named myPersister has already been created and connected to the database.
Storing and Modifying Events: Fetching, updating, and deleting events
// itemIdList is a previously created array of SingleStringSample event IDs
  try {
    Event newEvent = myPersister.GetEvent("xep.samples.SingleStringSample");
    int itemCount = 0;
    for (int i=0; i < itemIdList.Length; i++) {
      try { // arbitrarily update events for first 6 Ids and delete the rest
        SingleStringSample eventObject = (SingleStringSample)newEvent.GetObject(itemIdList[i]);
        if (i<6) {
          eventObject.name = eventObject.name + " (id=" + itemIdList[i] + ")" + " updated!";
          newEvent.UpdateObject(itemIdList[i], eventObject);
          itemCount++;
        } else {
          newEvent.DeleteObject(itemIdList[i]);
        }
      }
      catch (XEPException e) {Console.WriteLine("Failed to process event:\n" + e);}
    }
    Console.WriteLine("Updated " + itemCount + " of " + itemIdList.Length + " events");
    newEvent.Close();
  }
  catch (XEPException e) {Console.WriteLine("Event processing failed:\n" + e);}
  • The EventPersister.GetEvent() method creates an Event instance named newEvent with SingleStringSample as the target class.
  • Array itemIdList contains a list of object ID values for some previously stored SingleStringSample events (see “Creating and Storing Events” for the example that created itemIdList).
    In the loop, each item in itemIdList is processed. The first six items are changed and updated, and the rest of the items are deleted. The following operations are performed:
    • The Event.GetObject() method fetches the SingleStringSample object with the object ID specified in itemIdList[i], and assigns it to variable eventObject.
    • The value of the eventObject name field is changed.
    • If the eventObject is one of the first six items in the list, Event.UpdateObject() is called to update it in the database. Otherwise, Event.DeleteObject() is called to delete the object from the database.
  • After all of the IDs in itemIdList have been processed, the loop terminates and a message displays the number of events updated.
  • The Event.Close() method is called to release associated resources.
See “XEP Sample Applications” for information on the sample programs that define and use the SingleStringSample class.
See “Using Queries” for a description of how to access and modify persistent events fetched by a simple SQL query.
Deleting Test Data
When initializing a test database, it is frequently convenient to delete an entire class, or delete all events in a specified extent. The following EventPersister methods delete classes and extents from the InterSystems IRIS database:
  • DeleteClass() — takes a className string as an argument and deletes the specified InterSystems IRIS class.
  • DeleteExtent() — takes a className string as an argument and deletes all events in the extent of the specified class.
These methods are intended primarily for testing, and should be avoided in production code. See “Classes and Extents” in the Orientation Guide for Server-Side Programming for a detailed definition of these terms.

Controlling Index Updating

By default, indexes are not updated when a call is made to one of the Event methods that act on an event in the database (see “Accessing Stored Events”). Indexes are updated asynchronously, and updating is only performed after all transactions have been completed and the Event instance is closed. No uniqueness check is performed for unique indexes.
Note:
This section only applies to classes that use standard object IDs or generated IdKeys (see “Using IdKeys”). Classes with user-assigned IdKeys can only be updated synchronously.
There are a number of ways to change this default indexing behavior. When an Event instance is created by EventPersister.GetEvent() (see “Creating and Storing Events”), the optional indexMode parameter can be set to specify a default indexing behavior. The following options are available:
  • Event.INDEX_MODE_ASYNC_ON — enables asynchronous indexing. This is the default when the indexMode parameter is not specified.
  • Event.INDEX_MODE_ASYNC_OFF — no indexing will be performed unless the StartIndexing() method is called.
  • Event.INDEX_MODE_SYNC — indexing will be performed each time the extent is changed, which can be inefficient for large numbers of transactions. This index mode must be specified if the class has a user-assigned IdKey.
The following Event methods can be used to control asynchronous index updating for the extent of the target class:
  • StartIndexing() — starts asynchronous index building for the extent of the target class. Throws an exception if the index mode is Event.INDEX_MODE_SYNC.
  • StopIndexing() — stops asynchronous index building for the extent. If you do not want the index to be updated when the Event instance is closed, call this method before calling Event.Close().
  • WaitForIndexing() — takes an int timeout value as an argument and waits for asynchronous indexing to be completed. The timeout value specifies the number of seconds to wait (wait forever if -1, return immediately if 0). It returns true if indexing has been completed, or false if the wait timed out before indexing was completed. Throws an exception if the index mode is Event.INDEX_MODE_SYNC.

Using Queries

The Event class provides a way to create an instance of EventQuery<T>, which can execute a limited SQL query on the extent of the target class. EventQuery<T> methods are used to execute the SQL query, and to retrieve, update, or delete individual items in the query resultset.
The following topics are discussed:
Note:
The examples in this section assume that EventPersister object myPersister has already been created and connected, and that a schema has been imported for the SingleStringSample class. See “Simple Applications to Store and Query Persistent Events” for an example of how this is done.

Creating and Executing a Query

The following methods create and destroy an instance of EventQuery<T>:
Queries submitted by an instance of EventQuery<T> will return .NET objects of the specified generic type T (the target class of the Event instance that created the query object). Queries supported by the EventQuery<T> class are a subset of SQL select statements, as follows:
  • Queries must consist of a SELECT clause, a FROM clause, and (optionally) standard SQL clauses such as WHERE and ORDER BY.
  • The SELECT and FROM clauses must be syntactically legal, but they are actually ignored during query execution. All fields that have been mapped are always fetched from the extent of target class T.
  • SQL expressions may not refer to arrays of any type, nor to embedded objects or fields of embedded objects.
  • The InterSystems IRIS system-generated object ID may be referred to as %ID. Due to the leading %, this will not conflict with any field called id in a .NET class.
The following EventQuery<T> methods define and execute the query:
The following example executes a simple query on events in the xep.samples.SingleStringSample extent (see “XEP Sample Applications” for information on the sample programs that define and use the SingleStringSample class.).
Using Queries: Creating and executing a query
  Event newEvent = myPersister.GetEvent("xep.samples.SingleStringSample");
  EventQuery<SingleStringSample> myQuery = null;
  String sql =
    "SELECT * FROM xep_samples.SingleStringSample WHERE %ID BETWEEN ? AND ?";

  myQuery = newEvent.CreateQuery<SingleStringSample>(sql);
  myQuery.AddParameter(3);  // assign value 3 to first SQL parameter
  myQuery.AddParameter(12);  // assign value 12 to second SQL parameter
  myQuery.Execute();   // get resultset for IDs between 3 and 12
The EventPersister.GetEvent() method creates an Event instance named newEvent with SingleStringSample as the target class.
The Event.CreateQuery() method creates an instance of EventQuery<T> named myQuery, which will execute the SQL query and hold the resultset. The sql variable contains an SQL statement that selects all events in the target class with IDs between two parameter values.
The EventQuery<T>.AddParameter() method is called twice to assign values to the two parameters.
When the EventQuery<T>.Execute() method is called, the specified query is executed for the extent of the target class, and the resultset is stored in myQuery.
By default, all data is fetched for each object in the resultset, and each object is fully initialized. See “Defining the Fetch Level” for options that limit the amount and type of data fetched with each object.

Processing Query Data

After a query has been executed, the following EventQuery<T> methods can be used to access items in the query resultset, and update or delete the corresponding persistent events in the database:
  • GetNext() — returns the next object of the target class from the resultset. Returns null if there are no more items in the resultset.
  • UpdateCurrent() — takes an object of the target class as an argument and uses it to update the persistent event most recently returned by GetNext().
  • DeleteCurrent() — deletes the persistent event most recently returned by GetNext() from the database.
  • GetAll() — uses GetNext() to get all items from the resultset, and returns them in a List. Cannot be used for updating or deleting. GetAll() and GetNext() cannot access the same resultset — once either method has been called, the other method cannot be used until Execute() is called again.
See “Accessing Stored Events” for a description of how to access and modify persistent events identified by Id or IdKey.
Using Queries: Updating and Deleting Query Data
  myQuery.Execute();   // get resultset
  SingleStringSample currentEvent = myQuery.GetNext();
  while (currentEvent != null) {
    if (currentEvent.name.StartsWith("finished")) {
      myQuery.DeleteCurrent();   // Delete if already processed
    } else {
      currentEvent.name = "in process: " + currentEvent.name;
      myQuery.UpdateCurrent(currentEvent);    // Update if unprocessed
    }
    currentEvent = myQuery.GetNext();
  }
  myQuery.Close();
In this example, the call to EventQuery<T>.Execute() is assumed to execute the query described in the previous example (see “Creating and Executing a Query”), and the resultset is stored in myQuery. Each item in the resultset is a SingleStringSample object.
The first call to GetNext() gets the first item from the resultset and assigns it to currentEvent.
In the while loop, the following process is applied to each item in the resultset:
  • If currentEvent.name starts with the string "finished", DeleteCurrent() deletes the corresponding persistent event from the database.
  • Otherwise, the value of currentEvent.name is changed, and UpdateCurrent() is called. It takes currentEvent as its argument and uses it to update the persistent event in the database.
  • The call to GetNext() returns the next SingleStringSample object from the resultset and assigns it to currentEvent.
After the loop terminates, Close() is called to release the native code resources associated with myQuery.
Note:
Always call Close() to release resources
Always call Close() on an instance of EventPersister before it goes out of scope to ensure that all locks, licenses, and other resources associated with the connection are released.

Defining the Fetch Level

The fetch level is an Event property that can be used to control the amount of data returned when running a query. This is particularly useful when the underlying event is complex and only a small subset of event data is required.
The following EventQuery<T> methods set and return the current fetch level:
The following fetch level values are supported:
  • Event.OPTION_FETCH_LEVEL_ALL — This is the default. All data is fetched, and the returned event is fully initialized.
  • Event.OPTION_FETCH_LEVEL_DATATYPES_ONLY — Only datatype fields are fetched. This includes all simple numeric types and their associated System types, strings, and enumerations. All other fields are set to null.
  • Event.OPTION_FETCH_LEVEL_NO_ARRAY_TYPES — All types are fetched except arrays. All fields of array types, regardless of dimension, are set to null. All datatypes, object types (including serialized types) and collections are fetched.
  • Event.OPTION_FETCH_LEVEL_NO_COLLECTIONS — All types are fetched except implementations of collection classes.
  • Event.OPTION_FETCH_LEVEL_NO_OBJECT_TYPES — All types are fetched except object types. Serialized types are also considered object types and are not fetched. All datatypes, array types and collections are fetched.

Schema Customization and Mapping

This section provides details about how a .NET class is mapped to an InterSystems IRIS event schema, and how a schema can be customized for optimal performance. In many cases, a schema can be imported for a simple class without providing any meta-information. In other cases, it may be necessary or desirable to customize the way in which the schema is imported. The following sections provide information on customized schemas and how to generate them:
  • Schema Import Models — describes the two schema import models supported by XEP.
  • Using Attributes — XEP attributes can be added to a .NET class to specify the indexes that should be created. They can also be added to optimize performance by specifying fields that should not be imported or fields that should be serialized.
  • Using IdKeys — Annotations can be used to specify IdKeys (index values used in place of the default object ID), which are required when importing a full schema.
  • Implementing an InterfaceResolver — By default, a flat schema does not import fields declared as interfaces. Implementations of the InterfaceResolver interface can be used to during schema import to specify the actual class of a field declared as an interface.
  • Schema Mapping Rules — provides a detailed description of how .NET classes are mapped to InterSystems IRIS event schemas.

Schema Import Models

XEP provides two different schema import models: flat schema and full schema. The main difference between these models is the way in which .NET objects are projected to InterSystems IRIS events.
  • The Embedded Object Projection Model (Flat Schema) — imports a flat schema where all objects referenced by the imported class are serialized and embedded, and all fields declared in all ancestor classes are collected and projected as if they were declared in the imported class itself. All data for an instance of the class is stored as a single InterSystems IRIS %Library.Persistent object, and information about the original .NET class structure is not preserved.
  • The Full Object Projection Model (Full Schema) — imports a full schema where all objects referenced by the imported class are projected as separate InterSystems IRIS %Persistent objects. Inherited fields are projected as references to fields in the ancestor classes, which are also imported as InterSystems IRIS %Persistent classes. There is a one-to-one correspondence between .NET source classes and InterSystems IRIS projected classes, so the .NET class inheritance structure is preserved.
Full object projection preserves the inheritance structure of the original .NET classes, but may have an impact on performance. Flat object projection is the optimal choice if performance is essential and the event schema is fairly simple. Full object projection can be used for a richer set of features and more complex schemas if the performance penalty is acceptable.

The Embedded Object Projection Model (Flat Schema)

By default, XEP imports a schema that projects referenced objects by flattening. In other words, all objects referenced by the imported class are serialized and embedded, and all fields declared in all ancestor classes are collected and projected as if they were declared in the imported class itself. The corresponding InterSystems IRIS event extends %Library.Persistent, and contains embedded serialized objects where the original .NET object contained references to external objects.
This means that a flat schema does not preserve inheritance in the strict sense on the InterSystems IRIS side. For example, consider these three .NET classes:
class A {
    String a;
}
class B : class A {
    String b;
}
class C : class B {
    String c;
}
Importing class C results in the following InterSystems IRIS class:
Class C : %Persistent ... {
     Property a As %String;
     Property b As %String;
     Property c As %String;
}
No corresponding InterSystems IRIS events will be generated for the A or B classes unless they are specifically imported. Event C on the InterSystems IRIS side does not extend either A or B. If imported, A and B would have the following structures:
Class A : %Persistent ... {
     Property a As %String;
}
Class B : %Persistent ... {
     Property a As %String;
     Property b As %String;
}
All operations will be performed only on the corresponding InterSystems IRIS event. For example, calling Store() on objects of type C will only store the corresponding C InterSystems IRIS events.
If a .NET child class hides a field of the same name that is also declared in its superclass, the XEP engine always uses the value of the child field.

The Full Object Projection Model (Full Schema)

The full object model imports a schema that preserves the .NET inheritance model by creating a matching inheritance structure in InterSystems IRIS. Rather than serializing all object fields and storing all data in a single InterSystems IRIS object, the schema establishes a one-to-one relationship between the .NET source classes and InterSystems IRIS projected classes. The full object projection model stores each referenced class separately, and projects fields of a specified class as references to objects of the corresponding InterSystems IRIS class.
Referenced classes must include an attribute that creates a user-defined IdKey (either [Id] or [Index] — see “Using IdKeys”). When an object is stored, all referenced objects are stored first, and the resulting IdKeys are stored in the parent object. As with the rest of XEP, there are no uniqueness checks, and no attempts to change or overwrite existing data. The data is simply appended at the highest possible speed. If an IdKey value references an event that already exists, it will simply be skipped, without any attempt to overwrite the existing event.
The [Embedded] class level attribute can be used to optimize a full schema by embedding instances of the marked class as serialized objects rather than storing them separately.
Note:
See the FlightLog sample program (listed in “XEP Sample Applications”) for a demonstration of how to use the full object model.

Using Attributes

The XEP engine infers XEP event metadata by examining a .NET class. Additional information can be specified in the .NET class via attributes, which can be found in the Intersystems.XEP.attributes namespace. As long a .NET object conforms to the definition of an XEP persistent event (see “Requirements for Imported Classes”), it is projected as an InterSystems IRIS event, and there is no need to customize it.
Some attributes are applied to individual fields in the class to be projected, while others are applied to the entire class:
  • Field Attributes — are applied to a field in the class to be imported:
    • [Id] — specifies that the field will act as an IdKey.
    • [Serialized] — indicates that the field should be stored and retrieved in its serialized form.
    • [Transient] — indicates that the field should be excluded from import.
  • Class Attributes — are applied to the entire class to be imported:
    • [Embedded] — indicates that a field of this class in a full schema should be embedded (as in a flat schema) rather than referenced.
    • [Index] — declares an index for the class.
[Id] (field level attribute)
The value of a field marked with [Id] will be used as an IdKey that replaces the standard object ID (see “Using IdKeys”). Only one field per class can use this attribute, and the field must be a String, int, or long (double is permitted but not recommended). To create a compound IdKey, use the [Index] attribute instead. A class marked with [Id] cannot also declare a compound primary key with [Index]. An exception will be thrown if both attributes are used on the same class.
The following parameter must be specified:
  • generated — a bool specifying whether or not XEP should generate key values.
    • generated = true — (the default setting) key value will be generated by InterSystems IRIS and the field marked as [Id] must be Int64. This field is expected to be null prior to insert/store and will be filled automatically by XEP upon completion of such an operation.
    • generated=false — the user-assigned value of the marked field will be used as the IdKey value. Fields can be String, int, Int32, long or Int64.
In the following example, the user-assigned value of the ssn field will be used as the object ID:
using Id = InterSystems.XEP.Attributes.Id;
public class Person {
  [Id(generated=false)]
  Public String  ssn;
  public String  name;
  Public String dob;
}
[Serialized] (field level attribute)
The [Serialized] attribute indicates that the field should be stored and retrieved in its serialized form.
This attribute optimizes storage of serializable fields (including arrays, which are implicitly serializable). The XEP engine will call the relevant read or write method for the serial object, rather than using the default mechanism for storing or retrieving data. An exception will be thrown if the marked field is not serializable.
Example:
using Serialized = InterSystems.XEP.Attributes.Serialized;
public class MyClass {
  [Serialized]
  public  xep.samples.Serialized   serialized;
  [Serialized]
  public  int[,,,]   quadIntArray;
  [Serialized]
  public  String[,]   doubleStringArray;
}

// xep.samples.Serialized:
[Serializable]
public class Serialized {
  public  String  name;
  public  int     value;
}


[Transient] (field level attribute)
The [Transient] attribute indicates that the field should be excluded from import. The marked field will not be projected to InterSystems IRIS, and will be ignored when events are stored or loaded.
Example:
using Transient = InterSystems.XEP.Attributes.Transient;
public class MyClass {
  // this field will NOT be projected:
  [Transient]
  public  String  transientField;

  // this field WILL be projected:
  public  String  projectedField;
}
[Embedded] (class level attribute)
The [Embedded] attribute can be used when a full schema is to be generated (see “Schema Import Models”). It indicates that a field of this class should be serialized and embedded (as in a flat schema) rather than referenced when projected to InterSystems IRIS.
Examples:
using Embedded = InterSystems.XEP.Attributes.Embedded;
[Embedded]
public class Address {
  String  street;
  String  city;
  String  zip;
  String  state;
}

[Index] (class level attribute)
The [Index] attribute can be used to declare one or more composite indexes.
Arguments must be specified for the following parameters:
  • name — a String containing the name of the composite index
  • fields — an array of String containing the names of the fields that comprise the composite index
  • type — the index type. The xep.attributes.IndexType enumeration includes the following possible types:
Example:
using Index = InterSystems.XEP.Attributes.Index;
using IndexType = InterSystems.XEP.Attributes.IndexType;

[Index(name="indexOne",fields=new string[]{"ssn","dob"},type=IndexType.idkey)]
public class Person {
  public String  name;
  public String dob;
  public String  ssn;
}

Using IdKeys

IdKeys are index values that are used in place of the default object ID. Both simple and composite IdKeys are supported by XEP, and a user-generated IdKey is required for a .NET class that is imported with a full schema (see “Importing a Schema”). IdKeys on a single field can be created with the [Id] attribute. To create a composite IdKey, add an [Index] attribute with IndexType idkey. For example, given the following class:
  class Person {
    String name;
    int id;
    String dob;
  }
the default storage structure uses the standard object ID as a subscript:
^PersonD(1)=$LB("John",12,"1976-11-11")
The following attribute uses the name and id fields to create a composite IdKey named newIdKey that will replace the standard object ID:
  [Index(name="newIdKey", fields=new String[]{"name","id"},type=IndexType.idkey)]
This would result in the following global structure:
^PersonD("John",12)=$LB("1976-11-11")
XEP will also honor IdKeys added by other means, such as SQL commands (see “Using the Unique, PrimaryKey, and IDKey Keywords with Indices” in Using InterSystems SQL). The XEP engine will automatically determine whether the underlying class contains an IdKey, and generate the appropriate global structure.
There are a number of limitations on IdKey usage:
  • An IdKey value must be unique. If the IdKey is user-generated, uniqueness is the responsibility of the calling application, and is not enforced by XEP. If the application attempts to add an event with a key value that already exists in the database, the attempt will be silently ignored and the existing event will not be changed.
  • A class that declares an IdKey cannot be indexed asynchronously if it also declares other indexes.
  • There is no limit of the number of fields in a composite IdKey, but the fields must be String, int, or long, or their corresponding System types. Although double can also be used, it is not recommended.
  • There may be a performance penalty in certain rare situations requiring extremely high and sustained insert rates.
See “Accessing Stored Events” for a discussion of Event methods that allow retrieval, updating and deletion of events based on their IdKeys.
See “SQL and Object Use of Multidimensional Storage” in Using Globals for information on IdKeys and the standard InterSystems IRIS storage model. See “Defining and Building Indices” in Using InterSystems SQL for information on IdKeys in SQL.
Sample programs IdKeyTest and FlightLog provide demonstrations of IdKey usage (see “XEP Sample Applications” for details about the sample programs).

Implementing an InterfaceResolver

When a flat schema is imported, information on the inheritance hierarchy is not preserved (see “Schema Import Models”). This creates a problem when processing fields whose types are declared as interfaces, since the XEP engine must know the actual class of the field. By default, such fields are not imported into a flat schema. This behavior can be changed by creating implementations of Intersystems.XEP.InterfaceResolver to resolve specific interface types during processing.
Note:
InterfaceResolver is only relevant for the flat schema import model, which does not preserve the .NET class inheritance structure. The full schema import model establishes a one-to-one relationship between .NET and InterSystems IRIS classes, thus preserving the information needed to resolve an interface.
An implementation of InterfaceResolver is passed to EventPersister before calling the flat schema import method, ImportSchema() (see “Importing a Schema”). This provides the XEP engine with a way to resolve interface types during processing. The following EventPersister method specifies the implementation that will be used:
The following example imports two different classes, calling a different, customized implementation of InterfaceResolver for each class:
Schema Customization: Applying an InterfaceResolver
  try {
    myPersister.SetInterfaceResolver(new test.MyFirstInterfaceResolver());
    myPersister.ImportSchema("test.MyMainClass");

    myPersister.SetInterfaceResolver(new test.MyOtherInterfaceResolver());
    myPersister.ImportSchema("test.MyOtherClass");
  }
  catch (XEPException e) {Console.WriteLine("Import failed:\n" + e);}
The first call to SetInterfaceResolver() sets a new instance of MyFirstInterfaceResolver (described in the next example) as the implementation to be used during calls to the import methods. This implementation will be used in all calls to ImportSchema() until SetInterfaceResolver() is called again to specify a different implementation.
The first call to ImportSchema() imports class test.MyMainClass, which contains a field declared as interface test.MyFirstInterface. The instance of MyFirstInterfaceResolver will be used by the import method to resolve the actual class of this field.
The second call to SetInterfaceResolver() sets an instance of a different InterfaceResolver class as the new implementation to be used when ImportSchema() is called again.
All implementations of InterfaceResolver must define the following method:
  • InterfaceResolver.GetImplementationClass() returns the actual type of a field declared as an interface. This method has the following parameters:
    • interfaceClass — the interface to be resolved.
    • declaringClass — class that contains a field declared as interfaceClass.
    • fieldName — string containing the name of the field in declaringClass that has been declared as an interface.
The following example defines an interface, an implementation of that interface, and an implementation of InterfaceResolver that resolves instances of the interface.
Schema Customization: Implementing an InterfaceResolver
In this example, the interface to be resolved is test.MyFirstInterface:
namespace test {
  public interface MyFirstInterface{ }
}
The test.MyFirstImpl class is the implementation of test.MyFirstInterface that should be returned by the InterfaceResolver:
namespace test {
  public class MyFirstImpl : MyFirstInterface {
    public MyFirstImpl() {}
    public MyFirstImpl(String s) { fieldOne = s; }
    public String  fieldOne;
  }
}
The following implementation of InterfaceResolver returns class test.MyFirstImpl if the interface is test.MyFirstInterface, or null otherwise:
using Intersystems.XEP;
namespace test {
  public class MyFirstInterfaceResolver : InterfaceResolver {
    public MyFirstInterfaceResolver() {}
    public Type GetImplementationType(Type declaringClass,
           String fieldName, Type interfaceClass) {
      if (interfaceClass == typeof(test.MyFirstInterface)) {
        return typeof(test.MyFirstImpl);
      }
      return null;
    }
  }
When an instance of MyFirstInterfaceResolver is specified by SetInterfaceResolver(), subsequent calls to ImportSchema() will automatically use that instance to resolve any field declared as test.MyFirstInterface. For such each field, the GetImplementationClass() method will be called with parameter declaringClass set to the class that contains the field, fieldName set to the name of the field, and interfaceClass set to test.MyFirstInterface. The method will resolve the interface and return either test.MyFirstImpl or null.

Schema Mapping Rules

This section provides details about how an XEP schema is structured. The following topics are discussed:
  • Requirements for Imported Classes — describes the structural rules that a .NET class must satisfy to produce objects that can be projected as persistent events.
  • Naming Conventions — describes how .NET class and field names are translated to conform to InterSystems IRIS naming rules.

Requirements for Imported Classes

The XEP schema import methods cannot produce a valid schema for a .NET class unless it satisfies the following requirements:
  • If the imported InterSystems IRIS class or any derived class will be used to execute queries and access stored events, the .NET source class must explicitly declare an argumentless public constructor.
  • The .NET source class cannot contain fields declared as Object, or arrays or collections that use Object as part of their declaration. An exception will be thrown if the XEP engine encounters such fields. Use the [Transient] attribute (see “Using Attributes”) to prevent them from being imported.
The Event.IsEvent() method can be used to analyze a .NET class or object and determine if it can produce a valid event in the XEP sense. In addition to the conditions described above, this method throws an XEPException if any of the following conditions are detected:
  • a circular dependency
  • an untyped collection
  • a Dictionary key value that is not a String, a simple numeric type or its associated System type, or an enumeration.
Fields of a persistent event can be simple numeric types or their associated System types, objects (projected as embedded/serial objects), enumerations, and types derived from collection classes. These types can also be contained in arrays, nested collections, and collections of arrays.
By default, projected fields may not retain all features of the .NET class. Certain fields are changed in the following ways:
  • Although the .NET class may contain static fields, they are excluded from the projection by default. There will be no corresponding InterSystems IRIS properties. Additional fields can be excluded by using the [Transient] attribute (see “Using Attributes”).
  • In a flat schema (see “Schema Import Models”), all object types, including inner (nested) .NET classes, are projected as %SerialObject classes in InterSystems IRIS. The fields within the objects are not projected as separate InterSystems IRIS properties, and the objects are opaque from the viewpoint of ObjectScript.
  • A flat schema projects all inherited fields as if they were declared in the child class.

Naming Conventions

Corresponding InterSystems IRIS class and property names are identical to those in .NET, with the exception of two special characters allowed in .NET but not InterSystems IRIS:
  • $ (dollar sign) is projected as a single "d" character on the InterSystems IRIS side.
  • _ (underscore) is projected as a single "u" character on the InterSystems IRIS side.
Class names are limited to 255 characters, which should be sufficient for most applications. However, the corresponding global names have a limit of 31 characters. Since this is typically not sufficient for a one-to-one mapping, the XEP engine transparently generates unique global names for class names longer than 31 characters. Although the generated global names are not identical to the originals, they should still be easy to recognize. For example, the xep.samples.SingleStringSample class will receive global name xep.samples.SingleStrinA5BFD.
Previous section   Next section