org.springframework.data.mongodb.core.query.Update.setOnInsert()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(192)

本文整理了Java中org.springframework.data.mongodb.core.query.Update.setOnInsert()方法的一些代码示例,展示了Update.setOnInsert()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Update.setOnInsert()方法的具体详情如下:
包路径:org.springframework.data.mongodb.core.query.Update
类名称:Update
方法名:setOnInsert

Update.setOnInsert介绍

[英]Update using the $setOnInsert update modifier
[中]使用$setOnInsert更新修改器更新

代码示例

代码示例来源:origin: spring-projects/spring-integration

/**
 * If the specified key is not already associated with a value, associate it with the given value.
 * This is equivalent to
 * <pre> {@code
 * if (!map.containsKey(key))
 *   return map.put(key, value);
 * else
 *   return map.get(key);
 * }</pre>
 * except that the action is performed atomically.
 * <p>
 * @param key the metadata entry key
 * @param value the metadata entry value to store
 * @return null if successful, the old value otherwise.
 * @see java.util.concurrent.ConcurrentMap#putIfAbsent(Object, Object)
 */
@Override
public String putIfAbsent(String key, String value) {
  Assert.hasText(key, "'key' must not be empty.");
  Assert.hasText(value, "'value' must not be empty.");
  Query query = new Query(Criteria.where(ID_FIELD).is(key));
  query.fields().exclude(ID_FIELD);
  @SuppressWarnings("unchecked")
  Map<String, String> result = this.template.findAndModify(query, new Update().setOnInsert(VALUE, value),
      new FindAndModifyOptions().upsert(true), Map.class, this.collectionName);
  return result == null ? null : result.get(VALUE);
}

代码示例来源:origin: alturkovic/distributed-lock

@Override
public String acquire(final List<String> keys, final String storeId, final long expiration) {
 Assert.isTrue(keys.size() == 1, "Cannot acquire lock for multiple keys with this lock: " + keys);
 final String key = keys.get(0);
 final String token = tokenSupplier.get();
 if (StringUtils.isEmpty(token)) {
  throw new IllegalStateException("Cannot lock with empty token");
 }
 final Query query = Query.query(Criteria.where("_id").is(key));
 final Update update = new Update()
  .setOnInsert("_id", key)
  .setOnInsert("expireAt", LocalDateTime.now().plus(expiration, ChronoUnit.MILLIS))
  .setOnInsert("token", token);
 final FindAndModifyOptions options = new FindAndModifyOptions().upsert(true).returnNew(true);
 final LockDocument doc = mongoTemplate.findAndModify(query, update, options, LockDocument.class, storeId);
 final boolean locked = doc.getToken().equals(token);
 log.debug("Tried to acquire lock for key {} with token {} in store {}. Locked: {}", key, token, storeId, locked);
 return locked ? token : null;
}

代码示例来源:origin: org.springframework.integration/spring-integration-mongodb

/**
 * If the specified key is not already associated with a value, associate it with the given value.
 * This is equivalent to
 * <pre> {@code
 * if (!map.containsKey(key))
 *   return map.put(key, value);
 * else
 *   return map.get(key);
 * }</pre>
 * except that the action is performed atomically.
 * <p>
 * @param key the metadata entry key
 * @param value the metadata entry value to store
 * @return null if successful, the old value otherwise.
 * @see java.util.concurrent.ConcurrentMap#putIfAbsent(Object, Object)
 */
@Override
public String putIfAbsent(String key, String value) {
  Assert.hasText(key, "'key' must not be empty.");
  Assert.hasText(value, "'value' must not be empty.");
  Query query = new Query(Criteria.where(ID_FIELD).is(key));
  query.fields().exclude(ID_FIELD);
  @SuppressWarnings("unchecked")
  Map<String, String> result = this.template.findAndModify(query, new Update().setOnInsert(VALUE, value),
      new FindAndModifyOptions().upsert(true), Map.class, this.collectionName);
  return result == null ? null : result.get(VALUE);
}

相关文章