mirror of
https://github.com/twitter/the-algorithm.git
synced 2024-06-01 08:48:46 +02:00
ef4c5eb65e
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
37 lines
1.3 KiB
Java
37 lines
1.3 KiB
Java
package com.twitter.search.common.schema.earlybird;
|
|
|
|
import com.twitter.search.common.encoding.docvalues.CSFTypeUtil;
|
|
import com.twitter.search.common.schema.base.ImmutableSchemaInterface;
|
|
|
|
public final class EarlybirdEncodedFeaturesUtil {
|
|
private EarlybirdEncodedFeaturesUtil() {
|
|
}
|
|
|
|
/**
|
|
* Returns a byte array that can be stored in a ThriftDocument as bytesField.
|
|
*/
|
|
public static byte[] toBytesForThriftDocument(EarlybirdEncodedFeatures features) {
|
|
int numInts = features.getNumInts();
|
|
byte[] serializedFeatures = new byte[numInts * Integer.BYTES];
|
|
for (int i = 0; i < numInts; i++) {
|
|
CSFTypeUtil.convertToBytes(serializedFeatures, i, features.getInt(i));
|
|
}
|
|
return serializedFeatures;
|
|
}
|
|
|
|
/**
|
|
* Converts data in a given byte array (starting at the provided offset) into
|
|
* EarlybirdEncodedFeatures.
|
|
*/
|
|
public static EarlybirdEncodedFeatures fromBytes(
|
|
ImmutableSchemaInterface schema, EarlybirdFieldConstants.EarlybirdFieldConstant baseField,
|
|
byte[] data, int offset) {
|
|
EarlybirdEncodedFeatures features = EarlybirdEncodedFeatures.newEncodedTweetFeatures(
|
|
schema, baseField);
|
|
for (int idx = 0; idx < features.getNumInts(); ++idx) {
|
|
features.setInt(idx, CSFTypeUtil.convertFromBytes(data, offset, idx));
|
|
}
|
|
return features;
|
|
}
|
|
}
|