/*
 * Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.expression.reference.doc.lucene;

import io.crate.metadata.doc.DocSysColumns;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.StoredFieldVisitor;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.index.mapper.Uid;

import java.io.IOException;

public final class IdCollectorExpression extends LuceneCollectorExpression<BytesRef> {

    public static final String COLUMN_NAME = DocSysColumns.ID.name();
    private final IDVisitor visitor = new IDVisitor();
    private LeafReader reader;

    public IdCollectorExpression() {
        super(COLUMN_NAME);
    }

    @Override
    public void setNextDocId(int docId) throws IOException {
        visitor.canStop = false;
        reader.document(docId, visitor);
    }

    @Override
    public BytesRef value() {
        return visitor.id;
    }

    @Override
    public void setNextReader(LeafReaderContext context) {
        reader = context.reader();
    }


    private static class IDVisitor extends StoredFieldVisitor {

        private static final int UTF8 = 0xff;

        private boolean canStop = false;
        private BytesRef id;

        @Override
        public Status needsField(FieldInfo fieldInfo) {
            if (canStop) {
                return Status.STOP;
            }
            if (COLUMN_NAME.equals(fieldInfo.name)) {
                canStop = true;
                return Status.YES;
            }
            return Status.NO;
        }

        @Override
        public void binaryField(FieldInfo fieldInfo, byte[] value) {
            assert COLUMN_NAME.equals(fieldInfo.name) : "binaryField must only be called for id";
            id = decodeId(value);
        }

        static BytesRef decodeId(byte[] idBytes) {
            final int magicChar = Byte.toUnsignedInt(idBytes[0]);
            if (magicChar == UTF8) {
                // handle this directly to avoid BytesRef -> String -> BytesRef round-trip
                return new BytesRef(idBytes, 1, idBytes.length - 1);
            }
            return new BytesRef(Uid.decodeId(idBytes));
        }
    }
}

@@ -2807,32 +2807,335 @@
 ue);%0A        %7D%0A%0A
+        @Override%0A        public void stringField(FieldInfo fieldInfo, byte%5B%5D value) %7B%0A            assert COLUMN_NAME.equals(fieldInfo.name) : %22stringField must only be called for id%22;%0A            // Indices prior to CrateDB 3.0 have id stored as string%0A            id = new BytesRef(value);%0A        %7D%0A%0A
         static B

Make IdCollectorExpression compatible for old indices