Using Sequences in Java

The Java Sequence class is used to manage database fields of type sequence. As sequences are effectively vectors of values, they are accessed through iterators. Where the C type mco_seq_iterator_h is used in C applications, the Java class Sequence serves the equivalent role in Java applications. The Sequence class also provides a powerful set of Analytics Methods for performing mathematical and statistical operations on sequences.

Inserting and Updating Sequences in Java

Normally sequence data is inserted using the Java Sequence method append(). For example consider the following schema definition:

 
    @Persistent
    public class Quote {
        @Indexable(unique=true)
        @Dimensiohn(21)
        String           symbol;
         
        @Sequence(type=Sequence.Type.UInt4, order=Sequence.Order.Ascending)]
        OrderedSequence day;
         
        [Sequence(type=Sequence.Type.Float)]
        UnorderedSequence price;
    };
     

With this class definition, the Quote sequence fields day and price can be populated with code like the following:

 
    con.startTransaction(Database.TransactionType.ReadWrite);
    Quote q = new Quote();
    q.symbol_= "IBM";
    con.insert(q);
    q.day.append(new int[] { 1, 2, 4, 5 });
    q.price.append(new float[] { 10, 20, 40, 50 });
    con.commitTransaction();
     

It may sometimes be necessary to insert values into an ordered time series. Values can be inserted into an existing sequence using the Sequence method insert(). For example, the following code snippet searches for the Quote object with symbol "IBM", then inserts day and price values:

 
    con.startTransaction(Database.TransactionType.ReadWrite);
    Cursor<Quote> cursor = new Cursor<Quote>(con, Quote.class, "symbol");
    Quote q = cursor.find("IBM");
    long pos  = q.day.search(3, OrderedSequence.Boundary.Exclusive, 
                    0, OrderedSequence.Boundary.Open).firstPosition();
    q.day.insert(pos, new int[] { 3 });
    q.price.insert(pos, new float[] { 30 });
    con.commitTransaction();