Example usage for com.mongodb BasicDBList get

List of usage examples for com.mongodb BasicDBList get

Introduction

In this page you can find the example usage for com.mongodb BasicDBList get.

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value at an index.

Usage

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

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
private Map toMap(Class<?> type, BasicDBList value) {

    Map map = null;/*from ww  w .  j av a  2s  .  c  o m*/

    try {

        map = (Map) repository.getConstructor(type).newInstance();

        for (int i = 1; i < value.size(); i += 2) {
            Object key = fromDBObject(value.get(i));
            Object val = fromDBObject(value.get(i + 1));

            map.put(key, val);
        }

        return map;

    } catch (InvocationTargetException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (InstantiationException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (IllegalAccessException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    }
}

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

License:Open Source License

private Object[] toArray(Class<?> type, BasicDBList value) {

    Object[] array = (Object[]) Array.newInstance(type.getComponentType(), value.size() - 1);

    for (int i = 1; i < value.size(); i++) {
        Object v = fromDBObject(value.get(i));

        if (SpaceDocument.class.isAssignableFrom(type.getComponentType()))
            v = MongoDocumentObjectConverter.instance().toDocumentIfNeeded(v, SpaceDocumentSupport.CONVERT);

        array[i - 1] = v;//ww  w.  ja  v a  2  s  . c o m
    }

    return array;
}

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

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
private Collection toCollection(Class<?> type, BasicDBList value) {

    Collection collection = null;

    try {//from   w  w  w  .ja v  a2s.c o m
        collection = (Collection) repository.getConstructor(type).newInstance();

        for (int i = 1; i < value.size(); i++) {
            collection.add(fromDBObject(value.get(i)));
        }

    } catch (InvocationTargetException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (InstantiationException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (IllegalAccessException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    }

    return collection;
}

From source file:com.github.maasdi.mongo.wrapper.field.MongoField.java

License:Apache License

/**
 * Convert a mongo array object to a Kettle field value (for the field defined in this path)
 *
 * @param mongoObject/*www .j a va 2s.co m*/
 *          the array to convert
 * @return the kettle field value
 * @throws KettleException
 *           if a problem occurs
 */
public Object convertToKettleValue(BasicDBList mongoList) throws KettleException {

    if (mongoList == null) {
        return null;
    }

    if (m_tempParts.size() == 0) {
        throw new KettleException(BaseMessages.getString(PKG, "MongoDbInput.ErrorMessage.MalformedPathArray")); //$NON-NLS-1$
    }

    String part = m_tempParts.remove(0);
    if (!(part.charAt(0) == '[')) {
        // we're expecting an array at this point - this document does not
        // contain our field
        return null;
    }

    String index = part.substring(1, part.indexOf(']'));
    int arrayI = 0;
    try {
        arrayI = Integer.parseInt(index.trim());
    } catch (NumberFormatException e) {
        throw new KettleException(
                BaseMessages.getString(PKG, "MongoDbInput.ErrorMessage.UnableToParseArrayIndex", index)); //$NON-NLS-1$
    }

    if (part.indexOf(']') < part.length() - 1) {
        // more dimensions to the array
        part = part.substring(part.indexOf(']') + 1, part.length());
        m_tempParts.add(0, part);
    }

    if (arrayI >= mongoList.size() || arrayI < 0) {
        return null;
    }

    Object element = mongoList.get(arrayI);

    if (element == null) {
        return null;
    }

    if (m_tempParts.size() == 0) {
        // we're expecting a leaf primitive - let's see if that's what we have
        // here...
        return getKettleValue(element);
    }

    if (element instanceof BasicDBObject) {
        return convertToKettleValue(((BasicDBObject) element));
    }

    if (element instanceof BasicDBList) {
        return convertToKettleValue(((BasicDBList) element));
    }

    // must mean we have a primitive here, but we're expecting to process more
    // path so this doesn't match us - return null
    return null;
}

From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java

License:Apache License

private static void processList(BasicDBList list, String path, String name, Map<String, MongoField> lookup) {

    if (list.size() == 0) {
        return; // can't infer anything about an empty list
    }/*from   w ww  . j  a  va2  s . c  om*/

    String nonPrimitivePath = path + "[-]"; //$NON-NLS-1$
    String primitivePath = path;

    for (int i = 0; i < list.size(); i++) {
        Object element = list.get(i);

        if (element instanceof BasicDBObject) {
            processRecord((BasicDBObject) element, nonPrimitivePath, name + "[" + i //$NON-NLS-1$
                    + ":" + i + "]", lookup); //$NON-NLS-1$ //$NON-NLS-2$
        } else if (element instanceof BasicDBList) {
            processList((BasicDBList) element, nonPrimitivePath, name + "[" + i //$NON-NLS-1$
                    + ":" + i + "]", lookup); //$NON-NLS-1$ //$NON-NLS-2$
        } else {
            // some sort of primitive
            String finalPath = primitivePath + "[" + i + "]"; //$NON-NLS-1$ //$NON-NLS-2$
            String finalName = name + "[" + i + "]"; //$NON-NLS-1$ //$NON-NLS-2$
            if (!lookup.containsKey(finalPath)) {
                MongoField newField = new MongoField();
                int kettleType = mongoToKettleType(element);
                // Following suit of mongoToKettleType by interpreting null as String type
                newField.m_mongoType = String.class;
                if (element != null) {
                    newField.m_mongoType = element.getClass();
                }
                newField.m_fieldName = finalPath;
                newField.m_fieldPath = finalName;
                newField.m_kettleType = ValueMeta.getTypeDesc(kettleType);
                newField.m_percentageOfSample = 1;

                lookup.put(finalPath, newField);
            } else {
                // update max indexes in array parts of name
                MongoField m = lookup.get(finalPath);
                Class<?> elementClass = String.class;
                if (element != null) {
                    elementClass = element.getClass();
                }
                if (!m.m_mongoType.isAssignableFrom(elementClass)) {
                    m.m_disparateTypes = true;
                }
                m.m_percentageOfSample++;
                updateMaxArrayIndexes(m, finalName);
            }
        }
    }
}

From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java

License:Apache License

protected List<String> setupAllTags(BasicDBList members) {
    HashSet<String> tempTags = new HashSet<String>();

    if (members != null && members.size() > 0) {
        for (int i = 0; i < members.size(); i++) {
            Object m = members.get(i);

            if (m != null) {
                DBObject tags = (DBObject) ((DBObject) m).get("tags"); //$NON-NLS-1$
                if (tags == null) {
                    continue;
                }//from  w  w  w . ja  va  2 s .c o  m

                for (String tagName : tags.keySet()) {
                    String tagVal = tags.get(tagName).toString();
                    String combined = quote(tagName) + " : " + quote(tagVal); //$NON-NLS-1$
                    tempTags.add(combined);
                }
            }
        }
    }

    return new ArrayList<String>(tempTags);
}

From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java

License:Apache License

protected List<DBObject> checkForReplicaSetMembersThatSatisfyTagSets(List<DBObject> tagSets,
        BasicDBList members) {
    List<DBObject> satisfy = new ArrayList<DBObject>();
    if (members != null && members.size() > 0) {
        for (int i = 0; i < members.size(); i++) {
            Object m = members.get(i);

            if (m != null) {
                DBObject tags = (DBObject) ((DBObject) m).get("tags"); //$NON-NLS-1$
                if (tags == null) {
                    continue;
                }/*from   ww w.j  a v  a 2 s .com*/

                for (int j = 0; j < tagSets.size(); j++) {
                    boolean match = true;
                    DBObject toMatch = tagSets.get(j);

                    for (String tagName : toMatch.keySet()) {
                        String tagValue = toMatch.get(tagName).toString();

                        // does replica set member m's tags contain this tag?
                        Object matchVal = tags.get(tagName);

                        if (matchVal == null) {
                            match = false; // doesn't match this particular tag set
                            // no need to check any other keys in toMatch
                            break;
                        }

                        if (!matchVal.toString().equals(tagValue)) {
                            // rep set member m's tags has this tag, but it's value does not
                            // match
                            match = false;

                            // no need to check any other keys in toMatch
                            break;
                        }
                    }

                    if (match) {
                        // all tag/values present and match - add this member (only if its
                        // not already there)
                        if (!satisfy.contains(m)) {
                            satisfy.add((DBObject) m);
                        }
                    }
                }
            }
        }
    }

    return satisfy;
}

From source file:com.hangum.tadpole.mongodb.core.composite.result.MongodbResultComposite.java

License:Open Source License

/**
 * object array//w  w w. java 2  s  . co m
 * 
 * @param treeDto
 * @param dbObject
 * @throws Exception
 */
private void parseObjectArray(final DBObject rootDbObject, final MongodbTreeViewDTO treeDto,
        final BasicDBList dbObjectList) throws Exception {
    List<MongodbTreeViewDTO> listTrees = new ArrayList<MongodbTreeViewDTO>();

    for (int i = 0; i < dbObjectList.size(); i++) {
        MongodbTreeViewDTO mongodbDto = new MongodbTreeViewDTO();

        mongodbDto.setRealKey("" + i); //$NON-NLS-1$
        mongodbDto.setKey("(" + i + ")"); //$NON-NLS-1$ //$NON-NLS-2$
        mongodbDto.setDbObject(rootDbObject);

        Object keyVal = dbObjectList.get(i);
        if (keyVal instanceof BasicDBObject) {
            mongodbDto.setType("Document"); //$NON-NLS-1$

            parserTreeObject(rootDbObject, mongodbDto, (DBObject) keyVal);
        } else if (keyVal instanceof BasicDBList) {
            BasicDBList tmpDbObjectList = (BasicDBList) keyVal;

            mongodbDto.setType("Array"); //$NON-NLS-1$
            parseObjectArray(rootDbObject, mongodbDto, tmpDbObjectList);
        } else {
            mongodbDto.setType(keyVal != null ? keyVal.getClass().getName() : "Unknow"); //$NON-NLS-1$

            if (keyVal == null)
                mongodbDto.setValue(""); //$NON-NLS-1$
            else
                mongodbDto.setValue(keyVal.toString());
        }

        listTrees.add(mongodbDto);
    }

    treeDto.setChildren(listTrees);
}

From source file:com.ikanow.infinit.e.api.knowledge.processing.ScoringUtils_MultiCommunity.java

License:Open Source License

static boolean community_areDuplicates(TempDocBucket lhs, TempDocBucket rhs) {
    if (lhs.url.equals(rhs.url)) {
        // These are probably duplicates, we'll do some more tests...
        BasicDBList lhsEnts = (BasicDBList) lhs.dbo.get(DocumentPojo.entities_);
        BasicDBList rhsEnts = (BasicDBList) rhs.dbo.get(DocumentPojo.entities_);

        if (!((null != lhsEnts) ^ (null != rhsEnts))) // ie both null or neither null
        {/*from   w w  w.  j a  v a2s  .  c om*/
            if (null != lhsEnts) {
                if (lhsEnts.size() != rhsEnts.size()) {
                    return false;
                } //TESTED
                if (lhsEnts.size() > 0) {
                    BasicDBObject lhsFirstEnt = (BasicDBObject) lhsEnts.get(0);
                    BasicDBObject rhsFirstEnt = (BasicDBObject) rhsEnts.get(0);
                    String lhsIndex = lhsFirstEnt.getString(EntityPojo.index_);
                    String rhsIndex = rhsFirstEnt.getString(EntityPojo.index_);
                    if ((null != lhsIndex) && (null != rhsIndex)) {
                        if (!lhsIndex.equals(rhsIndex)) {
                            return false;
                        }
                    }
                } //(end "random" entity test)
                  //TESTED
            }
        } // (end entity count test)

        // Finally we'll just count the events:
        BasicDBList lhsEvents = (BasicDBList) lhs.dbo.get(DocumentPojo.associations_);
        BasicDBList rhsEvents = (BasicDBList) rhs.dbo.get(DocumentPojo.associations_);
        if (!((null != lhsEvents) ^ (null != rhsEvents))) // ie both null or neither null
        {
            if (null != lhsEvents) {
                if (lhsEvents.size() != rhsEvents.size()) {
                    return false;
                } //TESTED               
            }
        } //(end event count test)

        return true;
    }
    return false;
}

From source file:com.ikanow.infinit.e.api.knowledge.processing.ScoringUtils_MultiCommunity.java

License:Open Source License

static void community_combineDuplicateDocs(TempDocBucket masterDoc) {
    //(source key)
    BasicDBList masterSourceKeyList = (BasicDBList) masterDoc.dbo.get(DocumentPojo.sourceKey_); // (already had # stripped)
    String masterSourceKey = (masterSourceKeyList.size() > 0) ? (String) masterSourceKeyList.get(0) : null;
    HashSet<String> sourceKeySet = null;
    //(community Id)
    BasicDBList masterCommunityList = (BasicDBList) masterDoc.dbo.get(DocumentPojo.communityId_);
    ObjectId masterCommunityId = (masterCommunityList.size() > 0) ? (ObjectId) masterCommunityList.get(0)
            : null;/*  ww  w.j av a  2s  . co m*/
    HashSet<ObjectId> communityIdSet = null;
    //(source title)
    BasicDBList masterSourceList = null;
    String masterSource = null;
    HashSet<String> sourceSet = null;
    //(mediaType)
    BasicDBList masterTypeList = null;
    String masterType = null;
    HashSet<String> typeSet = null;
    //(tags)
    BasicDBList masterTagList = null;
    String masterTag = null;
    HashSet<String> tagSet = null;

    for (TempDocBucket slave = masterDoc.dupList; null != slave; slave = slave.dupList) {
        String sourceKey = DocumentPojo.getSourceKey(slave.dbo.getString(DocumentPojo.sourceKey_));
        int nCommunityPos = sourceKey.indexOf('#');
        if ((nCommunityPos > 0) && (null != sourceKey)) {
            sourceKey = sourceKey.substring(0, nCommunityPos);
        } //TESTED

        // Always combine communities:
        ObjectId communityIdObj = (ObjectId) slave.dbo.get(DocumentPojo.communityId_);
        communityIdSet = combineElements(communityIdSet, TypeErasureWorkaround.Type.OBJECTID,
                masterCommunityList, masterCommunityId, slave, null, communityIdObj);

        if ((null != masterSourceKey) && (null != sourceKey) && sourceKey.equals(masterSourceKey)) {
            continue;
        } //TESTED

        // else fall through to...

        // Combine source keys:
        sourceKeySet = combineElements(sourceKeySet, TypeErasureWorkaround.Type.STRING, masterSourceKeyList,
                masterSourceKey, slave, null, sourceKey);

        // Combine source titles:
        masterSourceList = (BasicDBList) masterDoc.dbo.get(DocumentPojo.source_);
        masterSource = (masterSourceList.size() > 0) ? (String) masterSourceList.get(0) : null;
        String slaveSource = slave.dbo.getString(DocumentPojo.source_);
        sourceSet = combineElements(sourceSet, TypeErasureWorkaround.Type.STRING, masterSourceList,
                masterSource, slave, null, slaveSource);

        // Combine media types:
        masterTypeList = (BasicDBList) masterDoc.dbo.get(DocumentPojo.mediaType_);
        masterType = (masterTypeList.size() > 0) ? (String) masterTypeList.get(0) : null;
        String slaveType = slave.dbo.getString(DocumentPojo.mediaType_);
        typeSet = combineElements(typeSet, TypeErasureWorkaround.Type.STRING, masterTypeList, masterType, slave,
                null, slaveType);

        // Combine tags:
        masterTagList = (BasicDBList) masterDoc.dbo.get(DocumentPojo.tags_);
        masterTag = (masterTagList.size() > 0) ? (String) masterTagList.get(0) : null;
        BasicDBList slaveTagList = (BasicDBList) slave.dbo.get(DocumentPojo.tags_);
        tagSet = combineElements(tagSet, TypeErasureWorkaround.Type.STRING, masterTagList, masterTag, slave,
                slaveTagList, null);

    } // (end loop over slaves)

    // Now use any sets created to fill in

    if (null != communityIdSet) {
        masterCommunityList.clear();
        for (ObjectId id : communityIdSet) {
            masterCommunityList.add(id);
        }
    } //TESTED

    if (null != sourceKeySet) {
        masterSourceKeyList.clear();
        for (String key : sourceKeySet) {
            masterSourceKeyList.add(key);
        }
    } //TESTED

    if (null != sourceSet) {
        masterSourceList.clear();
        for (String src : sourceSet) {
            masterSourceList.add(src);
        }
    } //TESTED

    if (null != typeSet) {
        masterTypeList.clear();
        for (String type : typeSet) {
            masterTypeList.add(type);
        }
    } //TESTED

    if (null != tagSet) {
        masterTagList.clear();
        for (String tag : tagSet) {
            masterTagList.add(tag);
        }
    } //TESTED

}