Example #1
0
  @Override
  public Map<String, ?> properties(RequestContext ctx) throws Exception {
    Map<String, Object> map = new HashMap<>();
    map.put(W, writeConcern.getWObject());
    map.put(WTIMEOUT, writeConcern.getWtimeout());
    map.put(J, writeConcern.getJ());
    map.put(FSYNC, writeConcern.getFsync());
    map.put(CONTINUEONERRORFORINSERT, writeConcern.getContinueOnErrorForInsert());

    return map;
  }
 private static WriteConcern toWriteConcern(
     final String writeConcernConstant, final String writeConcernConstantClassName) {
   WriteConcern writeConcern;
   if (Strings.isNotEmpty(writeConcernConstant)) {
     if (Strings.isNotEmpty(writeConcernConstantClassName)) {
       try {
         final Class<?> writeConcernConstantClass =
             LoaderUtil.loadClass(writeConcernConstantClassName);
         final Field field = writeConcernConstantClass.getField(writeConcernConstant);
         writeConcern = (WriteConcern) field.get(null);
       } catch (final Exception e) {
         LOGGER.error(
             "Write concern constant [{}.{}] not found, using default.",
             writeConcernConstantClassName,
             writeConcernConstant);
         writeConcern = DEFAULT_WRITE_CONCERN;
       }
     } else {
       writeConcern = WriteConcern.valueOf(writeConcernConstant);
       if (writeConcern == null) {
         LOGGER.warn(
             "Write concern constant [{}] not found, using default.", writeConcernConstant);
         writeConcern = DEFAULT_WRITE_CONCERN;
       }
     }
   } else {
     writeConcern = DEFAULT_WRITE_CONCERN;
   }
   return writeConcern;
 }
 static boolean bypassDocumentValidationNotSupported(
     final Boolean bypassDocumentValidation,
     final WriteConcern writeConcern,
     final ConnectionDescription description) {
   return bypassDocumentValidation != null
       && serverIsAtLeastVersionThreeDotTwo(description)
       && !writeConcern.isAcknowledged();
 }
  /** Gets the write concern for entity or returns the default write concern for this datastore */
  public WriteConcern getWriteConcern(Object clazzOrEntity) {
    WriteConcern wc = defConcern;
    if (clazzOrEntity != null) {
      Entity entityAnn = getMapper().getMappedClass(clazzOrEntity).getEntityAnnotation();
      if (entityAnn != null && !"".equals(entityAnn.concern()))
        wc = WriteConcern.valueOf(entityAnn.concern());
    }

    return wc;
  }
 static void checkBypassDocumentValidationIsSupported(
     final Connection connection,
     final Boolean bypassDocumentValidation,
     final WriteConcern writeConcern) {
   if (bypassDocumentValidation != null
       && serverIsAtLeastVersionThreeDotTwo(connection.getDescription())
       && !writeConcern.isAcknowledged()) {
     throw new MongoClientException(
         "Specifying bypassDocumentValidation with an unacknowledged WriteConcern is not supported");
   }
 }
 static void validateCollationAndWriteConcern(
     final Connection connection, final Collation collation, final WriteConcern writeConcern) {
   if (!serverIsAtLeastVersionThreeDotFour(connection.getDescription()) && collation != null) {
     throw new IllegalArgumentException(
         format(
             "Collation not supported by server version: %s",
             connection.getDescription().getServerVersion()));
   } else if (collation != null && !writeConcern.isAcknowledged()) {
     throw new MongoClientException(
         "Specifying collation with an unacknowledged WriteConcern is not supported");
   }
 }
  public void indexTweet(Tweet tweet) {

    this.connect();

    DBObject dbObject =
        BasicDBObjectBuilder.start()
            // .add("_id", status.getId())
            .add("id", tweet.getId())
            .add("text", tweet.getText())
            .add("created_at", tweet.getCreatedAt())
            .add("hashtags", tweet.getHashTags())
            .add("userId", tweet.getUserId())
            .add("userName", tweet.getUserName())
            .add("userScreenName", tweet.getUserScreenName())
            .add("coordinates", tweet.getCoordinates())
            .get();

    // WriteConcern wc = WriteConcern.UNACKNOWLEDGED;
    WriteConcern wc = WriteConcern.ACKNOWLEDGED;
    wc = wc.continueOnError(true);

    WriteResult writeResult = this.dbCollection.insert(dbObject, wc);
  }
 static void checkBypassDocumentValidationIsSupported(
     final AsyncConnection connection,
     final Boolean bypassDocumentValidation,
     final WriteConcern writeConcern,
     final AsyncCallableWithConnection callable) {
   Throwable throwable = null;
   if (bypassDocumentValidation != null
       && serverIsAtLeastVersionThreeDotTwo(connection.getDescription())
       && !writeConcern.isAcknowledged()) {
     throwable =
         new MongoClientException(
             "Specifying bypassDocumentValidation with an unacknowledged WriteConcern is "
                 + "not supported");
   }
   callable.call(connection, throwable);
 }
  private void configureDs_() {
    List<Class<?>> pending = new ArrayList<Class<?>>();
    Map<Class<?>, Integer> retries = new HashMap<Class<?>, Integer>();
    List<ApplicationClass> cs = Play.classes.all();
    for (ApplicationClass c : cs) {
      Class<?> clz = c.javaClass;
      if (clz.isAnnotationPresent(Entity.class)) {
        try {
          debug("mapping class: %1$s", clz.getName());
          morphia_.map(clz);
        } catch (ConstraintViolationException e) {
          error(e, "error mapping class [%1$s]", clz);
          pending.add(clz);
          retries.put(clz, 1);
        }
      }
    }

    while (!pending.isEmpty()) {
      for (Class<?> clz : pending) {
        try {
          debug("mapping class: ", clz.getName());
          morphia_.map(clz);
          pending.remove(clz);
        } catch (ConstraintViolationException e) {
          error(e, "error mapping class [%1$s]", clz);
          int retry = retries.get(clz);
          if (retry > 2) {
            throw new RuntimeException("too many errories mapping Morphia Entity classes");
          }
          retries.put(clz, retries.get(clz) + 1);
        }
      }
    }

    ds().ensureIndexes();

    String writeConcern = Play.configuration.getProperty("morphia.defaultWriteConcern", "safe");
    if (null != writeConcern) {
      ds().setDefaultWriteConcern(WriteConcern.valueOf(writeConcern.toUpperCase()));
    }
  }
  private WriteConcern extractWriteConcern(Exchange exchange) throws CamelMongoDbException {
    Object o = exchange.getIn().getHeader(MongoDbConstants.WRITECONCERN);

    if (o == null) {
      return null;
    } else if (o instanceof WriteConcern) {
      return ObjectHelper.cast(WriteConcern.class, o);
    } else if (o instanceof String) {
      WriteConcern answer = WriteConcern.valueOf(ObjectHelper.cast(String.class, o));
      if (answer == null) {
        throw new CamelMongoDbException(
            "WriteConcern specified in the "
                + MongoDbConstants.WRITECONCERN
                + " header, with value "
                + o
                + " could not be resolved to a WriteConcern type");
      }
    }

    // should never get here
    LOG.warn("A problem occurred while resolving the Exchange's Write Concern");
    return null;
  }
Example #11
0
 /**
  * Set the {@link WriteConcern} for write operations on MongoDB using the standard ones. Resolved
  * from the fields of the WriteConcern class by calling the {@link WriteConcern#valueOf(String)}
  * method.
  *
  * @param writeConcern the standard name of the WriteConcern
  * @see <a
  *     href="http://api.mongodb.org/java/current/com/mongodb/WriteConcern.html#valueOf(java.lang.String)">possible
  *     options</a>
  */
 public void setWriteConcern(String writeConcern) {
   this.writeConcern = WriteConcern.valueOf(writeConcern);
 }
Example #12
0
 /**
  * Returns the error status of the last operation on the current connection.
  *
  * @param concern a {@link WriteConcern} to be used while checking for the error status.
  * @return {@code DBObject} with error and status information
  * @throws MongoException
  * @see {@link DB#getLastError() }
  */
 public CommandResult getLastError(com.mongodb.WriteConcern concern) {
   return command(concern.getCommand());
 }