Esempio n. 1
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);
        }
Esempio n. 2
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");
			}
		}
Esempio n. 3
0
		private void CreateIdentityTable(JET_DBID dbid)
		{
			JET_TABLEID tableid;
			Api.JetCreateTable(session, dbid, "identity_table", 1, 80, out tableid);
			JET_COLUMNID columnid;

			Api.JetAddColumn(session, tableid, "key", new JET_COLUMNDEF
			{
				cbMax = 2048,
				coltyp = JET_coltyp.LongText,
				cp = JET_CP.Unicode,
				grbit = ColumnNotNullIfOnHigherThanWindowsXp()
			}, null, 0, out columnid);


			var defaultValue = BitConverter.GetBytes(0);
			Api.JetAddColumn(session, tableid, "val", new JET_COLUMNDEF
			{
				coltyp = JET_coltyp.Long,
				grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
			}, defaultValue, defaultValue.Length, out columnid);

			CreateIndexes(tableid, new JET_INDEXCREATE
			{
				szKey = "+key\0\0",
				szIndexName = "by_key",
				grbit = CreateIndexGrbit.IndexPrimary
			});
		}
Esempio n. 4
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);
        }
            public override void Create(JET_SESID sessionId, JET_DBID databaseId)
            {
                var idColumnCreate = CreateAutoIncrementIdColumn(IdColumnName);
                var identifierColumnCreate = CreateTextColumn(IdentifierColumnName);

                var columns = new JET_COLUMNCREATE[] { idColumnCreate, identifierColumnCreate };

                var primaryIndexKey = CreateIndexKey(IdColumnName);
                var identifierIndexKey = CreateIndexKey(IdentifierColumnName);

                var indexes = new JET_INDEXCREATE[]
                {
                    CreatePrimaryIndex(primaryIndexKey),
                    CreateUniqueTextIndex(IdentifierIndexName, identifierIndexKey)
                };

                var tableCreate = CreateTable(TableName, columns, indexes);

                Api.JetCreateTableColumnIndex3(sessionId, databaseId, tableCreate);

                _idColumnId = idColumnCreate.columnid;
                _identifierColumnId = identifierColumnCreate.columnid;

                Api.JetCloseTable(sessionId, tableCreate.tableid);
            }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        private void CreateKeysTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;
            Api.JetCreateTable(session, dbid, "keys", 16, 100, out tableid);
            JET_COLUMNID columnid;
            Api.JetAddColumn(session, tableid, "key", new JET_COLUMNDEF
            {
                cbMax = 255,
                coltyp = JET_coltyp.Text,
                cp = JET_CP.Unicode,
                grbit = ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "version", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnAutoincrement
            }, null, 0, out columnid);

            var indexDef = "+key\0+version\0\0";
            Api.JetCreateIndex(session, tableid, "pk", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
                               100);

            indexDef = "+key\0\0";
            Api.JetCreateIndex(session, tableid, "by_key", CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
                               100);
        }
Esempio n. 8
0
        private void CreateDetailsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;
            Api.JetCreateTable(session, dbid, "details", 16, 100, out tableid);
            JET_COLUMNID id;
            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                cbMax = 16,
                coltyp = JET_coltyp.Binary,
                grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out id);

            JET_COLUMNID schemaVersion;
            Api.JetAddColumn(session, tableid, "schema_version", new JET_COLUMNDEF
            {
                cbMax = Encoding.Unicode.GetByteCount(SchemaVersion),
                cp = JET_CP.Unicode,
                coltyp = JET_coltyp.Text,
                grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out schemaVersion);

            using (var update = new Update(session, tableid, JET_prep.Insert))
            {
                Api.SetColumn(session, tableid, id, Guid.NewGuid().ToByteArray());
                Api.SetColumn(session, tableid, schemaVersion, SchemaVersion, Encoding.Unicode);
                update.Save();
            }
        }
Esempio n. 9
0
		private void CreateIdentityTable(JET_DBID dbid)
		{
			JET_TABLEID tableid;
			Api.JetCreateTable(session, dbid, "identity_table", 1, 80, out tableid);
			JET_COLUMNID columnid;

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


			var defaultValue = BitConverter.GetBytes(0);
			Api.JetAddColumn(session, tableid, "val", new JET_COLUMNDEF
			{
				coltyp = JET_coltyp.Long,
				grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
			}, defaultValue, defaultValue.Length, out columnid);

			const string indexDef = "+key\0\0";
			Api.JetCreateIndex(session, tableid, "by_key", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
							   80);
		}
Esempio n. 10
0
        public void Create(JET_SESID session, JET_DBID dbid)
        {
            using (var tran = new Transaction(session))
            {
                JET_TABLEID tblID;
                Api.JetCreateTable(session, dbid, tableName, 1, 80, out tblID);

                JET_COLUMNID c;
                Api.JetAddColumn(session, tblID, colName_ID, new JET_COLUMNDEF()
                {
                    coltyp = JET_coltyp.Currency,
                    grbit = ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
                }, null, 0, out c);

                Api.JetAddColumn(session, tblID, colName_LogID, new JET_COLUMNDEF()
                {
                    coltyp = JET_coltyp.Currency,
                    grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
                }, null, 0, out c);

                var indexDef = "+" + colName_ID + "\0\0";
                Api.JetCreateIndex(session, tblID, idxName_Primary, CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length, 80);

                tran.Commit(CommitTransactionGrbit.None);
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the Table class. The table is
 /// opened from the given database.
 /// </summary>
 /// <param name="sesid">The session to use.</param>
 /// <param name="dbid">The database to open the table in.</param>
 /// <param name="name">The name of the table.</param>
 /// <param name="grbit">JetOpenTable options.</param>
 public Table(JET_SESID sesid, JET_DBID dbid, string name, OpenTableGrbit grbit)
 {
     this.sesid = sesid;
     this.name = name;
     Api.JetOpenTable(this.sesid, dbid, this.name, null, 0, grbit, out this.tableid);
     this.ResourceWasAllocated();
 }
        internal static void CreateTable(JET_SESID sesid, JET_DBID dbid)
        {
            JET_TABLEID tableid;
            Api.JetCreateTable(sesid, dbid, GeometryTableName, 8, 80, out tableid);

            using (var transaction = new Microsoft.Isam.Esent.Interop.Transaction(sesid))
            {
                JET_COLUMNID columnid;

                var columndef = new JET_COLUMNDEF
                {
                    coltyp = JET_coltyp.Long,
                    grbit = ColumndefGrbit.ColumnAutoincrement
                };

                Api.JetAddColumn(sesid, tableid, colNameGeometryLabel, columndef, null, 0, out columnid);

                columndef.grbit = ColumndefGrbit.ColumnNotNULL;

                Api.JetAddColumn(sesid, tableid, colNameProductLabel, columndef, null, 0, out columnid);

                columndef.coltyp = JET_coltyp.UnsignedByte;
                Api.JetAddColumn(sesid, tableid, colNameGeomType, columndef, null, 0, out columnid);
                
                columndef.coltyp = JET_coltyp.Short;
                Api.JetAddColumn(sesid, tableid, colNameProductIfcTypeId, columndef, null, 0, out columnid);
                Api.JetAddColumn(sesid, tableid, colNameSubPart, columndef, null, 0, out columnid);
               

                columndef.coltyp = JET_coltyp.Binary;
                columndef.grbit = ColumndefGrbit.ColumnMaybeNull;
                Api.JetAddColumn(sesid, tableid, colNameTransformMatrix, columndef, null, 0, out columnid);
               
                columndef.coltyp = JET_coltyp.LongBinary;
                //if (EsentVersion.SupportsWindows7Features)
                //    columndef.grbit |= Windows7Grbits.ColumnCompressed;
                Api.JetAddColumn(sesid, tableid, colNameShapeData, columndef, null, 0, out columnid);

                columndef.coltyp = JET_coltyp.Long;
                columndef.grbit = ColumndefGrbit.ColumnNotNULL;
                Api.JetAddColumn(sesid, tableid, colNameGeometryHash, columndef, null, 0, out columnid);
                columndef.grbit = ColumndefGrbit.ColumnNotNULL;
                Api.JetAddColumn(sesid, tableid, colNameStyleLabel, columndef, null, 0, out columnid);
                // The primary index is the type and the entity label.
                string indexDef = string.Format("+{0}\0\0", colNameGeometryLabel);
                Api.JetCreateIndex(sesid, tableid, geometryTablePrimaryIndex, CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length, 100);
                //create index by geometry hashes    
                indexDef = string.Format("+{0}\0\0", colNameGeometryHash);
                Api.JetCreateIndex(sesid, tableid, geometryTableHashIndex, CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length, 100);
                //Create index by product
                indexDef = string.Format("+{0}\0{1}\0{2}\0{3}\0{4}\0\0", colNameGeomType, colNameProductIfcTypeId, colNameProductLabel, colNameSubPart, colNameStyleLabel);
                Api.JetCreateIndex(sesid, tableid, geometryTableGeomTypeIndex, CreateIndexGrbit.IndexUnique, indexDef, indexDef.Length, 100);
                //create index by style
                indexDef = string.Format("+{0}\0{1}\0{2}\0{3}\0{4}\0\0", colNameGeomType, colNameStyleLabel, colNameProductIfcTypeId, colNameProductLabel, colNameGeometryLabel);
                Api.JetCreateIndex(sesid, tableid, geometryTableStyleIndex, CreateIndexGrbit.None, indexDef, indexDef.Length, 100);
                Api.JetCloseTable(sesid, tableid);
                transaction.Commit(CommitTransactionGrbit.LazyFlush);
            }
           
        }
Esempio 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");
            }
        }
Esempio n. 14
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");
        }
Esempio n. 15
0
		private static void CreateIndexingEtagsTable(JET_DBID dbid, JET_SESID session)
		{
			JET_TABLEID tableid;
			Api.JetCreateTable(session, dbid, "indexes_etag", 16, 100, out tableid);
			JET_COLUMNID columnid;

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

			var defaultValue = BitConverter.GetBytes(0);
			Api.JetAddColumn(session, tableid, "touches", new JET_COLUMNDEF
			{
				coltyp = JET_coltyp.Long,
				grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnEscrowUpdate
			}, defaultValue, defaultValue.Length, out columnid);

			const string indexDef = "+key\0\0";
			Api.JetCreateIndex(session, tableid, "by_key", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
							   100);
		}
            public override void Create(JET_SESID sessionId, JET_DBID databaseId)
            {
                var identifierColumnCreate = CreateIdColumn(IdentifierColumnName);
                var locationsColumnCreate = CreateBinaryColumn(LocationsColumnName);

                var columns = CreateProjectDocumentColumns(identifierColumnCreate, locationsColumnCreate);

                var primaryIndexKey = CreateProjectDocumentIndexKey(IdentifierColumnName);

                var indexes = new JET_INDEXCREATE[]
                {
                    CreatePrimaryIndex(primaryIndexKey)
                };

                var tableCreate = CreateTable(TableName, columns, indexes);

                Api.JetCreateTableColumnIndex3(sessionId, databaseId, tableCreate);

                GetColumnIds(columns, out _projectColumnId, out _projectNameColumnId, out _documentColumnId);

                _identifierColumnId = identifierColumnCreate.columnid;
                _locationsColumnId = locationsColumnCreate.columnid;

                Api.JetCloseTable(sessionId, tableCreate.tableid);
            }
Esempio n. 17
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);
			}
		}
Esempio n. 18
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);
            }
        }
Esempio n. 19
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);
            }
        }
Esempio n. 20
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");
        }
Esempio n. 21
0
        public static JET_TABLEID CreateTable(JET_DBID dbId, JET_SESID session, string tableName)
        {
            JET_TABLEID tableId;

            Api.JetCreateTable(session, dbId, tableName, 16, 100, out tableId);
            
            return tableId;
        }
Esempio n. 22
0
		public void Update(Session session, JET_DBID dbid, Action<string> output)
		{
			Api.JetDeleteTable(session, dbid, "mapped_results"); // just kill the old table, we won't use the data anyway
			CreateMapResultsTable(session, dbid);
			CreateReduceResultsTable(session, dbid);
			CreateScheduledReductionsTable(session, dbid);
			SchemaCreator.UpdateVersion(session, dbid, "3.8");
		}
Esempio n. 23
0
 public EsentDb(string dbname)
 {
     this.dbname = dbname;
       instance = OpenInstance(dbname);
       session = CreateSession(instance);
       dbid = CreateDatabase(dbname, instance, session);
       table = CreateTable(session, dbid, "store", new Tuple<string, JET_coltyp>[] { Tuple.Create("key", JET_coltyp.Long), Tuple.Create("data", JET_coltyp.LongText) });
 }
Esempio n. 24
0
		public void Update(Session session, JET_DBID dbid)
		{
			var defaultVal = BitConverter.GetBytes(0);
			Api.JetSetColumnDefaultValue(session, dbid, "reduce_keys_status", "reduce_type", defaultVal, defaultVal.Length,
			                             SetColumnDefaultValueGrbit.None);

			SchemaCreator.UpdateVersion(session, dbid, "4.2");
		}
 public override void Initialize(JET_SESID sessionId, JET_DBID databaseId)
 {
     using (var table = new Table(sessionId, databaseId, TableName, OpenTableGrbit.ReadOnly))
     {
         _idColumnId = Api.GetTableColumnid(sessionId, table, IdColumnName);
         _identifierColumnId = Api.GetTableColumnid(sessionId, table, IdentifierColumnName);
     }
 }
            public override void Create(JET_SESID sessionId, JET_DBID databaseId)
            {
                var projectColumnCreate = new JET_COLUMNCREATE()
                {
                    szColumnName = ProjectColumnName,
                    coltyp = JET_coltyp.Long,
                    grbit = ColumndefGrbit.ColumnNotNULL
                };

                var nameColumnCreate = new JET_COLUMNCREATE()
                {
                    szColumnName = NameColumnName,
                    coltyp = JET_coltyp.Long,
                    grbit = ColumndefGrbit.ColumnNotNULL
                };

                var valueColumnCreate = new JET_COLUMNCREATE()
                {
                    szColumnName = ValueColumnName,
                    coltyp = JET_coltyp.LongBinary,
                    grbit = ColumndefGrbit.None
                };

                var columns = new JET_COLUMNCREATE[] { projectColumnCreate, nameColumnCreate, valueColumnCreate };

                var projectAndNameIndexKey = "+" + ProjectColumnName + "\0+" + NameColumnName + "\0\0";

                var indexes = new JET_INDEXCREATE[]
                {
                    new JET_INDEXCREATE
                    {
                        szIndexName = ProjectAndNameIndexName,
                        szKey = projectAndNameIndexKey,
                        cbKey = projectAndNameIndexKey.Length,
                        grbit = CreateIndexGrbit.IndexPrimary | CreateIndexGrbit.IndexUnique | CreateIndexGrbit.IndexDisallowNull,
                        ulDensity = 80
                    }
                };

                var tableCreate = new JET_TABLECREATE()
                {
                    szTableName = TableName,
                    ulPages = 16,
                    ulDensity = 80,
                    rgcolumncreate = columns,
                    cColumns = columns.Length,
                    rgindexcreate = indexes,
                    cIndexes = indexes.Length
                };

                Api.JetCreateTableColumnIndex3(sessionId, databaseId, tableCreate);

                _projectColumnId = projectColumnCreate.columnid;
                _nameColumnId = nameColumnCreate.columnid;
                _valueColumnId = valueColumnCreate.columnid;

                Api.JetCloseTable(sessionId, tableCreate.tableid);
            }
Esempio n. 27
0
		public void Update(Session session, JET_DBID dbid)
		{
			Transaction tx;
			using (tx = new Transaction(session))
			{
				using ( var tbl = new Table(session, dbid, "indexes_stats",
					OpenTableGrbit.PermitDDL | OpenTableGrbit.DenyRead | OpenTableGrbit.DenyWrite))
				{
					var columnDictionary = Api.GetColumnDictionary(session, tbl);

					if (columnDictionary.ContainsKey("reduce_attempts"))
						Api.JetDeleteColumn(session, tbl, "reduce_attempts");
					if (columnDictionary.ContainsKey("reduce_errors"))
						Api.JetDeleteColumn(session, tbl, "reduce_errors");
					if (columnDictionary.ContainsKey("reduce_successes"))
						Api.JetDeleteColumn(session, tbl, "reduce_successes");
				}

				CreateIndexesStatsReduce(session, dbid);

				using (var stats = new Table(session, dbid, "indexes_stats",OpenTableGrbit.None))
				using (var reduce = new Table(session, dbid, "indexes_stats_reduce", OpenTableGrbit.None))
				{
					var tblKeyColumn = Api.GetColumnDictionary(session, stats)["key"];
					var reduceKeyCol = Api.GetColumnDictionary(session, reduce)["key"];

					Api.MoveBeforeFirst(session, stats);
					while (Api.TryMoveNext(session, stats))
					{
						using(var update = new Update(session, reduce, JET_prep.Insert))
						{
							var indexName = Api.RetrieveColumnAsString(session, stats, tblKeyColumn, Encoding.Unicode);
							Api.SetColumn(session, reduce, reduceKeyCol, indexName, Encoding.Unicode);
							update.Save();
						}
					}
				}

				tx.Commit(CommitTransactionGrbit.LazyFlush);
				tx.Dispose();
				tx = new Transaction(session);
			}

			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"], "3.4", Encoding.Unicode);

					update.Save();
				}
			}
			tx.Commit(CommitTransactionGrbit.None);
			tx.Dispose();
		}
Esempio n. 28
0
        private Table OpenSchema(JET_SESID sesid, JET_DBID dbid, out JET_COLUMNID primaryColumnId, out JET_COLUMNID secondaryColumnId)
        {
            var table = new Table(sesid, dbid, "table", OpenTableGrbit.None);

            primaryColumnId = Api.GetTableColumnid(sesid, table, "key");
            secondaryColumnId = Api.GetTableColumnid(sesid, table, "data");

            return table;
        }
 public override void Initialize(JET_SESID sessionId, JET_DBID databaseId)
 {
     using (var table = new Table(sessionId, databaseId, TableName, OpenTableGrbit.ReadOnly))
     {
         GetColumnIds(sessionId, table, out _projectColumnId, out _projectNameColumnId);
         _nameColumnId = Api.GetTableColumnid(sessionId, table, NameColumnName);
         _valueColumnId = Api.GetTableColumnid(sessionId, table, ValueColumnName);
     }
 }
Esempio n. 30
0
 public static void JetGetIndexInfo(
     JET_SESID sesid,
     JET_DBID dbid,
     string tablename,
     string ignored,
     out JET_INDEXLIST indexlist)
 {
     Api.JetGetIndexInfo(sesid, dbid, tablename, ignored, out indexlist, JET_IdxInfo.List);
 }
Esempio n. 31
0
        private void CreateTasksTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "tasks", 256, 100, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "supports_merging", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Bit,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

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

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

            Api.JetAddColumn(session, tableid, "added_at", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.DateTime,
                grbit  = ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            var indexDef = "+id\0\0";

            Api.JetCreateIndex(session, tableid, "by_id", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
                               100);
            indexDef = "+for_index\0\0";
            Api.JetCreateIndex(session, tableid, "by_index", CreateIndexGrbit.IndexIgnoreNull, indexDef, indexDef.Length,
                               100);

            indexDef = "+supports_merging\0+for_index\0+task_type\0\0";
            Api.JetCreateIndex2(session, tableid, new[] { new JET_INDEXCREATE
                                                          {
                                                              cbKey       = indexDef.Length,
                                                              cbKeyMost   = SystemParameters.KeyMost,
                                                              grbit       = CreateIndexGrbit.IndexIgnoreNull,
                                                              szIndexName = "mergables_by_task_type",
                                                              ulDensity   = 100,
                                                              szKey       = indexDef
                                                          }, }, 1);
        }
Esempio n. 32
0
        private void CreateFilesTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "files", 1, 80, out tableid);
            JET_COLUMNID columnid;


            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "name", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "metadata", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.LongText,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            CreateIndexes(tableid,
                          new JET_INDEXCREATE
            {
                szIndexName = "by_id",
                szKey       = "+id\0\0",
                grbit       = CreateIndexGrbit.IndexPrimary
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_name",
                szKey       = "+name\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_etag",
                szKey       = "+etag\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            });
        }
Esempio n. 33
0
        private void CreateIndexingStatsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "indexes_stats", 16, 100, out tableid);
            JET_COLUMNID columnid;

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

            var defaultValue = BitConverter.GetBytes(0);

            Api.JetAddColumn(session, tableid, "successes", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnEscrowUpdate
            }, defaultValue, defaultValue.Length, out columnid);

            Api.JetAddColumn(session, tableid, "last_indexed_etag", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Binary,
                cbMax  = 16,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);


            Api.JetAddColumn(session, tableid, "last_indexed_timestamp", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.DateTime,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "attempts", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
            }, defaultValue, defaultValue.Length, out columnid);

            Api.JetAddColumn(session, tableid, "errors", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
            }, defaultValue, defaultValue.Length, out columnid);

            Api.JetAddColumn(session, tableid, "reduce_attempts", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
            }, defaultValue, defaultValue.Length, out columnid);

            Api.JetAddColumn(session, tableid, "reduce_errors", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
            }, defaultValue, defaultValue.Length, out columnid);

            Api.JetAddColumn(session, tableid, "reduce_successes", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnEscrowUpdate
            }, defaultValue, defaultValue.Length, out columnid);


            const string indexDef = "+key\0\0";

            Api.JetCreateIndex(session, tableid, "by_key", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
                               100);
        }
Esempio n. 34
0
 public abstract void Create(JET_SESID sessionId, JET_DBID databaseId);
Esempio n. 35
0
        /// <summary>
        /// This whole function is called from within a tranaction
        /// </summary>
        /// <param name="session"></param>
        /// <param name="dbid"></param>
        protected override void CreateManagedTable(Session session, JET_DBID dbid)
        {
            JET_TABLEID tableID;
            var         columnids = new JET_COLUMNID[2];

            // Create the table
            Api.JetCreateTable(session, dbid, _config.TableName, 128, 100, out tableID);

            // Add the subscriber column which will have the trace request ID
            Api.JetAddColumn(
                session,
                tableID,
                _config.SubscriberColumnName,
                new JET_COLUMNDEF {
                coltyp = JET_coltyp.LongText,
                grbit  = ColumndefGrbit.TTKey,
                cp     = JET_CP.ASCII
            },
                null,
                0,
                out columnids[0]);

            // Add the multi-valued channel column which has the name of each channel
            // the connection has subscribed to
            Api.JetAddColumn(
                session,
                tableID,
                _config.ChannelColumnName,
                new JET_COLUMNDEF {
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.ASCII,
                grbit  = ColumndefGrbit.ColumnMultiValued
            },
                null,
                0,
                out columnids[1]);

            // Prepare key which summarises the index
            string pkIndexKey = string.Format(CultureInfo.InvariantCulture, "+{0}\0\0", _config.SubscriberColumnName);
            string chIndexKey = string.Format(CultureInfo.InvariantCulture, "+{0}\0\0", _config.ChannelColumnName);

            // Request index creation as one operation
            var indexcreates = new[]
            {
                new JET_INDEXCREATE
                {
                    cbKeyMost   = SystemParameters.KeyMost,
                    grbit       = CreateIndexGrbit.IndexPrimary,
                    szIndexName = "primary",
                    szKey       = pkIndexKey,
                    cbKey       = pkIndexKey.Length,
                    pidxUnicode = new JET_UNICODEINDEX
                    {
                        lcid       = CultureInfo.CurrentCulture.LCID,
                        dwMapFlags = Conversions.LCMapFlagsFromCompareOptions(CompareOptions.OrdinalIgnoreCase),
                    },
                }
            };

            // Create primary index
            Api.JetCreateIndex2(session, tableID, indexcreates, indexcreates.Length);

            // Create the secondary index on the multi-valued column
            Api.JetCreateIndex(
                session,
                tableID,
                "channelindex",
                CreateIndexGrbit.None,
                chIndexKey,
                chIndexKey.Length,
                100
                );

            // Clean up
            Api.JetCloseTable(session, tableID);
        }
Esempio n. 36
0
        private void CreateIndexedDocumentsReferencesTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "indexed_documents_references", 1, 80, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);


            Api.JetAddColumn(session, tableid, "view", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "key", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "ref", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            CreateIndexes(tableid,
                          new JET_INDEXCREATE
            {
                szIndexName = "by_id",
                szKey       = "+id\0\0",
                grbit       = CreateIndexGrbit.IndexPrimary
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_key",
                szKey       = "+key\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_view_and_key",
                szKey       = "+view\0+key\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_ref",
                szKey       = "+ref\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            });
        }
Esempio n. 37
0
        private void CreateDocumentsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "documents", 1, 80, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "key", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "last_modified", new JET_COLUMNDEF
            {
                cbMax  = 8, // 8 bytes - 64 bits
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "locked_by_transaction", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnTagged,
            }, null, 0, out columnid);

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

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

            CreateIndexes(tableid,
                          new JET_INDEXCREATE
            {
                szIndexName = "by_id",
                szKey       = "+id\0\0",
                grbit       = CreateIndexGrbit.IndexPrimary
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_etag",
                szKey       = "+etag\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            });
            Api.JetCreateIndex2(session, tableid, new[]
            {
                new JET_INDEXCREATE
                {
                    szIndexName = "by_key",
                    cbKey       = 6,
                    cbKeyMost   = SystemParameters.KeyMost,
                    cbVarSegMac = SystemParameters.KeyMost,
                    szKey       = "+key\0\0",
                    grbit       = CreateIndexGrbit.IndexDisallowNull | CreateIndexGrbit.IndexUnique,
                }
            }, 1);
        }
Esempio n. 38
0
        private JET_err DefragmentationStatusCallback(JET_SESID sesid, JET_DBID dbId, JET_TABLEID tableId, JET_cbtyp cbtyp, object data1, object data2, IntPtr ptr1, IntPtr ptr2)
        {
            defragmentationCompleted = cbtyp == JET_cbtyp.OnlineDefragCompleted;

            return(JET_err.Success);
        }
Esempio n. 39
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TableIndexInfoEnumerator"/> class.
 /// </summary>
 /// <param name="sesid">
 /// The session to use.
 /// </param>
 /// <param name="dbid">
 /// The database containing the table.
 /// </param>
 /// <param name="tablename">
 /// The name of the table.
 /// </param>
 public TableIndexInfoEnumerator(JET_SESID sesid, JET_DBID dbid, string tablename)
     : base(sesid)
 {
     this.dbid      = dbid;
     this.tablename = tablename;
 }
Esempio n. 40
0
        private void CreateDocumentsBeingModifiedByTransactionsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "documents_modified_by_transaction", 1024, 100, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "last_modified", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.DateTime,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "locked_by_transaction", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed,
            }, null, 0, out columnid);

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

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

            Api.JetAddColumn(session, tableid, "delete_document", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Bit,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            var indexDef = "+key\0\0";

            Api.JetCreateIndex(session, tableid, "by_key", CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
                               100);

            indexDef = "+id\0\0";
            Api.JetCreateIndex(session, tableid, "by_id", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
                               100);

            indexDef = "+locked_by_transaction\0\0";
            Api.JetCreateIndex2(session, tableid, new[]
            {
                new JET_INDEXCREATE
                {
                    cbKey       = indexDef.Length,
                    cbKeyMost   = SystemParameters.KeyMost,
                    grbit       = CreateIndexGrbit.IndexDisallowNull,
                    szIndexName = "by_tx",
                    szKey       = indexDef,
                    ulDensity   = 100,
                },
            }, 1);
        }
Esempio n. 41
0
        public void Update(Session session, JET_DBID dbid)
        {
            CreateReduceKeysCountsTable(session, dbid);
            CreateReduceKeysStatusTable(session, dbid);

            var countsPerKeyPerIndex = new Dictionary <string, Dictionary <string, int> >(StringComparer.InvariantCultureIgnoreCase);

            using (var mappedResults = new Table(session, dbid, "mapped_results", OpenTableGrbit.None))
            {
                SchemaCreator.CreateIndexes(session, mappedResults, new JET_INDEXCREATE
                {
                    szIndexName = "by_view_and_hashed_reduce_key",
                    szKey       = "+view\0+hashed_reduce_key\0\0",
                });

                var columnDictionary = Api.GetColumnDictionary(session, mappedResults);
                Api.MoveBeforeFirst(session, mappedResults);
                while (Api.TryMoveNext(session, mappedResults))
                {
                    var index       = Api.RetrieveColumnAsString(session, mappedResults, columnDictionary["view"], Encoding.Unicode);
                    var reduceKey   = Api.RetrieveColumnAsString(session, mappedResults, columnDictionary["reduce_key"], Encoding.Unicode);
                    var countPerKey = countsPerKeyPerIndex.GetOrAdd(index);
                    countPerKey[reduceKey] = countPerKey.GetOrDefault(reduceKey) + 1;
                }
            }
            using (var reduceKeys = new Table(session, dbid, "reduce_keys_status", OpenTableGrbit.None))
            {
                var columnDictionary = Api.GetColumnDictionary(session, reduceKeys);
                foreach (var countPerKey in countsPerKeyPerIndex)
                {
                    foreach (var keyCount in countPerKey.Value)
                    {
                        using (var update = new Update(session, reduceKeys, JET_prep.Insert))
                        {
                            Api.SetColumn(session, reduceKeys, columnDictionary["view"], countPerKey.Key, Encoding.Unicode);
                            Api.SetColumn(session, reduceKeys, columnDictionary["reduce_key"], keyCount.Key, Encoding.Unicode);
                            Api.SetColumn(session, reduceKeys, columnDictionary["hashed_reduce_key"], DocumentStorageActions.HashReduceKey(keyCount.Key));
                            Api.SetColumn(session, reduceKeys, columnDictionary["reduce_type"], (int)ReduceType.MultiStep);
                            update.Save();
                        }
                    }
                }
            }

            using (var reduceKeys = new Table(session, dbid, "reduce_keys_counts", OpenTableGrbit.None))
            {
                var columnDictionary = Api.GetColumnDictionary(session, reduceKeys);
                foreach (var countPerKey in countsPerKeyPerIndex)
                {
                    foreach (var keyCount in countPerKey.Value)
                    {
                        using (var update = new Update(session, reduceKeys, JET_prep.Insert))
                        {
                            Api.SetColumn(session, reduceKeys, columnDictionary["view"], countPerKey.Key, Encoding.Unicode);
                            Api.SetColumn(session, reduceKeys, columnDictionary["reduce_key"], keyCount.Key, Encoding.Unicode);
                            Api.SetColumn(session, reduceKeys, columnDictionary["hashed_reduce_key"], DocumentStorageActions.HashReduceKey(keyCount.Key));
                            Api.SetColumn(session, reduceKeys, columnDictionary["mapped_items_count"], keyCount.Value);
                            update.Save();
                        }
                    }
                }
            }

            using (var scheduledReductions = new Table(session, dbid, "scheduled_reductions", OpenTableGrbit.None))
            {
                SchemaCreator.CreateIndexes(session, scheduledReductions, new JET_INDEXCREATE
                {
                    szIndexName = "by_view_level_and_hashed_reduce_key",
                    szKey       = "+view\0+level\0+hashed_reduce_key\0\0",
                });
            }

            SchemaCreator.UpdateVersion(session, dbid, "4.1");
        }
Esempio n. 42
0
        /// <summary>
        /// Creates a table, adds columns, and indices on that table.
        /// </summary>
        /// <param name="sesid">The session to use.</param>
        /// <param name="dbid">The database to which to add the new table.</param>
        /// <param name="tablecreate">Object describing the table to create.</param>
        /// <returns>An error if the call fails.</returns>
        private static int CreateTableColumnIndex4(
            JET_SESID sesid,
            JET_DBID dbid,
            JET_TABLECREATE tablecreate)
        {
            NATIVE_TABLECREATE4 nativeTableCreate = tablecreate.GetNativeTableCreate4();

            unsafe
            {
                var handles = new GCHandleCollection();
                try
                {
                    // Convert/pin the column definitions.
                    nativeTableCreate.rgcolumncreate = (NATIVE_COLUMNCREATE *)GetNativeColumnCreates(tablecreate.rgcolumncreate, true, ref handles);

                    // Convert/pin the index definitions.
                    NATIVE_INDEXCREATE3[] nativeIndexCreates = GetNativeIndexCreate3s(tablecreate.rgindexcreate, ref handles);
                    nativeTableCreate.rgindexcreate = handles.Add(nativeIndexCreates);

                    // Convert/pin the space hints.
                    if (tablecreate.pSeqSpacehints != null)
                    {
                        NATIVE_SPACEHINTS nativeSpaceHints = tablecreate.pSeqSpacehints.GetNativeSpaceHints();
                        nativeTableCreate.pSeqSpacehints = (NATIVE_SPACEHINTS *)handles.Add(nativeSpaceHints);
                    }

                    if (tablecreate.pLVSpacehints != null)
                    {
                        NATIVE_SPACEHINTS nativeSpaceHints = tablecreate.pLVSpacehints.GetNativeSpaceHints();
                        nativeTableCreate.pLVSpacehints = (NATIVE_SPACEHINTS *)handles.Add(nativeSpaceHints);
                    }

                    int err = NativeMethods.JetCreateTableColumnIndex4W(sesid.Value, dbid.Value, ref nativeTableCreate);

                    // Modified fields.
                    tablecreate.tableid = new JET_TABLEID
                    {
                        Value = nativeTableCreate.tableid
                    };

                    tablecreate.cCreated = checked ((int)nativeTableCreate.cCreated);

                    if (tablecreate.rgcolumncreate != null)
                    {
                        for (int i = 0; i < tablecreate.rgcolumncreate.Length; ++i)
                        {
                            tablecreate.rgcolumncreate[i].SetFromNativeColumnCreate(nativeTableCreate.rgcolumncreate[i]);
                        }
                    }

                    if (tablecreate.rgindexcreate != null)
                    {
                        for (int i = 0; i < tablecreate.rgindexcreate.Length; ++i)
                        {
                            tablecreate.rgindexcreate[i].SetFromNativeIndexCreate(nativeIndexCreates[i]);
                        }
                    }

                    return(Err(err));
                }
                finally
                {
                    handles.Dispose();
                }
            }
        }
        /// <summary>
        /// Create the data table.
        /// </summary>
        /// <param name="session">The session to use.</param>
        /// <param name="dbid">The database to create the table in.</param>
        private void CreateDataTable(Session session, JET_DBID dbid)
        {
            JET_TABLEID  tableid;
            JET_COLUMNID keyColumnid;
            JET_COLUMNID valueColumnid;

            Api.JetCreateTable(session, dbid, this.config.DataTableName, 128, 100, out tableid);
            var columndef = new JET_COLUMNDEF {
                coltyp = this.converters.KeyColtyp, cp = JET_CP.Unicode, grbit = ColumndefGrbit.None
            };

            if (ColumnCanBeCompressed(columndef))
            {
                columndef.grbit |= Windows7Grbits.ColumnCompressed;
            }

            Api.JetAddColumn(
                session,
                tableid,
                this.config.KeyColumnName,
                columndef,
                null,
                0,
                out keyColumnid);

            columndef = new JET_COLUMNDEF {
                coltyp = this.converters.ValueColtyp, cp = JET_CP.Unicode, grbit = ColumndefGrbit.None
            };
            if (ColumnCanBeCompressed(columndef))
            {
                columndef.grbit |= Windows7Grbits.ColumnCompressed;
            }

            Api.JetAddColumn(
                session,
                tableid,
                this.config.ValueColumnName,
                columndef,
                null,
                0,
                out valueColumnid);

            string indexKey     = String.Format(CultureInfo.InvariantCulture, "+{0}\0\0", this.config.KeyColumnName);
            var    indexcreates = new[]
            {
                new JET_INDEXCREATE
                {
                    cbKeyMost   = SystemParameters.KeyMost,
                    grbit       = CreateIndexGrbit.IndexPrimary,
                    szIndexName = "primary",
                    szKey       = indexKey,
                    cbKey       = indexKey.Length,
                    pidxUnicode = new JET_UNICODEINDEX
                    {
                        lcid       = CultureInfo.CurrentCulture.LCID,
                        dwMapFlags = Conversions.LCMapFlagsFromCompareOptions(CompareOptions.None),
                    },
                },
            };

            Api.JetCreateIndex2(session, tableid, indexcreates, indexcreates.Length);

            Api.JetCloseTable(session, tableid);
        }
Esempio n. 44
0
        private void CreateMapResultsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "mapped_results", 1, 80, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "view", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "document_key", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "reduce_key", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "hashed_reduce_key", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Binary,
                cbMax  = 20,
                grbit  = ColumnNotNullIfOnHigherThanWindowsXp() | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "timestamp", new JET_COLUMNDEF
            {
                cbMax  = 8, // 64 bits
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "bucket", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            CreateIndexes(tableid,
                          new JET_INDEXCREATE
            {
                szIndexName = "by_id",
                szKey       = "+id\0\0",
                grbit       = CreateIndexGrbit.IndexPrimary
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_view",
                szKey       = "+view\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_view_and_doc_key",
                szKey       = "+view\0+document_key\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            },
                          new JET_INDEXCREATE
            {
                szIndexName = "by_view_hashed_reduce_key_and_bucket",
                szKey       = "+view\0+hashed_reduce_key\0+bucket\0\0",
            });
        }
Esempio n. 45
0
 public DdlTests(JET_SESID sesid, JET_DBID dbid, string table)
 {
     this.sesid = sesid;
     this.dbid  = dbid;
     this.table = table;
 }
Esempio n. 46
0
        public void CreateListsTable(Session session, JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "lists", 1, 80, out tableid);
            JET_COLUMNID columnid;


            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "name", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "key", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);


            SchemaCreator.CreateIndexes(session, tableid,
                                        new JET_INDEXCREATE
            {
                szIndexName = "by_id",
                szKey       = "+id\0\0",
                grbit       = CreateIndexGrbit.IndexPrimary
            },
                                        new JET_INDEXCREATE
            {
                szIndexName = "by_name_and_etag",
                szKey       = "+name\0+etag\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            },
                                        new JET_INDEXCREATE
            {
                szIndexName = "by_name_and_key",
                szKey       = "+name\0+key\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull | CreateIndexGrbit.IndexUnique
            });
        }
        /// <summary>
        /// Create the globals table.
        /// </summary>
        /// <param name="session">The session to use.</param>
        /// <param name="dbid">The database to create the table in.</param>
        private void CreateGlobalsTable(Session session, JET_DBID dbid)
        {
            JET_TABLEID  tableid;
            JET_COLUMNID versionColumnid;
            JET_COLUMNID countColumnid;
            JET_COLUMNID keyTypeColumnid;
            JET_COLUMNID valueTypeColumnid;

            Api.JetCreateTable(session, dbid, this.config.GlobalsTableName, 1, 100, out tableid);
            Api.JetAddColumn(
                session,
                tableid,
                this.config.VersionColumnName,
                new JET_COLUMNDEF {
                coltyp = JET_coltyp.LongText
            },
                null,
                0,
                out versionColumnid);

            byte[] defaultValue = BitConverter.GetBytes(0);

            Api.JetAddColumn(
                session,
                tableid,
                this.config.CountColumnName,
                new JET_COLUMNDEF {
                coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnEscrowUpdate
            },
                defaultValue,
                defaultValue.Length,
                out countColumnid);

            Api.JetAddColumn(
                session,
                tableid,
                this.config.FlushColumnName,
                new JET_COLUMNDEF {
                coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnEscrowUpdate
            },
                defaultValue,
                defaultValue.Length,
                out countColumnid);

            Api.JetAddColumn(
                session,
                tableid,
                this.config.KeyTypeColumnName,
                new JET_COLUMNDEF {
                coltyp = JET_coltyp.LongBinary
            },
                null,
                0,
                out keyTypeColumnid);

            Api.JetAddColumn(
                session,
                tableid,
                this.config.ValueTypeColumnName,
                new JET_COLUMNDEF {
                coltyp = JET_coltyp.LongBinary
            },
                null,
                0,
                out valueTypeColumnid);

            using (var update = new Update(session, tableid, JET_prep.Insert))
            {
                Api.SerializeObjectToColumn(session, tableid, keyTypeColumnid, typeof(TKey));
                Api.SerializeObjectToColumn(session, tableid, valueTypeColumnid, typeof(TValue));
                Api.SetColumn(session, tableid, versionColumnid, this.config.Version, Encoding.Unicode);
                update.Save();
            }

            Api.JetCloseTable(session, tableid);
        }
Esempio n. 48
0
        private void CreateOutgoingHistoryTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "outgoing_history", 16, 100, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "msg_id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Binary,
                cbMax  = 16,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);


            Api.JetAddColumn(session, tableid, "tx_id", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "port", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "number_of_retries", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "size_of_data", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "time_to_send", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.DateTime,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "sent_at", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.DateTime,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "send_status", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed,
            }, null, 0, out columnid);

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

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

            Api.JetAddColumn(session, tableid, "headers", new JET_COLUMNDEF
            {
                cbMax  = 8192,
                cp     = JET_CP.Unicode,
                coltyp = JET_coltyp.LongText,
                grbit  = ColumndefGrbit.None
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "data", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.LongBinary,
                // For Win2k3 support, it doesn't support long binary columsn that are not null
                grbit = ColumndefGrbit.None
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "deliver_by", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.DateTime,
                grbit  = ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "max_attempts", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            var indexDef = "+msg_id\0\0";

            Api.JetCreateIndex(session, tableid, "pk", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
                               100);

            indexDef = "+tx_id\0\0";
            Api.JetCreateIndex(session, tableid, "by_tx_id", CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
                               100);
        }
Esempio n. 49
0
 public abstract void Initialize(JET_SESID sessionId, JET_DBID databaseId);
Esempio n. 50
0
 public void Update(Session session, JET_DBID dbid, Action <string> output)
 {
     CreateIndexedDocumentsReferencesTable(session, dbid);
     SchemaCreator.UpdateVersion(session, dbid, "4.4");
 }
Esempio n. 51
0
        /// <summary>
        /// Retrieves information about indexes on a table.
        /// </summary>
        /// <param name="sesid">The session to use.</param>
        /// <param name="dbid">The database to use.</param>
        /// <param name="tablename">The name of the table to retrieve index information about.</param>
        /// <param name="indexname">The name of the index to retrieve information about.</param>
        /// <param name="result">Filled in with information about indexes on the table.</param>
        /// <param name="infoLevel">The type of information to retrieve.</param>
        /// <returns>An error if the call fails.</returns>
        public int JetGetIndexInfo(
            JET_SESID sesid,
            JET_DBID dbid,
            string tablename,
            string indexname,
            out JET_INDEXCREATE result,
            JET_IdxInfo infoLevel)
        {
            TraceFunctionCall("JetGetIndexInfo");
            CheckNotNull(tablename, "tablename");
            int err;

            switch (infoLevel)
            {
            case Microsoft.Isam.Esent.Interop.Windows7.Windows7IdxInfo.CreateIndex:
            case Microsoft.Isam.Esent.Interop.Windows7.Windows7IdxInfo.CreateIndex2:
            case Microsoft.Isam.Esent.Interop.Windows8.Windows8IdxInfo.InfoCreateIndex3:
                break;

            default:
                throw new ArgumentException(string.Format("{0} is not a valid value JET_IdxInfo for this JET_INDEXCREATE overload."));
            }

            if (this.Capabilities.SupportsWindows8Features)
            {
                {
                    int    bufferSize      = 10 * Marshal.SizeOf(typeof(NATIVE_INDEXCREATE3));
                    IntPtr unmanagedBuffer = Marshal.AllocHGlobal(bufferSize);
                    try
                    {
                        // var nativeIndexcreate = new NATIVE_INDEXCREATE3();
                        // nativeIndexcreate.cbStruct = checked((uint)bufferSize);
                        infoLevel = Windows8IdxInfo.InfoCreateIndex3;

                        err = Err(NativeMethods.JetGetIndexInfoW(
                                      sesid.Value,
                                      dbid.Value,
                                      tablename,
                                      indexname,
                                      unmanagedBuffer,
                                      (uint)bufferSize,
                                      (uint)infoLevel));

                        NATIVE_INDEXCREATE3 nativeIndexcreate = (NATIVE_INDEXCREATE3)Marshal.PtrToStructure(unmanagedBuffer, typeof(NATIVE_INDEXCREATE3));

                        result = new JET_INDEXCREATE();
                        result.SetAllFromNativeIndexCreate(ref nativeIndexcreate);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(unmanagedBuffer);
                    }
                }
            }
            else
            {
                result = null;
                err    = Err((int)JET_err.FeatureNotAvailable);
            }

            return(err);
        }
Esempio n. 52
0
        private void CreateDocumentsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "documents", 1024, 100, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "last_modified", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.DateTime,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "locked_by_transaction", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnTagged,
            }, null, 0, out columnid);

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

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


            var indexDef = "+key\0\0";

            Api.JetCreateIndex(session, tableid, "by_key", CreateIndexGrbit.IndexDisallowNull | CreateIndexGrbit.IndexUnique, indexDef, indexDef.Length,
                               100);

            indexDef = "+id\0\0";
            Api.JetCreateIndex(session, tableid, "by_id", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
                               100);

            indexDef = "+etag\0\0";
            Api.JetCreateIndex(session, tableid, "by_etag", CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
                               100);
        }
Esempio n. 53
0
        /// <summary>
        /// Create the globals table.
        /// </summary>
        /// <param name="sesid">The session to use.</param>
        /// <param name="dbid">The database to create the table in.</param>
        public static void CreateGlobalsTable(JET_SESID sesid, JET_DBID dbid)
        {
            using (var transaction = new Transaction(sesid))
            {
                JET_TABLEID tableid;
                Api.JetCreateTable(sesid, dbid, GlobalsTableName, 1, 100, out tableid);
                JET_COLUMNID versionColumnid;
                Api.JetAddColumn(
                    sesid,
                    tableid,
                    VersionColumnName,
                    new JET_COLUMNDEF {
                    coltyp = JET_coltyp.LongText
                },
                    null,
                    0,
                    out versionColumnid);

                var defaultValue = BitConverter.GetBytes(0);

                JET_COLUMNID countColumnid;
                Api.JetAddColumn(
                    sesid,
                    tableid,
                    EntityCountColumnName,
                    new JET_COLUMNDEF {
                    coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnEscrowUpdate
                },
                    defaultValue,
                    defaultValue.Length,
                    out countColumnid);

                Api.JetAddColumn(
                    sesid,
                    tableid,
                    GeometryCountColumnName,
                    new JET_COLUMNDEF {
                    coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnEscrowUpdate
                },
                    defaultValue,
                    defaultValue.Length,
                    out countColumnid);

                Api.JetAddColumn(
                    sesid,
                    tableid,
                    FlushColumnName,
                    new JET_COLUMNDEF {
                    coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.ColumnEscrowUpdate
                },
                    defaultValue,
                    defaultValue.Length,
                    out countColumnid);

                Api.JetAddColumn(
                    sesid,
                    tableid,
                    ifcHeaderColumnName,
                    new JET_COLUMNDEF {
                    coltyp = JET_coltyp.LongBinary
                },
                    null,
                    0,
                    out countColumnid);

                using (var update = new Update(sesid, tableid, JET_prep.Insert))
                {
                    Api.SetColumn(sesid, tableid, versionColumnid, Version, Encoding.Unicode);
                    update.Save();
                }

                Api.JetCloseTable(sesid, tableid);
                transaction.Commit(CommitTransactionGrbit.LazyFlush);
            }
        }
Esempio n. 54
0
        public void Update(Session session, JET_DBID dbid, Action <string> output)
        {
            var i = 0;

            JET_COLUMNID columnid;

            using (var scheduledReductions = new Table(session, dbid, "scheduled_reductions", OpenTableGrbit.None))
            {
                Api.JetAddColumn(session, scheduledReductions, "hashed_reduce_key", new JET_COLUMNDEF
                {
                    coltyp = JET_coltyp.Binary,
                    cbMax  = 20,
                    grbit  = ColumndefGrbit.ColumnFixed
                }, null, 0, out columnid);

                var scheduledReductionsColumns = Api.GetColumnDictionary(session, scheduledReductions);
                Api.MoveBeforeFirst(session, scheduledReductions);
                while (Api.TryMoveNext(session, scheduledReductions))
                {
                    using (var update = new Update(session, scheduledReductions, JET_prep.Replace))
                    {
                        var reduceKey = Api.RetrieveColumnAsString(session, scheduledReductions, scheduledReductionsColumns["reduce_key"]);

                        Api.SetColumn(session, scheduledReductions, scheduledReductionsColumns["hashed_reduce_key"],
                                      DocumentStorageActions.HashReduceKey(reduceKey));

                        update.Save();
                    }

                    if (i++ % 10000 == 0)
                    {
                        output("Processed " + (i - 1) + " rows in scheduled_reductions");
                    }
                }

                Api.JetDeleteIndex(session, scheduledReductions, "by_view_level_reduce_key_and_bucket");
                SchemaCreator.CreateIndexes(session, scheduledReductions, new JET_INDEXCREATE
                {
                    szIndexName = "by_view_level_bucket_and_hashed_reduce_key",
                    szKey       = "+view\0+level\0+bucket\0+hashed_reduce_key\0\0",
                });
            }

            output("Finished processing scheduled_reductions");
            i = 0;

            using (var mappedResults = new Table(session, dbid, "mapped_results", OpenTableGrbit.None))
            {
                Api.JetAddColumn(session, mappedResults, "hashed_reduce_key", new JET_COLUMNDEF
                {
                    coltyp = JET_coltyp.Binary,
                    cbMax  = 20,
                    grbit  = ColumndefGrbit.ColumnFixed
                }, null, 0, out columnid);

                var mappedResultsColumns = Api.GetColumnDictionary(session, mappedResults);
                Api.MoveBeforeFirst(session, mappedResults);
                while (Api.TryMoveNext(session, mappedResults))
                {
                    using (var update = new Update(session, mappedResults, JET_prep.Replace))
                    {
                        var reduceKey = Api.RetrieveColumnAsString(session, mappedResults, mappedResultsColumns["reduce_key"]);

                        Api.SetColumn(session, mappedResults, mappedResultsColumns["hashed_reduce_key"], DocumentStorageActions.HashReduceKey(reduceKey));

                        update.Save();
                    }

                    if (i++ % 10000 == 0)
                    {
                        output("Processed " + (i - 1) + " rows in mapped_results");
                    }
                }

                Api.JetDeleteIndex(session, mappedResults, "by_view_reduce_key_and_bucket");
                SchemaCreator.CreateIndexes(session, mappedResults, new JET_INDEXCREATE
                {
                    szIndexName = "by_view_bucket_and_hashed_reduce_key",
                    szKey       = "+view\0+bucket\0+hashed_reduce_key\0\0",
                });
            }

            output("Finished processing mapped_results");
            i = 0;

            using (var reduceResults = new Table(session, dbid, "reduce_results", OpenTableGrbit.None))
            {
                Api.JetAddColumn(session, reduceResults, "hashed_reduce_key", new JET_COLUMNDEF
                {
                    coltyp = JET_coltyp.Binary,
                    cbMax  = 20,
                    grbit  = ColumndefGrbit.ColumnFixed
                }, null, 0, out columnid);

                var reduceResultsColumns = Api.GetColumnDictionary(session, reduceResults);
                Api.MoveBeforeFirst(session, reduceResults);
                while (Api.TryMoveNext(session, reduceResults))
                {
                    using (var update = new Update(session, reduceResults, JET_prep.Replace))
                    {
                        var reduceKey = Api.RetrieveColumnAsString(session, reduceResults, reduceResultsColumns["reduce_key"]);

                        Api.SetColumn(session, reduceResults, reduceResultsColumns["hashed_reduce_key"], DocumentStorageActions.HashReduceKey(reduceKey));

                        update.Save();
                    }

                    if (i++ % 10000 == 0)
                    {
                        output("Processed " + (i - 1) + " rows in reduce_results");
                    }
                }

                Api.JetDeleteIndex(session, reduceResults, "by_view_level_reduce_key_and_bucket");
                Api.JetDeleteIndex(session, reduceResults, "by_view_level_reduce_key_and_source_bucket");
                SchemaCreator.CreateIndexes(session, reduceResults,
                                            new JET_INDEXCREATE
                {
                    szIndexName = "by_view_level_bucket_and_hashed_reduce_key",
                    szKey       = "+view\0+level\0+bucket\0+hashed_reduce_key\0\0",
                },
                                            new JET_INDEXCREATE
                {
                    szIndexName = "by_view_level_source_bucket_and_hashed_reduce_key",
                    szKey       = "+view\0+level\0+source_bucket\0+hashed_reduce_key\0\0",
                });
            }

            output("Finished processing reduce_results");

            SchemaCreator.UpdateVersion(session, dbid, "4.0");
        }
Esempio n. 55
0
        private void CreateIndexingStatsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "indexes_stats", 1, 80, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "key", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            var defaultValue = BitConverter.GetBytes(0);

            Api.JetAddColumn(session, tableid, "successes", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnEscrowUpdate
            }, defaultValue, defaultValue.Length, out columnid);

            Api.JetAddColumn(session, tableid, "last_indexed_etag", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Binary,
                cbMax  = 16,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);


            Api.JetAddColumn(session, tableid, "last_indexed_timestamp", new JET_COLUMNDEF
            {
                cbMax  = 8,                //64 bits
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);


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

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

            Api.JetAddColumn(session, tableid, "attempts", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
            }, defaultValue, defaultValue.Length, out columnid);

            Api.JetAddColumn(session, tableid, "priority", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "errors", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
            }, defaultValue, defaultValue.Length, out columnid);

            CreateIndexes(tableid, new JET_INDEXCREATE
            {
                szIndexName = "by_key",
                szKey       = "+key\0\0",
                grbit       = CreateIndexGrbit.IndexPrimary
            });
        }
Esempio n. 56
0
        public void Update(Session session, JET_DBID dbid, Action <string> output)
        {
            using (var tbl = new Table(session, dbid, "documents", OpenTableGrbit.None))
            {
                Api.JetDeleteIndex(session, tbl, "by_key");
                Api.JetCreateIndex2(session, tbl, new[]
                {
                    new JET_INDEXCREATE
                    {
                        szIndexName = "by_key",
                        cbKey       = 6,
                        cbKeyMost   = SystemParameters.KeyMost,
                        cbVarSegMac = SystemParameters.KeyMost,
                        szKey       = "+key\0\0",
                        grbit       = CreateIndexGrbit.IndexDisallowNull | CreateIndexGrbit.IndexUnique,
                    }
                }, 1);
            }

            // So first we allocate ids and crap
            // and write that to disk in a safe manner
            // I might want to look at keeping a list of written files to delete if it all goes t**s up at any point
            var filesToDelete = new List <string>();
            var nameToIds     = new Dictionary <string, int>();
            var indexDefPath  = Path.Combine(configuration.DataDirectory, "IndexDefinitions");

            var indexDefinitions = Directory.GetFiles(indexDefPath, "*.index")
                                   .Select(x => { filesToDelete.Add(x); return(x); })
                                   .Select(index => JsonConvert.DeserializeObject <IndexDefinition>(File.ReadAllText(index), Default.Converters))
                                   .ToList();

            indexDefinitions.ForEach(x => x.MaxIndexOutputsPerDocument = x.MaxIndexOutputsPerDocument ?? (16 * 1024));

            var transformDefinitions = Directory.GetFiles(indexDefPath, "*.transform")
                                       .Select(x => { filesToDelete.Add(x); return(x); })
                                       .Select(index => JsonConvert.DeserializeObject <TransformerDefinition>(File.ReadAllText(index), Default.Converters))
                                       .ToArray();

            int maxIndexId = 0;

            for (var i = 0; i < indexDefinitions.Count; i++)
            {
                var definition = indexDefinitions[i];
                definition.IndexId         = i;
                nameToIds[definition.Name] = definition.IndexId;
                var path = Path.Combine(indexDefPath, definition.IndexId + ".index");

                // TODO: This can fail, rollback
                File.WriteAllText(path, JsonConvert.SerializeObject(definition, Formatting.Indented, Default.Converters));

                var indexDirectory      = FixupIndexName(definition.Name, configuration.IndexStoragePath);
                var oldStorageDirectory = Path.Combine(configuration.IndexStoragePath, MonoHttpUtility.UrlEncode(indexDirectory));
                var newStorageDirectory = Path.Combine(configuration.IndexStoragePath, definition.IndexId.ToString());

                if (Directory.Exists(oldStorageDirectory)) // in-memory index
                {
                    Directory.Move(oldStorageDirectory, newStorageDirectory);
                }

                maxIndexId = i;
            }

            for (var i = 0; i < transformDefinitions.Length; i++)
            {
                var definition = transformDefinitions[i];
                definition.TransfomerId    = maxIndexId = indexDefinitions.Count + i;
                nameToIds[definition.Name] = definition.TransfomerId;
                var path = Path.Combine(indexDefPath, definition.TransfomerId + ".transform");

                // TODO: This can file, rollback
                File.WriteAllText(path, JsonConvert.SerializeObject(definition, Formatting.Indented, Default.Converters));
            }



            var tablesAndColumns = new[]
            {
                new { table = "scheduled_reductions", column = "view" },
                new { table = "mapped_results", column = "view" },
                new { table = "reduce_results", column = "view" },
                new { table = "reduce_keys_counts", column = "view" },
                new { table = "reduce_keys_status", column = "view" },
                new { table = "indexed_documents_references", column = "view" },
                new { table = "tasks", column = "for_index" },
                new { table = "indexes_stats", column = "key" },
                new { table = "indexes_etag", column = "key" },
                new { table = "indexes_stats_reduce", column = "key" }
            };

            foreach (var item in tablesAndColumns)
            {
                var         newTable = item.table + "_new";
                JET_TABLEID newTableId;
                try
                {
                    Api.JetDeleteTable(session, dbid, newTable);
                }
                catch (Exception)
                {
                    //if there is no such table - then it is not important
                    //this is a precaution against partially failed upgrade process
                }

                using (var sr = new Table(session, dbid, item.table, OpenTableGrbit.None))
                {
                    Api.JetCreateTable(session, dbid, newTable, 1, 80, out newTableId);
                    var existingColumns = Api.GetTableColumns(session, sr).ToList();
                    var existingIndexes = Api.GetTableIndexes(session, sr).ToList();

                    foreach (var column in existingColumns)
                    {
                        JET_COLUMNDEF columnDef = null;
                        Api.JetGetColumnInfo(session, dbid, item.table, column.Name, out columnDef);
                        JET_COLUMNID newColumndId;

                        if (column.Name == item.column)
                        {
                            Api.JetAddColumn(session, newTableId, item.column, new JET_COLUMNDEF
                            {
                                coltyp = JET_coltyp.Long,
                                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
                            }, null, 0, out newColumndId);
                        }
                        else
                        {
                            var defaultValue       = column.DefaultValue == null ? null : column.DefaultValue.ToArray();
                            var defaultValueLength = defaultValue == null ? 0 : defaultValue.Length;
                            Api.JetAddColumn(session, newTableId, column.Name, columnDef, defaultValue,
                                             defaultValueLength, out newColumndId);
                        }
                    }

                    foreach (var index in existingIndexes)
                    {
                        var indexDesc = String.Join("\0", index.IndexSegments.Select(x => "+" + x.ColumnName)) +
                                        "\0\0";
                        SchemaCreator.CreateIndexes(session, newTableId, new JET_INDEXCREATE()
                        {
                            szIndexName = index.Name,
                            szKey       = indexDesc,
                            grbit       = index.Grbit
                        });
                    }

                    var rows = 0;
                    using (var destTable = new Table(session, dbid, newTable, OpenTableGrbit.None))
                    {
                        Api.MoveBeforeFirst(session, sr);
                        Api.MoveBeforeFirst(session, destTable);
                        while (Api.TryMoveNext(session, sr))
                        {
                            using (var insert = new Update(session, destTable, JET_prep.Insert))
                            {
                                bool save = true;
                                foreach (var column in existingColumns)
                                {
                                    var destcolumn = Api.GetTableColumnid(session, destTable, column.Name);
                                    if (column.Name == item.column)
                                    {
                                        var viewName = Api.RetrieveColumnAsString(session, sr, column.Columnid,
                                                                                  Encoding.Unicode);
                                        int value;
                                        if (nameToIds.TryGetValue(viewName, out value) == false)
                                        {
                                            insert.Cancel();
                                            save = false;
                                            break;
                                        }
                                        Api.SetColumn(session, destTable, destcolumn, value);
                                    }
                                    else if ((column.Grbit & ColumndefGrbit.ColumnAutoincrement) == ColumndefGrbit.None)
                                    {
                                        var value = Api.RetrieveColumn(session, sr, column.Columnid);
                                        Api.SetColumn(session, destTable, destcolumn, value);
                                    }
                                }
                                if (save)
                                {
                                    insert.Save();
                                }
                            }

                            if (rows++ % 10000 == 0)
                            {
                                output("Processed " + (rows) + " rows in " + item.table);
                                Api.JetCommitTransaction(session, CommitTransactionGrbit.LazyFlush);
                                Api.JetBeginTransaction2(session, BeginTransactionGrbit.None);
                            }
                        }
                    }
                    output("Processed " + (rows - 1) + " rows in " + item.table + ", and done with this table");
                }
                Api.JetCommitTransaction(session, CommitTransactionGrbit.None);
                Api.JetDeleteTable(session, dbid, item.table);
                Api.JetRenameTable(session, dbid, newTable, item.table);
                Api.JetBeginTransaction2(session, BeginTransactionGrbit.None);
            }

            filesToDelete.ForEach(File.Delete);
            UpdateLastIdentityForIndexes(session, dbid, maxIndexId + 1);
            SchemaCreator.UpdateVersion(session, dbid, "5.0");
        }
Esempio n. 57
0
 public void Update(Session session, JET_DBID dbid, Action <string> output)
 {
     CreateListsTable(session, dbid);
     SchemaCreator.UpdateVersion(session, dbid, "3.7");
 }
Esempio n. 58
0
        private void CreateMapResultsTable(JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "mapped_results", 1024, 100, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

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

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

            Api.JetAddColumn(session, tableid, "reduce_key", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = ColumndefGrbit.None
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "reduce_key_and_view_hashed", new JET_COLUMNDEF
            {
                cbMax  = 32,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

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

            Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
            {
                cbMax  = 16,
                coltyp = JET_coltyp.Binary,
                grbit  = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            var indexDef = "+id\0\0";

            Api.JetCreateIndex(session, tableid, "by_id", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length,
                               100);

            indexDef = "+view\0+document_key\0\0";
            Api.JetCreateIndex(session, tableid, "by_view_and_doc_key", CreateIndexGrbit.IndexDisallowNull, indexDef,
                               indexDef.Length,
                               100);

            indexDef = "+view\0\0";
            Api.JetCreateIndex(session, tableid, "by_view", CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
                               100);

            indexDef = "+reduce_key_and_view_hashed\0\0";
            Api.JetCreateIndex(session, tableid, "by_reduce_key_and_view_hashed", CreateIndexGrbit.IndexDisallowNull, indexDef, indexDef.Length,
                               100);
        }
Esempio n. 59
0
        private void CreateReduceKeysStatusTable(Session session, JET_DBID dbid)
        {
            JET_TABLEID tableid;

            Api.JetCreateTable(session, dbid, "reduce_keys_status", 1, 80, out tableid);
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "view", new JET_COLUMNDEF
            {
                cbMax  = 2048,
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "reduce_key", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.LongText,
                cp     = JET_CP.Unicode,
                grbit  = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "hashed_reduce_key", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Binary,
                cbMax  = 20,
                grbit  = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp() | ColumndefGrbit.ColumnFixed
            }, null, 0, out columnid);

            Api.JetAddColumn(session, tableid, "reduce_type", new JET_COLUMNDEF
            {
                coltyp = JET_coltyp.Long,
                grbit  = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
            }, null, 0, out columnid);

            SchemaCreator.CreateIndexes(session, tableid,
                                        new JET_INDEXCREATE
            {
                szIndexName = "by_id",
                szKey       = "+id\0\0",
                grbit       = CreateIndexGrbit.IndexPrimary
            },
                                        new JET_INDEXCREATE
            {
                szIndexName = "by_view",
                szKey       = "+view\0\0",
                grbit       = CreateIndexGrbit.IndexDisallowNull
            },
                                        new JET_INDEXCREATE
            {
                szIndexName = "by_view_and_hashed_reduce_key",
                szKey       = "+view\0+hashed_reduce_key\0+reduce_key\0\0",
                grbit       = CreateIndexGrbit.IndexUnique
            });
        }
Esempio n. 60
0
        public void Update(Session session, JET_DBID dbid)
        {
            Transaction tx;

            using (tx = new Transaction(session))
            {
                using (var tbl = new Table(session, dbid, "indexes_stats",
                                           OpenTableGrbit.PermitDDL | OpenTableGrbit.DenyRead | OpenTableGrbit.DenyWrite))
                {
                    var columnDictionary = Api.GetColumnDictionary(session, tbl);

                    if (columnDictionary.ContainsKey("reduce_attempts"))
                    {
                        Api.JetDeleteColumn(session, tbl, "reduce_attempts");
                    }
                    if (columnDictionary.ContainsKey("reduce_errors"))
                    {
                        Api.JetDeleteColumn(session, tbl, "reduce_errors");
                    }
                    if (columnDictionary.ContainsKey("reduce_successes"))
                    {
                        Api.JetDeleteColumn(session, tbl, "reduce_successes");
                    }
                }

                CreateIndexesStatsReduce(session, dbid);

                using (var stats = new Table(session, dbid, "indexes_stats", OpenTableGrbit.None))
                    using (var reduce = new Table(session, dbid, "indexes_stats_reduce", OpenTableGrbit.None))
                    {
                        var tblKeyColumn = Api.GetColumnDictionary(session, stats)["key"];
                        var reduceKeyCol = Api.GetColumnDictionary(session, reduce)["key"];

                        Api.MoveBeforeFirst(session, stats);
                        while (Api.TryMoveNext(session, stats))
                        {
                            using (var update = new Update(session, reduce, JET_prep.Insert))
                            {
                                var indexName = Api.RetrieveColumnAsString(session, stats, tblKeyColumn, Encoding.Unicode);
                                Api.SetColumn(session, reduce, reduceKeyCol, indexName, Encoding.Unicode);
                                update.Save();
                            }
                        }
                    }

                tx.Commit(CommitTransactionGrbit.LazyFlush);
                tx.Dispose();
                tx = new Transaction(session);
            }

            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"], "3.4", Encoding.Unicode);

                    update.Save();
                }
            }
            tx.Commit(CommitTransactionGrbit.None);
            tx.Dispose();
        }