Ejemplo n.º 1
0
        public static V Get <K, V>(this StorageMap map, K key)
        {
            if (map.ContainsKey(key))
            {
                var bytes = map.Context.Get(ElementKey(map.BaseKey, key));

                if (typeof(IStorageCollection).IsAssignableFrom(typeof(V)))
                {
                    var args = new object[] { bytes, map.Context };
                    var obj  = (V)Activator.CreateInstance(typeof(V), args);
                    return(obj);
                }
                else
                {
                    return(Serialization.Unserialize <V>(bytes));
                }
            }

            if (typeof(IStorageCollection).IsAssignableFrom(typeof(V)))
            {
                var baseKey = MergeKey(map.BaseKey, key);
                var args    = new object[] { baseKey, map.Context };
                var obj     = (V)Activator.CreateInstance(typeof(V), args);
                return(obj);
            }

            return(default(V));
        }
Ejemplo n.º 2
0
        public static void Migrate <K, V>(this StorageMap map, K sourceKey, K destKey)
        {
            Throw.If(sourceKey.Equals(destKey), "map migrate: source and dest keys must be different");

            if (typeof(V) == typeof(StorageList))
            {
                var oldList = map.Get <K, StorageList>(sourceKey);
                var newList = map.Get <K, StorageList>(destKey);

                Throw.If(newList.Count() != 0, "target list already exists");

                var count = oldList.Count();
                for (int i = 0; i < count; i++)
                {
                    var bytes = oldList.GetRaw(i);
                    newList.AddRaw(bytes);
                }
                oldList.Clear();
            }
            else
            {
                // TODO this branch could be optimized with the Raw versions of Contains/Get/Set

                if (!map.ContainsKey <K>(sourceKey))
                {
                    return; // if they key does not exist, migration does nothing
                }

                V bytes = map.Get <K, V>(sourceKey);
                map.Set <K, V>(destKey, bytes);
                map.Remove <K>(sourceKey);
            }
        }
Ejemplo n.º 3
0
 public static void Remove <K>(this StorageMap map, K key)
 {
     if (map.ContainsKey(key))
     {
         map.Context.Delete(ElementKey(map.BaseKey, key));
         var size = map.Count() - 1;
         map.Context.Put(CountKey(map.BaseKey), size);
     }
 }
Ejemplo n.º 4
0
        public static byte[] GetRaw(this StorageMap map, byte[] key)
        {
            if (map.ContainsKey(key))
            {
                var bytes = map.Context.Get(ElementKey(map.BaseKey, key));
                return(bytes);
            }

            return(null);
        }
Ejemplo n.º 5
0
        public static void SetRaw(this StorageMap map, byte[] key, byte[] bytes)
        {
            bool exists = map.ContainsKey(key);

            map.Context.Put(ElementKey(map.BaseKey, key), bytes);

            if (!exists)
            {
                var size = map.Count() + 1;
                map.Context.Put(CountKey(map.BaseKey), size);
            }
        }
Ejemplo n.º 6
0
        public static void Set <K, V>(this StorageMap map, K key, V value)
        {
            bool exists = map.ContainsKey(key);

            byte[] bytes;
            if (typeof(IStorageCollection).IsAssignableFrom(typeof(V)))
            {
                var collection = (IStorageCollection)value;
                //bytes = MergeKey(map.BaseKey, key);
                bytes = collection.BaseKey;
            }
            else
            {
                bytes = Serialization.Serialize(value);
            }
            map.Context.Put(ElementKey(map.BaseKey, key), bytes);

            if (!exists)
            {
                var size = map.Count() + 1;
                map.Context.Put(CountKey(map.BaseKey), size);
            }
        }