Поделиться через


Метод BeginOrderedGroup

Открывает упорядоченную группу в пакете изменений. Эта группа сортируется по идентификатору элемента.

Пространство имен:  Microsoft.Synchronization
Сборка:  Microsoft.Synchronization (в Microsoft.Synchronization.dll)

Синтаксис

'Декларация
Public Sub BeginOrderedGroup ( _
    itemId As SyncId _
)
'Применение
Dim instance As ChangeBatchBase
Dim itemId As SyncId

instance.BeginOrderedGroup(itemId)
public void BeginOrderedGroup(
    SyncId itemId
)
public:
void BeginOrderedGroup(
    SyncId^ itemId
)
member BeginOrderedGroup : 
        itemId:SyncId -> unit 
public function BeginOrderedGroup(
    itemId : SyncId
)

Параметры

  • itemId
    Тип Microsoft.Synchronization. . :: . .SyncId
    Закрытая нижняя граница идентификаторов элементов для этой упорядоченной группы. Чтобы задать нижнюю границу, равную 0, используйте Zero.

Исключения

Исключение Условие
ArgumentNullException

Параметр itemId равен null Nothing nullptr unit пустая ссылка (Nothing в Visual Basic) .

ObjectDisposedException

Этот ChangeBatch уже был удален.

SyncInvalidOperationException

Пакет изменений содержит группу, которая была начата, но не была завершена.

ChangeBatchIsReadOnlyException

Пакет изменений уже был отправлен стороне, применяющей изменения, или сеансу синхронизации. Изменения нельзя добавлять в пакет после того, как он отправлен.

Замечания

Изменения элементов, добавляемые к пакету изменений после вызова этого метода, добавляются в открытую группу.

Изменения элементов нельзя добавить в пакет, пока не создана группа.

Примеры

В следующем примере реализуется метод GetChangeBatch путем добавления элементов к упорядоченной группе в пакете изменений. Элемент добавляется, только если его версия изменения не содержится в наборе знаний назначения.

Public Overrides Function GetChangeBatch(ByVal batchSize As UInteger, ByVal destinationKnowledge As SyncKnowledge) As ChangeBatch
    ' The destination knowledge must be converted to be compatible with the source replica
    ' before it can be used.
    Dim mappedDestKnowledge As SyncKnowledge = _knowledge.MapRemoteKnowledgeToLocal(destinationKnowledge)

    ' Create a new change batch, initialized by using the current knowledge of the source replica
    ' and a new ForgottenKnowledge object.
    Dim changeBatch As New ChangeBatch(IdFormats, GetKnowledge(), New ForgottenKnowledge())

    ' Start a group of changes in the change batch. The group is ordered by item ID.
    ' _getChangeBatchCurrent is 0 the first time GetChangeBatch is called, and is used to track the
    ' position in the metadata store for subsequent calls to GetChangeBatch.
    changeBatch.BeginOrderedGroup(_items.Values(_getChangeBatchCurrent).GlobalId)

    ' itemsAdded is incremented each time a change is added to the change batch. When itemsAdded
    ' is greater than the requested batch size, enumeration stops and the change batch is returned.
    Dim itemsAdded As Integer = 0

    Dim itemMeta As ItemMetadata

    ' Enumerate items and add a change to the change batch if it is not contained in the 
    ' destination knowledge.
    ' _items is a SortedList that contains ItemMetadata objects that are ordered by item ID.
    While itemsAdded <= batchSize AndAlso _getChangeBatchCurrent < _items.Count
        itemMeta = _items.Values(_getChangeBatchCurrent)
        Dim kind As ChangeKind = If((itemMeta.IsDeleted), ChangeKind.Deleted, ChangeKind.Update)
        Dim change As New ItemChange(IdFormats, ReplicaId, itemMeta.GlobalId, kind, itemMeta.CreationVersion, itemMeta.ChangeVersion)

        ' If the change is not contained in the destination knowledge, add it to the change batch.
        If Not mappedDestKnowledge.Contains(change) Then
            changeBatch.AddChange(change)
            itemsAdded += 1
        End If
        _getChangeBatchCurrent += 1
    End While

    ' End the group of changes in the change batch. Pass the current source knowledge.
    changeBatch.EndOrderedGroup(_items.Values(_getChangeBatchCurrent - 1).GlobalId, _knowledge)

    ' When all items in the metadata store have been enumerated, set this batch as the
    ' last batch.
    If _getChangeBatchCurrent = _items.Count Then
        changeBatch.SetLastBatch()
    End If

    Return changeBatch
End Function
public override ChangeBatch GetChangeBatch(uint batchSize, SyncKnowledge destinationKnowledge)
{
    // The destination knowledge must be converted to be compatible with the source replica
    // before it can be used.
    SyncKnowledge mappedDestKnowledge = _knowledge.MapRemoteKnowledgeToLocal(destinationKnowledge);

    // Create a new change batch, initialized by using the current knowledge of the source replica
    // and a new ForgottenKnowledge object.
    ChangeBatch changeBatch = new ChangeBatch(IdFormats, GetKnowledge(), new ForgottenKnowledge());

    // Start a group of changes in the change batch. The group is ordered by item ID.
    // _getChangeBatchCurrent is 0 the first time GetChangeBatch is called, and is used to track the
    // position in the metadata store for subsequent calls to GetChangeBatch.
    changeBatch.BeginOrderedGroup(_items.Values[_getChangeBatchCurrent].GlobalId);

    // itemsAdded is incremented each time a change is added to the change batch. When itemsAdded
    // is greater than the requested batch size, enumeration stops and the change batch is returned.
    int itemsAdded = 0;

    ItemMetadata itemMeta;

    // Enumerate items and add a change to the change batch if it is not contained in the 
    // destination knowledge.
    // _items is a SortedList that contains ItemMetadata objects that are ordered by item ID.
    for (; itemsAdded <= batchSize && _getChangeBatchCurrent < _items.Count; _getChangeBatchCurrent++)
    {
        itemMeta = _items.Values[_getChangeBatchCurrent];
        ChangeKind kind = (itemMeta.IsDeleted) ? ChangeKind.Deleted : ChangeKind.Update;
        ItemChange change = new ItemChange(IdFormats, ReplicaId, itemMeta.GlobalId, kind, itemMeta.CreationVersion, 
            itemMeta.ChangeVersion);

        // If the change is not contained in the destination knowledge, add it to the change batch.
        if (!mappedDestKnowledge.Contains(change))
        {
            changeBatch.AddChange(change);
            itemsAdded++;
        }
    }

    // End the group of changes in the change batch. Pass the current source knowledge.
    changeBatch.EndOrderedGroup(_items.Values[_getChangeBatchCurrent - 1].GlobalId, _knowledge);

    // When all items in the metadata store have been enumerated, set this batch as the
    // last batch.
    if (_getChangeBatchCurrent == _items.Count)
    {
        changeBatch.SetLastBatch();
    }

    return changeBatch;
}

См. также

Справочник

ChangeBatchBase Класс

Элементы ChangeBatchBase

Пространство имен Microsoft.Synchronization