/// <summary> /// Saves strongly typed document to collection. /// <remarks>If document has property of type <see cref="ObjectId"/> and named 'Id' - it will be set after save</remarks> /// </summary> /// <typeparam name="TDocument">Document type</typeparam> /// <param name="document">Document to save</param> /// <param name="merge">If true the merge will be performed with old and new objects. Otherwise old object will be replaced</param> /// <returns>Id of saved document</returns> public unsafe ObjectId Save <TDocument>(TDocument document, bool merge) { using (var stream = Database.StreamPool.GetStream()) using (var writer = new BsonWriter(stream)) { Serializer.Serialize(writer, document); ObjectId objectId; bool saveOk = false; fixed(byte *streamPointer = &stream.GetBuffer()[0]) { saveOk = _functions.SaveBson(CollectionHandle, streamPointer, &objectId, merge); } if (!saveOk) { throw EjdbException.FromDatabase(Database, "Failed to save document"); } IdHelper <TDocument> .SetId(document, ref objectId); return(objectId); } }
/// <summary> /// Saves document to collection /// </summary> /// <param name="doc">Document to save</param> /// <param name="merge">If true the merge will be performed with old and new objects. Otherwise old object will be replaced</param> public unsafe ObjectId Save(BsonDocument doc, bool merge) { BsonValue id = doc.GetBsonValue("_id"); ObjectId oiddata = new ObjectId(); if (id != null) { oiddata = (ObjectId)id.Value; } using (var stream = Database.StreamPool.GetStream()) { doc.Serialize(stream); bool saveOk = false; fixed(byte *streamPointer = &stream.GetBuffer()[0]) { saveOk = _functions.SaveBson(CollectionHandle, streamPointer, &oiddata, merge); } if (saveOk && id == null) { doc.SetOID("_id", oiddata); } if (!saveOk) { throw EjdbException.FromDatabase(Database, "Failed to save Bson"); } } return(oiddata); }
/// <summary> /// Synchronize database state with disk /// </summary> public void Synchronize() { if (_functions.Sync(DatabaseHandle)) { return; } throw EjdbException.FromDatabase(this, "Failed to synchronize database"); }
/// <summary> /// Rollbacks current transaction /// </summary> internal void RollbackTransactionInternal() { if (_functions.RollbackTransaction(CollectionHandle)) { return; } throw EjdbException.FromDatabase(Database, "Failed to rollback transaction"); }
/// <summary> /// Commits current transaction /// </summary> internal void CommitTransactionInternal() { if (_functions.CommitTransaction(CollectionHandle)) { return; } throw EjdbException.FromDatabase(Database, "Failed to commit transaction"); }
/// <summary> /// Loads JSON object identified by OID from the collection. /// </summary> /// <remarks> /// Returns <c>null</c> if object is not found. /// </remarks> /// <param name="id">Id of an object</param> public void Delete(ObjectId id) { if (_functions.DeleteBson(CollectionHandle, ref id)) { return; } throw EjdbException.FromDatabase(Database, "Failed to delete document"); }
/// <summary> /// Synchronize content of a EJDB collection database with the file on device /// </summary> public void Synchronize() { if (_functions.SyncCollection(CollectionHandle)) { return; } throw EjdbException.FromDatabase(Database, "Failed to sync collection"); }
internal void ThrowOnError() { var errorCode = LastErrorCode; if (errorCode != 0) { EjdbException.FromDatabase(this, string.Empty); } }
/// <summary> /// Begins new transaction /// </summary> internal void BeginTransactionInternal() { if (_functions.BeginTransaction(CollectionHandle)) { return; } throw EjdbException.FromDatabase(Database, "Failed to begin transaction"); }
/// <summary> /// Performs provided operations on collection indexes. /// </summary> /// <param name="path"></param> /// <param name="flags"></param> public unsafe void Index(string path, IndexOperations flags) { UnsafeBuffer buffer; UnsafeBuffer.FillFromString(&buffer, path); if (_functions.SetIndex(CollectionHandle, &buffer, (int)flags)) { return; } throw EjdbException.FromDatabase(Database, "Failed to perform index operation"); }
///// <summary> ///// Executes EJDB command. ///// </summary> ///// <remarks> ///// Supported commands: ///// ///// 1) Exports database collections data. See ejdbexport() method. ///// ///// "export" : { ///// "path" : string, //Exports database collections data ///// "cnames" : [string array]|null, //List of collection names to export ///// "mode" : int|null //Values: null|`JBJSONEXPORT` See ejdb.h#ejdbexport() method ///// } ///// ///// Command response: ///// { ///// "log" : string, //Diagnostic log about executing this command ///// "error" : string|null, //ejdb error message ///// "errorCode" : int|0, //ejdb error code ///// } ///// ///// 2) Imports previously exported collections data into ejdb. ///// ///// "import" : { ///// "path" : string //The directory path in which data resides ///// "cnames" : [string array]|null, //List of collection names to import ///// "mode" : int|null //Values: null|`JBIMPORTUPDATE`|`JBIMPORTREPLACE` See ejdb.h#ejdbimport() method ///// } ///// ///// Command response: ///// { ///// "log" : string, //Diagnostic log about executing this command ///// "error" : string|null, //ejdb error message ///// "errorCode" : int|0, //ejdb error code ///// } ///// </remarks> ///// <param name="cmd">Command object</param> ///// <returns>Command response.</returns> ////public BsonDocument Command(BsonDocument cmd) ////{ //// CheckDisposed(); //// byte[] cmdata = cmd.ToByteArray(); //// //internal static extern IntPtr _ejdbcommand([In] IntPtr db, [In] byte[] cmd); //// IntPtr cmdret = _command(this, cmdata); //// if (cmdret == IntPtr.Zero) //// { //// return null; //// } //// byte[] bsdata = BsonPtrIntoByteArray(cmdret); //// if (bsdata.Count == 0) //// { //// return null; //// } //// BsonIterator it = new BsonIterator(bsdata); //// return it.ToBsonDocument(); ////} /// <summary> /// Opens or creates database file depending on <see cref="OpenMode"/> /// </summary> /// <param name="dbFilePath">Database filename</param> /// <param name="openMode"></param> public unsafe void Open(string dbFilePath, OpenMode openMode = DefaultOpenMode) { UnsafeBuffer buffer; UnsafeBuffer.FillFromString(&buffer, dbFilePath); bool result = _functions.OpenDatabase(DatabaseHandle, &buffer, openMode); if (!result) { throw EjdbException.FromDatabase(this, "Error on open database"); } }