RecordSerializer { public static Bytes serializeIndexKey(DataAccessor record, ColumnsList index, String[] columns) { String[] indexedColumnsList = index.getPrimaryKey(); if (indexedColumnsList.length == 1) { String pkColumn = indexedColumnsList[0]; if (columns.length != 1 && !columns[0].equals(pkColumn)) { throw new IllegalArgumentException("SQLTranslator error, " + Arrays.toString(columns) + " != " + Arrays.asList(pkColumn)); } Column c = index.getColumn(pkColumn); Object v = record.get(c.name); if (v == null) { if (index.allowNullsForIndexedValues()) { return null; } throw new IllegalArgumentException("key field " + pkColumn + " cannot be null. Record data: " + record); } byte[] fieldValue = serialize(v, c.type); return Bytes.from_array(fieldValue); } else { VisibleByteArrayOutputStream key = new VisibleByteArrayOutputStream(columns.length * Long.BYTES); try (ExtendedDataOutputStream doo_key = new ExtendedDataOutputStream(key)) { int i = 0; for (String indexedColumn : columns) { if (!indexedColumn.equals(indexedColumnsList[i])) { throw new IllegalArgumentException("SQLTranslator error, " + Arrays.toString(columns) + " != " + Arrays.asList(indexedColumnsList)); } Column c = index.getColumn(indexedColumn); Object v = record.get(c.name); if (v == null) { if (!index.allowNullsForIndexedValues()) { throw new IllegalArgumentException("key field " + indexedColumn + " cannot be null. Record data: " + record); } if (i == 0) { return null; } else { return Bytes.from_array(key.getBuffer(), 0, key.size()); } } serializeTo(v, c.type, doo_key); i++; } } catch (IOException err) { throw new RuntimeException(err); } return Bytes.from_array(key.getBuffer(), 0, key.size()); } } private  RecordSerializer(); static Object deserialize(Bytes data, int type); static Object deserialize(byte[] data, int type); static int deserializeCompare(byte[] data, int type, Object cvalue); static SQLRecordPredicateFunctions.CompareResult deserializeCompare(Bytes data, int type, Object cvalue); static Object deserializeTypeAndValue(ByteArrayCursor dii); static SQLRecordPredicateFunctions.CompareResult compareDeserializeTypeAndValue(ByteArrayCursor dii, Object cvalue); static void skipTypeAndValue(ByteArrayCursor dii); static byte[] serialize(Object v, int type); static void serializeTo(Object v, int type, ExtendedDataOutputStream out); static void validate(Object v, int type); static void serializeTypeAndValue(Object v, int type, ExtendedDataOutputStream oo); static void serializeValue(Object v, int type, ExtendedDataOutputStream oo); static DateTimeFormatter getUTCTimestampFormatter(); static Object convert(int type, Object value); static DataAccessor buildRawDataAccessor(Record record, Table table); static DataAccessor buildRawDataAccessorForPrimaryKey(Bytes key, Table table); static Record makeRecord(Table table, Object... values); static Bytes serializePrimaryKey(Map<String, Object> record, ColumnsList table, String[] columns); static byte[] serializePrimaryKeyRaw(Map<String, Object> record, ColumnsList table, String[] columns); static Bytes serializeIndexKey(DataAccessor record, ColumnsList index, String[] columns); static void validateIndexableValue(DataAccessor record, ColumnsList indexDefinition, String[] columns); static Object deserializePrimaryKey(Bytes key, Table table); static Map<String, Object> deserializePrimaryKeyAsMap(Bytes key, Table table); static Bytes serializeValue(Map<String, Object> record, Table table); static byte[] serializeValueRaw(Map<String, Object> record, Table table, int expectedSize); static byte[] buildRecord(
            int expectedSize, Table table,
            Function<String, Object> evaluator
    ); static Record toRecord(Map<String, Object> record, Table table); @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED") static Map<String, Object> toBean(Record record, Table table);  }
@Test public void testSerializeIndexKey() throws Exception { Map<String, Object> data = new HashMap<>(); data.put("k1", "key1"); data.put("n1", 1); data.put("l1", 9L); data.put("s1", "aa"); data.put("n2", null); data.put("s2", null); MapDataAccessor map = new MapDataAccessor(data, new String[]{"k1", "n1", "l1", "s1", "n2", "s2"}); testSerializeIndexKey(map, Bytes.from_string("key1"), Column.column("k1", ColumnTypes.STRING)); testSerializeIndexKey(map, Bytes.from_int(1), Column.column("n1", ColumnTypes.INTEGER)); testSerializeIndexKey(map, Bytes.from_long(9), Column.column("l1", ColumnTypes.LONG)); testSerializeIndexKey(map, Bytes.from_string("aa"), Column.column("s1", ColumnTypes.STRING)); testSerializeIndexKey(map, concat(varInt(4), Bytes.from_string("key1"), varInt(4), Bytes.from_int(1)), Column.column("k1", ColumnTypes.STRING), Column.column("n1", ColumnTypes.INTEGER)); testSerializeIndexKey(map, concat(varInt(4), Bytes.from_string("key1"), varInt(4), Bytes.from_int(1), varInt(2), Bytes.from_string("aa")), Column.column("k1", ColumnTypes.STRING), Column.column("n1", ColumnTypes.INTEGER), Column.column("s1", ColumnTypes.STRING)); testSerializeIndexKey(map, concat(varInt(4), Bytes.from_string("key1"), varInt(4), Bytes.from_int(1), varInt(2), Bytes.from_string("aa")), Column.column("k1", ColumnTypes.STRING), Column.column("n1", ColumnTypes.INTEGER), Column.column("s1", ColumnTypes.STRING)); testSerializeIndexKey(map, null, Column.column("s2", ColumnTypes.STRING)); testSerializeIndexKey(map, null, Column.column("s2", ColumnTypes.STRING), Column.column("k1", ColumnTypes.STRING)); testSerializeIndexKey(map, null, Column.column("s2", ColumnTypes.STRING), Column.column("n1", ColumnTypes.INTEGER)); testSerializeIndexKey(map, concat(varInt(4), Bytes.from_string("key1")), Column.column("k1", ColumnTypes.STRING), Column.column("s2", ColumnTypes.STRING)); testSerializeIndexKey(map, concat(varInt(4), Bytes.from_string("key1"), varInt(4), Bytes.from_int(1)), Column.column("k1", ColumnTypes.STRING), Column.column("n1", ColumnTypes.INTEGER), Column.column("s2", ColumnTypes.STRING)); }