org.soyatec.windowsazure.table
Class TableServiceContext

java.lang.Object
  extended by org.soyatec.windowsazure.table.TableServiceContext
All Implemented Interfaces:
IBatchExecutor

public class TableServiceContext
extends java.lang.Object
implements IBatchExecutor

This class provide API to manipulate ITableServiceEntity, such as insert, update, delete etc.

Batch operations
The TableServiceContext implements the IBatchExecutor. Some TRANSCATION supported operations can be enlist a single batch operation. Example:
      SampleEntity sampleEntity1 = createSampleEntity();
                SampleEntity sampleEntity2 = createSampleEntity();
                try {
                        tableServiceContext.startBatch();
                        tableServiceContext.insertEntity(sampleEntity1);
                        tableServiceContext.insertEntity(sampleEntity2);
                        tableServiceContext.executeBatch();
            catch(Exception e){
                //
            }
 
For more detail about Batch operation, see IBatchExecutor.


Field Summary
 
Fields inherited from interface org.soyatec.windowsazure.table.IBatchExecutor
BATCH_BOUNDARY_PREFIX, BATCH_PATH, CHANGESET_BOUNDARY_PREFIX
 
Constructor Summary
TableServiceContext(ITable table)
           
 
Method Summary
 void clearBatch()
          Cleanup current batch
 void deleteEntity(ITableServiceEntity obj)
          Deletes an entity within a table
 void deleteEntityIfNotModified(ITableServiceEntity obj)
          Deletes a table entity if the entity is not modified after it is loaded from azure table storage.
 void executeBatch()
          Commit current batch.
 java.lang.Class<? extends ITableServiceEntity> getModelClass()
          Default, azure table entities are retrieved as the instances of class SimpleTableServiceEntity.
 void insertEntity(ITableServiceEntity obj)
          Inserts a new entity into a table.
 boolean isInBatch()
          This operation indicates whether there is batch.
<T extends ITableServiceEntity>
T
loadEntity(T entity)
          Load the entity within table by the entity identifier(PartitionKey and RowKey).
 void mergeEntity(ITableServiceEntity obj)
          Merges table entity
 java.util.List<ITableServiceEntity> retrieveEntities(java.lang.Class<? extends ITableServiceEntity> modelClass)
          This operation queries entities in a table and set the model class of the table.
 java.util.List<ITableServiceEntity> retrieveEntities(org.soyatec.windowsazure.table.internal.CloudTableQuery query, java.lang.Class<? extends ITableServiceEntity> modelClass)
          This operation queries entities in a table and set the model class of the table..
 java.util.List<ITableServiceEntity> retrieveEntities(java.lang.String queryExpression, java.lang.Class<? extends ITableServiceEntity> modelClass)
          This operation queries entities in a table and set the model class of the table..
 java.util.List<ITableServiceEntity> retrieveEntitiesByKey(java.lang.String partitionKey, java.lang.String rowKey, java.lang.Class<? extends ITableServiceEntity> modelClass)
           
 void setModelClass(java.lang.Class<? extends ITableServiceEntity> modelClass)
          Set model class for this table.
 void startBatch()
          Starts a new batch operation set.
 void updateEntity(ITableServiceEntity obj)
          Updates an existing entity within a table by replacing it.
 void updateEntityIfNotModified(ITableServiceEntity obj)
          Updates table entity if the entity is not modified after it is loaded from azure table storage.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TableServiceContext

public TableServiceContext(ITable table)
Method Detail

deleteEntity

public void deleteEntity(ITableServiceEntity obj)
                  throws StorageException
Deletes an entity within a table

When an entity is successfully deleted, the entity is immediately marked for deletion and is no longer accessible to clients. The entity is later removed from the Table service during garbage collection.

Parameters:
obj - the object to be deleted
Throws:
StorageException - If entity is not exists, an exception is also thrown.

deleteEntityIfNotModified

public void deleteEntityIfNotModified(ITableServiceEntity obj)
                               throws StorageException
Deletes a table entity if the entity is not modified after it is loaded from azure table storage. ITableServiceEntity should have etag value which is retrieved from table storage.

Parameters:
obj - the object to be deleted
Throws:
StorageException
See Also:
#deleteEntity(AbstractTableServiceEntity)}

loadEntity

public <T extends ITableServiceEntity> T loadEntity(T entity)
                                         throws StorageException
Load the entity within table by the entity identifier(PartitionKey and RowKey).

Type Parameters:
T - AbstractTableServiceEntity instance of subclass of AbstractTableServiceEntity instance
Parameters:
entity - Specified Entity
Returns:
Entity within table
Throws:
StorageException

retrieveEntities

public java.util.List<ITableServiceEntity> retrieveEntities(java.lang.String queryExpression,
                                                            java.lang.Class<? extends ITableServiceEntity> modelClass)
                                                     throws StorageException
This operation queries entities in a table and set the model class of the table.. A query against a table returns a list of entities conforming to the criteria specified in the query.

Parameters:
queryExpression - If queryExpression is not given, all rows are return.
modelClass - If modelClass is not null, set the given class to the table's model class.
Returns:
A list of ITableServiceEntity
Throws:
StorageException

retrieveEntities

public java.util.List<ITableServiceEntity> retrieveEntities(org.soyatec.windowsazure.table.internal.CloudTableQuery query,
                                                            java.lang.Class<? extends ITableServiceEntity> modelClass)
                                                     throws StorageException
This operation queries entities in a table and set the model class of the table.. A query against a table returns a list of entities conforming to the criteria specified in the query.

Parameters:
query - A cloudTableQuery
modelClass - If modelClass is not null, set the given class to the table's model class.
Returns:
A list of ITableServiceEntity
Throws:
StorageException

retrieveEntities

public java.util.List<ITableServiceEntity> retrieveEntities(java.lang.Class<? extends ITableServiceEntity> modelClass)
                                                     throws StorageException
This operation queries entities in a table and set the model class of the table. A query against a table returns a list of entities conforming to the criteria specified in the query.

Parameters:
modelClass - If modelClass is not null, set the given class to the table's model class.
Returns:
A list of ITableServiceEntity
Throws:
StorageException

retrieveEntitiesByKey

public java.util.List<ITableServiceEntity> retrieveEntitiesByKey(java.lang.String partitionKey,
                                                                 java.lang.String rowKey,
                                                                 java.lang.Class<? extends ITableServiceEntity> modelClass)
                                                          throws StorageException
Parameters:
partitionKey -
rowKey -
modelClass - If modelClass is not null, set the given class to the table's model class.
Returns:
A list of entities who's partition key and row key are all euqals with the given.
Throws:
StorageException

insertEntity

public void insertEntity(ITableServiceEntity obj)
                  throws StorageException
Inserts a new entity into a table.

When inserting an entity into a table, you must specify values for the PartitionKey and RowKey system properties. Together, these properties form the primary key and must be unique within the table.

Both the PartitionKey and RowKey values must be string values; each key value may be up to 64 KB in size. If you are using an integer value for the key value, you should convert the integer to a fixed-width string, because they are canonically sorted. For example, you should convert the value 1 to 0000001 to ensure proper sorting.

Reference Insert entity

Parameters:
obj - The object to be inserted. The entity shoule be instance of AbstractTableServiceEntity or subclass of AbstractTableServiceEntity
Throws:
StorageException

updateEntity

public void updateEntity(ITableServiceEntity obj)
                  throws StorageException
Updates an existing entity within a table by replacing it. When updating an entity, you must specify the PartitionKey and RowKey system properties as part of the update operation.

An entity's ETag provides default optimistic concurrency for update operations. The ETag value is opaque and should not be read or relied upon. Before an update operation occurs, the Table service verifies that the entity's current ETag value is identical to the ETag value included with the update request. If the values are identical, the Table service determines that the entity has not been modified since it was retrieved, and the update operation proceeds.

If the entity's ETag differs from that specified with the update request, the update operation fails with status code 412 (Precondition Failed). This error indicates that the entity has been changed on the server since it was retrieved. To resolve this error, retrieve the entity again and reissue the request.

To force an unconditional update operation, set the value of the If-Match header to the wildcard character (*) on the request. Passing this value to the operation will override the default optimistic concurrency and ignore any mismatch in ETag values.

If the If-Match header is missing from the request, the service returns status code 400 (Bad Request). A request malformed in other ways may also return 400; see Table Service Error Codes for more information.

If the request specifies a property with a null value, that property is ignored, the update proceeds, and the existing entity is replaced.

Reference Update entity

Parameters:
obj - the object to be updated
Throws:
StorageException

updateEntityIfNotModified

public void updateEntityIfNotModified(ITableServiceEntity obj)
                               throws StorageException
Updates table entity if the entity is not modified after it is loaded from azure table storage. TableStorageEntity should have etag value which is retrieved from table storage.

Parameters:
obj - the object to be updated
Throws:
StorageException
See Also:
#updateEntity(AbstractTableServiceEntity)}

getModelClass

public java.lang.Class<? extends ITableServiceEntity> getModelClass()
Default, azure table entities are retrieved as the instances of class SimpleTableServiceEntity. A list of SimpleTableServiceEntity is return when retrieve entities from table service.

Model class can be changed to specified table service to represent more Model details.

Model class must be subclass of AbstractTableServiceEntity. Get the model class for this table.

Returns:
#modelClass

setModelClass

public void setModelClass(java.lang.Class<? extends ITableServiceEntity> modelClass)
Set model class for this table.

Parameters:
modelClass -
See Also:
#modelClass}

mergeEntity

public void mergeEntity(ITableServiceEntity obj)
                 throws StorageException
Merges table entity

Updates an existing entity within a table by merging new property values into the entity.

Remark

Any properties with null values are ignored by the Merge Entity operation. All other properties will be updated.

A property cannot be removed with a Merge Entity operation. To remove a property from an entity, replace the entity by calling the Update Entity operation.

When merging an entity, you must specify the PartitionKey and RowKey system properties as part of the merge operation.

An entity's ETag provides default optimistic concurrency for merge operations. The ETag value is opaque and should not be read or relied upon. Before a merge operation occurs, the Table service verifies that the entity's current ETag value is identical to the ETag value included with the request. If the values are identical, the Table service determines that the entity has not been modified since it was retrieved, and the merge operation proceeds.

If the entity's ETag differs from that specified with the merge request, the merge operation fails with status code 412 (Precondition Failed). This error indicates that the entity has been changed on the server since it was retrieved. To resolve this error, retrieve the entity again and reissue the request.

To force an unconditional merge operation, set the value of the If-Match header to the wildcard character (*) on the request. Passing this value to the operation will override the default optimistic concurrency and ignore any mismatch in ETag values.

Reference Merge entity

Parameters:
obj - the object to be merged
Throws:
StorageException

startBatch

public void startBatch()
Starts a new batch operation set. Don't make call to service.

Specified by:
startBatch in interface IBatchExecutor

clearBatch

public void clearBatch()
Cleanup current batch

Specified by:
clearBatch in interface IBatchExecutor

executeBatch

public void executeBatch()
Commit current batch. Make call to table service. Whether the batch operation is success or fail, current batch will be cleaned.

Specified by:
executeBatch in interface IBatchExecutor

isInBatch

public boolean isInBatch()
This operation indicates whether there is batch.

Specified by:
isInBatch in interface IBatchExecutor
Returns: