A class that encapsulates an update on a JET_TABLEID.
Inheritance: Microsoft.Isam.Esent.Interop.EsentResource
Exemplo n.º 1
0
		public void PutReducedResult(string view, string reduceKey, int level, int sourceBucket, int bucket, RavenJObject data)
		{
			Guid etag = uuidGenerator.CreateSequentialUuid();

			using (var update = new Update(session, ReducedResults, JET_prep.Insert))
			{
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["view"], view, Encoding.Unicode);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["level"], level);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["reduce_key"], reduceKey, Encoding.Unicode);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["hashed_reduce_key"], HashReduceKey(reduceKey));
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["bucket"], bucket);
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["source_bucket"], sourceBucket);

				using (Stream stream = new BufferedStream(new ColumnStream(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["data"])))
				{
					using (var dataStream = documentCodecs.Aggregate(stream, (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds)))
					{
						data.WriteTo(dataStream);
						dataStream.Flush();
					}
				}

				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["etag"], etag.TransformToValueForEsentSorting());
				Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["timestamp"], SystemTime.UtcNow);

				update.Save();
			}
		}
Exemplo n.º 2
0
		public void Update(Session session, JET_DBID dbid)
		{
			using (var tx = new Transaction(session))
			{
				using (var files = new Table(session, dbid, "files", OpenTableGrbit.None))
				{

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

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

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

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

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

                SchemaCreator.UpdateVersion(session, dbid, "5.3");
            }
        }
Exemplo n.º 4
0
        public void SortDataWithJetOpenTempTable()
        {
            JET_TABLEID tableid;
            var columns = new[]
            {
                new JET_COLUMNDEF { coltyp = JET_coltyp.Long, grbit = ColumndefGrbit.TTKey },
                new JET_COLUMNDEF { coltyp = JET_coltyp.Text, cp = JET_CP.Unicode },
            };
            var columnids = new JET_COLUMNID[columns.Length];

            Api.JetOpenTempTable(this.session, columns, columns.Length, TempTableGrbit.Scrollable, out tableid, columnids);

            for (int i = 5; i >= 0; --i)
            {
                using (var update = new Update(this.session, tableid, JET_prep.Insert))
                {
                    Api.SetColumn(this.session, tableid, columnids[0], i);
                    Api.SetColumn(this.session, tableid, columnids[1], i.ToString(), Encoding.Unicode);
                    update.Save();
                }
            }

            var expected = new[] { "0", "1", "2", "3", "4", "5" };
            CollectionAssert.AreEqual(expected, this.RetrieveAllRecordsAsString(tableid, columnids[1]).ToArray());
            Api.JetCloseTable(this.session, tableid);
        }
Exemplo n.º 5
0
		public void Update(Session session, JET_DBID dbid, Action<string> output)
		{
			using (var tbl = new Table(session, dbid, "lists", OpenTableGrbit.None))
			{
				JET_COLUMNID columnid;
				Api.JetAddColumn(session, tbl, "created_at", new JET_COLUMNDEF
				{
					coltyp = JET_coltyp.DateTime,
					grbit = ColumndefGrbit.ColumnMaybeNull,
				}, null, 0, out columnid);

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

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

				SchemaCreator.UpdateVersion(session, dbid, "5.1");
			}
		}
Exemplo n.º 6
0
        /// <summary>
        /// Установить размер.
        /// </summary>
        /// <param name="tt">Транзакция таблицы.</param>
        /// <param name="item">Элемент.</param>
        public void SetFileSize(TableTransaction tt, SizeCacheEsentItem item)
        {
            Api.JetSetCurrentIndex(tt.Session, tt.Table, null);
            Api.MakeKey(tt.Session, tt.Table, item.FileId, Encoding.Unicode, MakeKeyGrbit.NewKey);
            if (Api.TrySeek(tt.Session, tt.Table, SeekGrbit.SeekEQ))
            {
                Api.JetDelete(tt.Session, tt.Table);
            }
            using (var updater = new Update(tt.Session, tt.Table, JET_prep.Insert))
            {
                var columnId = Api.GetTableColumnid(tt.Session, tt.Table, "FileId");
                Api.SetColumn(tt.Session, tt.Table, columnId, item.FileId, Encoding.Unicode);

                var columnSize = Api.GetTableColumnid(tt.Session, tt.Table, "Size");
                Api.SetColumn(tt.Session, tt.Table, columnSize, item.Size);

                var columnDTicks = Api.GetTableColumnid(tt.Session, tt.Table, "DTicks");
                Api.SetColumn(tt.Session, tt.Table, columnDTicks, item.DTicks);

                var columnOTicks = Api.GetTableColumnid(tt.Session, tt.Table, "OTicks");
                Api.SetColumn(tt.Session, tt.Table, columnOTicks, item.OTicks);

                updater.Save();
            }
        }
Exemplo n.º 7
0
        public void Update(Session session, JET_DBID dbid)
        {
            using (var tx = new Transaction(session))
            {
                using (var tasks = new Table(session, dbid, "tasks", OpenTableGrbit.None))
                {
                    JET_COLUMNID columnid;
                    Api.JetAddColumn(session, tasks, "added_at",new JET_COLUMNDEF
                    {
                        coltyp = JET_coltyp.DateTime,
                        grbit = ColumndefGrbit.ColumnNotNULL
                    }, null, 0, out columnid);

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

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

                            update.Save();
                        }
                    }
                }
                tx.Commit(CommitTransactionGrbit.None);
            }
        }
Exemplo n.º 8
0
		public void Set(string name, string key, RavenJObject data, UuidType uuidType)
		{
			Api.JetSetCurrentIndex(session, Lists, "by_name_and_key");
			Api.MakeKey(session, Lists, name, Encoding.Unicode, MakeKeyGrbit.NewKey);
			Api.MakeKey(session, Lists, key, Encoding.Unicode, MakeKeyGrbit.None);

			var exists = Api.TrySeek(session, Lists, SeekGrbit.SeekEQ);


			using (var update = new Update(session, Lists, exists ? JET_prep.Replace : JET_prep.Insert))
			{
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["name"], name, Encoding.Unicode);
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["key"], key, Encoding.Unicode);
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["etag"], uuidGenerator.CreateSequentialUuid(uuidType).TransformToValueForEsentSorting());
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["created_at"], SystemTime.UtcNow);

				using (var columnStream = new ColumnStream(session, Lists, tableColumnsCache.ListsColumns["data"]))
				{
					if (exists)
						columnStream.SetLength(0);
					using (Stream stream = new BufferedStream(columnStream))
					{
						data.WriteTo(stream);
						stream.Flush();
					}
				}
				update.Save();
			}
		}
Exemplo n.º 9
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();
            }
        }
Exemplo n.º 10
0
		public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data)
		{
			Etag etag = uuidGenerator.CreateSequentialUuid(UuidType.MappedResults);
			using (var update = new Update(session, MappedResults, JET_prep.Insert))
			{
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["view"], view, Encoding.Unicode);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["document_key"], docId, Encoding.Unicode);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["reduce_key"], reduceKey, Encoding.Unicode);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["hashed_reduce_key"], HashReduceKey(reduceKey));
				var mapBucket = IndexingUtil.MapBucket(docId);
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["bucket"], mapBucket);

				using (Stream stream = new BufferedStream(new ColumnStream(session, MappedResults, tableColumnsCache.MappedResultsColumns["data"])))
				{
					using (var dataStream = documentCodecs.Aggregate(stream, (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds)))
					{
						data.WriteTo(dataStream);
						dataStream.Flush();
					}
				}

				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["etag"], etag.TransformToValueForEsentSorting());
				Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["timestamp"], SystemTime.UtcNow.ToBinary());

				update.Save();
			}
		}
Exemplo n.º 11
0
		public Etag AddAttachment(string key, Etag etag, Stream data, RavenJObject headers)
		{
			Api.JetSetCurrentIndex(session, Files, "by_name");
			Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey);
			var isUpdate = Api.TrySeek(session, Files, SeekGrbit.SeekEQ);
			if (isUpdate)
			{
				var existingEtag = Etag.Parse(Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]));
				if (existingEtag != etag && etag != null)
				{
					throw new ConcurrencyException("PUT attempted on attachment '" + key +
						"' using a non current etag")
					{
						ActualETag = existingEtag,
						ExpectedETag = etag
					};
				}
			}
			else
			{
				if (data == null)
					throw new InvalidOperationException("When adding new attachment, the attachment data must be specified");

				if (Api.TryMoveFirst(session, Details))
					Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["attachment_count"], 1);
			}

			Etag newETag = uuidGenerator.CreateSequentialUuid(UuidType.Attachments);
			using (var update = new Update(session, Files, isUpdate ? JET_prep.Replace : JET_prep.Insert))
			{
				Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], key, Encoding.Unicode);
				if (data != null)
				{
					long written;
					using (var columnStream = new ColumnStream(session, Files, tableColumnsCache.FilesColumns["data"]))
					{
						if (isUpdate)
							columnStream.SetLength(0);
						using (var stream = new BufferedStream(columnStream))
						{
							data.CopyTo(stream);
							written = stream.Position;
							stream.Flush();
						}
					}
					if (written == 0) // empty attachment
					{
						Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["data"], new byte[0]);
					}
				}

				Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], newETag.TransformToValueForEsentSorting());
				Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], headers.ToString(Formatting.None), Encoding.Unicode);

				update.Save();
			}
			logger.Debug("Adding attachment {0}", key);

			return newETag;
		}
Exemplo n.º 12
0
        public Guid AddDocument(string key, Guid? etag, JObject data, JObject metadata)
        {
            Api.JetSetCurrentIndex(session, Documents, "by_key");
            Api.MakeKey(session, Documents, key, Encoding.Unicode, MakeKeyGrbit.NewKey);
            var isUpdate = Api.TrySeek(session, Documents, SeekGrbit.SeekEQ);
            if (isUpdate)
            {
                EnsureNotLockedByTransaction(key, null);
                EnsureDocumentEtagMatch(key, etag, "PUT");
            }
            else
            {
                EnsureDocumentIsNotCreatedInAnotherTransaction(key, Guid.NewGuid());
                if (Api.TryMoveFirst(session, Details))
                    Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["document_count"], 1);
            }
            Guid newEtag = DocumentDatabase.CreateSequentialUuid();

            using (var update = new Update(session, Documents, isUpdate ? JET_prep.Replace : JET_prep.Insert))
            {
                Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["key"], key, Encoding.Unicode);
                Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["data"], Encoding.UTF8.GetBytes(data.ToString()));
                Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["etag"], newEtag.ToByteArray());
                Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["metadata"], Encoding.UTF8.GetBytes(metadata.ToString()));

                update.Save();
            }

            logger.DebugFormat("Inserted a new document with key '{0}', update: {1}, ",
                               key, isUpdate);

            return newEtag;
        }
Exemplo n.º 13
0
        public void Update(Session session, JET_DBID dbid, Action<string> output)
        {
            using (var table = new Table(session, dbid, "config", OpenTableGrbit.DenyRead | OpenTableGrbit.PermitDDL))
            {
                JET_COLUMNID newMetadataColumnId;

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

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

                var rows = 0;

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

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

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

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

                        insert.Save();
                    }

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

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

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

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

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


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

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

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

                            update.Save();
                        }
                    }


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

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

                            update.Save();
                        }
                    }
                }
                tx.Commit(CommitTransactionGrbit.None);
            }
        }
Exemplo n.º 15
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();
		}
Exemplo n.º 16
0
        public void JetRetrieveColumns()
        {
            short s = Any.Int16;
            string str = Any.String;
            double d = Any.Double;

            var setcolumns = new[]
            {
                new JET_SETCOLUMN { cbData = sizeof(short), columnid = this.columnDict["Int16"], pvData = BitConverter.GetBytes(s) },
                new JET_SETCOLUMN { cbData = sizeof(double), columnid = this.columnDict["Double"], pvData = BitConverter.GetBytes(d) },
                new JET_SETCOLUMN { cbData = str.Length * sizeof(char), columnid = this.columnDict["Unicode"], pvData = Encoding.Unicode.GetBytes(str) },
                new JET_SETCOLUMN { cbData = 0, columnid = this.columnDict["Binary"], pvData = null },
            };

            using (var trx = new Transaction(this.session))
            using (var update = new Update(this.session, this.tableid, JET_prep.Insert))
            {
                Api.JetSetColumns(this.session, this.tableid, setcolumns, setcolumns.Length);
                update.Save();
                trx.Commit(CommitTransactionGrbit.None);
            }

            Api.TryMoveFirst(this.session, this.tableid);

            var retrievecolumns = new[]
            {
                new JET_RETRIEVECOLUMN { cbData = sizeof(short), columnid = this.columnDict["Int16"], pvData  = new byte[sizeof(short)] },
                new JET_RETRIEVECOLUMN { cbData = sizeof(double), columnid = this.columnDict["Double"], pvData = new byte[sizeof(double)] },
                new JET_RETRIEVECOLUMN { cbData = str.Length * sizeof(char) * 2, columnid = this.columnDict["Unicode"], pvData = new byte[str.Length * sizeof(char) * 2] },
                new JET_RETRIEVECOLUMN { cbData = 10, columnid = this.columnDict["Binary"], pvData = new byte[10] },
            };

            for (int i = 0; i < retrievecolumns.Length; ++i)
            {
                retrievecolumns[i].itagSequence = 1;
            }

            Api.JetRetrieveColumns(this.session, this.tableid, retrievecolumns, retrievecolumns.Length);

            // retrievecolumns[0] = short
            Assert.AreEqual(sizeof(short), retrievecolumns[0].cbActual);
            Assert.AreEqual(JET_wrn.Success, retrievecolumns[0].err);
            Assert.AreEqual(s, BitConverter.ToInt16(retrievecolumns[0].pvData, 0));

            // retrievecolumns[1] = double
            Assert.AreEqual(sizeof(double), retrievecolumns[1].cbActual);
            Assert.AreEqual(JET_wrn.Success, retrievecolumns[1].err);
            Assert.AreEqual(d, BitConverter.ToDouble(retrievecolumns[1].pvData, 0));

            // retrievecolumns[2] = string
            Assert.AreEqual(str.Length * sizeof(char), retrievecolumns[2].cbActual);
            Assert.AreEqual(JET_wrn.Success, retrievecolumns[2].err);
            Assert.AreEqual(str, Encoding.Unicode.GetString(retrievecolumns[2].pvData, 0, retrievecolumns[2].cbActual));

            // retrievecolumns[3] = null
            Assert.AreEqual(0, retrievecolumns[3].cbActual);
            Assert.AreEqual(JET_wrn.ColumnNull, retrievecolumns[3].err);
        }
Exemplo n.º 17
0
        public void AddIndex(string name)
        {
            using (var update = new Update(session, IndexesStats, JET_prep.Insert))
            {
                Api.SetColumn(session, IndexesStats, tableColumnsCache.IndexesStatsColumns["key"], name, Encoding.Unicode);

                update.Save();
            }
        }
Exemplo n.º 18
0
        public void Update(Session session, JET_DBID dbid)
        {
            Transaction tx;
            using (tx = new Transaction(session))
            {
                var count = 0;
                const int rowsInTxCount = 100;
                var tablesWithEtags = new[]
                {
                    new {Table = "indexes_stats", Column = "last_indexed_etag"},
                    new {Table = "documents", Column = "etag"},
                    new {Table = "mapped_results", Column = "etag"},
                    new {Table = "files", Column = "etag"},
                    new {Table = "documents_modified_by_transaction", Column = "etag"},
                };
                foreach (var tablesWithEtag in tablesWithEtags)
                {
                    using (var tbl = new Table(session, dbid, tablesWithEtag.Table, OpenTableGrbit.None))
                    {
                        var columnid = Api.GetColumnDictionary(session, tbl)[tablesWithEtag.Column];
                        Api.MoveBeforeFirst(session, tbl);
                        while (Api.TryMoveNext(session, tbl))
                        {
                            using (var update = new Update(session, tbl, JET_prep.Replace))
                            {
                                Api.SetColumn(session, tbl, columnid, Guid.Empty.TransformToValueForEsentSorting());
                                update.Save();
                            }
                            if (count++%rowsInTxCount == 0)
                            {
                                tx.Commit(CommitTransactionGrbit.LazyFlush);
                                tx.Dispose();
                                tx = new Transaction(session);
                            }
                        }
                        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.2", Encoding.Unicode);

                        update.Save();
                    }
                }
                tx.Commit(CommitTransactionGrbit.None);
                tx.Dispose();
            }
        }
Exemplo n.º 19
0
		public void AddIndex(string name)
		{
			using (var update = new Update(session, IndexesStats, JET_prep.Insert))
			{
				Api.SetColumn(session, IndexesStats, tableColumnsCache.IndexesStatsColumns["key"], name, Encoding.Unicode);
				Api.SetColumn(session, IndexesStats, tableColumnsCache.IndexesStatsColumns["last_indexed_etag"], Guid.Empty.TransformToValueForEsentSorting());
				Api.SetColumn(session, IndexesStats, tableColumnsCache.IndexesStatsColumns["last_indexed_timestamp"], DateTime.MinValue);
				update.Save();
			}
		}
Exemplo n.º 20
0
        public void EnqueueToQueue(string name, byte[] data)
        {
            using (var update = new Update(session, Queue, JET_prep.Insert))
            {
                Api.SetColumn(session, Queue, tableColumnsCache.QueueColumns["name"], name, Encoding.Unicode);
                Api.SetColumn(session, Queue, tableColumnsCache.QueueColumns["data"], data);

                update.Save();
            }
        }
            protected void PrepareUpdate(JET_prep mode)
            {
                Contract.ThrowIfFalse(_table != null);

                if (_updater != null)
                {
                    _updater.Dispose();
                }

                _updater = new Update(_session.SessionId, TableId, mode);
            }
Exemplo n.º 22
0
        /// <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>
        public static void CreateGlobalsTable(Session session, JET_DBID dbid)
        {
            JET_TABLEID tableid;
            JET_COLUMNID primaryKeyColumnid;
            JET_COLUMNID countColumnid;

            Api.JetCreateTable(session, dbid, GlobalsTableName, 1, 100, out tableid);
            byte[] defaultValue = BitConverter.GetBytes(0);

            Api.JetAddColumn(
                session,
                tableid,
                GlobalsPrimaryKeyColumnName,
                new JET_COLUMNDEF {coltyp = JET_coltyp.Long},
                null,
                0,
                out primaryKeyColumnid);

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

            string indexKey = string.Format(CultureInfo.InvariantCulture, "+{0}\0\0", GlobalsPrimaryKeyColumnName);
            JET_INDEXCREATE[] indexcreates = new[]
                {
                    new JET_INDEXCREATE
                        {
                            cbKeyMost = SystemParameters.KeyMost,
                            grbit = CreateIndexGrbit.IndexPrimary,
                            szIndexName = "by_id",
                            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);

            using (var update = new Update(session, tableid, JET_prep.Insert))
            {
                Api.SetColumn(session, tableid, countColumnid, 0);
                update.Save();
            }

            Api.JetCloseTable(session, tableid);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Updates the specified row with the specified value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="prep">The type of update.</param>
        private void UpdateRow(TKey key, TValue value, ESENT.JET_prep prep)
        {
            using (var update = new ESENT.Update(this.Session, this.Table, prep))
            {
                PersistentTablePool <TKey, TValue> .Converters.SetKeyColumn(this.Session, this.Table, this.KeyColumn, key);

                PersistentTablePool <TKey, TValue> .Converters.SetValueColumn(this.Session, this.Table, this.ValueColumn, value);

                update.Save();
            }
        }
Exemplo n.º 24
0
		public void Update(Session session, JET_DBID dbid)
		{
			// those actions are no longer required, and are actually removed on the next version
			// also, they don't work on non empty tables, so we skip them
			//using (tx = new Transaction(session))
			//{
			//    using (var tbl = new Table(session, dbid, "indexes_stats", OpenTableGrbit.PermitDDL | OpenTableGrbit.DenyRead|OpenTableGrbit.DenyWrite))
			//    {
			//        JET_COLUMNID columnid;
			//        var defaultValue = BitConverter.GetBytes(0);
			//        Api.JetAddColumn(session, tbl, "reduce_attempts", new JET_COLUMNDEF
			//        {
			//            coltyp = JET_coltyp.Long,
			//            grbit =
			//                ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
			//        }, defaultValue, defaultValue.Length, out columnid);

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

			//        Api.JetAddColumn(session, tbl, "reduce_successes", new JET_COLUMNDEF
			//        {
			//            coltyp = JET_coltyp.Long,
			//            grbit =
			//                ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnEscrowUpdate
			//        }, defaultValue, defaultValue.Length, out columnid);
			//    }
			//    tx.Commit(CommitTransactionGrbit.LazyFlush);
			//    tx.Dispose();
			//    tx = new Transaction(session);
			//}

			using (var 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.3", Encoding.Unicode);

						update.Save();
					}
				}
				tx.Commit(CommitTransactionGrbit.None);
			}
		}
Exemplo n.º 25
0
		public void Update(Session session, JET_DBID dbid)
		{
			using (var tx = new Transaction(session))
			{
				using (var indexStats = new Table(session, dbid, "indexes_stats", OpenTableGrbit.None))
				using (var documents = new Table(session, dbid, "documents", OpenTableGrbit.None))
				using (var documentsInTx = new Table(session, dbid, "documents_modified_by_transaction", OpenTableGrbit.None))
				{
					var defaultValue = Guid.Empty.ToByteArray();
					JET_COLUMNID columnid;
					Api.JetAddColumn(session, indexStats, "last_indexed_etag", new JET_COLUMNDEF
					{
						coltyp = JET_coltyp.Binary,
						cbMax = 16,
						grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
					}, defaultValue, defaultValue.Length, out columnid);

					defaultValue = BitConverter.GetBytes(SystemTime.UtcNow.AddSeconds(-1).ToOADate());

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

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

					Api.JetAddColumn(session, documentsInTx, "last_modified", new JET_COLUMNDEF
					{
						coltyp = JET_coltyp.DateTime,
						grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL,
					}, defaultValue, defaultValue.Length, 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.6", Encoding.Unicode);

							update.Save();
						}
					}
				}
				tx.Commit(CommitTransactionGrbit.None);
			}
		}
Exemplo n.º 26
0
        public void Update(Session session, JET_DBID dbid, Action<string> output)
        {
            using (var table = new Table(session, dbid, "pages", OpenTableGrbit.DenyRead | OpenTableGrbit.PermitDDL))
            {
                JET_COLUMNID newDataColumnId;

                Api.JetAddColumn(session, table, "data_new", new JET_COLUMNDEF
                {
                    cbMax = 4 * StorageConstants.MaxPageSize, // handle possible data expansion because of codecs usage
                    coltyp = JET_coltyp.LongBinary,
                    grbit = ColumndefGrbit.ColumnMaybeNull
                }, null, 0, out newDataColumnId);
            }
            
            using (var table = new Table(session, dbid, "pages", OpenTableGrbit.None))
            {
                Api.MoveBeforeFirst(session, table);

                var dataColumnId = Api.GetTableColumnid(session, table, "data");
                var newDataColumnId = Api.GetTableColumnid(session, table, "data_new");

                var rows = 0;

                while (Api.TryMoveNext(session, table))
                {
                    using (var insert = new Update(session, table, JET_prep.Replace))
                    {
                        var value = Api.RetrieveColumn(session, table, dataColumnId);
                        Api.SetColumn(session, table, newDataColumnId, value);

                        insert.Save();
                    }

                    if (rows++ % 1000 == 0)
                    {
                        output("Processed " + (rows) + " rows from data column in pages 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, "data");
                Api.JetRenameColumn(session, table, "data_new", "data", RenameColumnGrbit.None);
                
                Api.JetBeginTransaction2(session, BeginTransactionGrbit.None);
            }

            SchemaCreator.UpdateVersion(session, dbid, "0.4");
        }
Exemplo n.º 27
0
		private void EnsureTransactionExists(TransactionInformation transactionInformation)
		{
			Api.JetSetCurrentIndex(session, Transactions, "by_tx_id");
			Api.MakeKey(session, Transactions, transactionInformation.Id.ToByteArray(), MakeKeyGrbit.NewKey);
			var isUpdate = Api.TrySeek(session, Transactions, SeekGrbit.SeekEQ);
			using (var update = new Update(session, Transactions, isUpdate ? JET_prep.Replace : JET_prep.Insert))
			{
				Api.SetColumn(session, Transactions, tableColumnsCache.TransactionsColumns["tx_id"], transactionInformation.Id.ToByteArray());
				Api.SetColumn(session, Transactions, tableColumnsCache.TransactionsColumns["timeout"],
							  DateTime.UtcNow + transactionInformation.Timeout);
				update.Save();
			}
		}
Exemplo n.º 28
0
		public void Update(Session session, JET_DBID dbid, Action<string> output)
		{
			var tableAndColumns = new[]
			{
				new {Table = "indexes_stats", Column = "last_indexed_timestamp"},
				new {Table = "indexes_stats_reduce", Column = "last_reduced_timestamp"},
				new {Table = "transactions", Column = "timeout"},
				new {Table = "documents", Column = "last_modified"},
				new {Table = "documents_modified_by_transaction", Column = "last_modified"},
				new {Table = "scheduled_reductions", Column = "timestamp"},
				new {Table = "scheduled_reductions", Column = "timestamp"},
				new {Table = "mapped_results", Column = "timestamp"},
				new {Table = "reduce_results", Column = "timestamp"},
				new {Table = "tasks", Column = "added_at"},
			};

			int rows = 0;
			foreach (var tableAndColumn in tableAndColumns)
			{
				using (var table = new Table(session, dbid, tableAndColumn.Table, OpenTableGrbit.None))
				{
					Api.MoveBeforeFirst(session, table);
					while (Api.TryMoveNext(session, table))
					{
						var columnid = Api.GetTableColumnid(session, table, tableAndColumn.Column);
						using (var update = new Update(session, table, JET_prep.Replace))
						{
							var bytes = Api.RetrieveColumn(session, table, columnid);
							var date = DateTime.FromOADate(BitConverter.ToDouble(bytes, 0));
							Api.SetColumn(session, table, columnid, date.ToBinary());
							update.Save();
						}

						if (rows++ % 10000 == 0)
						{
							output("Processed " + (rows - 1) + " rows in " + tableAndColumn.Table);
							continue;
						}

						// pulsing transaction
						Api.JetCommitTransaction(session, CommitTransactionGrbit.None);
						Api.JetBeginTransaction2(session, BeginTransactionGrbit.None);
					}
				}

				output("Finished processing " + tableAndColumn.Table);
			}

			SchemaCreator.UpdateVersion(session, dbid, "4.3");
		}
Exemplo n.º 29
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))
				{
					JET_COLUMNID columnid;
					var defaultValue = BitConverter.GetBytes(0);
					Api.JetAddColumn(session, tbl, "reduce_attempts", new JET_COLUMNDEF
					{
						coltyp = JET_coltyp.Long,
						grbit =
							ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnEscrowUpdate | ColumndefGrbit.ColumnNotNULL
					}, defaultValue, defaultValue.Length, out columnid);

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

					Api.JetAddColumn(session, tbl, "reduce_successes", new JET_COLUMNDEF
					{
						coltyp = JET_coltyp.Long,
						grbit =
							ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnEscrowUpdate
					}, defaultValue, defaultValue.Length, out columnid);
				}
				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.3", Encoding.Unicode);

					update.Save();
				}
			}
			tx.Commit(CommitTransactionGrbit.None);
			tx.Dispose();
		}
Exemplo n.º 30
0
        public void CreateQueueIfDoesNotExists(string queueName)
        {
            Api.MakeKey(session, queues, queueName, Encoding.Unicode, MakeKeyGrbit.NewKey);
            if (Api.TrySeek(session, queues, SeekGrbit.SeekEQ))
                return;

            new QueueSchemaCreator(session, dbid, queueName).Create();
            using (var updateQueue = new Update(session, queues, JET_prep.Insert))
            {
                Api.SetColumn(session, queues, queuesColumns["name"], queueName, Encoding.Unicode);
                Api.SetColumn(session, queues, queuesColumns["created_at"], DateTime.Now.ToOADate());
                updateQueue.Save();
            }
        }
Exemplo n.º 31
0
		public static void UpdateSchemaVersion(this Session session, JET_DBID dbid, string schemaVersion)
		{
			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"], schemaVersion, Encoding.Unicode);

					update.Save();
				}
			}
		}