Possible to store objects that are not strings, url's, etc

Nov 2, 2012 at 3:00 AM
Edited Nov 2, 2012 at 3:00 AM

Can I store something like this

// Class
public class myObject
{
   public string myObjectMemeber { get; set;}
} 

PersistentDictionary<string, BO> a = new PersistentDictionary<string, BO>(Directory.GetCurrentDirectory() + "temp");            
a.Add("a", new myObject() {myObjectMemeber  = "mem" }); 

This results in "Not supported for SetColumn" which in result means that object cannot be serialized I tried applying [Serializable] attribute to the class or inherit from ISerializable and still got the same result 

Nov 2, 2012 at 3:29 PM

I've created "DictionaryDemo" project for my Esent Serialization library, that does what you want.

Take a look: http://esentserialize.codeplex.com

Nov 2, 2012 at 4:56 PM

If you go into ColumnConverter.cs and remove the "if (!(type.IsValueType && type.IsSerializable))" line from the IsSerializable method then you will be able to add serializable classes to the dictionary. Remember that if you add an object to the dictionary and then alter it, the version that was stored in the dictionary won't be updated.

Nov 3, 2012 at 1:57 PM

@laurionb - If I remove the following line "if (!(type.IsValueType && type.IsSerializable))" I will get the following error 

System.TypeLoadException was unhandled
  HResult=-2146233054
  Message=Could not load type 'ConsoleApplication2.bo' from assembly 'ConsoleApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
  Source=mscorlib
  TypeName=ConsoleApplication2.bo
  StackTrace:
       at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
       at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
       at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
       at System.Runtime.Serialization.ObjectManager.ResolveObjectReference(ObjectHolder holder)
       at System.Runtime.Serialization.ObjectManager.DoFixups()
       at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
       at Microsoft.Isam.Esent.Interop.Api.DeserializeObjectFromColumn(JET_SESID sesid, JET_TABLEID tableid, JET_COLUMNID columnid, RetrieveColumnGrbit grbit) in C:\ManagedEsent\EsentInterop\RetrieveColumnHelpers.cs:line 759
       at Microsoft.Isam.Esent.Interop.Api.DeserializeObjectFromColumn(JET_SESID sesid, JET_TABLEID tableid, JET_COLUMNID columnid) in C:\ManagedEsent\EsentInterop\RetrieveColumnHelpers.cs:line 737
       at Microsoft.Isam.Esent.Collections.Generic.PersistentDictionary`2.CheckDatabaseMetaData(String database) in c:\Users\Ecco\Downloads\managedesent-79962\EsentCollections\PersistentDictionary.cs:line 760
       at Microsoft.Isam.Esent.Collections.Generic.PersistentDictionary`2..ctor(String directory) in c:\Users\Ecco\Downloads\managedesent-79962\EsentCollections\PersistentDictionary.cs:line 154
       at ConsoleApplication2.Program.Main(String[] args) in c:\Users\Ecco\Documents\Visual Studio 2012\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 59
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

on line 154 in PersistentDictionary.cs right in the constructor

 

 public PersistentDictionary(string directory)
        {
            if (null == directory)
            {
                throw new ArgumentNullException("directory");
            }

            CollectionsSystemParameters.Init();
            this.converters = new PersistentDictionaryConverters();
            this.config = new PersistentDictionaryConfig();
            this.databaseDirectory = directory;
            this.databasePath = Path.Combine(directory, this.config.Database);

            this.updateLocks = new object[NumUpdateLocks];
            for (int i = 0; i < this.updateLocks.Length; ++i)
            {
                this.updateLocks[i] = new object();
            }

            this.instance = new Instance(Guid.NewGuid().ToString());            
            this.instance.Parameters.SystemDirectory = directory;
            this.instance.Parameters.LogFileDirectory = directory;
            this.instance.Parameters.TempDirectory = directory;

            // If the database has been moved while inconsistent recovery
            // won't be able to find the database (logfiles contain the
            // absolute path of the referenced database). Set this parameter
            // to indicate a directory which contains any databases that couldn't
            // be found by recovery.
            this.instance.Parameters.AlternateDatabaseRecoveryDirectory = directory;

            this.instance.Parameters.CreatePathIfNotExist = true;
            this.instance.Parameters.BaseName = this.config.BaseName;
            this.instance.Parameters.EnableIndexChecking = false;       // TODO: fix unicode indexes
            this.instance.Parameters.CircularLog = true;
            this.instance.Parameters.CheckpointDepthMax = 64 * 1024 * 1024;
            this.instance.Parameters.LogFileSize = 1024;    // 1MB logs
            this.instance.Parameters.LogBuffers = 1024;     // buffers = 1/2 of logfile
            this.instance.Parameters.MaxTemporaryTables = 0;
            this.instance.Parameters.MaxVerPages = 1024;
            this.instance.Parameters.NoInformationEvent = true;
            this.instance.Parameters.WaypointLatency = 1;
            this.instance.Parameters.MaxSessions = 256;
            this.instance.Parameters.MaxOpenTables = 256;

            InitGrbit grbit = EsentVersion.SupportsWindows7Features
                                  ? Windows7Grbits.ReplayIgnoreLostLogs
                                  : InitGrbit.None;
            this.instance.Init(grbit);

            try
            {
                if (!File.Exists(this.databasePath))
                {
                    this.CreateDatabase(this.databasePath);
                }
                else
                {
                    this.CheckDatabaseMetaData(this.databasePath);
                }

                this.cursors = new PersistentDictionaryCursorCache(
                    this.instance, this.databasePath, this.converters, this.config);
            }
            catch (Exception)
            {
                // We have failed to initialize for some reason. Terminate
                // the instance.
                this.instance.Term();                
                throw;
            }
        }

 

 

How come it won't be updated? That means that every time I alter value I have to remove it and re-insert it?

Dec 9, 2015 at 11:09 PM
How come it won't be updated? That means that every time I alter value I have to remove it and re-insert it?
The data that is stored is a serialized copy. ie it's actually written to the disk. By modifying an object in memory, you are modifying references, you are not modifying the disk. Updating the dictionary would overwrite the serialized form with the current data.