Querying

Index Data Grid entries with a Protobuf schema and run Ickle queries against the fields, from the CLI and from REST.

Updated CompTech Team

A cache is a map. get(sku-42) works until the question is "every account in branch DAC". Ickle is Data Grid's query language, close to a reduced JPQL. It only sees fields you indexed, and it only indexes values it can read as a schema. The schema format is Protobuf. JSON in the cache is convenient for REST and invisible to the index.

The cluster and the Hot Rod settings are the ones from Connecting applications. Queries from a client use the same connection.

The schema

A schema is a .proto file you register with the server, not a file you leave on a laptop.

package lab;

message Account {
    optional int32 id = 1;
    optional string branch = 2;
    optional string currency = 3;
    optional int64 balance = 4;
}

Field numbers are forever. Renaming branch later is fine. Reusing number 2 for a different type is how you corrupt an index. package lab plus message Account is the type name lab.Account in the queries below.

Register it. From a pod that already has the developer password:

oc exec -n datagrid example-infinispan-0 -- \
  curl -sk -u "developer:${PASS}" \
  -X POST -H 'Content-Type: text/plain' \
  --data-binary @accounts.proto \
  https://example-infinispan:11222/rest/v2/schemas/accounts.proto

GET /rest/v2/schemas/accounts.proto should return the file. The name accounts.proto is the name you will also see in a backup of protoSchemas.

A cache that indexes it

apiVersion: infinispan.org/v2alpha1
kind: Cache
metadata:
  name: accounts
  namespace: datagrid
spec:
  clusterName: example-infinispan
  name: accounts
  template: |
    distributedCache:
      mode: "SYNC"
      owners: "2"
      statistics: "true"
      encoding:
        mediaType: "application/x-protostream"
      indexing:
        enabled: "true"
        storage: "local-heap"
        indexed-entities:
          - "lab.Account"

indexed-entities must be the fully qualified message name. A typo here does not fail the apply. It fails later, with an empty query and a log line. storage: filesystem keeps the index on disk and is the right choice once the index is larger than you want in the heap. local-heap is fine for this lab.

Indexing has a cost on the write. A cache you only ever get by key should not enable it. accounts is a query cache. sessions is not.

Write a value the index can see

The value has to be a Protobuf message, or a JSON document the server converts with the schema. The REST endpoint accepts Protobuf-encoded JSON when the content type says so:

oc exec -n datagrid example-infinispan-0 -- \
  curl -sk -u "developer:${PASS}" \
  -X POST \
  -H 'Content-Type: application/json;type=lab.Account' \
  https://example-infinispan:11222/rest/v2/caches/accounts/7 \
  -d '{"id":7,"branch":"DAC","currency":"BDT","balance":1000}'

_type or the type= parameter has to resolve to lab.Account. If the server rejects the body, the schema is not loaded or the field types do not match (balance as a string will do it). Fix the body. Do not turn indexing off to "make the put work".

Ickle

oc exec -n datagrid example-infinispan-0 -- \
  curl -sk -u "developer:${PASS}" \
  -G --data-urlencode 'query=from lab.Account a where a.branch = "DAC"' \
  https://example-infinispan:11222/rest/v2/caches/accounts?action=search

The query names the type, not the cache. The cache is in the URL. a.branch has to be an indexed field of that type. A field you forgot in indexed-entities or in the schema is not a filter. It is a query error or a silently unfiltered scan, depending on the version. Read the HTTP status.

A few shapes you will actually use:

from lab.Account a where a.branch = "DAC" and a.currency = "BDT"
from lab.Account a where a.balance > 500
select a.branch, sum(a.balance) from lab.Account a group by a.branch

Aggregations are real Ickle. Joins across caches are not the reason to pick Data Grid. If the question is a join of customers and accounts, the answer is usually PostgreSQL, and the cache holds the result you just computed. Ickle is for "the entries I already cached, filtered".

From Java, the same string goes through the Hot Rod query API on a RemoteCache<Integer, Account> whose marshaller is ProtoStream and whose SerializationContext knows lab.Account. The REST check above is enough to prove the index before you debug the marshaller.

When the query is wrong

What you see What is usually true
Empty hits, and the key exists via GET The value was stored as a plain string. The index never saw lab.Account.
Error naming an unknown type The schema was not registered on this cluster, or the package differs.
A field "is not indexed" It is missing from the schema the cache was configured with. Reindex after you fix it.
Stale hits after an update The update bypassed the cache and wrote to a store or a database directly. Write through the cache.

Reindexing is a cache operation, not a pod restart. Changing the schema's field numbers requires a plan. Adding an optional field is the compatible change.

Do not run an unbounded query from a user-facing request. from lab.Account on a large cache is a full index walk on the request thread. Page it (startOffset, maxResults on the search parameters) or require a selective where.

What's next

Cross-site replication copies accounts to the DR cluster. The schema has to be registered on both sites, or the DR site will store bytes it cannot query. Security decides who is allowed to run a query that returns every branch.