当前位置: 首页>>代码示例>>Java>>正文


Java MongoWriteException类代码示例

本文整理汇总了Java中com.mongodb.MongoWriteException的典型用法代码示例。如果您正苦于以下问题:Java MongoWriteException类的具体用法?Java MongoWriteException怎么用?Java MongoWriteException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


MongoWriteException类属于com.mongodb包,在下文中一共展示了MongoWriteException类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: toResponse

import com.mongodb.MongoWriteException; //导入依赖的package包/类
@Override
public Response toResponse(final Exception exception) {
    Log.e(TAG, "[toResponse]" + exception.getMessage());
    if (exception instanceof WebApplicationException) {
        Response _response = ((WebApplicationException) exception).getResponse();
        return Response.fromResponse(_response)
            .entity(new Failure(_response.getStatusInfo().getReasonPhrase()))
            .build();

    } else if (exception instanceof MongoWriteException) {
        MongoWriteException _exception = (MongoWriteException) exception;
        return Response.status(BAD_REQUEST)
            .entity(new Failure(_exception.getError().getMessage()))
            .build();

    } else if (exception instanceof NullPointerException) {
        return Response.status(NOT_FOUND)
            .entity(new Failure(exception.getMessage()))
            .build();

    } else {
        return Response.status(BAD_REQUEST)
            .entity(new Failure(exception.getMessage()))
            .build();
    }
}
 
开发者ID:drakeet,项目名称:rebase-server,代码行数:27,代码来源:WebExceptionMapper.java

示例2: saveEntry

import com.mongodb.MongoWriteException; //导入依赖的package包/类
/**
 * Saves an entry to file
 * @param entry
 * @param dbName usually scrapig
 * @return true if success
 */
public static boolean saveEntry(DBEntry entry, String dbName){

    if(entry == null || !entry.isValid())
        return false;

    Logger log = Logger.getLogger(DAO.class);

    MongoDatabase db = MongoDB.INSTANCE.getDatabase(dbName);

    String collectionName = getCollectionName(entry);


    MongoCollection collection = db.getCollection(collectionName,BasicDBObject.class);

    try {
        collection.insertOne(entry);
        return true;
    }
    catch (MongoWriteException ex){
        if (ex.getCode() != 11000) // Ignore errors about duplicates
            log.error(ex.getError().getMessage());
        return false;
    }

}
 
开发者ID:gidim,项目名称:Babler,代码行数:32,代码来源:DAO.java

示例3: insert

import com.mongodb.MongoWriteException; //导入依赖的package包/类
/**
 * Inserts a new element in the database. The element's type must be mapped to an already
 * existent collection, otherwise {@link NoSuchCollection} will be thrown. If the element
 * already exists (i.e. a unique constraint is violated), mongoDB will throw an exception.
 * 
 * @param element element to insert
 */
@SuppressWarnings("unchecked")
public <T extends Element> boolean insert(T element) {
	Preconditions.checkArgument(element != null, "Cannot insert a null element");
	final SmofCollection<T> collection = (SmofCollection<T>) dispatcher.getCollection(element.getClass());
	final CollectionOptions<T> options = collection.getCollectionOptions();
	boolean success = false;
	try {
		success = dispatcher.insert(element);
	} catch(MongoWriteException e) {
		if(options.isThrowOnInsertDuplicate()) {
			parser.reset();
			throw e;
		}
	}
	parser.reset();
	return success;
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:25,代码来源:Smof.java

示例4: removeDuplicate

import com.mongodb.MongoWriteException; //导入依赖的package包/类
/**
 * removeDuplicate
 * by add a unique index
 */
private static void removeDuplicate(){
    MongoClient client = new MongoClient("127.0.0.1");
    MongoDatabase db = client.getDatabase("airvis");
    MongoCollection preprocess = db.getCollection("pm_preProcess");
    MongoCollection process = db.getCollection("pmProcess");
    IndexOptions option = new IndexOptions();
    option.unique(true);
    process.createIndex(new Document().append("time",1).append("code",1), option);
    MongoCursor cur = preprocess.find().iterator();

    while(cur.hasNext()){
        Document obj = (Document)cur.next();
        try {
            process.insertOne(obj);
        }catch(MongoWriteException e){
            //duplicate error
            System.out.println(obj.toString());
        }
    }
}
 
开发者ID:yiducn,项目名称:airvisprocessing,代码行数:25,代码来源:AllParsersAir.java

示例5: removeDuplicate

import com.mongodb.MongoWriteException; //导入依赖的package包/类
/**
 * removeDuplicate
 * by add a unique index
 */
private static void removeDuplicate(){
    MongoClient client = new MongoClient("127.0.0.1");
    MongoDatabase db = client.getDatabase("pm");
    MongoCollection preprocess = db.getCollection("pm_preProcess");
    MongoCollection process = db.getCollection("pmProcess");
    IndexOptions option = new IndexOptions();
    option.unique(true);
    process.createIndex(new Document().append("time",1).append("code",1), option);
    MongoCursor cur = preprocess.find().iterator();

    while(cur.hasNext()){
        Document obj = (Document)cur.next();
        try {
            process.insertOne(obj);
        }catch(MongoWriteException e){
            //duplicate error
            System.out.println(obj.toString());
        }
    }
}
 
开发者ID:yiducn,项目名称:airvisprocessing,代码行数:25,代码来源:AllParsers.java

示例6: acquireLock

import com.mongodb.MongoWriteException; //导入依赖的package包/类
public boolean acquireLock(MongoDatabase db) {

    Document insertObj = new Document(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL).append("status", "LOCK_HELD");

    // acquire lock by attempting to insert the same value in the collection - if it already exists (i.e. lock held)
    // there will be an exception
    try {
      db.getCollection(lockCollectionName).insertOne(insertObj);
    } catch (MongoWriteException ex) {
      if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
        logger.warn("Duplicate key exception while acquireLock. Probably the lock has been already acquired.");
      }
      return false;
    }
    return true;
  }
 
开发者ID:mongobee,项目名称:mongobee,代码行数:17,代码来源:LockDao.java

示例7: removeProperty

import com.mongodb.MongoWriteException; //导入依赖的package包/类
/**
 * Un-assigns a key/value property from the element. The object value of the
 * removed property is returned.
 *
 * @param key
 *            the key of the property to remove from the element
 * @return the object value associated with that key prior to removal. Should be
 *         instance of BsonValue
 */
@Override
public <T> T removeProperty(final String key) {
	try {
		BsonValue value = getProperty(key);
		BsonDocument filter = new BsonDocument();
		filter.put(Tokens.ID, new BsonString(this.id));
		BsonDocument update = new BsonDocument();
		update.put("$unset", new BsonDocument(key, new BsonNull()));
		if (this instanceof ChronoVertex) {
			graph.getVertexCollection().updateOne(filter, update, new UpdateOptions().upsert(true));
			return (T) value;
		} else {
			graph.getEdgeCollection().updateOne(filter, update, new UpdateOptions().upsert(true));
			return (T) value;
		}
	} catch (MongoWriteException e) {
		throw e;
	}
}
 
开发者ID:JaewookByun,项目名称:epcis,代码行数:29,代码来源:ChronoElement.java

示例8: noDuplicatesAllowed

import com.mongodb.MongoWriteException; //导入依赖的package包/类
@Test(expected = MongoWriteException.class)
public void noDuplicatesAllowed() {
    String data = "this is a blog post !";
    String url = "http://www.columbia.edu";
    BlogPost post = new BlogPost(data,"tst",null,"source",url,"GUID231253423","");
    posts.insertOne(post);
    String data2 = "this is a blog post !";
    String url2 = "http://www.columbia.edu";
    BlogPost post2 = new BlogPost(data,"tst",null,"source",url,"GUID231253423","");
    posts.insertOne(post2);
}
 
开发者ID:gidim,项目名称:Babler,代码行数:12,代码来源:TestBlogPosts.java

示例9: noDuplicatesAllowed

import com.mongodb.MongoWriteException; //导入依赖的package包/类
@Test(expected = MongoWriteException.class)
public void noDuplicatesAllowed() {
        String data = "this is a tweet !";
        String url = "http://www.columbia.edu";
        Tweet tweet = new Tweet(data,data,"tst","test",null,"topsy",url,"123456","filename");
        tweets.insertOne(tweet);
        String data2 = "this is a tweet !";
        String url2 = "http://www.columbia.edu";
        Tweet tweet2 = new Tweet(data2,data,"tst","test",null,"topsy",url2,"123456","filename");
        tweets.insertOne(tweet2);
    }
 
开发者ID:gidim,项目名称:Babler,代码行数:12,代码来源:TestTweets.java

示例10: handleRequest

import com.mongodb.MongoWriteException; //导入依赖的package包/类
@Override
public void handleRequest(
        HttpServerExchange exchange,
        RequestContext context)
        throws Exception {
    if (context.isInError()) {
        next(exchange, context);
        return;
    }

    final BsonValue _metadata = context.getContent();

    // must be an object
    if (!_metadata.isDocument()) {
        ResponseHelper.endExchangeWithMessage(
                exchange,
                context,
                HttpStatus.SC_NOT_ACCEPTABLE,
                "data cannot be an array");
        next(exchange, context);
        return;
    }

    BsonDocument metadata = _metadata.asDocument();

    BsonValue id = context.getDocumentId();

    if (metadata.get("_id") != null
            && metadata.get("_id").equals(id)) {
        ResponseHelper.endExchangeWithMessage(
                exchange,
                context,
                HttpStatus.SC_NOT_ACCEPTABLE,
                "_id in content body is different than id in URL");
        next(exchange, context);
        return;
    }

    metadata.put("_id", id);

    OperationResult result;

    try {
        if (context.getFilePath() != null) {
            result = gridFsDAO
                    .createFile(getDatabase(),
                            context.getDBName(),
                            context.getCollectionName(),
                            metadata,
                            context.getFilePath());
        } else {
            // throw new RuntimeException("error. file data is null");
            // try to pass to next handler in order to PUT new metadata on existing file.
            next(exchange, context);
            return;
        }
    } catch (IOException | RuntimeException t) {
        if (t instanceof MongoWriteException
                && ((MongoException) t).getCode() == 11000) {
            // update not supported
            String errMsg = "file resource update is not yet implemented";
            ResponseHelper.endExchangeWithMessage(
                    exchange,
                    context,
                    HttpStatus.SC_NOT_IMPLEMENTED,
                    errMsg);
            next(exchange, context);
            return;
        }

        throw t;
    }

    context.setDbOperationResult(result);

    context.setResponseStatusCode(result.getHttpCode());

    next(exchange, context);
}
 
开发者ID:SoftInstigate,项目名称:restheart,代码行数:80,代码来源:PutFileHandler.java


注:本文中的com.mongodb.MongoWriteException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。