Generic Storage Adapter

  GigaSpaces 5.X

Documentation Home
Quick Start Guide
Release Notes

Previous release

  Search Here
Searching GigaSpaces Platform 5.X Documentation

                                               

Summary: Using the Generic SA interfaces to perform write-through or read-through.

This page is specific to:
GigaSpaces 5.x

If you're interested in another version, click it below:
GigaSpaces 6.0

Overview

The Generic Storage Adapter interfaces are designed to allow JavaSpace API users the ability to load data into the space from an external data source (ODBMS and RDBMS), and store space Entries in the external data source using Gigaspaces low level API.

The Generic SA interface includes the following interfaces:

  • IBasicPersistence – the basic mandatory interface. Includes basic methods for the SA to function correctly. This supports all space basic operations: write, read, take.
  • IQueryblePersistentAdapter – an optional interface. This interface supports queries and data lookup operations, e.g.,SQL,Regular expressions.
  • ITransactionalPersistentAdapter – an optional interface. This interface supports transactional operations and allows the space to perform operations together with transactional data sources.
  • IMultipleOperations – an optional interface. This interface supports the IJSpace.writeMultiple operation.
Unlike the JDBC Storage Adapter, the generic SA does support Enum data types.

PersistentContext

The PersistentContext (com.j_spaces.sadapter.GenericPA.PersistentContext; see Javadoc) is used by the Generic SA interfaces. It can be used in order to store database connections, file handles, transaction info, etc.

For a complete documentation of the class's methods, see Javadoc

PersistentEntry

The PersistentEntry (com.j_spaces.sadapter.GenericPA.PersistentEntry; see Javadoc) stores the Entry's fields in the data store. The UID is the unique key of the Entry, the className corresponds to table/metadata class name, and the field values include both data fields and administrative fields (such as lease, version, etc.).

For a complete documentation of the class's methods, see Javadoc

PersistentMatchTemplate

The PersistentMatchTemplate (com.j_spaces.sadapter.GenericPA.PersistentMatchTemplate; see Javadoc) is used to describe the match type. According to PersistentMatchTemplate Entries are selected from the underlying data source.

For a complete documentation of the class's methods, see Javadoc).

SAException

The SAException (com.j_spaces.core.sadapter.SAException; see Javadoc) is thrown when there are problems with the storage adapter implementation.

For a complete documentation of the class's methods, see Javadoc.

Example

The following code example illustrates an implementation of a Space Store class that loads and stores data into the space using the IBasicPersistence interface.

The Entry that is persistent and has loaded TestData is described below:

package com.j_spaces.examples.spacestore;

import java.sql.Timestamp;
import com.j_spaces.core.client.MetaDataEntry;
import com.j_spaces.core.client.ClientUIDHandler;
import com.j_spaces.core.client.EntryInfo;

public class TestData extends com.j_spaces.core.client.MetaDataEntry {
  public Integer id = null;
  public Timestamp date = null;
  public String item = null;
  public Double price = null;
  public Double quantity = null;

  public TestData() {
  }

  public TestData(Integer id) {
    setId(id);
  }

  public TestData(Integer id, Timestamp date, String item, Double price, Double quantity) {
    setId(id);
    this.date = date;
    this.item = item;
    this.price = price;
    this.quantity = quantity;
  }

  public String toString() {
    return "id <" + id + "> date <" + date + "> item <" + item + "> price <" + price +"> quantity <" 
    + quantity + ">";
  }

  void setId(Integer id) {
    this.id = id;
    if (id != null) {
      String uid = ClientUIDHandler.createUIDFromName(id.toString(),
          TestData.class.getName());
      EntryInfo info = new EntryInfo(uid, 0);
      this.__setEntryInfo(info);
    }
  }
  void setDate(Timestamp date) {
    this.date = date;
  }
  void setItem(String item) {
    this.item = item;
  }
  void setPrice(Double price) {
    this.price = price;
  }
  void setQuantity(Double quantity) {
    this.quantity = quantity;
  }

  Integer getId() {
    return id;
  }
  Timestamp getDate() {
    return date;
  }
  String getItem() {
    return item;
  }
  Double getPrice() {
    return price;
  }
  Double getQuantity() {
    return quantity;
  }

  public boolean equals(Object o) {
    if (o == this)
      return true;
    if (!(o instanceof TestData))
      return false;
    return (this.id.equals(((TestData)o).getId()));
  }

  public int hashCode() {
    int result = 17;
    result = 37*result + id.intValue();
    return result;
  }
}
package com.j_spaces.examples.spacestore;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import com.j_spaces.core.EntryHolder;
import com.j_spaces.core.sadapter.SAException;
import com.j_spaces.sadapter.GenericPA.GenericPersistentAdapter;
import com.j_spaces.sadapter.GenericPA.IBasicPersistence;
import com.j_spaces.sadapter.GenericPA.IQueryblePersistentAdapter;
import com.j_spaces.sadapter.GenericPA.PAObjectTypes;
import com.j_spaces.sadapter.GenericPA.PersistentContext;
import com.j_spaces.sadapter.GenericPA.PersistentEntry;
import com.j_spaces.sadapter.GenericPA.PersistentMatchTemplate;
import com.j_spaces.sadapter.GenericPA.Record;

public class SpaceStore implements IBasicPersistence {
       String myTableName = "MyTable";

       String myClassName = TestData.class.getName();

       Connection dbConnection;

       public void init(String url, String user, String password,
                     boolean isColdMode, Properties properties) throws SAException {
              try {
                     // connect to database
                     Class.forName("org.hsqldb.jdbcDriver").newInstance();
                     System.out.println("Connection to HSQL database - URL:" + url);// + "
                     // tableName = properties.getProperty("TableName");
                     dbConnection = DriverManager.getConnection(url, "sa", "");
                     System.out.println();
                     System.out.println(" ********* SPACE STORE STARTED ********* ");
                     System.out.println();
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }

       public void initContext(PersistentContext context) throws SAException {
              // TODO Auto-generated method stub

       }

       public void closeContext(PersistentContext context) throws SAException {
              // TODO Auto-generated method stub

       }

       public void insert(PersistentContext context, PersistentEntry entry)
                     throws SAException {
              if (entry.m_ClassName == null)
                     return;
              try {
                     if (entry.m_ClassName.equals(myClassName)) {
                            Vector fields = getFields("Insert", entry);
                            String insertSQL = "INSERT INTO " + myTableName
                                          + " VALUES(?,?,?,?,?,?)";
                            PreparedStatement psInsert = dbConnection
                                          .prepareStatement(insertSQL);
                            /*
                             * Field:0 - _DATE , TimeStamp Field:1 - ID INTEGER Field:2 -
                             * ITEM VARCHAR Field:3 - PRICE DOUBLE Field:4 - QUANTITY DOUBLE
                             */
                            psInsert.setString(1, (String) entry.getUID());
                            psInsert.setTimestamp(2, (Timestamp) fields.get(0));
                            psInsert.setInt(3, (Integer) fields.get(1));
                            psInsert.setString(4, (String) fields.get(2));
                            psInsert.setDouble(5, (Double) fields.get(3));
                            psInsert.setDouble(6, (Double) fields.get(4));
                            int r = psInsert.executeUpdate();
                     }
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }

       public int delete(PersistentContext context, String className, Object uid)
                     throws SAException {
              try {
                     if (className.equals(myClassName)) {
                            System.out.println("delete:" + className + " UID:" + uid);
                            String deleteSQL = "DELETE FROM " + myTableName
                                          + " WHERE __UID='" + uid + "'";
                            Statement st = dbConnection.createStatement();
                            st.executeUpdate(deleteSQL);
                            st.close();
                            return 1;
                     }
              } catch (Exception e) {
                     e.printStackTrace();
              }
              return 0;
       }

       public void update(PersistentContext context, PersistentEntry entry)
                     throws SAException {
              getFields("Update", entry);
       }

       public void createClassTable(PersistentContext context, String className,
                     String[] fieldNames, String[] fieldTypes, String uidFieldName,
                     String uidFieldType, boolean[] indexIndicators,
                     boolean isFifoSupported) throws SAException {
              // TODO Auto-generated method stub
              // create table
              try {
                     if (className.equals(myClassName)) {
                            System.out.println("createClassTable - ClassName:" + className);
                            /*
                             * Field:0 - _DATE , DATE Field:1 - ID INTEGER Field:2 - ITEM
                             * VARCHAR Field:3 - PRICE DOUBLE Field:4 - QUANTITY DOUBLE
                             */
                            String createSQL = "CREATE TABLE "
                                          + myTableName
                                          + "(__UID VARCHAR(3000) , _DATE date , ID INTEGER ,
                                           ITEM VARCHAR(3000), PRICE  DOUBLE,QUANTITY DOUBLE)";
                            Statement st = dbConnection.createStatement();
                            st.executeUpdate(createSQL);
                            st.executeUpdate("CREATE INDEX __UIDIndex ON " + myTableName
                                          + " (__UID )");
                            st.executeUpdate("CREATE INDEX _DATEIndex ON " + myTableName
                                          + " (_DATE )");
                            st.executeUpdate("CREATE INDEX IDIndex ON " + myTableName
                                          + " (ID)");
                            st.executeUpdate("CREATE INDEX QUANTITYIndex ON " + myTableName
                                          + " (QUANTITY )");
                            st.executeUpdate("CREATE INDEX PRICEIndex ON " + myTableName
                                          + " (PRICE)");
                            st.close();
                            System.out.println("createClassTable - ClassName:" + className
                                          + " OK!");
                     }
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }

       public void dropClassTable(PersistentContext context, String className)
                     throws SAException {
              try {
                     if (className == null)
                            return;
                     if (className.equals(myClassName)) {
                            Statement st = dbConnection.createStatement();
                            st.executeUpdate("DROP TABLE " + myTableName);
                     }
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }

       public Iterator getEntries(PersistentContext context, String className,
                     Object[] uids) throws SAException {
              // TODO Auto-generated method stub
              Iterator iter = null;
              Vector vec = new Vector();
              return vec.iterator();
       }

       public PersistentEntry getEntry(PersistentContext context,
                     String className, Object uid) throws SAException {
              // TODO Auto-generated method stub
              //System.out.println("getEntry By UID " + uid);
              return null;
       }

       public void shutDown() throws SAException {

       }

       public void createIndex(PersistentContext context, String className,
                     String fieldType, int fieldPosition) throws SAException {

       }

       public void dropIndex(PersistentContext context, String className,
                     String indexName) throws SAException {

       }

       public void flush(PersistentContext arg0) throws SAException {

       }

       public int getCount(PersistentContext context, String className)
                     throws SAException {
              return 0;
       }

       private Vector getFields(String operation, PersistentEntry entry) {
              Vector fields = new Vector();
              System.out.println("Operation:" + operation + " Entry "
                            + entry.getUID() + " Class:" + entry.getClazzName());
              int fldCount = entry.getFieldsValues().length;
              fldCount = fldCount - 7;
              for (int i = 0; i < fldCount; i++) {
                     System.out.println("Field:" + i + " " + entry.getFieldsValues()[i]);
                     fields.add(entry.getFieldsValues()[i]);
              }
              return fields;
       }

       private Vector getTemplateFields(String operation, PersistentMatchTemplate entry) {
              Vector fields = new Vector();
              System.out.println("Operation:" + operation + " Class:" + entry.getClazzName());
              int fldCount = entry.getFieldValues().length;
              for (int i = 0; i < fldCount; i++) {
                     System.out.println("Field:" + i + " " + entry.getFieldValues()[i]);
                     fields.add(entry.getFieldValues()[i]);
              }
              return fields;
       }
}

Wiki Content Tree


Your Feedback Needed!

We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.

Labels

 
(None)