//
 // Constructors
 //
 public PersistentObjects(string path)
 {
     this.objectDictionary     = new Dictionary <Guid, Object> ();
     this.persistentDictionary = new PersistentDictionary(path);
     try
     {
         Guid[] keys = this.persistentDictionary.Keys;
         for (int i = 0; i < keys.Length; i++)
         {
             Guid   guid   = keys [i];
             byte[] source = this.persistentDictionary.Get(guid);
             Guid   guid2  = new Guid(source.Take(16).ToArray <byte> ());
             if (
                 !(Serialization.GetTypeByGuid(guid2) == typeof(Blob)))
             {
                 byte[] content = source.Skip(16).ToArray <byte> ();
                 Object @object = (Object)Serialization.Deserialize(guid2, content);
                 @object.Key = guid;
                 this.objectDictionary.Add(guid, @object);
             }
         }
     }
     catch
     {
         this.persistentDictionary.Dispose();
         this.objectDictionary     = null;
         this.persistentDictionary = null;
         throw;
     }
 }
Ejemplo n.º 2
0
        public void InsertObject(Manager.Model.Object obj)
        {
            Guid guid = obj.Key;

            obj.Key = Guid.Empty;

            /*
             * Tuple<Guid, Byte []> tuple;
             * using (MemoryStream memoryStream = new MemoryStream ()) {
             *  tuple=new Tuple<Guid, byte []> (obj.GetType ().GetCustomAttribute<GuidAttribute> ().Value, memoryStream.ToArray ());
             * }
             */

            byte [] memoryStreamBytes;
            using (MemoryStream memoryStream = new MemoryStream()) {
                Serializer.NonGeneric.Serialize(memoryStream, obj);
                memoryStreamBytes = memoryStream.ToArray();
            }


            //Tuple<Guid,Byte[]> tuple=Manager.Serialization.Serialize(obj);
            IDbCommand dbcmd = CreateCommand();

            dbcmd.CommandText = String.Format("insert into Objects (Key,ContentType,Content) values(@key,@type,@BIN)");

            IDbDataParameter param = dbcmd.CreateParameter();

            param.DbType        = DbType.AnsiString;
            param.ParameterName = "@key";
            param.Value         = guid.ToString();
            dbcmd.Parameters.Add(param);
            param               = dbcmd.CreateParameter();
            param.DbType        = DbType.AnsiString;
            param.ParameterName = "@type";
            //param.Value = Manager.Serialization.GetGuidByType(obj.GetType()).ToString();
            param.Value = Manager.Model.Object.GetGuidByType(obj.GetType()).ToString();
            dbcmd.Parameters.Add(param);
            param               = dbcmd.CreateParameter();
            param.DbType        = DbType.Binary;
            param.ParameterName = "@BIN";
            //param.Value = tuple.Item2;
            param.Value = memoryStreamBytes;
            dbcmd.Parameters.Add(param);

            //dbcmd.Parameters.Add("@BIN", SqlDbType.Binary, tuple.Item2.Length).Value = tuple.Item2;

            dbcmd.ExecuteNonQuery();
            dbcmd.Dispose();
        }
        //
        // Methods
        //
        public Guid Put(Object o)
        {
            Tuple <Guid, byte[]> tuple        = Serialization.Serialize(o);
            MemoryStream         memoryStream = new MemoryStream();
            BinaryWriter         binaryWriter = new BinaryWriter(memoryStream);

            binaryWriter.Write(tuple.Item1.ToByteArray());
            binaryWriter.Write(tuple.Item2);
            binaryWriter.Flush();
            this.persistentDictionary.Put(o.Key, memoryStream.ToArray());
            if (this.objectDictionary.ContainsKey(o.Key))
            {
                this.objectDictionary[o.Key] = o;
            }
            else
            {
                this.objectDictionary.Add(o.Key, o);
            }
            return(o.Key);
        }