private static void buildKeyValueSet()

in stack/core/src/main/java/org/apache/usergrid/utils/IndexUtils.java [59:179]


    private static void buildKeyValueSet( Object node, Map<String, List<Object>> keyValues, String path,
                                          boolean fulltextIndex, Object... history ) {

        if ( node == null ) {
            return;
        }

        if ( node instanceof Collection ) {
            Object[] newHistory = Arrays.copyOf( history, history.length + 1 );
            newHistory[history.length] = node;
            @SuppressWarnings("unchecked") Collection<Object> c = ( Collection<Object> ) node;
            for ( Object o : c ) {
                buildKeyValueSet( o, keyValues, path, fulltextIndex, newHistory );
            }
        }
        else if ( node instanceof Map ) {
            Object[] newHistory = Arrays.copyOf( history, history.length + 1 );
            newHistory[history.length] = node;
            @SuppressWarnings("unchecked") Map<Object, Object> m = ( Map<Object, Object> ) node;
            for ( Entry<Object, Object> e : m.entrySet() ) {
                String newPath;
                String key = e.getKey().toString();
                key = quoteString( key );
                if ( key.indexOf( '\\' ) == -1 ) {
                    if ( path != null ) {
                        newPath = path + "." + key;
                    }
                    else {
                        newPath = "" + key;
                    }
                }
                else {
                    if ( path != null ) {
                        newPath = path + "[\"" + key + "\"]";
                    }
                    else {
                        newPath = "" + "[\"" + key + "\"]";
                    }
                }
                buildKeyValueSet( e.getValue(), keyValues, newPath, fulltextIndex, newHistory );
            }
        }
        else if ( node instanceof ArrayNode ) {
            Object[] newHistory = Arrays.copyOf( history, history.length + 1 );
            newHistory[history.length] = node;
            ArrayNode c = ( ArrayNode ) node;
            for ( JsonNode o : c ) {
                buildKeyValueSet( o, keyValues, path, fulltextIndex, newHistory );
            }
        }
        else if ( node instanceof ObjectNode ) {
            Object[] newHistory = Arrays.copyOf( history, history.length + 1 );
            newHistory[history.length] = node;
            ObjectNode c = ( ObjectNode ) node;
            Iterator<Entry<String, JsonNode>> i = c.fields();
            while ( i.hasNext() ) {
                Entry<String, JsonNode> e = i.next();
                String newPath;
                String key = e.getKey();
                key = quoteString( key );
                if ( key.indexOf( '\\' ) == -1 ) {
                    if ( path != null ) {
                        newPath = path + "." + key;
                    }
                    else {
                        newPath = "" + key;
                    }
                }
                else {
                    if ( path != null ) {
                        newPath = path + "[\"" + key + "\"]";
                    }
                    else {
                        newPath = "" + "[\"" + key + "\"]";
                    }
                }
                buildKeyValueSet( e.getValue(), keyValues, newPath, fulltextIndex, newHistory );
            }
        }
        else if ( !isBasicType( node.getClass() ) && ( !( node instanceof JsonNode ) ) ) {
            buildKeyValueSet( toJsonNode( node ), keyValues, path, fulltextIndex, history );
        }
        else {

            if ( node instanceof JsonNode ) {
                if ( ( ( JsonNode ) node ).isTextual() ) {
                    node = ( ( JsonNode ) node ).asText();
                    UUID uuid = UUIDUtils.tryGetUUID( ( String ) node );
                    if ( uuid != null ) {
                        node = uuid;
                    }
                }
                else if ( ( ( JsonNode ) node ).isNumber() ) {
                    node = ( ( JsonNode ) node ).asInt();
                }
                else {
                    return;
                }
            }

            if ( path == null ) {
                path = "";
            }
            List<Object> l = keyValues.get( path );
            if ( l == null ) {
                l = new ArrayList<Object>();
                keyValues.put( path, l );
            }

            l.add( node );

            if ( ( node instanceof String ) && fulltextIndex ) {
                String keywordsPath = ( path.length() > 0 ) ? path + ".keywords" : "keywords";
                List<Object> keywords = cast( keywords( ( String ) node ) );

                if ( keywords.size() > 0 ) {
                    keyValues.put( keywordsPath, keywords );
                }
            }
        }
    }