Example usage for com.mongodb BasicDBObjectBuilder start

List of usage examples for com.mongodb BasicDBObjectBuilder start

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder start.

Prototype

public static BasicDBObjectBuilder start() 

Source Link

Document

Creates a builder intialized with an empty document.

Usage

From source file:com.gigaspaces.persistency.datasource.MongoSqlQueryDataIterator.java

License:Open Source License

private void init() {
    DBCollection collection = client.getCollection(query.getTypeDescriptor().getTypeName());

    BasicDBObjectBuilder q = BasicDBObjectBuilder.start();

    logger.debug(query);/*from   www.  java  2 s  .  c  om*/

    if (query.supportsAsSQLQuery())
        q = MongoQueryFactory.create(query);
    else if (query.supportsTemplateAsDocument()) {
        Map m = new DefaultSpaceDocumentMapper(query.getTypeDescriptor())
                .toDBObject(query.getTemplateAsDocument()).toMap();

        q = BasicDBObjectBuilder.start(m);
    }

    cursor = collection.find(q.get());
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private DBObject toDBObjectDocument(SpaceDocument document) {
    BasicDBObjectBuilder bson = BasicDBObjectBuilder.start();

    Set<String> keys = document.getProperties().keySet();

    bson.add(Constants.TYPE, document.getTypeName());

    for (String property : keys) {

        Object value = document.getProperty(property);

        if (value == null)
            continue;

        if (spaceTypeDescriptor.getIdPropertyName().equals(property))
            property = Constants.ID_PROPERTY;

        bson.add(property, toObject(value));
    }//from ww w  . java  2  s  .  com

    return bson.get();
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private DBObject toDBObjectPojo(Object pojo) {

    BasicDBObjectBuilder bson = BasicDBObjectBuilder.start();

    Map<String, Method> getters = repository.getGetters(pojo.getClass());

    Class<?> type = pojo.getClass();

    bson.add(Constants.TYPE, type.getName());

    for (String property : getters.keySet()) {
        Object value = null;//from  ww  w  . j  a v  a 2  s  .  co m
        try {

            value = repository.getGetter(type, property).get(pojo);

            if (value == null)
                continue;

            if (spaceTypeDescriptor.getIdPropertyName().equals(property))
                property = Constants.ID_PROPERTY;

            bson.add(property, toObject(value));

        } catch (IllegalArgumentException e) {
            throw new SpaceMongoException("Argument is: " + value, e);
        } catch (IllegalAccessException e) {
            throw new SpaceMongoException("Can not access method", e);
        } catch (InvocationTargetException e) {
            throw new SpaceMongoException("Can not invoke method", e);
        }
    }

    return bson.get();
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

public Object toObject(Object property) {
    if (property == null)
        return null;

    switch (type(property.getClass())) {

    case TYPE_CHAR:
    case TYPE_FLOAT:
    case TYPE_BYTE:
    case TYPE_BIGDECIMAL:
    case TYPE_BIGINT:
        return toSpecialType(property);
    case TYPE_OBJECT:
        if (property instanceof SpaceDocument)
            return toDBObject((SpaceDocument) property);
        else if (property instanceof Class)
            return toSpecialType(property);
        else if (property instanceof Locale)
            return toSpecialType(property);
        else if (property instanceof URI)
            return toSpecialType(property);
        else if (property instanceof Timestamp)
            return toSpecialType(property);

        if (!(property instanceof Serializable))
            return toDBObject(property);

        byte[] result = serializeObject(property);

        BasicDBObjectBuilder blob = BasicDBObjectBuilder.start();

        blob.add(Constants.TYPE, Constants.CUSTOM_BINARY);

        blob.add(Constants.VALUE, result);

        blob.add(Constants.HASH, Arrays.hashCode(result));

        return blob.get();
    case TYPE_ENUM:
        return toEnum(property);
    case TYPE_ARRAY:
        return toArray(property);
    case TYPE_COLLECTION:
        return toCollection(property);
    case TYPE_MAP:
        return toMap(property);
    default://  w w  w .j av a 2  s .com
        return property;
    }
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private Object toEnum(Object property) {

    BasicDBObjectBuilder document = BasicDBObjectBuilder.start();

    return document.add(Constants.TYPE, property.getClass().getName()).add(Constants.VALUE, property.toString())
            .get();//from   ww w  .  j  a  v a 2 s. c  o m
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private DBObject toSpecialType(Object property) {
    BasicDBObjectBuilder document = BasicDBObjectBuilder.start();

    String toString = toString(property);

    return document.add(Constants.TYPE, property.getClass().getName()).add(Constants.VALUE, toString).get();
}

From source file:com.gigaspaces.persistency.metadata.IndexBuilder.java

License:Open Source License

public void ensureIndexes(SpaceTypeDescriptor spaceTypeDescriptor) {

    Map<String, SpaceIndex> indexes = spaceTypeDescriptor.getIndexes();

    String id = spaceTypeDescriptor.getIdPropertyName();
    String routing = spaceTypeDescriptor.getRoutingPropertyName();

    for (SpaceIndex idx : indexes.values()) {

        if (idx.getIndexType() == SpaceIndexType.NONE || idx.getName().equals(id)
                || idx.getName().equals(routing))
            continue;

        createIndex(spaceTypeDescriptor.getTypeName(), idx);
    }//from  ww w .  ja va2 s. co m

    if (id != null && !id.equals(routing)) {
        createIndex(spaceTypeDescriptor.getTypeName(), routing, SpaceIndexType.BASIC,
                BasicDBObjectBuilder.start());
    }
}

From source file:com.gigaspaces.persistency.metadata.IndexBuilder.java

License:Open Source License

private void createIndex(String collectionName, SpaceIndex idx) {
    createIndex(collectionName, idx.getName(), idx.getIndexType(), BasicDBObjectBuilder.start());
}

From source file:com.gigaspaces.persistency.MongoClientConnector.java

License:Open Source License

public void introduceType(SpaceTypeDescriptor typeDescriptor) {

    DBCollection m = getConnection().getCollection(METADATA_COLLECTION_NAME);

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add(Constants.ID_PROPERTY,
            typeDescriptor.getTypeName());
    try {/*from ww  w .j a  v a2  s . c om*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        IOUtils.writeObject(out,
                SpaceTypeDescriptorVersionedSerializationUtils.toSerializableForm(typeDescriptor));

        builder.add(TYPE_DESCRIPTOR_FIELD_NAME, bos.toByteArray());

        WriteResult wr = m.save(builder.get());

        if (logger.isTraceEnabled())
            logger.trace(wr);

        indexBuilder.ensureIndexes(typeDescriptor);

    } catch (IOException e) {
        logger.error(e);

        throw new SpaceMongoException(
                "error occurs while serialize and save type descriptor: " + typeDescriptor, e);
    }
}

From source file:com.gigaspaces.persistency.MongoClientConnector.java

License:Open Source License

public void performBatch(List<BatchUnit> rows) {
    if (logger.isTraceEnabled()) {
        logger.trace("MongoClientWrapper.performBatch(" + rows + ")");
        logger.trace("Batch size to be performed is " + rows.size());
    }//w  w w.j a  v  a2s.c  o  m
    //List<Future<? extends Number>> pending = new ArrayList<Future<? extends Number>>();

    for (BatchUnit row : rows) {
        SpaceDocument spaceDoc = row.getSpaceDocument();
        SpaceTypeDescriptor typeDescriptor = types.get(row.getTypeName()).getTypeDescriptor();
        SpaceDocumentMapper<DBObject> mapper = getMapper(typeDescriptor);

        DBObject obj = mapper.toDBObject(spaceDoc);

        DBCollection col = getCollection(row.getTypeName());
        switch (row.getDataSyncOperationType()) {

        case WRITE:
        case UPDATE:
            col.save(obj);
            break;
        case PARTIAL_UPDATE:
            DBObject query = BasicDBObjectBuilder.start()
                    .add(Constants.ID_PROPERTY, obj.get(Constants.ID_PROPERTY)).get();

            DBObject update = normalize(obj);
            col.update(query, update);
            break;
        // case REMOVE_BY_UID: // Not supported by this implementation
        case REMOVE:
            col.remove(obj);
            break;
        default:
            throw new IllegalStateException(
                    "Unsupported data sync operation type: " + row.getDataSyncOperationType());
        }
    }

    /*long totalCount = waitFor(pending);
            
    if (logger.isTraceEnabled()) {
       logger.trace("total accepted replies is: " + totalCount);
    }*/
}