Nuwan Sameera
Nuwan Sameera

Reputation: 819

Ignite/GridGain putAllIfAbsent

I am use GridGain as my persistence database. I have following requirements.

  1. Insert multiple records if key already not exists
  2. Put multiple records if key exists or not.

For 1, I saw cache.putIfAbsent(key, value) method to insert single record if not exists. But I didn't find cache.putAllIfAbsent(Map<key, value>) like method. I can you loop to insert multiple records one by one. Is it given performance issue?

For 2, I think I can use cache.putAll(Map<key, value>) method. If it proper way?

I run server in Google cloud Kubernetes engine as thick clients.

Upvotes: 0

Views: 92

Answers (1)

Pavel Tupitsyn
Pavel Tupitsyn

Reputation: 8986

  • putAll always overwrites existing records
  • putIfAbsent in a loop will be slower than putAll. Measure your specific use case to see by how much

If there is no requirement for ordering and atomicity, DataStreamer is a good choice. When allowOverwrite flag is false (default), you get putIfAbsent behavior, and good performance.

try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer("myCache")) {    
    stmr.allowOverwrite(false); // Don't overwrite existing data

    Map<Integer, String> entries = getMydata()
    stmr.addData(entries);
}

Upvotes: 3

Related Questions