internal Action <IInternalObjectDBTransaction, AbstractBufferedWriter, object, object> GetSecondaryKeysKeySaver (uint secondaryKeyIndex, string name) { return(_secondaryKeysSavers.GetOrAdd(secondaryKeyIndex, idx => CreateSaverWithApartFields(ClientRelationVersionInfo.GetSecondaryKeyFields(secondaryKeyIndex), $"Relation_{Name}_SK_{name}_KeySaver"))); }
void CalculateSecondaryKey(IInternalObjectDBTransaction tr, IList <KeyValuePair <uint, SecondaryKeyInfo> > indexes) { var keyWriter = new ByteBufferWriter(); var enumeratorType = typeof(RelationEnumerator <>).MakeGenericType(_clientType); keyWriter.WriteByteArrayRaw(ObjectDB.AllRelationsPKPrefix); keyWriter.WriteVUInt32(Id); var enumerator = (IEnumerator)Activator.CreateInstance(enumeratorType, tr, this, keyWriter.GetDataAndRewind().ToAsyncSafe(), new SimpleModificationCounter()); var keySavers = new Action <IInternalObjectDBTransaction, AbstractBufferedWriter, object> [indexes.Count]; for (int i = 0; i < indexes.Count; i++) { keySavers[i] = CreateSaver(ClientRelationVersionInfo.GetSecondaryKeyFields(indexes[i].Key), $"Relation_{Name}_Upgrade_SK_{indexes[i].Value.Name}_KeySaver"); } while (enumerator.MoveNext()) { var obj = enumerator.Current; tr.TransactionProtector.Start(); tr.KeyValueDBTransaction.SetKeyPrefix(ObjectDB.AllRelationsSKPrefix); for (int i = 0; i < indexes.Count; i++) { keyWriter.WriteVUInt32(Id); keyWriter.WriteVUInt32(indexes[i].Key); keySavers[i](tr, keyWriter, obj); var keyBytes = keyWriter.GetDataAndRewind(); if (!tr.KeyValueDBTransaction.CreateOrUpdateKeyValue(keyBytes, ByteBuffer.NewEmpty())) { throw new BTDBException("Internal error, secondary key bytes must be always unique."); } } } }