use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class CursorMark method parseSerializedTotem.
/**
* Parses the serialized version of a CursorMark from a client
* (which must conform to the existing sortSpec) and populates this object.
*
* @see #getSerializedTotem
*/
public void parseSerializedTotem(final String serialized) {
if (CURSOR_MARK_START.equals(serialized)) {
values = null;
return;
}
final SortField[] sortFields = sortSpec.getSort().getSort();
final List<SchemaField> schemaFields = sortSpec.getSchemaFields();
List<Object> pieces = null;
try {
final byte[] rawData = Base64.base64ToByteArray(serialized);
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
try {
pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
boolean b = false;
for (Object o : pieces) {
if (o instanceof BytesRefBuilder || o instanceof BytesRef || o instanceof String) {
b = true;
break;
}
}
if (b) {
in.reset();
pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
}
} finally {
in.close();
}
} catch (Exception ex) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Unable to parse '" + CURSOR_MARK_PARAM + "' after totem: " + "value must either be '" + CURSOR_MARK_START + "' or the " + "'" + CURSOR_MARK_NEXT + "' returned by a previous search: " + serialized, ex);
}
assert null != pieces : "pieces wasn't parsed?";
if (sortFields.length != pieces.size()) {
throw new SolrException(ErrorCode.BAD_REQUEST, CURSOR_MARK_PARAM + " does not work with current sort (wrong size): " + serialized);
}
this.values = new ArrayList<>(sortFields.length);
final BytesRef tmpBytes = new BytesRef();
for (int i = 0; i < sortFields.length; i++) {
SortField curSort = sortFields[i];
SchemaField curField = schemaFields.get(i);
Object rawValue = pieces.get(i);
if (null != curField) {
FieldType curType = curField.getType();
rawValue = curType.unmarshalSortValue(rawValue);
}
this.values.add(rawValue);
}
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class CursorMark method getSerializedTotem.
/**
* Generates a Base64 encoded serialized representation of the sort values
* encapsulated by this object, for use in cursor requests.
*
* @see #parseSerializedTotem
*/
public String getSerializedTotem() {
if (null == this.values) {
return CURSOR_MARK_START;
}
final List<SchemaField> schemaFields = sortSpec.getSchemaFields();
final ArrayList<Object> marshalledValues = new ArrayList<>(values.size() + 1);
for (int i = 0; i < schemaFields.size(); i++) {
SchemaField fld = schemaFields.get(i);
Object safeValue = values.get(i);
if (null != fld) {
FieldType type = fld.getType();
safeValue = type.marshalSortValue(safeValue);
}
marshalledValues.add(safeValue);
}
try {
ByteArrayOutputStream out = new ByteArrayOutputStream(256);
try {
new JavaBinCodec().marshal(marshalledValues, out);
byte[] rawData = out.toByteArray();
return Base64.byteArrayToBase64(rawData, 0, rawData.length);
} finally {
out.close();
}
} catch (Exception ex) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Unable to format search after totem", ex);
}
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class DocBasedVersionConstraintsProcessorFactory method inform.
@Override
public void inform(SolrCore core) {
if (core.getUpdateHandler().getUpdateLog() == null) {
throw new SolrException(SERVER_ERROR, "updateLog must be enabled.");
}
if (core.getLatestSchema().getUniqueKeyField() == null) {
throw new SolrException(SERVER_ERROR, "schema must have uniqueKey defined.");
}
SchemaField userVersionField = core.getLatestSchema().getField(versionField);
if (userVersionField == null || !userVersionField.stored() || userVersionField.multiValued()) {
throw new SolrException(SERVER_ERROR, "field " + versionField + " must be defined in schema, be stored, and be single valued.");
}
try {
ValueSource vs = userVersionField.getType().getValueSource(userVersionField, null);
useFieldCache = true;
} catch (Exception e) {
log.warn("Can't use fieldcache/valuesource: " + e.getMessage());
}
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class IndexFingerprint method getFingerprint.
public static IndexFingerprint getFingerprint(SolrIndexSearcher searcher, LeafReaderContext ctx, Long maxVersion) throws IOException {
SchemaField versionField = VersionInfo.getAndCheckVersionField(searcher.getSchema());
ValueSource vs = versionField.getType().getValueSource(versionField, null);
Map funcContext = ValueSource.newContext(searcher);
vs.createWeight(funcContext, searcher);
IndexFingerprint f = new IndexFingerprint();
f.maxVersionSpecified = maxVersion;
f.maxDoc = ctx.reader().maxDoc();
f.numDocs = ctx.reader().numDocs();
int maxDoc = ctx.reader().maxDoc();
Bits liveDocs = ctx.reader().getLiveDocs();
FunctionValues fv = vs.getValues(funcContext, ctx);
for (int doc = 0; doc < maxDoc; doc++) {
if (liveDocs != null && !liveDocs.get(doc))
continue;
long v = fv.longVal(doc);
f.maxVersionEncountered = Math.max(v, f.maxVersionEncountered);
if (v <= f.maxVersionSpecified) {
f.maxInHash = Math.max(v, f.maxInHash);
f.versionsHash += Hash.fmix64(v);
f.numVersions++;
}
}
return f;
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class VersionInfo method getAndCheckVersionField.
/**
* Gets and returns the {@link org.apache.solr.common.params.CommonParams#VERSION_FIELD} from the specified
* schema, after verifying that it is indexed, stored, and single-valued.
* If any of these pre-conditions are not met, it throws a SolrException
* with a user suitable message indicating the problem.
*/
public static SchemaField getAndCheckVersionField(IndexSchema schema) throws SolrException {
final String errPrefix = VERSION_FIELD + " field must exist in schema and be searchable (indexed or docValues) and retrievable(stored or docValues) and not multiValued";
SchemaField sf = schema.getFieldOrNull(VERSION_FIELD);
if (null == sf) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " does not exist)");
}
if (!sf.indexed() && !sf.hasDocValues()) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " not searchable");
}
if (!sf.stored() && !sf.hasDocValues()) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " not retrievable");
}
if (sf.multiValued()) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " is multiValued");
}
return sf;
}
Aggregations