Showing posts with label KeyAssociatedFilter. Show all posts
Showing posts with label KeyAssociatedFilter. Show all posts
Wednesday, July 10, 2013
Coherence Data Affinity Part 1 using KeyAssociation
Data affinity can be used to keep related objects in the same partition. If you have an order and line item caches, We can use this to keep related objects together thus increasing the efficiency of coherence.
The objects can be unevenly distributed if the child keys are not unique.
This can be done in two ways
1. Using a Key that implements KeyAssociation Interface in the Child Cache
using this method is illustrated below
2. Creating a class that implements KeyAssociator and configuring it CacheConfig.xml
Below is an illustration of how to achieve it using KeyAssociation
1. Lets create a Order class
package com.coher.dto;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import java.io.Serializable;
@Portable
public class OrderBean
implements Serializable
{
public OrderBean()
{
super();
}
@PortableProperty
private String mOrderNumber;
@PortableProperty
private String mCustomerName;
public OrderBean(String mOrderNumber, String mCustomerName)
{
super();
this.mOrderNumber = mOrderNumber;
this.mCustomerName = mCustomerName;
}
public void setOrderNumber(String mOrderNumber)
{
this.mOrderNumber = mOrderNumber;
}
public String getOrderNumber()
{
return mOrderNumber;
}
public void setCustomerName(String mCustomerName)
{
this.mCustomerName = mCustomerName;
}
public String getCustomerName()
{
return mCustomerName;
}
}
2. Lets Create a line item class
package com.coher.dto;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import java.io.Serializable;
@Portable
public class OrderLineItemBean
implements Serializable
{
public OrderLineItemBean()
{
super();
}
@PortableProperty
String mLineItemNumber;
@PortableProperty
String mOrderNumber;
@PortableProperty
String mItemName;
@PortableProperty
Integer mQuantity;
public OrderLineItemBean(String pOrderNumber, String pLineItemNumber,
String pItemName, Integer pQuantity)
{
super();
this.mOrderNumber = pOrderNumber;
this.mLineItemNumber = pLineItemNumber;
this.mItemName = pItemName;
this.mQuantity = pQuantity;
}
public void setOrderNumber(String pOrderNumber)
{
this.mOrderNumber = pOrderNumber;
}
public String getOrderNumber()
{
return mOrderNumber;
}
public void setItemNumber(String pLineItemNumber)
{
this.mLineItemNumber = pLineItemNumber;
}
public String getItemNumber()
{
return mLineItemNumber;
}
public void setItemName(String pItemName)
{
this.mItemName = pItemName;
}
public String getItemName()
{
return mItemName;
}
public void setQuantity(Integer pQuantity)
{
this.mQuantity = pQuantity;
}
public Integer getQuantity()
{
return mQuantity;
}
public String toString()
{
return mLineItemNumber + " - " + mOrderNumber + " - " + mItemName +
" - " + mQuantity;
}
}
3. Create a key association.
This class is used as a key for the detail cache entries and
package com.coher.dto;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import com.tangosol.net.cache.KeyAssociation;
import java.io.Serializable;
@Portable
public class OrderNumberKey implements KeyAssociation, Serializable
{
@PortableProperty
private String mOrderNumber;
@PortableProperty
private String mLineItemNumber;
public OrderNumberKey()
{
super();
}
public OrderNumberKey(String pOrderNumber, String pLineItemNumber)
{
super();
this.mOrderNumber = pOrderNumber;
this.mLineItemNumber = pLineItemNumber;
}
public Object getAssociatedKey()
{
return getOrderNumber();
}
public void setOrderNumber(String pOrderNumber)
{
this.mOrderNumber = pOrderNumber;
}
public String getOrderNumber()
{
return mOrderNumber;
}
public void setLineItemNumber(String pLineItemNumber)
{
this.mLineItemNumber = pLineItemNumber;
}
public String getLineItemNumber()
{
return mLineItemNumber;
}
public String toString()
{
return mOrderNumber +" "+ mLineItemNumber;
}
}
4. Package the jar and place it in the classpath of coherence server
5. Lets create a class that loads the cache. In this cache when loading the detail objects into the cache we use the Key Association we created above in Step 3.
package com.coher.dataaffinity;
import com.coher.dto.OrderBean;
import com.coher.dto.OrderLineItemBean;
import com.coher.dto.OrderNumberKey;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import java.util.Random;
public class DataAffinityLoader
{
public DataAffinityLoader()
{
super();
}
public static void main(String[] args)
{
NamedCache orderCache = CacheFactory.getCache("DistributedOrders");
NamedCache orderLineCache =
CacheFactory.getCache("DistributedOrderLines");
orderCache.clear();
orderLineCache.clear();
int k = 0;
for (int i = 0; i < 4; i++)
{
OrderBean ob = new OrderBean("" + i, "Customer" + i);
orderCache.put(ob.getOrderNumber(), ob);
Random r = new Random();
int l = r.nextInt(20);
for (int j = 0; j <= 3; j++)
{
k++;
OrderLineItemBean olib =
new OrderLineItemBean(ob.getOrderNumber(), "lineitem" + k,
"" + k, k);
orderLineCache.put(new OrderNumberKey(ob.getOrderNumber(),
olib.getItemNumber()), olib);
}
}
}
}
6. Lets create a class that reads and shows us the distribution
package com.coher.dataaffinity;
import com.coher.dto.OrderNumberKey;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.Member;
import com.tangosol.net.NamedCache;
import com.tangosol.net.PartitionedService;
import com.tangosol.util.Filter;
import com.tangosol.util.filter.EqualsFilter;
import com.tangosol.util.filter.KeyAssociatedFilter;
import java.util.Set;
public class DataAffinityCacheViewer
{
public DataAffinityCacheViewer()
{
super();
}
public static void main(String[] args)
{
NamedCache orderCache = CacheFactory.getCache("DistributedOrders");
NamedCache orderLineCache = CacheFactory.getCache("DistributedOrderLines");
System.out.println("orders Cache.size() = " + orderCache.size());
PartitionedService ps1 = (PartitionedService) orderCache.getCacheService();
Set<String> odrKeySet = (Set<String>) orderCache.keySet();
for (String key: odrKeySet)
{
Member member = ps1.getKeyOwner(key);
System.out.println("Coherence member:" + member.getId() + "; order key:" + key );
EqualsFilter filterEq = new EqualsFilter("getOrderNumber", key);
Filter filterAsc = new KeyAssociatedFilter(filterEq, key);
Set<OrderNumberKey> ONKeySet = (Set<OrderNumberKey>) orderLineCache.keySet(filterAsc);
PartitionedService ps2 = (PartitionedService) orderLineCache.getCacheService();
for (OrderNumberKey key1: ONKeySet)
{
Member member1 = ps2.getKeyOwner(key1);
System.out.println(" Coherence member:" + member1.getId() + "; Order Line Key:" + key1 );
}
}
}
}
7. Start the caches
8. Run the Data affinity loader to load the cache
9. Run the cache viewer to read the cache
Coherence member:1; order key:2
Coherence member:1; Order Line Key:2 lineitem9
Coherence member:1; Order Line Key:2 lineitem12
Coherence member:1; Order Line Key:2 lineitem11
Coherence member:1; Order Line Key:2 lineitem10
Coherence member:1; order key:3
Coherence member:1; Order Line Key:3 lineitem14
Coherence member:1; Order Line Key:3 lineitem15
Coherence member:1; Order Line Key:3 lineitem13
Coherence member:1; Order Line Key:3 lineitem16
Coherence member:5; order key:0
Coherence member:5; Order Line Key:0 lineitem3
Coherence member:5; Order Line Key:0 lineitem2
Coherence member:5; Order Line Key:0 lineitem4
Coherence member:5; Order Line Key:0 lineitem1
Coherence member:5; order key:1
Coherence member:5; Order Line Key:1 lineitem5
Coherence member:5; Order Line Key:1 lineitem8
Coherence member:5; Order Line Key:1 lineitem7
Coherence member:5; Order Line Key:1 lineitem6
rohith raj puchalapalli
Subscribe to:
Posts (Atom)