A class that encapsulates a JET_SESID in a disposable object.
Inheritance: Microsoft.Isam.Esent.Interop.EsentResource
Exemplo n.º 1
0
 public void TestSessionToString()
 {
     using (var session = new Session(this.instance))
     {
         StringAssert.StartsWith(session.ToString(), "Session (");
     }
 }
Exemplo n.º 2
0
        public void Setup()
        {
            Microsoft.Isam.Esent.Interop.JET_TABLEID tableid;

            this.directory = SetupHelper.CreateRandomDirectory();
            this.database = Path.Combine(this.directory, "database.edb");
            this.tablename = "table";
            this.instance = SetupHelper.CreateNewInstance(this.directory);

            // turn off logging so initialization is faster
            Microsoft.Isam.Esent.Interop.Api.JetSetSystemParameter(this.instance, Microsoft.Isam.Esent.Interop.JET_SESID.Nil, Microsoft.Isam.Esent.Interop.JET_param.Recovery, 0, "off");
            Microsoft.Isam.Esent.Interop.Api.JetSetSystemParameter(this.instance, Microsoft.Isam.Esent.Interop.JET_SESID.Nil, Microsoft.Isam.Esent.Interop.JET_param.MaxTemporaryTables, 0, null);
            Microsoft.Isam.Esent.Interop.Api.JetInit(ref this.instance);
            this.session = new Session(this.instance);
            Microsoft.Isam.Esent.Interop.Api.JetCreateDatabase(this.session, this.database, String.Empty, out this.dbid, Microsoft.Isam.Esent.Interop.CreateDatabaseGrbit.None);
            Microsoft.Isam.Esent.Interop.Api.JetBeginTransaction(this.session);
            Microsoft.Isam.Esent.Interop.Api.JetCreateTable(this.session, this.dbid, this.tablename, 0, 100, out tableid);

            var columndef = new Microsoft.Isam.Esent.Interop.JET_COLUMNDEF() { coltyp = Microsoft.Isam.Esent.Interop.JET_coltyp.Long };
            Microsoft.Isam.Esent.Interop.Api.JetAddColumn(this.session, tableid, "Long", columndef, null, 0, out this.columnid);

            string indexDef = "+long\0\0";
            Microsoft.Isam.Esent.Interop.Api.JetCreateIndex(this.session, tableid, "primary", Microsoft.Isam.Esent.Interop.CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length, 100);

            Microsoft.Isam.Esent.Interop.Api.JetCloseTable(this.session, tableid);
            Microsoft.Isam.Esent.Interop.Api.JetCommitTransaction(this.session, Microsoft.Isam.Esent.Interop.CommitTransactionGrbit.LazyFlush);

            Microsoft.Isam.Esent.Interop.Api.JetOpenTable(this.session, this.dbid, this.tablename, null, 0, Microsoft.Isam.Esent.Interop.OpenTableGrbit.None, out this.tableid);
        }
Exemplo n.º 3
0
        internal static void CreateTable(Session session, JET_DBID dbid)
        {
            JET_TABLEID tableid;
            Api.JetCreateTable(session, dbid, ifcHeaderTableName, 1, 100, out tableid);

            using (var transaction = new Microsoft.Isam.Esent.Interop.Transaction(session))
            {
                JET_COLUMNID columnid;
                
                var columndef = new JET_COLUMNDEF
                {
                    coltyp = JET_coltyp.Long,
                    grbit = ColumndefGrbit.ColumnAutoincrement
                };
                Api.JetAddColumn(session, tableid, _colNameHeaderId, columndef, null, 0, out columnid);
                columndef.coltyp = JET_coltyp.Currency;
                columndef.grbit = ColumndefGrbit.ColumnNotNULL;
                Api.JetAddColumn(session, tableid, _colNameEntityCount, columndef, null, 0, out columnid);
                
                columndef.coltyp = JET_coltyp.LongBinary;
            
                columndef.grbit = ColumndefGrbit.ColumnNotNULL;
                Api.JetAddColumn(session, tableid, _colNameHeaderData, columndef, null, 0, out columnid);
                columndef.coltyp = JET_coltyp.Text;
                columndef.grbit = ColumndefGrbit.ColumnNotNULL;
                columndef.cbMax = 32;

                Api.JetAddColumn(session, tableid, _colNameFileVersion, columndef, null, 0, out columnid);
                transaction.Commit(CommitTransactionGrbit.LazyFlush);
            }
        }
Exemplo n.º 4
0
 public static void WithDatabase(this JET_INSTANCE instance, string database, Func<Session, JET_DBID, Transaction, Transaction> action)
 {
     using (var session = new Session(instance))
     {
         var tx = new Transaction(session);
         try
         {
             JET_DBID dbid;
             Api.JetOpenDatabase(session, database, "", out dbid, OpenDatabaseGrbit.None);
             try
             {
                 tx = action(session, dbid, tx);
             }
             finally
             {
                 Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
             }
             tx.Commit(CommitTransactionGrbit.None);
         }
         finally
         {
             if(tx != null)
                 tx.Dispose();
         }
     }
 }
Exemplo n.º 5
0
		public StorageActionsAccessor(TableColumnsCache tableColumnsCache, JET_INSTANCE instance, string databaseName, UuidGenerator uuidGenerator, OrderedPartCollection<AbstractFileCodec> fileCodecs)
		{
			this.tableColumnsCache = tableColumnsCache;
			this.uuidGenerator = uuidGenerator;
			this.fileCodecs = fileCodecs;
			try
			{
				session = new Session(instance);
				transaction = new Transaction(session);
				Api.JetOpenDatabase(session, databaseName, null, out database, OpenDatabaseGrbit.None);
			}
			catch (Exception original)
			{
				log.WarnException("Could not create accessor", original);
				try
				{
					Dispose();
				}
				catch (Exception e)
				{
					log.WarnException("Could not properly dispose accessor after exception in ctor.", e);
				}
				throw;
			}
		}
Exemplo n.º 6
0
		public void Update(Session session, JET_DBID dbid, Action<string> output)
        {
            using (var table = new Table(session, dbid, "indexes_stats", OpenTableGrbit.None))
            {
                byte[] defaultValue = BitConverter.GetBytes(1);
                JET_COLUMNID columnid;
                Api.JetAddColumn(session, table, "priority", new JET_COLUMNDEF
                {
                    coltyp = JET_coltyp.Long,
                    grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
                }, defaultValue, defaultValue.Length, out columnid);

				defaultValue = BitConverter.GetBytes(0);
				Api.JetAddColumn(session, table, "created_timestamp", new JET_COLUMNDEF
                {
                    cbMax = 8, //64 bits
                    coltyp = JET_coltyp.Binary,
                    grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
				}, defaultValue, defaultValue.Length, out columnid);

				Api.JetAddColumn(session, table, "last_indexing_time", new JET_COLUMNDEF
				{
					cbMax = 8, //64 bits
					coltyp = JET_coltyp.Binary,
					grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
				}, defaultValue, defaultValue.Length, out columnid);
            }

            SchemaCreator.UpdateVersion(session,   dbid, "4.6");
        }
Exemplo n.º 7
0
 public void CreateAndEndSession()
 {
     using (var session = new Session(this.instance))
     {
         session.End();
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Create a new database and return a connection to
        /// the database. The database will be overwritten if
        /// it already exists.
        /// </summary>
        /// <param name="database">The path to the database.</param>
        /// <param name="mode">Creation mode for the database.</param>
        /// <returns>A new connection to the database.</returns>
        public virtual Connection CreateDatabase(string database, DatabaseCreationMode mode)
        {
            database = Path.GetFullPath(database);

            lock (this.lockObject)
            {
                this.SetGlobalParameters();
                this.Tracer.TraceInfo("create database '{0}'", database);

                // Create the database then open it
                using (var instance = new Instance(this.GetNewInstanceName()))
                {
                    SetParametersAndInitializeInstance(database, instance);

                    using (var session = new Session(instance))
                    {
                        CreateDatabaseGrbit grbit = (DatabaseCreationMode.OverwriteExisting == mode) ?
                            CreateDatabaseGrbit.OverwriteExisting : CreateDatabaseGrbit.None;

                        JET_DBID dbid;
                        Api.JetCreateDatabase(session, database, String.Empty, out dbid, grbit);
                        Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
                        Api.JetDetachDatabase(session, database);
                    }
                }

                return this.AttachDatabase(database, DatabaseOpenMode.ReadWrite);
            }
        }
Exemplo n.º 9
0
		public static void DumpTable(Session session, Table table, Stream stream)
		{
			using (var writer = new StreamWriter(stream))
			{
				var cols = Api.GetTableColumns(session, table).ToArray();
				foreach (var col in cols)
				{
					writer.Write(col);
					writer.Write(",");
				}
				writer.WriteLine("#");
				Api.MoveBeforeFirst(session, table);
				int count = 0;
				while (Api.TryMoveNext(session, table))
				{
					foreach (var col in cols)
					{
						var val = GetvalueFromTable(session, table, col) ?? "NULL";
						writer.Write(val);
						writer.Write(",");
					}
					writer.WriteLine(++count);
				}
				writer.Flush();
			}
		}
Exemplo n.º 10
0
 public EsentSession(IEsentDatabase database, Session session, IWriteLockable writeLockable)
 {
     Database = database;
     _session = session;
     _writeLockable = writeLockable;
     JetDbid = JET_DBID.Nil;
 }
Exemplo n.º 11
0
		public void InitColumDictionaries(JET_INSTANCE instance, string database)
	    {
	        using (var session = new Session(instance))
	        {
	            var dbid = JET_DBID.Nil;
	            try
	            {
	                Api.JetOpenDatabase(session, database, null, out dbid, OpenDatabaseGrbit.None);
	                using (var usage = new Table(session, dbid, "usage", OpenTableGrbit.None))
						UsageColumns = Api.GetColumnDictionary(session, usage);
					using (var details = new Table(session, dbid, "details", OpenTableGrbit.None))
						DetailsColumns = Api.GetColumnDictionary(session, details);
					using (var pages = new Table(session, dbid, "pages", OpenTableGrbit.None))
						PagesColumns = Api.GetColumnDictionary(session, pages);
	                using (var files = new Table(session, dbid, "files", OpenTableGrbit.None))
	                    FilesColumns = Api.GetColumnDictionary(session, files);
					using (var signatures = new Table(session, dbid, "signatures", OpenTableGrbit.None))
						SignaturesColumns = Api.GetColumnDictionary(session, signatures);
					using (var config = new Table(session, dbid, "config", OpenTableGrbit.None))
						ConfigColumns = Api.GetColumnDictionary(session, config);
	            }
	            finally
	            {
	                if (Equals(dbid, JET_DBID.Nil) == false)
	                    Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
	            }
	        }
	    }
Exemplo n.º 12
0
		public void Update(Session session, JET_DBID dbid)
		{
			using (var tx = new Transaction(session))
			{
				using (var files = new Table(session, dbid, "files", OpenTableGrbit.None))
				{

					const string indexDef = "+etag\0\0";
					Api.JetCreateIndex(session, files, "by_etag", CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
									   100);

					using (var details = new Table(session, dbid, "details", OpenTableGrbit.None))
					{
						Api.JetMove(session, details, JET_Move.First, MoveGrbit.None);
						var columnids = Api.GetColumnDictionary(session, details);

						using (var update = new Update(session, details, JET_prep.Replace))
						{
							Api.SetColumn(session, details, columnids["schema_version"], "2.7", Encoding.Unicode);

							update.Save();
						}
					}
				}
				tx.Commit(CommitTransactionGrbit.None);
			}
		}
Exemplo n.º 13
0
        public void Update(Session session, JET_DBID dbid, Action<string> output)
        {
            using (var tbl = new Table(session, dbid, "tasks", OpenTableGrbit.None))
            {
                int rows = 0;
                if (Api.TryMoveFirst(session, tbl))
                {
                    var taskTypeColumnId = Api.GetTableColumnid(session, tbl, "task_type");
                    do
                    {
                        using (var update = new Update(session, tbl, JET_prep.Replace))
                        {
                            var taskType = Api.RetrieveColumnAsString(session, tbl, taskTypeColumnId, Encoding.Unicode);
                            Api.SetColumn(session, tbl, taskTypeColumnId, taskType, Encoding.ASCII);
                            update.Save();
                        }

                        if (rows++ % 10000 == 0)
                        {
                            output("Processed " + (rows) + " rows in tasks");
                            Api.JetCommitTransaction(session, CommitTransactionGrbit.LazyFlush);
                            Api.JetBeginTransaction2(session, BeginTransactionGrbit.None);
                        }
                    } while (Api.TryMoveNext(session, tbl));
                }

                SchemaCreator.UpdateVersion(session, dbid, "5.3");
            }
        }
Exemplo n.º 14
0
		public void Update(Session session, JET_DBID dbid, Action<string> output)
		{
			using (var tbl = new Table(session, dbid, "lists", OpenTableGrbit.None))
			{
				JET_COLUMNID columnid;
				Api.JetAddColumn(session, tbl, "created_at", new JET_COLUMNDEF
				{
					coltyp = JET_coltyp.DateTime,
					grbit = ColumndefGrbit.ColumnMaybeNull,
				}, null, 0, out columnid);

				if (Api.TryMoveFirst(session, tbl))
				{
					do
					{
						using (var update = new Update(session, tbl, JET_prep.Replace))
						{
							var createdAt = Api.GetTableColumnid(session, tbl, "created_at");
							Api.SetColumn(session, tbl, createdAt, SystemTime.UtcNow);
							update.Save();
						}
					} while (Api.TryMoveNext(session, tbl));
				}

				SchemaCreator.CreateIndexes(session, tbl, new JET_INDEXCREATE
				{
					szIndexName = "by_name_and_created_at",
					szKey = "+name\0+created_at\0\0",
					grbit = CreateIndexGrbit.IndexDisallowNull
				});

				SchemaCreator.UpdateVersion(session, dbid, "5.1");
			}
		}
Exemplo n.º 15
0
        public static void OpenDatabase(string jetDatabase, Instance jetInstance, bool readOnly)
        {
            using (var jetSession = new Session(jetInstance))
            {
                var attachGrbit = AttachDatabaseGrbit.None;
                if (readOnly)
                    attachGrbit |= AttachDatabaseGrbit.ReadOnly;
                if (EsentVersion.SupportsWindows7Features)
                    attachGrbit |= Windows7Grbits.EnableAttachDbBackgroundMaintenance;

                Api.JetAttachDatabase(jetSession, jetDatabase, attachGrbit);
                var success = false;
                try
                {
                    using (var cursor = new EsentChainStateCursor(jetDatabase, jetInstance))
                    {
                        // reset flush column
                        using (var jetUpdate = cursor.jetSession.BeginUpdate(cursor.flushTableId, JET_prep.Replace))
                        {
                            Api.SetColumn(cursor.jetSession, cursor.flushTableId, cursor.flushColumnId, 0);

                            jetUpdate.Save();
                        }
                    }

                    success = true;
                }
                finally
                {
                    if (!success)
                        Api.JetDetachDatabase(jetSession, jetDatabase);
                }
            }
        }
Exemplo n.º 16
0
        protected override void DoCreateTable(Session session, JET_TABLEID tableid)
        {
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "FileId", new JET_COLUMNDEF()
            {
                coltyp = JET_coltyp.Text,
                cp = JET_CP.Unicode,
                cbMax = 255,
                grbit = ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "Size", new JET_COLUMNDEF()
            {
                coltyp = Windows10Coltyp.UnsignedLongLong,
                grbit = ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "DTicks", new JET_COLUMNDEF()
            {
                coltyp = VistaColtyp.LongLong,
                grbit = ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "OTicks", new JET_COLUMNDEF()
            {
                coltyp = VistaColtyp.LongLong,
                grbit = ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            var indexDef = "+FileId\0\0";
            Api.JetCreateIndex(session, tableid, PrimaryIndexName, CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length, 100);
        }
        private static void CreateDatabase()
        {
            using (var databaseInstance = new Instance(DatabasePath))
            {
                InitializeDatabaseInstance(databaseInstance);

                if (File.Exists(DatabasePath))
                {
                    return;
                }

                using (var session = new Session(databaseInstance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, DatabasePath, null, out dbid, CreateDatabaseGrbit.OverwriteExisting);
                    using (var transaction = new Transaction(session))
                    {
                        JET_TABLEID tableid;
                        Api.JetCreateTable(session, dbid, "Message", 0, 100, out tableid);

                        CreateIdColumn(session, tableid);
                        CreateDateCreatedColumn(session, tableid);
                        CreateIndexes(session, tableid);

                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }

                    Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
                    Api.JetDetachDatabase(session, DatabasePath);
                }
            }
        }
Exemplo n.º 18
0
        protected AbstractActions(JET_INSTANCE instance, ColumnsInformation columnsInformation, string database, Guid instanceId)
        {
            logger = LogManager.GetLogger(GetType());
            try
            {
                this.instanceId = instanceId;
                ColumnsInformation = columnsInformation;
                session = new Session(instance);

                transaction = new Transaction(session);
                Api.JetOpenDatabase(session, database, null, out dbid, OpenDatabaseGrbit.None);

                queues = new EsentTable(session, new Table(session, dbid, "queues", OpenTableGrbit.None));
                subqueues = new EsentTable(session, new Table(session, dbid, "subqueues", OpenTableGrbit.None));
                txs = new EsentTable(session, new Table(session, dbid, "transactions", OpenTableGrbit.None));
                recovery = new EsentTable(session, new Table(session, dbid, "recovery", OpenTableGrbit.None));
                outgoing = new EsentTable(session, new Table(session, dbid, "outgoing", OpenTableGrbit.None));
                outgoingHistory = new EsentTable(session, new Table(session, dbid, "outgoing_history", OpenTableGrbit.None));
                recveivedMsgs = new EsentTable(session, new Table(session, dbid, "recveived_msgs", OpenTableGrbit.None));
            }
            catch (Exception)
            {
                Dispose();
                throw;
            }
        }
Exemplo n.º 19
0
        private static void CreateDocumentsTable(Session session, JET_TABLEID tableid)
        {
            JET_COLUMNID columnid;

            var guidColumn = new JET_COLUMNDEF
                                 {
                                     cbMax = 255,
                                     coltyp = JET_coltyp.Text,
                                     cp = JET_CP.Unicode,
                                     grbit = ColumndefGrbit.ColumnTagged
                                 };
            Api.JetAddColumn(session, tableid, "id", guidColumn, null, 0, out columnid);

            var collectionColumn = new JET_COLUMNDEF
                                       {
                                           cbMax = 1000,
                                           coltyp = JET_coltyp.Text,
                                           cp = JET_CP.Unicode,
                                           grbit = ColumndefGrbit.ColumnTagged
                                       };
            Api.JetAddColumn(session, tableid, "collection_name", collectionColumn, null, 0, out columnid);

            const string colectionIndexDef = "+collection_name\0\0";
            Api.JetCreateIndex(session, tableid, "by_collection_name", CreateIndexGrbit.None, colectionIndexDef, colectionIndexDef.Length, 100);

            var textColumn = new JET_COLUMNDEF
                                 {
                                     coltyp = JET_coltyp.LongText,
                                     grbit = ColumndefGrbit.ColumnTagged
                                 };
            Api.JetAddColumn(session, tableid, "data", textColumn, null, 0, out columnid);

            const string idIndexDef = "+id\0\0";
            Api.JetCreateIndex(session, tableid, "by_id", CreateIndexGrbit.IndexPrimary, idIndexDef, idIndexDef.Length, 100);
        }
Exemplo n.º 20
0
        public void CreateBasicTableColumnIndex3OnXp()
        {
            var tablecreate = new JET_TABLECREATE { szTableName = "table" };

            string directory = SetupHelper.CreateRandomDirectory();
            string database = Path.Combine(directory, "test.db");

            using (var instance = new Instance("XpCreateBasicTableColumnIndex3"))
            {
                instance.Parameters.Recovery = false;
                instance.Parameters.NoInformationEvent = true;
                instance.Parameters.MaxTemporaryTables = 0;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    using (var transaction = new Transaction(session))
                    {
                        Api.JetCreateTableColumnIndex3(session, dbid, tablecreate);
                        Assert.AreNotEqual(JET_TABLEID.Nil, tablecreate.tableid);
                        Assert.AreEqual(tablecreate.cCreated, 1);
                        Api.JetCloseTable(session, tablecreate.tableid);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }
                }
            }
        }
Exemplo n.º 21
0
        private static void CreateHeadersTable(JET_DBID utxoDbId, Session jetSession)
        {
            JET_TABLEID headersTableId;
            JET_COLUMNID blockHashColumnId;
            JET_COLUMNID headerBytesColumnId;

            Api.JetCreateTable(jetSession, utxoDbId, "Headers", 0, 0, out headersTableId);
            Api.JetAddColumn(jetSession, headersTableId, "BlockHash", new JET_COLUMNDEF { coltyp = JET_coltyp.Binary, cbMax = 32, grbit = ColumndefGrbit.ColumnNotNULL }, null, 0, out blockHashColumnId);
            Api.JetAddColumn(jetSession, headersTableId, "HeaderBytes", new JET_COLUMNDEF { coltyp = JET_coltyp.Binary, grbit = ColumndefGrbit.ColumnNotNULL }, null, 0, out headerBytesColumnId);

            Api.JetCreateIndex2(jetSession, headersTableId,
                new JET_INDEXCREATE[]
                    {
                        new JET_INDEXCREATE
                        {
                            cbKeyMost = 255,
                            grbit = CreateIndexGrbit.IndexUnique | CreateIndexGrbit.IndexDisallowNull,
                            szIndexName = "IX_BlockHash",
                            szKey = "+BlockHash\0\0",
                            cbKey = "+BlockHash\0\0".Length
                        }
                    }, 1);

            Api.JetCloseTable(jetSession, headersTableId);
        }
Exemplo n.º 22
0
 private static object GetvalueFromTable(Session session, Table table, ColumnInfo col)
 {
     switch (col.Coltyp)
     {
         case JET_coltyp.Long:
             return Api.RetrieveColumnAsInt32(session, table, col.Columnid);
         case JET_coltyp.DateTime:
             return Api.RetrieveColumnAsDateTime(session, table, col.Columnid);
         case JET_coltyp.Binary:
             var bytes = Api.RetrieveColumn(session, table, col.Columnid);
             if (bytes == null)
                 return null;
             if (bytes.Length == 16)
                 return new Guid(bytes);
             return Convert.ToBase64String(bytes);
         case JET_coltyp.LongText:
         case JET_coltyp.Text:
             var str = Api.RetrieveColumnAsString(session, table, col.Columnid);
             if (str == null)
                 return null;
             if (str.Contains("\""))
                 return "\"" + str.Replace("\"", "\"\"") + "\"";
             return str;
         case JET_coltyp.LongBinary:
             return "long binary val";
         default:
             throw new ArgumentOutOfRangeException("don't know how to handle coltype: " + col.Coltyp);
     }
 }
Exemplo n.º 23
0
        public void Update(Session session, JET_DBID dbid)
        {
            using (var tx = new Transaction(session))
            {
                using (var tasks = new Table(session, dbid, "tasks", OpenTableGrbit.None))
                {
                    JET_COLUMNID columnid;
                    Api.JetAddColumn(session, tasks, "added_at",new JET_COLUMNDEF
                    {
                        coltyp = JET_coltyp.DateTime,
                        grbit = ColumndefGrbit.ColumnNotNULL
                    }, null, 0, out columnid);

                    using (var details = new Table(session, dbid, "details", OpenTableGrbit.None))
                    {
                        Api.JetMove(session, details, JET_Move.First, MoveGrbit.None);
                        var columnids = Api.GetColumnDictionary(session, details);

                        using (var update = new Update(session, details, JET_prep.Replace))
                        {
                            Api.SetColumn(session, details, columnids["schema_version"], "2.5", Encoding.Unicode);

                            update.Save();
                        }
                    }
                }
                tx.Commit(CommitTransactionGrbit.None);
            }
        }
Exemplo n.º 24
0
        private static void CreateGlobalsTable(JET_DBID utxoDbId, Session jetSession)
        {
            JET_TABLEID globalsTableId;
            JET_COLUMNID chainTipColumnId;
            JET_COLUMNID unspentTxCountColumnId;
            JET_COLUMNID unspentOutputCountColumnId;
            JET_COLUMNID totalTxCountColumnId;
            JET_COLUMNID totalInputCountColumnId;
            JET_COLUMNID totalOutputCountColumnId;

            Api.JetCreateTable(jetSession, utxoDbId, "Globals", 0, 0, out globalsTableId);
            Api.JetAddColumn(jetSession, globalsTableId, "ChainTip", new JET_COLUMNDEF { coltyp = JET_coltyp.Binary }, null, 0, out chainTipColumnId);
            Api.JetAddColumn(jetSession, globalsTableId, "UnspentTxCount", new JET_COLUMNDEF { coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnNotNULL }, null, 0, out unspentTxCountColumnId);
            Api.JetAddColumn(jetSession, globalsTableId, "UnspentOutputCount", new JET_COLUMNDEF { coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnNotNULL }, null, 0, out unspentOutputCountColumnId);
            Api.JetAddColumn(jetSession, globalsTableId, "TotalTxCount", new JET_COLUMNDEF { coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnNotNULL }, null, 0, out totalTxCountColumnId);
            Api.JetAddColumn(jetSession, globalsTableId, "TotalInputCount", new JET_COLUMNDEF { coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnNotNULL }, null, 0, out totalInputCountColumnId);
            Api.JetAddColumn(jetSession, globalsTableId, "TotalOutputCount", new JET_COLUMNDEF { coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnNotNULL }, null, 0, out totalOutputCountColumnId);

            // initialize global data
            using (var jetUpdate = jetSession.BeginUpdate(globalsTableId, JET_prep.Insert))
            {
                Api.SetColumn(jetSession, globalsTableId, unspentTxCountColumnId, 0);
                Api.SetColumn(jetSession, globalsTableId, unspentOutputCountColumnId, 0);
                Api.SetColumn(jetSession, globalsTableId, totalTxCountColumnId, 0);
                Api.SetColumn(jetSession, globalsTableId, totalInputCountColumnId, 0);
                Api.SetColumn(jetSession, globalsTableId, totalOutputCountColumnId, 0);

                jetUpdate.Save();
            }

            Api.JetCloseTable(jetSession, globalsTableId);
        }
Exemplo n.º 25
0
 public Cursor(Instance instance, string databaseFileName)
 {
     m_DatabaseFileName = databaseFileName;
     Session = new Session(instance);
     Api.JetAttachDatabase(Session, databaseFileName, AttachDatabaseGrbit.None);
     Api.JetOpenDatabase(Session, databaseFileName, null, out Dbid, OpenDatabaseGrbit.None);
 }
Exemplo n.º 26
0
        public void Initialize()
        {
            _instance = CreateEsentInstance();
            _primarySessionId = new Session(_instance);

            InitializeDatabaseAndTables();
        }
Exemplo n.º 27
0
		public DocumentStorageActions(
			JET_INSTANCE instance,
			string database,
			TableColumnsCache tableColumnsCache,
			OrderedPartCollection<AbstractDocumentCodec> documentCodecs,
			IUuidGenerator uuidGenerator,
			IDocumentCacher cacher,
			TransactionalStorage transactionalStorage)
		{
			this.tableColumnsCache = tableColumnsCache;
			this.documentCodecs = documentCodecs;
			this.uuidGenerator = uuidGenerator;
			this.cacher = cacher;
			this.transactionalStorage = transactionalStorage;
			try
			{
				session = new Session(instance);
				transaction = new Transaction(session);
				Api.JetOpenDatabase(session, database, null, out dbid, OpenDatabaseGrbit.None);
			}
			catch (Exception)
			{
				Dispose();
				throw;
			}
		}
Exemplo n.º 28
0
        public void Update(Session session, JET_DBID dbid, Action<string> output)
        {
            using (var table = new Table(session, dbid, "config", OpenTableGrbit.DenyRead | OpenTableGrbit.PermitDDL))
            {
                JET_COLUMNID newMetadataColumnId;

                Api.JetAddColumn(session, table, "metadata_new", new JET_COLUMNDEF
                {
                    cbMax = 1024*512,
                    coltyp = JET_coltyp.LongText,
                    cp = JET_CP.Unicode,
                    grbit = ColumndefGrbit.ColumnNotNULL
                }, null, 0, out newMetadataColumnId);
            }

            using (var table = new Table(session, dbid, "config", OpenTableGrbit.None))
            {
                Api.MoveBeforeFirst(session, table);

                var rows = 0;

                var columnDictionary = Api.GetColumnDictionary(session, table);

                var metadataColumn = columnDictionary["metadata"];
                var nameColumn = columnDictionary["name"];
                var newMetadataColumn = columnDictionary["metadata_new"];

                while (Api.TryMoveNext(session, table))
                {
                    using (var insert = new Update(session, table, JET_prep.Replace))
                    {
                        var name = Api.RetrieveColumnAsString(session, table, nameColumn, Encoding.Unicode);
                        var metadata = Api.RetrieveColumnAsString(session, table, metadataColumn, Encoding.Unicode);
                        var fixedMetadata = GuidToEtagMigrationInConfigurations(metadata, name);

                        Api.SetColumn(session, table, newMetadataColumn, fixedMetadata, Encoding.Unicode);

                        insert.Save();
                    }

                    if (rows++ % 100 == 0)
                    {
                        output("Processed " + (rows) + " rows from metadata column in config table");
                        Api.JetCommitTransaction(session, CommitTransactionGrbit.LazyFlush);
                        Api.JetBeginTransaction2(session, BeginTransactionGrbit.None);
                    }
                }

                Api.JetCommitTransaction(session, CommitTransactionGrbit.None);
                
                // they cannot be run in transaction scope
                Api.JetDeleteColumn(session, table, "metadata");
                Api.JetRenameColumn(session, table, "metadata_new", "metadata", RenameColumnGrbit.None);
                
                Api.JetBeginTransaction2(session, BeginTransactionGrbit.None);
            }

            SchemaCreator.UpdateVersion(session, dbid, "0.5");
        }
Exemplo n.º 29
0
        public void Update(Session session, JET_DBID dbid)
        {
            using(var tx = new Transaction(session))
            {
                using (var mappedResults = new Table(session, dbid, "mapped_results", OpenTableGrbit.None))
                {
                    JET_COLUMNID columnid;
                    Api.JetAddColumn(session, mappedResults, "reduce_key_and_view_hashed", new JET_COLUMNDEF
                    {
                        cbMax = 32,
                        coltyp = JET_coltyp.Binary,
                        grbit = ColumndefGrbit.ColumnNotNULL
                    }, null, 0, out columnid);

                    const string indexDef = "+reduce_key_and_view_hashed\0\0";
                    Api.JetCreateIndex(session, mappedResults, "by_reduce_key_and_view_hashed",
                                       CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
                                       100);

                    Api.JetDeleteIndex(session, mappedResults, "by_view_and_reduce_key");


                    var columnDictionary = Api.GetColumnDictionary(session, mappedResults);

                    Api.MoveBeforeFirst(session, mappedResults);
                    while (Api.TryMoveNext(session, mappedResults))
                    {
                        using (var update = new Update(session, mappedResults, JET_prep.Replace))
                        {
                            var computeHash = MapReduceIndex.ComputeHash(
                                Api.RetrieveColumnAsString(session, mappedResults, columnDictionary["view"],
                                                           Encoding.Unicode),
                                Api.RetrieveColumnAsString(session, mappedResults, columnDictionary["reduce_key"],
                                                           Encoding.Unicode));

                            Api.SetColumn(session, mappedResults, columnDictionary["reduce_key_and_view_hashed"],
                                          computeHash);

                            update.Save();
                        }
                    }


                    using (var details = new Table(session, dbid, "details", OpenTableGrbit.None))
                    {
                        Api.JetMove(session, details, JET_Move.First, MoveGrbit.None);
                        var columnids = Api.GetColumnDictionary(session, details);

                        using(var update = new Update(session, details, JET_prep.Replace))
                        {
                            Api.SetColumn(session, details, columnids["schema_version"], "2.2", Encoding.Unicode);

                            update.Save();
                        }
                    }
                }
                tx.Commit(CommitTransactionGrbit.None);
            }
        }
Exemplo n.º 30
0
 public void SessionCanConvertToJetSesid()
 {
     using (var session = new Session(this.instance))
     {
         JET_SESID sesid = session;
         Assert.AreEqual(sesid, session.JetSesid);
     }
 }