in core/src/main/java/com/jetbrains/youtrackdb/internal/core/serialization/serializer/record/string/RecordSerializerCSVAbstract.java [349:546]
public void fieldToStream(
DatabaseSessionInternal session, final EntityImpl iRecord,
final StringWriter iOutput,
final PropertyTypeInternal iType,
final SchemaClass iLinkedClass,
final PropertyTypeInternal iLinkedType,
final String iName,
final Object iValue) {
if (iValue == null) {
return;
}
switch (iType) {
case LINK: {
if (!(iValue instanceof Identifiable)) {
throw new SerializationException(session,
"Found an unexpected type during marshalling of a LINK where a Identifiable (RID"
+ " or any Record) was expected. The string representation of the object is: "
+ iValue);
}
if (!((RecordIdInternal) ((Identifiable) iValue).getIdentity()).isValidPosition()
&& iValue instanceof EntityImpl
&& ((EntityImpl) iValue).isEmbedded()) {
// WRONG: IT'S EMBEDDED!
fieldToStream(session,
iRecord,
iOutput,
PropertyTypeInternal.EMBEDDED,
iLinkedClass,
iLinkedType,
iName,
iValue);
} else {
final Object link = linkToStream(session, iOutput, iRecord, iValue);
if (link != null)
// OVERWRITE CONTENT
{
iRecord.setProperty(iName, link);
}
}
break;
}
case LINKLIST: {
iOutput.append(StringSerializerHelper.LIST_BEGIN);
final EntityLinkListImpl coll;
final Iterator<Identifiable> it;
if (iValue instanceof MultiCollectionIterator<?>) {
final var iterator =
(MultiCollectionIterator<Identifiable>) iValue;
iterator.reset();
it = iterator;
coll = null;
} else if (!(iValue instanceof EntityLinkListImpl)) {
// FIRST TIME: CONVERT THE ENTIRE COLLECTION
coll = new EntityLinkListImpl(iRecord);
if (iValue.getClass().isArray()) {
var iterab = MultiValue.getMultiValueIterable(iValue);
for (var i : iterab) {
coll.add((Identifiable) i);
}
} else {
coll.addAll((Collection<? extends Identifiable>) iValue);
((Collection<? extends Identifiable>) iValue).clear();
}
iRecord.setProperty(iName, coll);
it = coll.iterator();
} else {
// LAZY LIST
coll = (EntityLinkListImpl) iValue;
it = coll.iterator();
}
if (it != null && it.hasNext()) {
final var buffer = new StringWriter(128);
for (var items = 0; it.hasNext(); items++) {
if (items > 0) {
buffer.append(StringSerializerHelper.RECORD_SEPARATOR);
}
final var item = it.next();
final var newRid = linkToStream(session, buffer, iRecord, item);
if (newRid != null) {
((LazyIterator<Identifiable>) it).update(newRid);
}
}
iOutput.append(buffer.toString());
}
iOutput.append(StringSerializerHelper.LIST_END);
break;
}
case LINKSET: {
if (!(iValue instanceof StringWriterSerializable coll)) {
final Collection<Identifiable> coll;
// FIRST TIME: CONVERT THE ENTIRE COLLECTION
if (!(iValue instanceof EntityLinkSetImpl)) {
final var set = new EntityLinkSetImpl(iRecord);
set.addAll((Collection<Identifiable>) iValue);
iRecord.setProperty(iName, set);
coll = set;
} else {
coll = (Collection<Identifiable>) iValue;
}
serializeSet(coll, iOutput);
} else {
// LAZY SET
coll.toStream(session, iOutput);
}
break;
}
case LINKMAP: {
iOutput.append(StringSerializerHelper.MAP_BEGIN);
var map = (Map<String, Object>) iValue;
var invalidMap = false;
var items = 0;
for (var entry : map.entrySet()) {
if (items++ > 0) {
iOutput.append(StringSerializerHelper.RECORD_SEPARATOR);
}
fieldTypeToString(session, iOutput, PropertyTypeInternal.STRING, entry.getKey());
iOutput.append(StringSerializerHelper.ENTRY_SEPARATOR);
final Object link = linkToStream(session, iOutput, iRecord, entry.getValue());
if (link != null && !invalidMap)
// IDENTITY IS CHANGED, RE-SET INTO THE COLLECTION TO RECOMPUTE THE HASH
{
invalidMap = true;
}
}
if (invalidMap) {
final var newMap = new EntityLinkMapIml(iRecord);
// REPLACE ALL CHANGED ITEMS
for (var entry : map.entrySet()) {
newMap.put(entry.getKey(), (Identifiable) entry.getValue());
}
map.clear();
iRecord.setProperty(iName, newMap);
}
iOutput.append(StringSerializerHelper.MAP_END);
break;
}
case EMBEDDED:
if (iValue instanceof DBRecord) {
iOutput.append(StringSerializerHelper.EMBEDDED_BEGIN);
toString(session, (DBRecord) iValue, iOutput, null, true);
iOutput.append(StringSerializerHelper.EMBEDDED_END);
} else if (iValue instanceof EntitySerializable) {
final var entity = ((EntitySerializable) iValue).toEntity(session);
entity.setProperty(EntitySerializable.CLASS_NAME, iValue.getClass().getName());
iOutput.append(StringSerializerHelper.EMBEDDED_BEGIN);
toString(session, entity, iOutput, null, true);
iOutput.append(StringSerializerHelper.EMBEDDED_END);
} else {
iOutput.append(iValue.toString());
}
break;
case EMBEDDEDLIST:
embeddedCollectionToStream(
session, iOutput, iLinkedClass, iLinkedType, iValue, false);
break;
case EMBEDDEDSET:
embeddedCollectionToStream(
session, iOutput, iLinkedClass, iLinkedType, iValue, true);
break;
case EMBEDDEDMAP: {
embeddedMapToStream(session, iOutput, iLinkedType, iValue);
break;
}
case LINKBAG: {
throw new UnsupportedOperationException();
}
default:
fieldTypeToString(session, iOutput, iType, iValue);
}
}