Query on Data Structures
The data structures can be queried in the cache if they have searchable attributes associated with them during data structure creation. These searchable attributes include:
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- For API details, refer to: ICache, QueryCommand, ICacheReader, SearchService, ExecuteReader, GetValue, IDistributedList, GetGroupData, FieldCount, IDistributedDictionary, GetByTag.
Search Data Structures by Groups
SQL Query
The following code sample fetches all items from the cache that have been associated with a specified Group, Electronics. The Group can be associated using DataTypeAttributes
class during data structure creation.
Important
- If there are multiple lists specified against the group, all of them will be returned in one result, granted that their data structures are similar.
- For multiple cache items, for example, if a
CacheItem
and a list belong to the same group, you need to ensure that the data is handled accordingly.
// Preconditions: Cache is already connected
// Items with groups exist in cache
// Custom class is query indexed through the NCache Management Center or config.ncconf
// Search for items with group
// Provide Fully Qualified Name (FQN) of your custom class
string query = "SELECT ProductID FROM FQN.Customer WHERE $Group$ = ?";
/// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("$Group$", "Electronics");
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
string ProductID = reader.GetValue<string>(1);
// Perform operations
}
}
else
{
// Null query result set retrieved
}
Group API
The following code sample fetches all the lists from the cache that have been associated with a specified group, Electronics. The group can be associated using DataTypeAttributes
class during the data structure creation.
// List with this group already exists in cache
string groupName = "Electronics";
IDictionary<string, IDistributedList<Product>> result = cache.SearchService.GetGroupData<IDistributedList<Product>>(groupName);
if (result != null && result.Count > 0)
{
// Iterate over list
foreach (var item in result)
{
foreach (var i in item.Value)
{
// Perform operations
}
}
}
else
{
// No data against the group found
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Search Data Structures by Tags
SQL Query
The following code sample fetches the items from the cache that have been associated with a specified tag, Stainless Steel. The tag can be associated using DataTypeAttributes
class during the data structure creation.
// Items are already present in the cache with tags
// Custom class is query indexed through NCache Management Center or config.ncconf
// Search for items with tags
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "Select $Value$ FROM FQN.Product WHERE $Tag$ = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "Stainless Steel");
// Executing query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Product result = reader.GetValue<Product>(1);
}
}
else
{
// Null query result set retrieved
}
Tag API
The following code sample fetches all the dictionary instances from the cache that have been associated with a specified tag, Stainless Steel. The tag can be associated using the DataTypeAttributes
class during the data structure creation.
// Items against this tag exist in cache
Tag tag = new Tag("Stainless Steel");
// Get dictionary against tag
IDictionary<string, IDistributedDictionary<string, Product>> result = cache.SearchService.GetByTag<IDistributedDictionary<string, Product>>(tag);
if (result != null && result.Count > 0)
{
foreach (var item in result)
{
// Perform operations
}
}
else
{
// Dictionary does not exist against tag
}
Search Data Structures by Named Tags
NCache uses the $DataType$
keyword that will fetch all the data structures with the specified Named Tag. The following code assumes a list has been added with Named Tag Discount with a value 0.4 and fetches it using queries.
// Data Structures exist with NamedTag Discount and value 0.4
// Create query
string query = "SELECT * FROM $DataType$ WHERE Discount = 0.4";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Execute Query to search cache
ICacheReader queryResult = cache.SearchService.ExecuteReader(queryCommand);
// queryResult contains all the keys and metadata of result
if (queryResult.FieldCount > 0)
{
while (queryResult.Read())
{
// Key of list can be fetched through
queryResult.GetValue<DistributedList>(1);
}
}
else
{
// Null query result set retrieved
}
Additional Resources
NCache provides a sample application for querying data structures on GitHub.
See Also
.NET: Alachisoft.NCache.Client.DataTypes namespace.
Java: com.alachisoft.ncache.client.datastructures namespace.
Python: ncache.client.datastructures class.
Node.js: DataStructureManager class.