Share via


CustomFilterInfo Constructor

Initializes a new instance of the CustomFilterInfo class that contains the specified ID format schema and custom filter.

Namespace: Microsoft.Synchronization
Assembly: Microsoft.Synchronization (in microsoft.synchronization.dll)

Syntax

'Declaration
Public Sub New ( _
    idFormats As SyncIdFormatGroup, _
    syncFilter As ISyncFilter _
)
'Usage
Dim idFormats As SyncIdFormatGroup
Dim syncFilter As ISyncFilter

Dim instance As New CustomFilterInfo(idFormats, syncFilter)
public CustomFilterInfo (
    SyncIdFormatGroup idFormats,
    ISyncFilter syncFilter
)
public:
CustomFilterInfo (
    SyncIdFormatGroup^ idFormats, 
    ISyncFilter^ syncFilter
)
public CustomFilterInfo (
    SyncIdFormatGroup idFormats, 
    ISyncFilter syncFilter
)
public function CustomFilterInfo (
    idFormats : SyncIdFormatGroup, 
    syncFilter : ISyncFilter
)

Parameters

  • idFormats
    The ID format schema of the provider.
  • syncFilter
    A custom filter object.

Exceptions

Exception type Condition

ArgumentNullException

A required parameter is a null reference (Nothing in Visual Basic).

Remarks

The custom filter that is represented by syncFilter is typically implemented by the developer of the filtered provider.

Example

The following example uses a custom filter to enumerate changes from a filtered replica.

Public Overloads Overrides Function GetChangeBatch(ByVal batchSize As UInteger, ByVal destinationKnowledge As SyncKnowledge, ByRef changeDataRetriever As Object) As ChangeBatch
    ' Return this object as the IChangeDataRetriever object that is called to retrieve item data.
    changeDataRetriever = Me

    ' The metadata storage service does not support filter tracking, so enumerate changes manually.
    Dim changeBatch As ChangeBatch
    If _filterForSync Is Nothing Then
        changeBatch = New ChangeBatch(IdFormats, destinationKnowledge, _ContactStore.ContactReplicaMetadata.GetForgottenKnowledge())
    Else
        Dim filterInfo As New CustomFilterInfo(IdFormats, _filterForSync)
        changeBatch = New ChangeBatch(IdFormats, destinationKnowledge, _filterForSync.FilterForgottenKnowledge, filterInfo)
    End If

    ' First, set the filter key map, if the destination replica tracks any filters that are
    ' tracked by the source replica.
    If 0 < FilterKeyMap.Count Then
        ' Add the filter key map to the change batch before any groups are started.
        changeBatch.FilterKeyMap = FilterKeyMap
    End If

    ' Get all the items from the metadata store.
    Dim allItems As IEnumerable(Of ItemMetadata) = _ContactStore.ContactReplicaMetadata.GetAllItems(True)

    ' Convert the destination knowledge for use with local versions.
    Dim mappedDestKnowledge As SyncKnowledge = _ContactStore.ContactReplicaMetadata.GetKnowledge().MapRemoteKnowledgeToLocal(destinationKnowledge)

    ' Enumerate the items in the change batch.
    Dim itemChanges As New List(Of ItemChange)(CInt(batchSize))
    Dim cItemsInBatch As UInteger = 0
    Dim replicaId As SyncId = _ContactStore.ContactReplicaMetadata.ReplicaId
    For Each itemMeta As ItemMetadata In allItems
        ' Process all items if this is an unfiltered enumeration. Otherwise, only process an item that has been in the filter.
        If _filterForSync Is Nothing OrElse _ContactStore.HasBeenInFilter(itemMeta, _filterForSync) Then
            ' If a change is not contained in the destination knowledge, add it to the change batch.
            If Not mappedDestKnowledge.Contains(replicaId, itemMeta.GlobalId, itemMeta.ChangeVersion) Then
                Dim kind As ChangeKind
                If itemMeta.IsDeleted Then
                    kind = ChangeKind.Deleted
                ElseIf _filterForSync IsNot Nothing AndAlso Not _filterForSync.IsInFilter(_ContactStore.ContactList(itemMeta.GlobalId)) Then
                    kind = ChangeKind.Ghost
                Else
                    kind = ChangeKind.Update
                End If

                Dim itemChange As New ItemChange(IdFormats, _ContactStore.ContactReplicaMetadata.ReplicaId, itemMeta.GlobalId, kind, itemMeta.CreationVersion, itemMeta.ChangeVersion)

                ' Pass along any filter information for filters tracked by both the source and destination replicas.
                _ContactStore.AddFilterChanges(_filterKeyMap, itemMeta, mappedDestKnowledge, itemChange)

                ' Add the item to the change list. Include ghosts only if the destination requested ghosts.
                If kind <> ChangeKind.Ghost OrElse (kind = ChangeKind.Ghost AndAlso FilteringType.CurrentItemsAndVersionsForMovedOutItems = _filteringType) Then
                    itemChanges.Add(itemChange)
                End If
                cItemsInBatch += 1
            End If
        End If

        If batchSize <= cItemsInBatch Then
            Exit For
        End If
    Next

    If 0 < itemChanges.Count Then
        changeBatch.BeginOrderedGroup(itemChanges(0).ItemId)

        ' Set the filter forgotten knowledge for each filter that the destination has requested.
        For iFilter As Integer = 0 To FilterKeyMap.Count - 1
            Dim addressFilter As AddressFilter = DirectCast(FilterKeyMap(iFilter), AddressFilter)
            changeBatch.SetFilterForgottenKnowledge(CUInt(iFilter), addressFilter.FilterForgottenKnowledge)
        Next

        changeBatch.AddChanges(itemChanges)

        ' End the group of changes in the change batch. Pass the current source knowledge.
        changeBatch.EndOrderedGroup(itemChanges(itemChanges.Count - 1).ItemId, _ContactStore.ContactReplicaMetadata.GetKnowledge())

        ' If all items were enumerated before the batch was filled, then this is the last batch.
        If cItemsInBatch < batchSize Then
            changeBatch.SetLastBatch()
        End If
    Else
        Throw New InvalidOperationException("GetChangeBatch called but there are no new changes to enumerate.")
    End If

    Return changeBatch
End Function
public override ChangeBatch GetChangeBatch(uint batchSize, SyncKnowledge destinationKnowledge, out object changeDataRetriever)
{
    // Return this object as the IChangeDataRetriever object that is called to retrieve item data.
    changeDataRetriever = this;

    // The metadata storage service does not support filter tracking, so enumerate changes manually.
    ChangeBatch changeBatch;
    if (null == _filterForSync)
    {
        changeBatch = new ChangeBatch(IdFormats, destinationKnowledge, _ContactStore.ContactReplicaMetadata.GetForgottenKnowledge());
    }
    else
    {
        CustomFilterInfo filterInfo = new CustomFilterInfo(IdFormats, _filterForSync);
        changeBatch = new ChangeBatch(IdFormats, destinationKnowledge, _filterForSync.FilterForgottenKnowledge, filterInfo);
    }

    // First, set the filter key map, if the destination replica tracks any filters that are
    // tracked by the source replica.
    if (0 < FilterKeyMap.Count)
    {
        // Add the filter key map to the change batch before any groups are started.
        changeBatch.FilterKeyMap = FilterKeyMap;
    }

    // Get all the items from the metadata store.
    IEnumerable<ItemMetadata> allItems = _ContactStore.ContactReplicaMetadata.GetAllItems(true);

    // Convert the destination knowledge for use with local versions.
    SyncKnowledge mappedDestKnowledge = _ContactStore.ContactReplicaMetadata.GetKnowledge().MapRemoteKnowledgeToLocal(destinationKnowledge);

    // Enumerate the items in the change batch.
    List<ItemChange> itemChanges = new List<ItemChange>((int)batchSize);
    uint cItemsInBatch = 0;
    SyncId replicaId = _ContactStore.ContactReplicaMetadata.ReplicaId;
    foreach (ItemMetadata itemMeta in allItems)
    {
        // Process all items if this is an unfiltered enumeration. Otherwise, only process an item that has been in the filter.
        if (null == _filterForSync || _ContactStore.HasBeenInFilter(itemMeta, _filterForSync))
        {
            // If a change is not contained in the destination knowledge, add it to the change batch.
            if (!mappedDestKnowledge.Contains(replicaId, itemMeta.GlobalId, itemMeta.ChangeVersion))
            {
                ChangeKind kind;
                if (itemMeta.IsDeleted)
                {
                    kind = ChangeKind.Deleted;
                }
                else if (null != _filterForSync && !_filterForSync.IsInFilter(_ContactStore.ContactList[itemMeta.GlobalId]))
                {
                    kind = ChangeKind.Ghost;
                }
                else
                {
                    kind = ChangeKind.Update;
                }

                ItemChange itemChange = new ItemChange(IdFormats, _ContactStore.ContactReplicaMetadata.ReplicaId,
                    itemMeta.GlobalId, kind, itemMeta.CreationVersion, itemMeta.ChangeVersion);

                // Pass along any filter information for filters tracked by both the source and destination replicas.
                _ContactStore.AddFilterChanges(_filterKeyMap, itemMeta, mappedDestKnowledge, itemChange);

                // Add the item to the change list. Include ghosts only if the destination requested ghosts.
                if (kind != ChangeKind.Ghost || (kind == ChangeKind.Ghost && FilteringType.CurrentItemsAndVersionsForMovedOutItems == _filteringType))
                {
                    itemChanges.Add(itemChange);
                }
                cItemsInBatch++;
            }
        }

        if (batchSize <= cItemsInBatch)
        {
            break;
        }
    }

    if (0 < itemChanges.Count)
    {
        changeBatch.BeginOrderedGroup(itemChanges[0].ItemId);

        // Set the filter forgotten knowledge for each filter that the destination has requested.
        for (int iFilter = 0; iFilter < FilterKeyMap.Count; iFilter++)
        {
            AddressFilter addressFilter = (AddressFilter)FilterKeyMap[iFilter];
            changeBatch.SetFilterForgottenKnowledge((uint)iFilter, addressFilter.FilterForgottenKnowledge);
        }

        changeBatch.AddChanges(itemChanges);

        // End the group of changes in the change batch. Pass the current source knowledge.
        changeBatch.EndOrderedGroup(itemChanges[itemChanges.Count - 1].ItemId, _ContactStore.ContactReplicaMetadata.GetKnowledge());

        // If all items were enumerated before the batch was filled, then this is the last batch.
        if (cItemsInBatch < batchSize)
        {
            changeBatch.SetLastBatch();
        }
    }
    else
    {
        throw new InvalidOperationException("GetChangeBatch called but there are no new changes to enumerate.");
    }

    return changeBatch;
}

See Also

Reference

CustomFilterInfo Class
CustomFilterInfo Members
Microsoft.Synchronization Namespace