Search in sources :

Example 1 with IndexByteEncoder

use of com.google.firebase.firestore.index.IndexByteEncoder in project firebase-android-sdk by firebase.

the class SQLiteIndexManager method encodeValues.

/**
 * Encodes the given field values according to the specification in {@code target}. For IN
 * queries, a list of possible values is returned.
 */
@Nullable
private Object[] encodeValues(FieldIndex fieldIndex, Target target, @Nullable Collection<Value> values) {
    if (values == null)
        return null;
    List<IndexByteEncoder> encoders = new ArrayList<>();
    encoders.add(new IndexByteEncoder());
    Iterator<Value> position = values.iterator();
    for (FieldIndex.Segment segment : fieldIndex.getDirectionalSegments()) {
        Value value = position.next();
        for (IndexByteEncoder encoder : encoders) {
            if (isInFilter(target, segment.getFieldPath()) && isArray(value)) {
                encoders = expandIndexValues(encoders, segment, value);
            } else {
                DirectionalIndexByteEncoder directionalEncoder = encoder.forKind(segment.getKind());
                FirestoreIndexValueWriter.INSTANCE.writeIndexValue(value, directionalEncoder);
            }
        }
    }
    return getEncodedBytes(encoders);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) ArrayList(java.util.ArrayList) Value(com.google.firestore.v1.Value) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) IndexByteEncoder(com.google.firebase.firestore.index.IndexByteEncoder) Nullable(androidx.annotation.Nullable)

Example 2 with IndexByteEncoder

use of com.google.firebase.firestore.index.IndexByteEncoder in project firebase-android-sdk by firebase.

the class SQLiteIndexManager method encodeDirectionalElements.

/**
 * Returns the byte encoded form of the directional values in the field index. Returns {@code
 * null} if the document does not have all fields specified in the index.
 */
@Nullable
private byte[] encodeDirectionalElements(FieldIndex fieldIndex, Document document) {
    IndexByteEncoder encoder = new IndexByteEncoder();
    for (FieldIndex.Segment segment : fieldIndex.getDirectionalSegments()) {
        Value field = document.getField(segment.getFieldPath());
        if (field == null) {
            return null;
        }
        DirectionalIndexByteEncoder directionalEncoder = encoder.forKind(segment.getKind());
        FirestoreIndexValueWriter.INSTANCE.writeIndexValue(field, directionalEncoder);
    }
    return encoder.getEncodedBytes();
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) Value(com.google.firestore.v1.Value) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) IndexByteEncoder(com.google.firebase.firestore.index.IndexByteEncoder) Nullable(androidx.annotation.Nullable)

Example 3 with IndexByteEncoder

use of com.google.firebase.firestore.index.IndexByteEncoder in project firebase-android-sdk by firebase.

the class SQLiteIndexManager method expandIndexValues.

/**
 * Creates a separate encoder for each element of an array.
 *
 * <p>The method appends each value to all existing encoders (e.g. filter("a", "==",
 * "a1").filter("b", "in", ["b1", "b2"]) becomes ["a1,b1", "a1,b2"]). A list of new encoders is
 * returned.
 */
private List<IndexByteEncoder> expandIndexValues(List<IndexByteEncoder> encoders, FieldIndex.Segment segment, Value value) {
    List<IndexByteEncoder> prefixes = new ArrayList<>(encoders);
    List<IndexByteEncoder> results = new ArrayList<>();
    for (Value arrayElement : value.getArrayValue().getValuesList()) {
        for (IndexByteEncoder prefix : prefixes) {
            IndexByteEncoder clonedEncoder = new IndexByteEncoder();
            clonedEncoder.seed(prefix.getEncodedBytes());
            FirestoreIndexValueWriter.INSTANCE.writeIndexValue(arrayElement, clonedEncoder.forKind(segment.getKind()));
            results.add(clonedEncoder);
        }
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) Value(com.google.firestore.v1.Value) DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) IndexByteEncoder(com.google.firebase.firestore.index.IndexByteEncoder)

Example 4 with IndexByteEncoder

use of com.google.firebase.firestore.index.IndexByteEncoder in project firebase-android-sdk by firebase.

the class SQLiteIndexManager method encodeSingleElement.

/**
 * Encodes a single value to the ascending index format.
 */
private byte[] encodeSingleElement(Value value) {
    IndexByteEncoder encoder = new IndexByteEncoder();
    FirestoreIndexValueWriter.INSTANCE.writeIndexValue(value, encoder.forKind(FieldIndex.Segment.Kind.ASCENDING));
    return encoder.getEncodedBytes();
}
Also used : DirectionalIndexByteEncoder(com.google.firebase.firestore.index.DirectionalIndexByteEncoder) IndexByteEncoder(com.google.firebase.firestore.index.IndexByteEncoder)

Aggregations

DirectionalIndexByteEncoder (com.google.firebase.firestore.index.DirectionalIndexByteEncoder)4 IndexByteEncoder (com.google.firebase.firestore.index.IndexByteEncoder)4 Value (com.google.firestore.v1.Value)3 Nullable (androidx.annotation.Nullable)2 FieldIndex (com.google.firebase.firestore.model.FieldIndex)2 ArrayList (java.util.ArrayList)2