public void TestMapBsonBinaryData() { var value = new BsonBinaryData(new byte[] { 1, 2, 3 }); var bsonValue = (BsonBinaryData) BsonTypeMapper.MapToBsonValue(value); Assert.AreSame(value, bsonValue); var bsonBinary = (BsonBinaryData) BsonTypeMapper.MapToBsonValue(value, BsonType.Binary); Assert.AreSame(value, bsonBinary); }
public void TestBsonBinaryDataEquals() { var a = new BsonBinaryData(new byte[] { 1, 2, 3 }); var b = new BsonBinaryData(new byte[] { 1, 2, 3 }); var c = new BsonBinaryData(new byte[] { 2, 3, 4 }); var n = (BsonBinaryData)null; Assert.IsTrue(object.Equals(a, b)); Assert.IsFalse(object.Equals(a, c)); Assert.IsFalse(object.Equals(a, BsonNull.Value)); Assert.IsFalse(a.Equals(n)); Assert.IsFalse(a.Equals(null)); Assert.IsTrue(a == b); Assert.IsFalse(a == c); Assert.IsFalse(a == BsonNull.Value); Assert.IsFalse(a == null); Assert.IsFalse(null == a); Assert.IsTrue(n == null); Assert.IsTrue(null == n); Assert.IsFalse(a != b); Assert.IsTrue(a != c); Assert.IsTrue(a != BsonNull.Value); Assert.IsTrue(a != null); Assert.IsTrue(null != a); Assert.IsFalse(n != null); Assert.IsFalse(null != n); }
public void TestBsonBinaryDataEquals() { BsonBinaryData lhs = new BsonBinaryData(new byte[] { 1, 2, 3 }, BsonBinarySubType.Binary); BsonBinaryData rhs = new BsonBinaryData(new byte[] { 1, 2, 3 }, BsonBinarySubType.Binary); Assert.AreNotSame(lhs, rhs); Assert.AreEqual(lhs, rhs); Assert.AreEqual(lhs.GetHashCode(), rhs.GetHashCode()); }
public void BsonBinaryReader_should_support_reading_more_than_2GB() { RequireEnvironmentVariable.IsDefined("EXPLICIT"); var binaryData = new BsonBinaryData(new byte[1024 * 1024]); var tempFileName = Path.GetTempFileName(); try { using (var stream = new FileStream(tempFileName, FileMode.Open)) { using (var binaryWriter = new BsonBinaryWriter(stream)) { while (stream.Position < (long)int.MaxValue * 4) { binaryWriter.WriteStartDocument(); binaryWriter.WriteName("x"); binaryWriter.WriteBinaryData(binaryData); binaryWriter.WriteEndDocument(); } } var endOfFilePosition = stream.Position; stream.Position = 0; using (var binaryReader = new BsonBinaryReader(stream)) { while (!binaryReader.IsAtEndOfFile()) { binaryReader.ReadStartDocument(); var bookmark = binaryReader.GetBookmark(); binaryReader.ReadName("x"); binaryReader.ReturnToBookmark(bookmark); binaryReader.ReadName("x"); var readBinaryData = binaryReader.ReadBinaryData(); Assert.Equal(binaryData.Bytes.Length, readBinaryData.Bytes.Length); binaryReader.ReadEndDocument(); } } Assert.Equal(endOfFilePosition, stream.Position); } } finally { try { File.Delete(tempFileName); } catch { // ignore exceptions } } }
public void TestGuidJavaLegacy() { var guid = new Guid("01020304-0506-0708-090a-0b0c0d0e0f10"); var binaryData = new BsonBinaryData(guid, GuidRepresentation.JavaLegacy); var expected = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1, 16, 15, 14, 13, 12, 11, 10, 9 }; Assert.IsTrue(expected.SequenceEqual(binaryData.Bytes)); Assert.AreEqual(BsonBinarySubType.UuidLegacy, binaryData.SubType); Assert.AreEqual(GuidRepresentation.JavaLegacy, binaryData.GuidRepresentation); Assert.AreEqual(guid, binaryData.AsGuid); Assert.AreEqual(guid, binaryData.RawValue); }
public void TestGuidBigEndian() { var guid = new Guid("01020304-0506-0708-090a-0b0c0d0e0f10"); var binaryData = new BsonBinaryData(guid, GuidByteOrder.BigEndian); var expected = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; Assert.IsTrue(expected.SequenceEqual(binaryData.Bytes)); Assert.AreEqual(BsonBinarySubType.Uuid, binaryData.SubType); Assert.AreEqual(GuidByteOrder.BigEndian, binaryData.GuidByteOrder); Assert.AreEqual(guid, binaryData.AsGuid); Assert.AreEqual(guid, binaryData.RawValue); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="binaryData">The binary data.</param> public override void WriteBinaryData(BsonBinaryData binaryData) { if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } WriteValue(binaryData); State = GetNextState(); }
public void TestGuidPythonLegacy() { var guid = new Guid("01020304-0506-0708-090a-0b0c0d0e0f10"); var binaryData = new BsonBinaryData(guid, GuidRepresentation.PythonLegacy); var expected = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; Assert.IsTrue(expected.SequenceEqual(binaryData.Bytes)); Assert.AreEqual(BsonBinarySubType.UuidLegacy, binaryData.SubType); Assert.AreEqual(GuidRepresentation.PythonLegacy, binaryData.GuidRepresentation); Assert.AreEqual(guid, binaryData.AsGuid); #pragma warning disable 618 Assert.AreEqual(guid, binaryData.RawValue); #pragma warning restore }
public Session(string id, string applicationName, int timeout, BsonBinaryData sessionItems, int sessionItemsCount, SessionStateActions actionFlags) { DateTime now = DateTime.Now; SessionID = id; ApplicationName = applicationName; LockDate = now; LockID = 0; Timeout = timeout; Locked = false; SessionItems = sessionItems; SessionItemsCount = sessionItemsCount; Flags = (int)actionFlags; Created = now; Expires = now.AddMinutes((Double)this.Timeout); }
public void TestBsonBinaryData() { var value = new BsonBinaryData(new byte[] { 1, 2 }); Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(value)); Assert.Throws<InvalidCastException>(() => Convert.ToByte(value)); Assert.Throws<InvalidCastException>(() => Convert.ToChar(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDecimal(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDouble(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt16(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt32(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt64(value)); Assert.Throws<InvalidCastException>(() => Convert.ToSByte(value)); Assert.Throws<InvalidCastException>(() => Convert.ToSingle(value)); Assert.Throws<InvalidCastException>(() => Convert.ToString(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt16(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt32(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt64(value)); }
public void BackpatchSize_should_throw_when_size_is_larger_than_2GB() { using (var stream = new NullBsonStream()) using (var binaryWriter = new BsonBinaryWriter(stream)) { var bytes = new byte[int.MaxValue / 2]; // 1GB var binaryData = new BsonBinaryData(bytes); binaryWriter.WriteStartDocument(); binaryWriter.WriteName("array"); binaryWriter.WriteStartArray(); binaryWriter.WriteBinaryData(binaryData); binaryWriter.WriteBinaryData(binaryData); Action action = () => binaryWriter.WriteEndArray(); // indirectly calls private BackpatchSize method action.ShouldThrow<FormatException>(); } }
public void TestBsonBinaryData() { var value = new BsonBinaryData(new byte[] { 1, 2 }); Assert.AreSame(value, ((IConvertible)value).ToType(typeof(object), null)); Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(value)); Assert.Throws<InvalidCastException>(() => Convert.ToByte(value)); Assert.Throws<InvalidCastException>(() => Convert.ToChar(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDecimal(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDouble(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt16(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt32(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt64(value)); Assert.Throws<InvalidCastException>(() => Convert.ToSByte(value)); Assert.Throws<InvalidCastException>(() => Convert.ToSingle(value)); Assert.Throws<InvalidCastException>(() => Convert.ToString(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt16(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt32(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt64(value)); }
public override void CreateUninitializedItem(HttpContext context, string id, int timeout) { byte[] serializedItems = new byte[0]; BsonBinaryData sessionItems = new BsonBinaryData(serializedItems); Session session = new Session(id, this._applicationName, timeout, sessionItems, 0, SessionStateActions.InitializeItem); var sessionStore = new SessionStore(_applicationName, _connectionString); try { sessionStore.Insert(session); } catch (Exception e) { if (WriteExceptionsToEventLog) { WriteToEventLog(e, "CreateUninitializedItem"); throw new ProviderException(e.Message, e.InnerException); } else throw e; } }
private async void saveScript_Click(object sender, EventArgs e) { if (!IsEmpty(scriptIdValue.Text) && !IsEmpty(scriptValue.Text) && !ScriptError) { byte[] scriptBytes = System.Text.Encoding.ASCII.GetBytes(scriptValue.Text); BsonBinaryData scriptArray = new BsonBinaryData(scriptBytes); Triggers.Script newScript = new LuaScript() { ID = scriptIdValue.Text, ScriptByteArray = scriptBytes, ScriptType = (ScriptTypes)Enum.Parse(typeof(ScriptTypes), scriptTypeValue.SelectedItem.ToString()) }; var collection = MongoUtils.MongoData.GetCollection<Triggers.Script>("Scripts", (string)scriptTypesValue.SelectedItem); await collection.ReplaceOneAsync<Triggers.Script>(s => s.ID == scriptIdValue.Text, newScript, new UpdateOptions { IsUpsert = true }); scriptValidatedValue.Visible = false; } else if (ScriptError) { DisplayErrorBox("Script file contains errors. Test script before saving."); } }
public void BsonBinaryWriter_should_support_writing_more_than_2GB() { using (var stream = new NullBsonStream()) using (var binaryWriter = new BsonBinaryWriter(stream)) { var bigBinaryData = new BsonBinaryData(new byte[int.MaxValue / 2 - 1000]); for (var i = 0; i < 3; i++) { binaryWriter.WriteStartDocument(); binaryWriter.WriteName("x"); binaryWriter.WriteBinaryData(bigBinaryData); binaryWriter.WriteEndDocument(); } var smallBinaryData = new BsonBinaryData(new byte[2000]); binaryWriter.WriteStartDocument(); binaryWriter.WriteName("x"); binaryWriter.WriteBinaryData(smallBinaryData); binaryWriter.WriteEndDocument(); } }
private void saveScript_Click(object sender, EventArgs e) { if (!IsEmpty(scriptIdValue.Text) && !IsEmpty(scriptValue.Text) && !ScriptError) { byte[] scriptBytes = System.Text.Encoding.ASCII.GetBytes(scriptValue.Text); BsonBinaryData scriptArray = new BsonBinaryData(scriptBytes); BsonDocument doc = new BsonDocument(); doc.Add("_id", scriptIdValue.Text); doc.Add(new BsonElement("Bytes", scriptArray.AsBsonValue)); MongoCollection collection = MongoUtils.MongoData.GetCollection("Scripts", (string)scriptTypesValue.SelectedItem); collection.Save(doc); scriptValidatedValue.Visible = false; } else if (ScriptError) { DisplayErrorBox("Script file contains errors. Test script before saving."); } }
public Pilgrim(string fullName, string pilgrimId, string userId, string nationality, string residency, BsonBinaryData fpData, double HCredit, string phone) { _id = new ObjectId(); this.fullName = fullName; this.pilgrimId = pilgrimId; this.userId = userId; this.nationality = nationality; this.residency = residency; this.fingerprintData = fpData; this.HCredit = HCredit; this.phone = phone; }
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) { var data = new BsonBinaryData(value.OfType <Guid>(), GuidRepresentation.Standard); context.Writer.WriteBinaryData(data); }
public void SaveOrUpdate <TEntity>(TEntity entity) where TEntity : class, IEntity, new() { var id = (int)entity.TryGetValue("Id"); if (GetById <TEntity>(id) == null) { Save(entity); return; } var update = new UpdateBuilder(); foreach (var property in typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(r => !r.Name.EqualsWithInvariant("Id"))) { var value = property.GetValue(entity, null); BsonValue bsonValue = BsonNull.Value; if (value != null) { var type = (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) ? property.PropertyType.GetGenericArguments()[0] : property.PropertyType; if (type == typeof(string)) { bsonValue = new BsonString(value.ToString()); } else if (type == typeof(bool)) { bsonValue = new BsonBoolean((bool)value); } else if (type == typeof(DateTime)) { bsonValue = new BsonDateTime((DateTime)value); } else if (type == typeof(long)) { bsonValue = new BsonInt64((long)value); } else if (type == typeof(int)) { bsonValue = new BsonInt32((int)value); } else if (type == typeof(byte[])) { bsonValue = new BsonBinaryData((byte[])value); } else if (type == typeof(Guid)) { bsonValue = new BsonBinaryData((Guid)value); } else if (type.IsEnum) { bsonValue = new BsonString(value.ToString()); } else if (type.IsImplement <IEnumerable>()) { bsonValue = new BsonArray((IEnumerable)value); } else if (type.IsClass && type.IsImplement <IEntity>()) { bsonValue = new BsonDocumentWrapper(value); } else { throw new ArgumentOutOfRangeException("propertyType {0} does not bson value".F(type)); } } update.Set(property.Name, bsonValue); } GetCollection <TEntity>().Update(MongoDB.Driver.Builders.Query <TEntity> .EQ(r => r.Id, id), update); }
private void AddMissingChunks() { var query = Query.EQ("files_id", fileInfo.Id); var fields = Fields.Include("n"); var chunkCount = (int) ((length + fileInfo.ChunkSize - 1) / fileInfo.ChunkSize); var chunksFound = new HashSet<int>(); var foundExtraChunks = false; foreach (var chunk in gridFS.Chunks.Find(query).SetFields(fields)) { var n = chunk["n"].ToInt32(); chunksFound.Add(n); if (n >= chunkCount) { foundExtraChunks = true; } } if (foundExtraChunks) { var extraChunksQuery = Query.And( Query.EQ("files_id", fileInfo.Id), Query.GTE("n", chunkCount) ); gridFS.Chunks.Remove(extraChunksQuery); } BsonBinaryData zeros = null; // delay creating it until it's actually needed for (var n = 0; n < chunkCount; n++) { if (!chunksFound.Contains(n)) { if (zeros == null) { zeros = new BsonBinaryData(new byte[fileInfo.ChunkSize]); } var missingChunk = new BsonDocument { { "_id", ObjectId.GenerateNewId() }, { "files_id", fileInfo.Id }, { "n", n }, { "data", zeros } }; gridFS.Chunks.Insert(missingChunk); } } }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="binaryData">The binary data.</param> public override void WriteBinaryData(BsonBinaryData binaryData) { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } var bytes = binaryData.Bytes; var subType = binaryData.SubType; var guidRepresentation = binaryData.GuidRepresentation; switch (subType) { case BsonBinarySubType.OldBinary: if (_binaryWriterSettings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } break; case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: if (_binaryWriterSettings.GuidRepresentation != GuidRepresentation.Unspecified) { var expectedSubType = (_binaryWriterSettings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; if (subType != expectedSubType) { var message = string.Format( "The GuidRepresentation for the writer is {0}, which requires the subType argument to be {1}, not {2}.", _binaryWriterSettings.GuidRepresentation, expectedSubType, subType); throw new BsonSerializationException(message); } if (guidRepresentation != _binaryWriterSettings.GuidRepresentation) { var message = string.Format( "The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.", _binaryWriterSettings.GuidRepresentation, guidRepresentation); throw new BsonSerializationException(message); } } break; } _buffer.WriteByte((byte)BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) _buffer.WriteInt32(bytes.Length + 4); _buffer.WriteByte((byte)subType); _buffer.WriteInt32(bytes.Length); } else { _buffer.WriteInt32(bytes.Length); _buffer.WriteByte((byte)subType); } _buffer.WriteBytes(bytes); State = GetNextState(); }
/// <summary> /// Serializes a value. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="args">The serialization args.</param> /// <param name="value">The object.</param> public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) { var bsonWriter = context.Writer; if (value == null) { bsonWriter.WriteNull(); } else { var actualType = value.GetType(); if (actualType == typeof(object)) { bsonWriter.WriteStartDocument(); bsonWriter.WriteEndDocument(); } else { // certain types can be written directly as BSON value // if we're not at the top level document, or if we're using the JsonWriter if (bsonWriter.State == BsonWriterState.Value || bsonWriter is JsonWriter) { switch (Type.GetTypeCode(actualType)) { case TypeCode.Boolean: bsonWriter.WriteBoolean((bool)value); return; case TypeCode.DateTime: // TODO: is this right? will lose precision after round trip var bsonDateTime = new BsonDateTime(BsonUtils.ToUniversalTime((DateTime)value)); bsonWriter.WriteDateTime(bsonDateTime.MillisecondsSinceEpoch); return; case TypeCode.Double: bsonWriter.WriteDouble((double)value); return; case TypeCode.Int16: // TODO: is this right? will change type to Int32 after round trip bsonWriter.WriteInt32((short)value); return; case TypeCode.Int32: bsonWriter.WriteInt32((int)value); return; case TypeCode.Int64: bsonWriter.WriteInt64((long)value); return; case TypeCode.Object: if (actualType == typeof(Guid)) { var guid = (Guid)value; var guidRepresentation = bsonWriter.Settings.GuidRepresentation; var binaryData = new BsonBinaryData(guid, guidRepresentation); bsonWriter.WriteBinaryData(binaryData); return; } if (actualType == typeof(ObjectId)) { bsonWriter.WriteObjectId((ObjectId)value); return; } break; case TypeCode.String: bsonWriter.WriteString((string)value); return; } } SerializeDiscriminatedValue(context, args, value, actualType); } } }
/// <summary> /// [Beta] Decrypts the specified value. /// </summary> /// <param name="value">The value.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The decrypted value.</returns> public BsonValue Decrypt(BsonBinaryData value, CancellationToken cancellationToken) { return(_libMongoCryptController.DecryptField(value, cancellationToken)); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="binaryData">The binary data.</param> public abstract void WriteBinaryData(BsonBinaryData binaryData);
/// <summary> /// 获得值的表示文字 /// </summary> /// <param name="mElement"></param> /// <returns></returns> private static string GetDisplayString(ref BsonElement mElement) { string strColumnText; try { strColumnText = mElement.Value.ToString(); if (mElement.Value.IsObjectId) { var oid = mElement.Value.AsObjectId; strColumnText = oid.ToString(); return(strColumnText); } //日期型处理 if (mElement.Value.IsValidDateTime || mElement.Value.IsBsonTimestamp) { DateTime datetime; if (mElement.Value.IsBsonTimestamp) { BsonTimestamp bsonTime = mElement.Value.AsBsonTimestamp; datetime = new DateTime(1970, 1, 1).AddSeconds(bsonTime.Timestamp); if (IsUtc) { datetime = datetime.ToUniversalTime(); } else { datetime = datetime.ToLocalTime(); } } else { if (IsUtc) { datetime = mElement.Value.AsBsonDateTime.ToUniversalTime(); } else { datetime = mElement.Value.AsBsonDateTime.ToLocalTime(); } } switch (DateTimeFormat) { case DateTimePickerFormat.Long: strColumnText = datetime.ToLongDateString(); break; case DateTimePickerFormat.Short: strColumnText = datetime.ToShortDateString(); break; case DateTimePickerFormat.Time: strColumnText = datetime.ToShortTimeString(); break; case DateTimePickerFormat.Custom: strColumnText = datetime.ToString(DateTimeCustomFormat); break; default: break; } return(strColumnText); } //数字型处理 if (mElement.Value.IsNumeric) { if (IsDisplayNumberWithKSystem) { if (mElement.Value.IsInt32) { strColumnText = Utility.GetKSystemInt32(mElement.Value.AsInt32); } if (mElement.Value.IsInt64) { strColumnText = Utility.GetKSystemInt64(mElement.Value.AsInt64); } if (mElement.Value.IsDouble) { strColumnText = Utility.GetKSystemDouble(mElement.Value.AsDouble); } } else { strColumnText = mElement.Value.ToString(); } return(strColumnText); } if (mElement.Value.IsBsonBinaryData) { BsonBinaryData bin = mElement.Value.AsBsonBinaryData; strColumnText = "Byte[" + bin.Bytes.Length + "](Binary)"; return(strColumnText); } } catch (Exception) { strColumnText = mElement.Value.ToString(); } return(strColumnText); }
public static BsonValue Create(this BsonType bsonType, object o) { BsonValue value = BsonNull.Value; try { switch (bsonType) { case BsonType.EndOfDocument: break; case BsonType.Double: value = BsonDouble.Create(o); break; case BsonType.String: value = BsonString.Create(o); break; case BsonType.Document: value = BsonDocument.Create(o); break; case BsonType.Array: value = BsonArray.Create(o); break; case BsonType.Binary: value = BsonBinaryData.Create(o); break; case BsonType.Undefined: break; case BsonType.ObjectId: value = BsonObjectId.Create(o); break; case BsonType.Boolean: value = BsonBoolean.Create(o); break; case BsonType.DateTime: value = BsonDateTime.Create(o); break; case BsonType.Null: value = BsonNull.Value; break; case BsonType.RegularExpression: value = BsonRegularExpression.Create(o); break; case BsonType.JavaScript: value = BsonJavaScript.Create(o); break; case BsonType.Symbol: value = BsonSymbol.Create(o); break; case BsonType.JavaScriptWithScope: value = BsonJavaScriptWithScope.Create(o); break; case BsonType.Int32: value = BsonInt32.Create(o); break; case BsonType.Timestamp: value = BsonTimestamp.Create(o); break; case BsonType.Int64: value = BsonInt64.Create(o); break; case BsonType.MaxKey: value = BsonValue.Create(o); break; case BsonType.MinKey: value = BsonValue.Create(o); break; } } catch { } return(value); }
/// <summary> /// Writes a BSON binary data element to the writer. /// </summary> /// <param name="name">The name of the element.</param> /// <param name="binaryData">The binary data.</param> public void WriteBinaryData(string name, BsonBinaryData binaryData) { WriteName(name); WriteBinaryData(binaryData); }
private static BsonDocument CreateEncryptMetadata(string keyIdBase64) { var keyId = new BsonBinaryData(Convert.FromBase64String(keyIdBase64), BsonBinarySubType.UuidStandard); return(new BsonDocument("keyId", new BsonArray(new[] { keyId }))); }
/// <summary> /// Writes a BSON binary data element to the writer. /// </summary> /// <param name="writer">The writer.</param> /// <param name="name">The name of the element.</param> /// <param name="binaryData">The binary data.</param> public static void WriteBinaryData(this IBsonWriter writer, string name, BsonBinaryData binaryData) { writer.WriteName(name); writer.WriteBinaryData(binaryData); }
/// <summary> /// 各种基本类型的初始值 /// </summary> /// <param name="DataType"></param> /// <returns></returns> public static BsonValue GetInitValue(BsonValueEx.BasicType DataType) { BsonValue InitValue = BsonNull.Value; switch (DataType) { case BasicType.BsonString: InitValue = new BsonString(string.Empty); break; case BasicType.BsonInt32: InitValue = new BsonInt32(0); break; case BasicType.BsonInt64: InitValue = new BsonInt64(0); break; case BasicType.BsonDecimal128: InitValue = new BsonDecimal128(0); break; case BasicType.BsonDouble: InitValue = new BsonDouble(0); break; case BasicType.BsonDateTime: InitValue = new BsonDateTime(DateTime.Now); break; case BasicType.BsonBoolean: InitValue = BsonBoolean.False; break; case BasicType.BsonArray: InitValue = new BsonArray(); break; case BasicType.BsonDocument: InitValue = new BsonDocument(); break; case BasicType.BsonLegacyPoint: InitValue = new BsonArray() { 0, 0 }; break; case BasicType.BsonGeoJSON: InitValue = new BsonDocument("type", "Point"); InitValue.AsBsonDocument.Add("coordinates", new BsonArray() { 0, 0 }); break; case BasicType.BsonMaxKey: InitValue = BsonMaxKey.Value; break; case BasicType.BsonMinKey: InitValue = BsonMinKey.Value; break; case BasicType.BsonBinary: InitValue = new BsonBinaryData(new byte[0]); break; default: break; } return(InitValue); }
public void CorpusTest( [Values(false, true)] bool useLocalSchema, [Values(false, true)] bool async) { RequireServer.Check().Supports(Feature.ClientSideEncryption); var corpusSchema = JsonFileReader.Instance.Documents["corpus.corpus-schema.json"]; var schemaMap = useLocalSchema ? new BsonDocument("db.coll", corpusSchema) : null; using (var client = ConfigureClient()) using (var clientEncrypted = ConfigureClientEncrypted(schemaMap)) using (var clientEncryption = ConfigureClientEncryption(clientEncrypted.Wrapped as MongoClient)) { CreateCollection(client, __collCollectionNamespace, new BsonDocument("$jsonSchema", corpusSchema)); var corpusKeyLocal = JsonFileReader.Instance.Documents["corpus.corpus-key-local.json"]; var corpusKeyAws = JsonFileReader.Instance.Documents["corpus.corpus-key-aws.json"]; var keyVaultCollection = GetCollection(client, __keyVaultCollectionNamespace); Insert(keyVaultCollection, async, corpusKeyLocal, corpusKeyAws); var corpus = JsonFileReader.Instance.Documents["corpus.corpus.json"]; var corpusCopied = new BsonDocument { corpus.GetElement("_id"), corpus.GetElement("altname_aws"), corpus.GetElement("altname_local") }; foreach (var corpusElement in corpus.Elements.Where(c => c.Value.IsBsonDocument)) { var corpusValue = corpusElement.Value.DeepClone(); var kms = corpusValue["kms"].AsString; var abbreviatedAlgorithmName = corpusValue["algo"].AsString; var identifier = corpusValue["identifier"].AsString; var allowed = corpusValue["allowed"].ToBoolean(); var value = corpusValue["value"]; var method = corpusValue["method"].AsString; switch (method) { case "auto": corpusCopied.Add(corpusElement); continue; case "explicit": { var encryptionOptions = CreateEncryptOptions(abbreviatedAlgorithmName, identifier, kms); BsonBinaryData encrypted = null; var exception = Record.Exception(() => { encrypted = ExplicitEncrypt( clientEncryption, encryptionOptions, value, async); }); if (allowed) { exception.Should().BeNull(); encrypted.Should().NotBeNull(); corpusValue["value"] = encrypted; } else { exception.Should().NotBeNull(); } corpusCopied.Add(new BsonElement(corpusElement.Name, corpusValue)); } break; default: throw new ArgumentException($"Unsupported method name {method}.", nameof(method)); } } var coll = GetCollection(clientEncrypted, __collCollectionNamespace); Insert(coll, async, corpusCopied); var corpusDecrypted = Find(coll, new BsonDocument(), async).Single(); corpusDecrypted.Should().Be(corpus); var corpusEncryptedExpected = JsonFileReader.Instance.Documents["corpus.corpus-encrypted.json"]; coll = GetCollection(client, __collCollectionNamespace); var corpusEncryptedActual = Find(coll, new BsonDocument(), async).Single(); foreach (var expectedElement in corpusEncryptedExpected.Elements.Where(c => c.Value.IsBsonDocument)) { var expectedElementValue = expectedElement.Value; var expectedAlgorithm = ParseAlgorithm(expectedElementValue["algo"].AsString); var expectedAllowed = expectedElementValue["allowed"].ToBoolean(); var expectedValue = expectedElementValue["value"]; var actualValue = corpusEncryptedActual.GetValue(expectedElement.Name)["value"]; switch (expectedAlgorithm) { case EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic: actualValue.Should().Be(expectedValue); break; case EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random: if (expectedAllowed) { actualValue.Should().NotBe(expectedValue); } break; default: throw new ArgumentException($"Unsupported expected algorithm {expectedAllowed}.", nameof(expectedAlgorithm)); } if (expectedAllowed) { var actualDecryptedValue = ExplicitDecrypt(clientEncryption, actualValue.AsBsonBinaryData, async); var expectedDecryptedValue = ExplicitDecrypt(clientEncryption, expectedValue.AsBsonBinaryData, async); actualDecryptedValue.Should().Be(expectedDecryptedValue); } else { actualValue.Should().Be(expectedValue); } } } EncryptOptions CreateEncryptOptions(string algorithm, string identifier, string kms) { Guid? keyId = null; string alternateName = null; if (identifier == "id") { switch (kms) { case "local": keyId = GuidConverter.FromBytes(Convert.FromBase64String("LOCALAAAAAAAAAAAAAAAAA=="), GuidRepresentation.Standard); break; case "aws": keyId = GuidConverter.FromBytes(Convert.FromBase64String("AWSAAAAAAAAAAAAAAAAAAA=="), GuidRepresentation.Standard); break; default: throw new ArgumentException($"Unsupported kms type {kms}."); } } else if (identifier == "altname") { alternateName = kms; } else { throw new ArgumentException($"Unsupported identifier {identifier}.", nameof(identifier)); } return(new EncryptOptions(ParseAlgorithm(algorithm).ToString(), alternateName, keyId)); } EncryptionAlgorithm ParseAlgorithm(string algorithm) { switch (algorithm) { case "rand": return(EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random); case "det": return(EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic); default: throw new ArgumentException($"Unsupported algorithm {algorithm}."); } } }
// private methods private void AddMissingChunks() { using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance)) { var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings); var database = gridFS.GetDatabase(ReadPreference.Primary); var chunksCollection = gridFS.GetChunksCollection(database); var query = Query.EQ("files_id", _fileInfo.Id); var fields = Fields.Include("n"); var chunkCount = (_length + _fileInfo.ChunkSize - 1) / _fileInfo.ChunkSize; var chunksFound = new HashSet<long>(); var foundExtraChunks = false; foreach (var chunk in chunksCollection.Find(query).SetFields(fields)) { var n = chunk["n"].ToInt64(); chunksFound.Add(n); if (n >= chunkCount) { foundExtraChunks = true; } } if (foundExtraChunks) { var extraChunksQuery = Query.And(Query.EQ("files_id", _fileInfo.Id), Query.GTE("n", chunkCount)); chunksCollection.Remove(extraChunksQuery); } BsonBinaryData zeros = null; // delay creating it until it's actually needed for (var n = 0L; n < chunkCount; n++) { if (!chunksFound.Contains(n)) { if (zeros == null) { zeros = new BsonBinaryData(new byte[_fileInfo.ChunkSize]); } var missingChunk = new BsonDocument { { "_id", ObjectId.GenerateNewId() }, { "files_id", _fileInfo.Id }, { "n", (n < int.MaxValue) ? (BsonValue)new BsonInt32((int)n) : new BsonInt64(n) }, { "data", zeros } }; chunksCollection.Insert(missingChunk); } } } }
public void TestCreateNull() { object obj = null; Assert.Throws <ArgumentNullException>(() => { BsonBinaryData.Create(obj); }); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="binaryData">The binary data.</param> public override void WriteBinaryData(BsonBinaryData binaryData) { if (Disposed) { throw new ObjectDisposedException("JsonWriter"); } if (State != BsonWriterState.Value && State != BsonWriterState.Initial) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value, BsonWriterState.Initial); } var subType = binaryData.SubType; var bytes = binaryData.Bytes; var guidRepresentation = binaryData.GuidRepresentation; WriteNameHelper(Name); switch (_jsonWriterSettings.OutputMode) { case JsonOutputMode.Strict: _textWriter.Write("{{ \"$binary\" : \"{0}\", \"$type\" : \"{1}\" }}", Convert.ToBase64String(bytes), ((int)subType).ToString("x2")); break; case JsonOutputMode.Shell: default: switch (subType) { case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: _textWriter.Write(GuidToString(subType, bytes, guidRepresentation)); break; default: _textWriter.Write("new BinData({0}, \"{1}\")", (int)subType, Convert.ToBase64String(bytes)); break; } break; } State = GetNextState(); }
public void TestClassWithBsonValueId() { // repeats all tee TestClassWithBsonXyzId tests using ClassWithBsonValueId { // same as TestClassWithBonArrayId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = new BsonArray(), X = 1 }; Assert.Throws <MongoSafeModeException>(() => { collection.Insert(doc); }); doc = new ClassWithBsonValueId { Id = new BsonArray { 1, 2, 3 }, X = 1 }; Assert.Throws <MongoSafeModeException>(() => { collection.Insert(doc); }); } { // same as TestClastWithBsonBinaryDataId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonBinaryData.Create(new byte[] { }), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonBinaryData.Create(new byte[] { 1, 2, 3 }), X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonBooleanId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonBoolean.Create(false), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonBoolean.Create(true), X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonDocumentId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = new BsonDocument(), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = new BsonDocument { { "A", 1 }, { "B", 2 } }, X = 3 }; collection.Insert(doc); } { // same as TestClassWithBsonDateTimeId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonDateTime.Create(DateTime.MinValue), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonDateTime.Create(DateTime.UtcNow), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonDateTime.Create(DateTime.MaxValue), X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonDoubleId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonDouble.Create(0.0), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonDouble.Create(1.0), X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonInt32Id collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonInt32.Create(0), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonInt32.Create(1), X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonInt64Id collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonInt64.Create(0), X = 1 }; collection.Insert(doc); doc = new ClassWithBsonValueId { Id = BsonInt64.Create(1), X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonMaxKeyId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); Assert.AreEqual(null, doc.Id); doc = new ClassWithBsonValueId { Id = BsonMaxKey.Value, X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonMinKeyId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); Assert.AreEqual(null, doc.Id); doc = new ClassWithBsonValueId { Id = BsonMinKey.Value, X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonNullId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); Assert.AreEqual(null, doc.Id); doc = new ClassWithBsonValueId { Id = BsonNull.Value, X = 1 }; Assert.Throws <MongoSafeModeException>(() => { collection.Insert(doc); }); } { // same as TestClassWithBsonObjectId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); Assert.IsNull(doc.Id); // BsonObjectIdGenerator is not invoked when nominalType is BsonValue doc = new ClassWithBsonValueId { Id = BsonObjectId.Empty, X = 1 }; collection.Insert(doc); Assert.AreEqual(ObjectId.Empty, doc.Id.AsObjectId); // BsonObjectIdGenerator is not invoked when nominalType is BsonValue doc = new ClassWithBsonValueId { Id = BsonObjectId.GenerateNewId(), X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonStringId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); Assert.IsNull(doc.Id); doc = new ClassWithBsonValueId { Id = "", X = 1 }; collection.Insert(doc); Assert.AreEqual("", doc.Id.AsString); doc = new ClassWithBsonValueId { Id = "123", X = 1 }; collection.Insert(doc); } { // same as TestClassWithBsonTimestampId collection.RemoveAll(); var doc = new ClassWithBsonValueId { Id = null, X = 1 }; collection.Insert(doc); Assert.IsNull(doc.Id); doc = new ClassWithBsonValueId { Id = BsonTimestamp.Create(0, 0), X = 1 }; collection.Insert(doc); Assert.AreEqual(BsonTimestamp.Create(0, 0), doc.Id); doc = new ClassWithBsonValueId { Id = BsonTimestamp.Create(1, 2), X = 1 }; collection.Insert(doc); } }
public void WriteBinaryData_should_have_expected_result() { var subject = CreateSubject(); var value = new BsonBinaryData(new byte[] { 0 }, BsonBinarySubType.UserDefined); WriteNested(subject, () => subject.WriteBinaryData(value)); AssertBsonEquals(subject, "{ x : { $binary : \"AA==\", $type : \"80\" } }"); }
/// <summary> /// [Beta] Decrypts the specified value. /// </summary> /// <param name="value">The value.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The decrypted value.</returns> public Task <BsonValue> DecryptAsync(BsonBinaryData value, CancellationToken cancellationToken) { return(_libMongoCryptController.DecryptFieldAsync(value, cancellationToken)); }
public async Task <BsonDocument> AppendAsync(string symbol, DataFrame df, int chunksize = 0, bool skipAlreadyWrittenDates = true) { if (df.Index == null) { throw new ArgumentException("Please specify DataFrame.Index column before saving"); } if (chunksize > 0 && df.Rows.Count > chunksize) { var rng = Range.R(0, chunksize); BsonDocument ver = null; int chunkscount = 0; while (rng.First < df.Rows.Count) { var chunk = df[rng]; ver = await AppendAsync(symbol, chunk); rng = Range.R(rng.First + chunksize, rng.Last + chunksize); chunkscount++; } return(ver); } int attemptNo = 0; for (;;) { var previous_version = await ReadVersionAsync(symbol); var version = await GetNewVersion(symbol, previous_version); /*var previous_version = await (_versions.AsQueryable () * .Where (v => v ["symbol"] == symbol && v ["version"] < version ["version"]) * .OrderByDescending (v => v ["version"]) as IAsyncCursorSource<BsonDocument>) * .FirstOrDefaultAsync ();*/ var dtype = version.GetValue("dtype", "").ToString(); Log.Debug("loaded dtype {0}", dtype); if (dtype != "" && df.DType.ToString() != dtype) { // dtype changed. need reload old data and repack it. throw new NotImplementedException("old dtype {0}, new dtype {1}: not implemented".Args(dtype, df.DType)); } var sdt = df.DType.ToString(); version["metadata"] = df.Metadata; version["dtype"] = sdt; Log.Debug("saved dtype {0}", sdt); version["shape"] = new BsonArray { { -1 } }; version["dtype_metadata"] = new BsonDocument { { "index", new BsonArray { { df.Index.Name } } }, { "columns", new BsonArray(df.Columns.Select(c => c.Name).ToList()) } }; version["type"] = "pandasdf"; version["segment_count"] = previous_version != null ? previous_version["segment_count"].AsInt32 + 1 : 1; version["append_count"] = previous_version != null ? previous_version["append_count"].AsInt32 + 1 : 0; var seg_ind_buf = new ByteBuffer(); int segment_offset = 0; bool is_date_time_index = DType.DateTime64.ToString().Equals(df.Index.DType.ToString()); //version ["base_sha"] = version ["sha"]; if (previous_version != null) { var seg_ind = previous_version["segment_index"].AsByteArray; seg_ind_buf.AppendDecompress(seg_ind); segment_offset = previous_version["up_to"].AsInt32; if (is_date_time_index && skipAlreadyWrittenDates) { long date = seg_ind_buf.Read <long>(seg_ind_buf.Length - 16); DateTime dt = DateTime64.ToDateTime(date); var range = df.Index.AsDateTime().RangeOf(dt, 0, df.FilledCount - 1, Location.GT); if (range.Last <= range.First) { Log.Information($"Skipped DataFrame.Append because date {dt} already written for {symbol}"); return(null); // Hey all was skipped } else if (range.First != 0) { Log.Information($"Skipped DataFrame.Append initial {range.First} elements date {dt} already written for {symbol}"); } df = df[range]; } } var up_to = segment_offset + df.Rows.Count; var buf = new ByteBuffer(); // add index that is last datetime + 0-based int64 index of last appended record like (segment_count-1) if (is_date_time_index && df.Rows.Count > 0) { var date = df.Index.AsDateTime().Source[-1]; seg_ind_buf.Append <long>(date); seg_ind_buf.Append <long>(up_to - 1); } var seg_ind_buf2 = new ByteBuffer(); seg_ind_buf2.AppendCompress(seg_ind_buf.GetBytes()); version["segment_index"] = seg_ind_buf2.GetBytes(); version["up_to"] = up_to; var bin = df.ToBuffer(); buf.AppendCompress(bin); var sha1 = SHA1.Create(); var sha = version.GetValue("sha", null); if (sha == null) { byte[] hashBytes = sha1.ComputeHash(bin); version["sha"] = new BsonBinaryData(hashBytes); } #if false var buf2 = new ByteBuffer(); buf2.AppendDecompress(buf.GetBytes()); var bin2 = buf2.GetBytes(); if (!bin.SequenceEqual(bin2)) { throw new InvalidOperationException(); } var df2 = DataFrame.FromBuffer(bin2, df.DType, df.Rows.Count); #endif var segment = new BsonDocument { { "symbol", symbol }, { "data", new BsonBinaryData(buf.GetBytes()) }, { "compressed", true }, { "segment", segment_offset + df.Rows.Count - 1 }, { "parent", new BsonArray { version["_id"] } }, }; var hash = new ByteBuffer(); hash.Append(Encoding.ASCII.GetBytes(symbol)); foreach (var key in segment.Names.OrderByDescending(x => x)) { var value = segment.GetValue(key); if (value is BsonBinaryData) { hash.Append(value.AsByteArray); } else { var str = value.ToString(); hash.Append(Encoding.ASCII.GetBytes(str)); } } segment["sha"] = sha1.ComputeHash(hash.GetBytes()); //await _versions.InsertOneAsync(version); try { await _versions.ReplaceOneAsync(BF.Eq("symbol", symbol), version, Upsert); }catch (MongoWriteException e) { Log.Information("Retrying append symbol {symbol}, attempt {attemptNo}", symbol, attemptNo++); continue; } await _segments.InsertOneAsync(segment); //Log.Information ("inserted new segment {segment} for symbol {symbol}", segment["_id"], symbol); //Log.Information ("replaced version {0} for symbol {symbol} sha1 {sha}", version["_id"], symbol, sha); // update parents in versions //var res = await _segments.UpdateManyAsync (BF.Eq("symbol", symbol), BU.Set ("parent", new BsonArray{ version ["_id"] })); //Log.Debug ("updated segments parents {0}".Args(res.MatchedCount)); return(version); } }
private void SaveChunk() { var lastChunkIndex = (int) ((length + fileInfo.ChunkSize - 1) / fileInfo.ChunkSize) - 1; if (chunkIndex == -1 || chunkIndex > lastChunkIndex) { var message = string.Format("Invalid chunk index: {0}", chunkIndex); throw new MongoGridFSException(message); } var lastChunkSize = (int) (length % fileInfo.ChunkSize); if (lastChunkSize == 0) { lastChunkSize = fileInfo.ChunkSize; } BsonBinaryData data; if (chunkIndex < lastChunkIndex || lastChunkSize == fileInfo.ChunkSize) { data = new BsonBinaryData(chunk); } else { var lastChunk = new byte[lastChunkSize]; Buffer.BlockCopy(chunk, 0, lastChunk, 0, lastChunkSize); data = new BsonBinaryData(lastChunk); } var query = Query.EQ("_id", chunkId); var update = new UpdateDocument { { "_id", chunkId }, { "files_id", fileInfo.Id }, { "n", chunkIndex }, { "data", data } }; gridFS.Chunks.Update(query, update, UpdateFlags.Upsert); chunkIsDirty = false; }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="binaryData">The binary data.</param> public override void WriteBinaryData(BsonBinaryData binaryData) { if (Disposed) { throw new ObjectDisposedException("JsonWriter"); } if (State != BsonWriterState.Value && State != BsonWriterState.Initial) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value, BsonWriterState.Initial); } var subType = binaryData.SubType; var bytes = binaryData.Bytes; var guidRepresentation = binaryData.GuidRepresentation; if (_jsonWriterSettings.OutputMode == JsonOutputMode.Shell) { WriteNameHelper(Name); switch (subType) { case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: if (bytes.Length != 16) { var message = string.Format("Length of binary subtype {0} must be 16, not {1}.", subType, bytes.Length); throw new ArgumentException(message); } if (subType == BsonBinarySubType.UuidLegacy && guidRepresentation == GuidRepresentation.Standard) { throw new ArgumentException("GuidRepresentation for binary subtype UuidLegacy must not be Standard."); } if (subType == BsonBinarySubType.UuidStandard && guidRepresentation != GuidRepresentation.Standard) { var message = string.Format("GuidRepresentation for binary subtype UuidStandard must be Standard, not {0}.", guidRepresentation); throw new ArgumentException(message); } if (_jsonWriterSettings.ShellVersion >= new Version(2, 0, 0)) { if (guidRepresentation == GuidRepresentation.Unspecified) { var s = BsonUtils.ToHexString(bytes); var parts = new string[] { s.Substring(0, 8), s.Substring(8, 4), s.Substring(12, 4), s.Substring(16, 4), s.Substring(20, 12) }; _textWriter.Write("HexData({0}, \"{1}\")", (int)subType, string.Join("-", parts)); } else { string uuidConstructorName; switch (guidRepresentation) { case GuidRepresentation.CSharpLegacy: uuidConstructorName = "CSUUID"; break; case GuidRepresentation.JavaLegacy: uuidConstructorName = "JUUID"; break; case GuidRepresentation.PythonLegacy: uuidConstructorName = "PYUUID"; break; case GuidRepresentation.Standard: uuidConstructorName = "UUID"; break; default: throw new BsonInternalException("Unexpected GuidRepresentation"); } var guid = GuidConverter.FromBytes(bytes, guidRepresentation); _textWriter.Write("{0}(\"{1}\")", uuidConstructorName, guid.ToString()); } } else { _textWriter.Write("new BinData({0}, \"{1}\")", (int)subType, Convert.ToBase64String(bytes)); } break; default: _textWriter.Write("new BinData({0}, \"{1}\")", (int)subType, Convert.ToBase64String(bytes)); break; } } else { WriteStartDocument(); WriteString("$binary", Convert.ToBase64String(bytes)); WriteString("$type", ((int)subType).ToString("x2")); WriteEndDocument(); } State = GetNextState(); }
/// <summary> /// 使用属性会发生一些MONO上的移植问题 /// </summary> /// <returns></returns> public BsonValue GetValue(BsonValueEx.BasicType DataType) { BsonValue mValue = null; switch (DataType) { case BsonValueEx.BasicType.BsonString: mValue = new BsonString(txtBsonValue.Text); break; case BsonValueEx.BasicType.BsonInt32: mValue = new BsonInt32(Convert.ToInt32(NumberPick.Value)); break; case BsonValueEx.BasicType.BsonInt64: mValue = new BsonInt64(Convert.ToInt64(NumberPick.Value)); break; case BsonValueEx.BasicType.BsonDecimal128: mValue = new BsonDecimal128(Convert.ToDecimal(NumberPick.Value)); break; case BsonValueEx.BasicType.BsonDouble: mValue = new BsonDouble(Convert.ToDouble(txtBsonValue.Text)); break; case BsonValueEx.BasicType.BsonDateTime: mValue = new BsonDateTime(dateTimePicker.Value); break; case BsonValueEx.BasicType.BsonBoolean: mValue = radTrue.Checked ? BsonBoolean.True : BsonBoolean.False; break; case BsonValueEx.BasicType.BsonArray: case BsonValueEx.BasicType.BsonLegacyPoint: mValue = _mBsonArray; break; case BsonValueEx.BasicType.BsonGeoJSON: case BsonValueEx.BasicType.BsonDocument: mValue = _mBsonDocument; break; case BsonValueEx.BasicType.BsonMaxKey: mValue = BsonMaxKey.Value; break; case BsonValueEx.BasicType.BsonMinKey: mValue = BsonMinKey.Value; break; case BsonValueEx.BasicType.BsonBinary: mValue = new BsonBinaryData(Encoding.Default.GetBytes(txtBsonValue.Text)); break; } return mValue; }
public void WriteBinaryData(BsonBinaryData value) { SetWriteState(Newtonsoft.Json.JsonToken.Bytes, null); _wrappedWriter.WriteBinaryData(value); }
private static BsonBinaryData ConvertBinaryData( BsonBinaryData binary ) { if (binary.SubType == BsonBinarySubType.Uuid || binary.SubType == BsonBinarySubType.UuidLegacy) { if (binary.GuidRepresentation != toRepresentation) { var guid = binary.ToGuid(); return new BsonBinaryData(guid, toRepresentation); } } return binary; }
private void SaveChunk() { using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance)) { var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings); var database = gridFS.GetDatabase(ReadPreference.Primary); var chunksCollection = gridFS.GetChunksCollection(database); var lastChunkIndex = (_length + _fileInfo.ChunkSize - 1) / _fileInfo.ChunkSize - 1; if (_chunkIndex == -1 || _chunkIndex > lastChunkIndex) { var message = string.Format("Invalid chunk index {0}.", _chunkIndex); throw new MongoGridFSException(message); } var lastChunkSize = (int)(_length % _fileInfo.ChunkSize); if (lastChunkSize == 0) { lastChunkSize = _fileInfo.ChunkSize; } BsonBinaryData data; if (_chunkIndex < lastChunkIndex || lastChunkSize == _fileInfo.ChunkSize) { data = new BsonBinaryData(_chunk); } else { var lastChunk = new byte[lastChunkSize]; Buffer.BlockCopy(_chunk, 0, lastChunk, 0, lastChunkSize); data = new BsonBinaryData(lastChunk); } var query = Query.EQ("_id", _chunkId); var update = new UpdateDocument { { "_id", _chunkId }, { "files_id", _fileInfo.Id }, { "n", (_chunkIndex < int.MaxValue) ? (BsonValue)new BsonInt32((int)_chunkIndex) : new BsonInt64(_chunkIndex) }, { "data", data } }; chunksCollection.Update(query, update, UpdateFlags.Upsert); _chunkIsDirty = false; } }
public void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { var binaryData = new BsonBinaryData(bytes, subType, guidRepresentation); WriteBinaryData(binaryData); }
public TestClass( BsonBinaryData value ) { this.B = value; this.V = value; }
public async Task <Auth> GetById(Guid id) { var binaryId = new BsonBinaryData(id, GuidRepresentation.Standard); return((await MongoCollection.FindAsync(new BsonDocument("_id", binaryId))).FirstOrDefault()); }
public void SerializeValue_should_convert_representation_when_required( [ClassValues(typeof(GuidModeValues))] GuidMode mode, [Values( GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation writerGuidRepresentation, [Values( GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation valueGuidRepresentation) { mode.Set(); #pragma warning disable 618 var subject = new BsonBinaryDataSerializer(); var mockWriter = new Mock <IBsonWriter>(); var writerSettings = new BsonBinaryWriterSettings(); if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { writerSettings.GuidRepresentation = writerGuidRepresentation; } mockWriter.SetupGet(m => m.Settings).Returns(writerSettings); var context = BsonSerializationContext.CreateRoot(mockWriter.Object); var args = new BsonSerializationArgs(); var bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var subType = valueGuidRepresentation == GuidRepresentation.Unspecified ? BsonBinarySubType.UuidLegacy : GuidConverter.GetSubType(valueGuidRepresentation); var value = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? new BsonBinaryData(bytes, subType, valueGuidRepresentation) : new BsonBinaryData(bytes, subType); var isExceptionExpected = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && writerGuidRepresentation != GuidRepresentation.Unspecified && valueGuidRepresentation == GuidRepresentation.Unspecified; if (!isExceptionExpected) { subject.Serialize(context, args, value); var shouldConvertRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && writerGuidRepresentation != GuidRepresentation.Unspecified && valueGuidRepresentation != GuidRepresentation.Unspecified && valueGuidRepresentation != writerGuidRepresentation; var writtenValue = value; if (shouldConvertRepresentation) { var guid = GuidConverter.FromBytes(bytes, valueGuidRepresentation); var convertedBytes = GuidConverter.ToBytes(guid, writerGuidRepresentation); var convertedSubType = GuidConverter.GetSubType(writerGuidRepresentation); writtenValue = new BsonBinaryData(convertedBytes, convertedSubType, writerGuidRepresentation); } mockWriter.Verify(m => m.WriteBinaryData(writtenValue), Times.Once); } #pragma warning restore 618 }
/// <inheritdoc /> public virtual void WriteBinaryData(BsonBinaryData binaryData) { ThrowIfDisposed(); _wrapped.WriteBinaryData(binaryData); }
// public methods /// <summary> /// Generates an Id for a document. /// </summary> /// <param name="container">The container of the document (will be a MongoCollection when called from the C# driver). </param> /// <param name="document">The document.</param> /// <returns>An Id.</returns> public object GenerateId(object container, object document) { return(BsonBinaryData.Create(Guid.NewGuid(), _guidRepresentation)); }