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(); } }
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; }
public void ShrinkColumnStream() { var bookmark = new byte[SystemParameters.BookmarkMost]; int bookmarkSize; const int Length = 1345; var data = Any.BytesOfLength(Length); using (var transaction = new Transaction(this.sesid)) using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert)) using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText)) { stream.Write(data, 0, data.Length); stream.Write(data, 0, data.Length); Assert.AreEqual(Length * 2, stream.Length); stream.SetLength(Length); Assert.AreEqual(Length, stream.Length); update.Save(bookmark, bookmark.Length, out bookmarkSize); transaction.Commit(CommitTransactionGrbit.LazyFlush); } Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize); using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText)) { Assert.AreEqual(Length, stream.Length); var buffer = new byte[Length]; stream.Read(buffer, 0, buffer.Length); CollectionAssert.AreEqual(data, buffer); } }
public void SetColumnStreamToZeroLength() { var bookmark = new byte[SystemParameters.BookmarkMost]; int bookmarkSize; using (var transaction = new Transaction(this.sesid)) using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert)) using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText)) { byte[] data = Any.Bytes; stream.Write(data, 0, data.Length); stream.SetLength(0); Assert.AreEqual(0, stream.Length); update.Save(bookmark, bookmark.Length, out bookmarkSize); transaction.Commit(CommitTransactionGrbit.LazyFlush); } Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize); CollectionAssert.AreEqual(new byte[0], Api.RetrieveColumn(this.sesid, this.tableid, this.columnidLongText)); }
public void SetColumnStreamLength() { var bookmark = new byte[SystemParameters.BookmarkMost]; int bookmarkSize; const long Length = 1345; using (var transaction = new Transaction(this.sesid)) using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert)) using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText)) { stream.SetLength(Length); Assert.AreEqual(Length, stream.Length); update.Save(bookmark, bookmark.Length, out bookmarkSize); transaction.Commit(CommitTransactionGrbit.LazyFlush); } Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize); using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText)) { Assert.AreEqual(Length, stream.Length); } }
public void ColumnStreamSetLengthThrowsExceptionWhenLengthIsTooLong() { using (var t = new Transaction(this.sesid)) using (var u = new Update(this.sesid, this.tableid, JET_prep.Insert)) using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText)) { stream.SetLength(0x800000000); } }