GigaSpaces XAP 7.0 API

com.j_spaces.core.client
Class TemplateMatchCodes

java.lang.Object
  extended by com.j_spaces.core.client.TemplateMatchCodes

public class TemplateMatchCodes
extends Object

This class provides constants to be used when performing Query using templates.
The extended-indexing space attribute should be defined for templates from classes that use
the GE, LE, LT, GT match codes in order to perform fast search.
The compareTo method MUST be defined for modifiers in order to perform
GE, LE, LT, GT match.

Code example:
The min/max example shows how to query the space to find an entry with min/max attribute value. 
Note the space configuration file should include the following:

 <JavaSpaces>
        <space-config>
                <engine>
                        <extended-match>
                                <enabled-classes>*</enabled-classes> 
                                <min_ext_index_activation_size>1</min_ext_index_activation_size>
                        </extended-match>
                </engine>
        </space-config>
 </JavaSpaces>


import com.j_spaces.core.IJSpace;
import com.j_spaces.core.client.TemplateMatchCodes;
import com.j_spaces.core.client.ExternalEntry;
import com.j_spaces.core.client.SQLQuery;
import com.j_spaces.core.client.SpaceFinder;
import net.jini.core.lease.Lease;
import net.jini.space.JavaSpace;

import java.util.*;

public class MinMax {

    IJSpace space;

    String fields[] = new String [] {"First_Name", "Last_Name", "ID"} ;
    String types[] = new String [] {"java.lang.String", "java.lang.String", "java.lang.Integer"};
    boolean indexes[] = new boolean [] {false, false, true};

    int min = 0;
    int max = 0;

    public void populateSpace(int q) throws Exception {

        Random rnd = new Random();

        Object[] values = new Object[3];
                System.out.println("about to write " + q +" entries to space");
        for (int i = 0; i < q; i++) {
            values[0] = "FirstName"+ i;
            values[1] = "LastName" + i;
            int next = rnd.nextInt();
            if (next < min)
                min = next;
            if (next > max)
                max = next;
            values[2] = new Integer(next);
            ExternalEntry msg = new ExternalEntry("Employee", values, fields, types);
            msg.setIndexIndicators(indexes);
            space.write(msg, null, Lease.FOREVER);
                        if (i%10000==0)
                        {
                        System.out.println( new Date(System.currentTimeMillis()) + " - wrote " + i +" entries to space");
                        }
        }

        System.out.println("Inserted " + q + " entries; range from " + min + " to " + max);
    }

    public void queryMax() throws Exception {
        Object[] values = new Object[3];
        values[0] = null;
        values[1] = null;
        values[2] = new Integer(Integer.MAX_VALUE);
        short[] matchCodes = new short[3];
        matchCodes[2] = TemplateMatchCodes.LE;
        ExternalEntry template = new ExternalEntry("Employee", values);
        template.setExtendedMatchCodes(matchCodes);
        long time = System.currentTimeMillis();
        ExternalEntry result = (ExternalEntry)space.read(template, null, JavaSpace.NO_WAIT);
        time = System.currentTimeMillis() - time;
        Object[] v = result.getFieldsValues();
        System.out.println("Max value for Field ID is : " + v[2] + " ; Query time : " + time + "ms" + " - Entry UID:" + result.m_UID );
                showEntry(result);
    }

    public void queryMin() throws Exception {
        Object[] values = new Object[3];
        values[0] = null;
        values[1] = null;
        values[2] = new Integer(Integer.MIN_VALUE);
        short[] matchCodes = new short[3];
        matchCodes[2] = TemplateMatchCodes.GE;
        ExternalEntry template = new ExternalEntry("Employee", values);
        template.setExtendedMatchCodes(matchCodes);
        long time = System.currentTimeMillis();
        ExternalEntry result = (ExternalEntry)space.read(template, null, JavaSpace.NO_WAIT);
        time = System.currentTimeMillis() - time;
        Object[] v = result.getFieldsValues();
        System.out.println("Min value for Field ID is : " + v[2] + " ; Query time : " + time + "ms"+ " - Entry UID:" + result.m_UID );
                showEntry(result);
}

    public static void main(String[] args) throws Exception {

        System.out.println("Welcome to GigaSpaces min/max demo!");
        MinMax mm = new MinMax();
        System.out.println("About to connect to space " + args[0] +  "..." );
        mm.space = (IJSpace)SpaceFinder.find(args[0]);
        System.out.println("Connect to space " + args[0] +  " OK!" );
        System.out.println("Clean Space");
        mm.space.clean();
        mm.populateSpace(new Integer(args[1]).intValue());
        mm.queryMax();
        mm.queryMin();
    }


        public void showEntry(ExternalEntry entry)
        {
                System.out.println(" ------  ENTRY DATA ---------");  
                System.out.println("First_Name=" + entry.getFieldsValues()[0]);  
                System.out.println("Last_Name=" + entry.getFieldsValues() [1]);  
                System.out.println("ID="+ entry.getFieldsValues() [2]);  

        }
}

Version:
4.0
Author:
Yechiel Fefer

Field Summary
static short EQ
          equal operation
static short GE
          grater-equal operation
static short GT
          greater than operation
static short IS_NULL
          entry field is null operation (template field not relevant)
static short LE
          less-equal operation
static short LT
          less than operation
static short NE
          not equal operation
static short NOT_NULL
          entry field is not null operation (template field not relevant)
static short REGEX
          regular-expression matching operation (of a string field)
 
Constructor Summary
TemplateMatchCodes()
           
 
Method Summary
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EQ

public static final short EQ
equal operation

See Also:
Constant Field Values

NE

public static final short NE
not equal operation

See Also:
Constant Field Values

GT

public static final short GT
greater than operation

See Also:
Constant Field Values

GE

public static final short GE
grater-equal operation

See Also:
Constant Field Values

LT

public static final short LT
less than operation

See Also:
Constant Field Values

LE

public static final short LE
less-equal operation

See Also:
Constant Field Values

IS_NULL

public static final short IS_NULL
entry field is null operation (template field not relevant)

See Also:
Constant Field Values

NOT_NULL

public static final short NOT_NULL
entry field is not null operation (template field not relevant)

See Also:
Constant Field Values

REGEX

public static final short REGEX
regular-expression matching operation (of a string field)

See Also:
Constant Field Values
Constructor Detail

TemplateMatchCodes

public TemplateMatchCodes()

GigaSpaces XAP 7.0 API

Copyright © GigaSpaces.