XAP8 == "XAP9NET" || XAP8 == "XAP9" || XAP8 == "XAP9NET")

Document (Schema-Free) API


Search XAP 8.0
Searching XAP 8.0 Documentation
Browse XAP 8.0
Offline Documentation

Download latest offline documentation
in HTML format:

xap-8.0-documentation.zip (15MB)

                                                              

Overview

The GigaSpaces document API exposes the space as Document Store. A document, which is represented by the class SpaceDocument, is essentially collection of key-value pairs, where the keys are strings and the values are primitives, String, Date, other documents, or collections thereof. Most importantly, the Space is aware of the internal structure of a document, and thus can index document properties at any nesting level and expose rich query semantics for retrieving documents.

Unlike POJOs, which force users to design a fixed data schema (in the form of a class definition) and adhere to it, a document is much more dynamic - users can add and remove properties at runtime as necessary. A Document always belongs to a certain type, represented by the class SpaceTypeDescriptor.

Before a certain Document instance is written to the space, its type should be introduced to it. The type has a name and controls metadata such as identifier property, routing property and which properties are initially indexed (naturally, you can also index new properties at runtime after adding them to your documents).

The Type controls metadata - so only the metadata is part of the type. A document can introduce new properties at will.

Note that the Document type does not describe the properties themselves (except for the names of the ID and Routing properties). These are completely dynamic and each instance can have a different set of properties (although in most cases Document instances of the same type are likely to have identical or similar set of properties).

Schema Evolution with Space Documents

Since a SpaceDocument is completely dynamic by nature, it is very easy to change or evolve your data model without ever taking down the Space. Simply change your application code to add additional properties or remove existing ones, and you're good to go. Even better, old and new versions can co-exist since the space does not enforce any restriction with regards to the property set of documents that belong to a certain type. This is a much more lightweight model in comparison to the classic POJO model, where a recompilation and in many cases a full space restart is required to change the data schema.

If POJO model cannot be replaced with Document model, yet some level of schema evolution is desired within the POJO model, Dynamic Properties can be used.

Type Definition

Before we start writing and reading SpaceDocument from the space, we need an initial schema definition of the document type.

For example, suppose we're implementing an electronic commerce system, and decided we need a type called Product with the following properties:

  • CatalogNumber : String
  • Category : String
  • Name : String
  • Description : String
  • Price : float
  • Features : Nested document (for example: Manufacturer=Acme, RequiresAssembly=false, weight=7.5)
  • Tags : Collection of Strings
  • Reviews : Collection of nested documents

We also decide that CatalogNumber will be a primary key, partitioning will be done by the Category property, and the properties Name, Price should be indexed since they participate in most of the queries executed. Remember, the type definition is for metadata only, so we're not concerned about Description and other such fields in the type definition, because Description isn't used for indexing or any other metadata.

The following is an example of how to introduce a new document type:

Note that this code does not reflect the complete model - most of the properties does not need to be introduced to the schema. Only properties with special roles (ID, Routing) are part of the schema definition. These meta model settings cannot be changed without restarting the space or dropping the type, clearing all its instances and reintroducing it again.

Creating and Writing Documents

To create a document create a Map<String,Object> with the requested properties, create a SpaceDocument object using the type name and properties, and write it to the space using the regular GigaSpace write method:

public void writeProduct1(GigaSpace gigaspace) {
    // 1. Create the properties:
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("CatalogNumber", "hw-1234");
    properties.put("Category", "Hardware");
    properties.put("Name", "Anvil");
    properties.put("Price", 9.99f);
    properties.put("Tags", new String[] {"heavy", "anvil"});        

    Map<String, Object> features = new HashMap<String, Object>();
    features.put("Manufacturer", "Acme");
    features.put("RequiresAssembly", false);
    features.put("Weight", 100);
    properties.put("Features", features);
        
    Map<String, Object> review1 = new HashMap<String, Object>();
    review1.put("Name", "Wile E. Coyote");
    review1.put("Rate", 1);
    review1.put("Comments", "Don't drop this on your toe, it will hurt.");
    Map<String, Object> review2 = new HashMap<String, Object>();
    review2.put("Name", "Road Runner");
    review2.put("Rate", 5);
    review2.put("Comments", "Beep Beep!");        
    properties.put("Reviews", new Map[] {review1, review2});

    // 2. Create the document using the type name and properties: 
    SpaceDocument document = new SpaceDocument("Product", properties);
    // 3. Write the document to the space:
    gigaspace.write(document);
}

Another way is to use the DocumentProperties class provided, which extends HashMap to provide fluent coding:

public void writeProduct2(GigaSpace gigaspace) {
    // 1. Create the properties:
    DocumentProperties properties = new DocumentProperties()
        .setProperty("CatalogNumber", "av-9876")
        .setProperty("Category", "Aviation")
        .setProperty("Name", "Jet Propelled Pogo Stick")
        .setProperty("Price", 19.99f)
        .setProperty("Tags", new String[] {"New", "Cool", "Pogo", "Jet"})
        .setProperty("Features", new DocumentProperties()
            .setProperty("Manufacturer", "Acme")
            .setProperty("RequiresAssembly", true)
            .setProperty("NumberOfParts", 42))
        .setProperty("Reviews", new DocumentProperties[] {
            new DocumentProperties()
                .setProperty("Name", "Wile E. Coyote")
                .setProperty("Rate", 1),
            new DocumentProperties()
                .setProperty("Name", "Road Runner")
                .setProperty("Rate", 5)});
    
    // 2. Create the document using the type name and properties: 
    SpaceDocument document = new SpaceDocument("Product", properties);
    // 3. Write the document to the space:
    gigaspace.write(document);
}

The GigaSpace.writeMultiple method can be used to write a batch of documents.
Update semantics are the same as POJO, except partial update that is not currently supported.
Use only alphanumeric characters (a-z, A-Z, 0-9) and the underscore ('_') to construct properties keys. Other characters might have special behaviours in GigaSpaces (for example: the dot ('.') is used to distinguish nested paths).

Reading and Removing Documents

There are three types of document queries:

Template Query

This type of query uses a SpaceDocument with type and any other set of properties values as a template for the query
For example: Read a document of type Product whose Name is Anvil:

public SpaceDocument readProductByTemplate(GigaSpace gigaSpace) {
    // Create template:
    SpaceDocument template = new SpaceDocument("Product");
    template.setProperty("Name", "Anvil");
    // Read:
    SpaceDocument result = gigaSpace.read(template);
    return result;
}

SQL Query

You can use the SQLQuery to search for matching SpaceDocument entries.
For example: Read a document of type Product whose Price is greater than 15:

public SpaceDocument readProductBySQL(GigaSpace gigaSpace) {
    // Create query:
    SQLQuery<SpaceDocument> query = 
        new SQLQuery<SpaceDocument>("Product", "Price > ?");
    query.setParameter(1, 15f);
    // Read:
    SpaceDocument result = gigaSpace.read(query);
    return result;
}
Consider indexing properties used in queries to boost performance.

Queries on nested properties are supported. For example, to read products manufactured by Acme:

public SpaceDocument[] readProductBySQLNested(GigaSpace gigaSpace) {
    // Create query:
    SQLQuery<SpaceDocument> query = 
        new SQLQuery<SpaceDocument>("Product", "Features.Manufacturer = ?");
    query.setParameter(1, "Acme");
    // Read:
    SpaceDocument[] result = gigaSpace.readMultiple(query, 10);
    return result;
}

ID Based Query

For example: Read a document of type Product whose ID is hw-1234:

public SpaceDocument readProductById(GigaSpace gigaSpace) {
    return gigaSpace.readById(new IdQuery<SpaceDocument>("Product", "hw-1234"));
}

Queries by multiple Ids are supported. For example:

public SpaceDocument[] readProductByMultipleIds(GigaSpace gigaSpace) {
    Object[] ids = new Object[] {"hw-1234", "av-9876"};
    ReadByIdsResult<SpaceDocument> result = 
        gigaSpace.readByIds(new IdsQuery<SpaceDocument>("Product", ids));
    return result.getResultsArray();
}

All other GigaSpace query operations (readIfExists, readMultiple, take, takeIfExists, takeMultiple, count, clear) are supported for documents entries as well.
All other Id based operations (readIfExists, takeById, takeIfExistsById, takeByIds) are supported for documents as well.
All overloads of those operations with timeout, transactions, modifiers etc. are supported for documents. The semantics is similar to POJOs.

Nested Properties

The Document properties values can be either scalars (integers, strings, enumuerations, etc), collections (arrays, lists), or nested properties (Map or an extension of map, such as DocumentProperties). Values must adhere to the same restrictions as in the POJO model (e.g. be serializable). Nested properties can be queried by using the dot ('.') notation to describe paths, as shown above.

It's highly recommended to use DocumentProperties for nested documents since it contains performance and memory footprint optimizations which are tailored for GigaSpaces usage.
While it's possible to use SpaceDocument as a property, it is probably a mistake, since it contains extra information which is not relevant for nested properties (type name, version, etc.).
Changing nested properties in an embedded space is not safe.

Indexing

Properties and nested paths can be indexed to boost queries performance. In the type registration sample above the Name and Price properties are indexed.

Since the schema is flexible and new properties might be added after the type has been registered, it is possible to add indexes dynamically as well.

For more information about indexing, see the Indexing page.

Events

Event containers (both Polling Container and Notify Container) support Space Document entries.

Here is a simple example of a polling event container configuration using a Document:

FIFO

FIFO Support is off by default with Document entries (same as with POJO). To enable FIFO support, modify the type introduction code and set the desired FIFO support mode. For example:

Changing FIFO support after a type has been registered is not supported.
For more information about FIFO, see the FIFO Support page.

Transactions and Optimistic Locking

Transactions and isolation modifiers semantics is identical to the POJO semantics. For more information about transactions, see the Transaction Management page.

Optimistic locking is disabled by default with Document entries (same as with POJO). To enable it, modify the type introduction code and set the optimistic locking support. For example:

Changing optimistic locking after a type has been registered is not supported.
For more information about optimistic locking, see the Optimistic Locking page.

Local Cache / Local View

Local View and Local Cache are supported for Documents. By default, the SpaceDocument instance is stored in the cache which speeds up query performance since the data does not need to be transformed from internal structure to SpaceDocument.

If you intend to use local cache or local view in a mixed POJO-Document environment, please refer to Document-POJO Interoperability.

Persistency

External Data Source is supported for space documents.
Example on how to implement an EDS that persists SpaceDocuments of type "Trade":

Different document database can be used to implement the document persistency - MongoDB, CouchDB and others.
Pojos can be persisted via document EDS as well, in the same way.

In order to support initialLoad of documents the relevant types must be declared in the "space" bean, so that they are registered in the space before initialLoad is invoked.
Document persistence is currently not provided by default - If needed, the External Data Source should be implemented to fit the required solution.

Space Filters

Space Filter are supported for space documents.

If you intend to use space filters in a mixed POJO-Document environment, please refer to Document-POJO Interoperability.

Space Replication Filters

Space Replication Filter are supported for space documents.

If you intend to use space filters in a mixed POJO-Document environment, please refer to Document-POJO Interoperability.

Advanced Options

IMPORTANT: This is an old version of GigaSpaces XAP. Click here for the latest version.

Additional resources: XAP Application Server | XAP Data Grid | XAP for Cloud Computing | XAP J2EE Support

Labels

 
(None)