Sync101 with Custom Filtering

Shows how to track custom filters and how to use a standard custom provider to send changes from a filter-tracking replica to two different filtered replicas.

C# (58.2 KB)
 
 
 
 
 
(0)
1,322 times
Add to favorites
5/13/2011
E-mail Twitter del.icio.us Digg Facebook
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Synchronization;

namespace CustomFilterSyncSample
{
    public class DataStore<T> where T:ICloneable
    {
        
              
        public DataStore(IDataItemOperations<T> dalObject)
        {
            _items = new SortedDictionary<SyncId, SyncableItem<T>>();
            _dalOperations = dalObject;
        }

        internal void AddItem(ItemChange change, T changeData)
        {
            if (!_items.ContainsKey(change.ItemId))
            {
                SyncableItem<T> item = new SyncableItem<T>(change.ItemId, changeData);
                _items.Add(change.ItemId, item);
                item.CreationVersion = change.CreationVersion;
                item.CurrentVersion = change.ChangeVersion;                
            }
        }

        internal void CreateGhost(ItemChange change)
        {
            SyncableItem<T> item = new SyncableItem<T>(change.ItemId, default(T) /* item = null */);
            item.ChangeKind = ChangeKind.Ghost;
            _items.Add(change.ItemId, item);
        }

        internal void MarkItemAsGhost(ItemChange change)
        {
            SyncableItem<T> item = _items[change.ItemId];
            item.Item = default(T);
            item.ChangeKind = ChangeKind.Ghost;
        }

        internal void UnMarkItemAsGhost(ItemChange change, T changeData)
        {
            SyncableItem<T> item = _items[change.ItemId];
            item.Item = changeData;
            item.ChangeKind = ChangeKind.Update;
        }

        internal void UpdateItem(ItemChange change, object changeData)
        {
            SyncableItem<T> item = _items[change.ItemId];
            _dalOperations.UpdateItem(item.Item, changeData);
            item.CurrentVersion = change.ChangeVersion;            
        }

        internal void UpdateItem(ItemChange change)
        {
            SyncableItem<T> item = _items[change.ItemId];
            item.CurrentVersion = change.ChangeVersion;            
        }
             
        public SyncableItem<T> CreateItem(Guid itemId)
        {
            // Create
            SyncableItem<T> item = new SyncableItem<T>(new SyncId(itemId),_dalOperations.CreateItem());
            item.CreationVersion = new SyncVersion(0, _tickCount);
            item.CurrentVersion = new SyncVersion(0, _tickCount);
            
            _items.Add(item.GlobalId, item);
            return item;
        }

        public void UpdateItem(SyncableItem<T> item, object value)
        {
            _dalOperations.UpdateItem(item.Item, value);
        }


        public ulong GetNextTickCount()
        {
            return ++_tickCount;
        }

        public ulong TickCount
        {
            get { return _tickCount; }
            set { _tickCount = value; }
        }
                
        public SyncableItem<T> GetSyncableItem(SyncId id)
        {           
            return _items[id];           
        }

        public SyncableItem<T> this[int itemIndex]
        {
            get
            {
                SortedDictionary<SyncId, SyncableItem<T>>.Enumerator itemEnumerator = _items.GetEnumerator();
                itemEnumerator.MoveNext();
                
                for (int index = 0; index < itemIndex; index++)
                {
                    itemEnumerator.MoveNext();
                }

                return itemEnumerator.Current.Value;            
            }
        }

        public bool TryGetValue(SyncId itemId, out SyncableItem<T> item)
        {
            return _items.TryGetValue(itemId, out item);
        }

        public bool ContainsKey(SyncId itemId)
        {
            return _items.ContainsKey(itemId);
        }

        public int Count
        {
            get
            {
                return _items.Count;
            }
        }

        public SortedDictionary<SyncId, SyncableItem<T>>.Enumerator GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        public override string ToString()
        {
            StringBuilder buffer = new StringBuilder();
            foreach (SyncId id in _items.Keys)
            {
                //Print out the item's ID and the Metadata associated
                buffer.AppendFormat("[{0}]: {1}", id.ToString().Substring(0, 5), _items[id].ToString());
                buffer.Append(Environment.NewLine);
            }

            return buffer.ToString();            
        }

        public string ItemsDataToString()
        {
            StringBuilder buffer = new StringBuilder();
            foreach (SyncId id in _items.Keys)
            {
                //Print out the item's ID and the data associated
                if (_items[id].Item != null)
                {
                    buffer.AppendFormat("[{0}]: {1}", id.ToString().Substring(0, 5), _items[id].Item.ToString());
                    buffer.Append(Environment.NewLine);
                }
            }

            if (_items.Count == 0)
            {
                buffer.Append("Empty Store.");
                buffer.Append(Environment.NewLine);
            }

            return buffer.ToString();
        }

        private SortedDictionary<SyncId, SyncableItem<T>> _items;
        private IDataItemOperations<T> _dalOperations;
        private ulong _tickCount = 1;
    }
}