Ejemplo n.º 1
0
        /// <summary>
        ///     Initializes fields and properties of the object o, with
        ///     data of the specified category and of the specified id.
        /// </summary>
        /// <param name="o">Object to get its fields initialized.</param>
        /// <param name="type">
        ///     The category this object belongs, all objects
        ///     of the same category will get the same values.
        /// </param>
        /// <param name="id">
        ///     The id of the object which identifies it inside
        ///     its category, it
        /// </param>
        public void InitializeObject(IPersistent o)
        {
            var t   = o.GetType();
            var ids = o.Ids.Split(CommaDelimiter, StringSplitOptions.RemoveEmptyEntries);

            // Hey, lock here, there might be other threads
            // trying to initialize their values also.
            lock (_data) {
                foreach (var id in ids)
                {
                    if (!_data.ContainsKey(id)) // Do we have something for this id?
                    {
                        continue;
                    }

                    // Iterate over what we have for this id
                    foreach (var pair in _data[id])
                    {
                        // Does o has a member we can write to?
                        var members = t.GetMember(pair.Key);
                        foreach (var info in members)
                        {
                            if (info is FieldInfo)
                            {
                                TrySetValue(o, (FieldInfo)info, _data[id][info.Name]);
                            }
                            else if (info is PropertyInfo)
                            {
                                TrySetValue(o, (PropertyInfo)info, _data[id][info.Name]);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Each subscriber can call this method to include any data it wishes to persist.  This data is intended to be user state metadata. It is not intended to be used for
        /// wholesale application database data.
        /// </summary>
        /// <param name="model">
        /// The model to persist.  The type of this model will be used as a key and when data is loaded back from persistent storage this key (type) will be used
        /// to retrieve it.  No other component should use this type, or collisions will occur.
        /// </param>
        /// <exception cref="System.Data.DuplicateNameException">Attempt to save application state with a model that has already been saved.</exception>
        public void PersistThisModel(IPersistent model)
        {
            if (this.modelsToPersist.Any(m => m.GetType() == model.GetType()))
            {
                throw new DuplicateNameException(
                    "Attempt to save application state with a model that has already been saved.");
            }

            this.modelsToPersist.Add(model);
        }
Ejemplo n.º 3
0
 // Compare methods is used by Update difference
 private void CompareListItems(IDifferenceObject difference, IPersistent target, IPersistent source)
 {
     if (typeof(Namespace) == target.GetType())
     {
         CompareNamespaces(difference, (Namespace)target, (Namespace)source);
     }
     else if (typeof(Entity) == target.GetType())
     {
         CompareEntities(difference, (Entity)target, (Entity)source);
     }
     else if (typeof(Property) == target.GetType())
     {
         CompareProperties(difference, (Property)target, (Property)source);
     }
     else if (typeof(Table) == target.GetType())
     {
         CompareTables(difference, (Table)target, (Table)source);
     }
     else if (typeof(Field) == target.GetType())
     {
         CompareFields(difference, (Field)target, (Field)source);
     }
 }
Ejemplo n.º 4
0
 public virtual void LoadObject(IPersistent obj)
 {
     lock (this)
     {
         if (obj.IsRaw())
         {
             LoadStub(obj.Oid, obj, obj.GetType());
         }
     }
 }
Ejemplo n.º 5
0
 internal byte[] PackObject(IPersistent obj)
 {
     ByteBuffer buf = new ByteBuffer();
     int offs = ObjectHeader.Sizeof;
     buf.Extend(offs);
     ClassDescriptor desc = GetClassDescriptor(obj.GetType());
     if (obj is FastSerializable)
     {
         offs = ((FastSerializable) obj).Pack(buf, offs, encoding);
     }
     else
     {
         try
         {
             offs = PackObject(obj, desc, offs, buf, obj);
         }
         catch (System.Exception x)
         {
             throw new StorageError(StorageError.ACCESS_VIOLATION, x);
         }
     }
     ObjectHeader.SetSize(buf.arr, 0, offs);
     ObjectHeader.SetType(buf.arr, 0, desc.Oid);
     return buf.arr;
 }
Ejemplo n.º 6
0
 public virtual IPersistent[] ToPersistentArray(IPersistent[] arr)
 {
     if (arr.Length < nElems)
     {
         arr = (IPersistent[]) System.Array.CreateInstance(arr.GetType().GetElementType(), nElems);
     }
     if (root != null)
     {
         root.TraverseForward(height, arr, 0);
     }
     if (arr.Length > nElems)
     {
         arr[nElems] = null;
     }
     return arr;
 }
Ejemplo n.º 7
0
 public virtual IPersistent[] ToArray(IPersistent[] arr)
 {
     if (arr.Length < used)
     {
         arr = (IPersistent[]) System.Array.CreateInstance(arr.GetType().GetElementType(), used);
     }
     for (int i = used; --i >= 0; )
     {
         arr[i] = LoadElem(i);
     }
     if (arr.Length > used)
     {
         arr[used] = null;
     }
     return arr;
 }
Ejemplo n.º 8
0
 /// <summary> Get all objects in the index as array ordered by index key.
 /// The runtime type of the returned array is that of the specified array.
 /// If the index fits in the specified array, it is returned therein.
 /// Otherwise, a new array is allocated with the runtime type of the
 /// specified array and the size of this index.<p>
 ///
 /// If this index fits in the specified array with room to spare
 /// (i.e., the array has more elements than this index), the element
 /// in the array immediately following the end of the index is set to
 /// <tt>null</tt>. This is useful in determining the length of this
 /// index <i>only</i> if the caller knows that this index does
 /// not contain any <tt>null</tt> elements.)<p>
 /// </summary>
 /// <returns> array of objects in the index ordered by key value
 /// </returns>
 public virtual IPersistent[] ToPersistentArray(IPersistent[] arr)
 {
     if (arr.Length < nMembers)
     {
         arr = (IPersistent[]) System.Array.CreateInstance(arr.GetType().GetElementType(), nMembers);
     }
     if (root != null)
     {
         root.ToArray(arr, 0);
     }
     if (arr.Length > nMembers)
     {
         arr[nMembers] = null;
     }
     return arr;
 }