/// <summary> /// Update a document in this collection. Returns false if not found document in collection /// </summary> public virtual bool Update(T doc) { if (doc == null) { throw new ArgumentNullException("doc"); } // gets document Id var id = BsonSerializer.GetIdValue(doc); if (id == null) { throw new LiteException("Document Id can't be null"); } // serialize object var bytes = BsonSerializer.Serialize(doc); // start transaction _engine.Transaction.Begin(); try { var col = this.GetCollectionPage(false); // if no collection, no updates if (col == null) { _engine.Transaction.Abort(); return(false); } // find indexNode from pk index var indexNode = _engine.Indexer.FindOne(col.PK, id); // if not found document, no updates if (indexNode == null) { _engine.Transaction.Abort(); return(false); } // update data storage var dataBlock = _engine.Data.Update(col, indexNode.DataBlock, bytes); // delete/insert indexes - do not touch on PK for (byte i = 1; i < col.Indexes.Length; i++) { var index = col.Indexes[i]; if (!index.IsEmpty) { var key = BsonSerializer.GetFieldValue(doc, index.Field); var node = _engine.Indexer.GetNode(dataBlock.IndexRef[i]); // check if my index node was changed if (node.Key.CompareTo(new IndexKey(key)) != 0) { // remove old index node _engine.Indexer.Delete(index, node.Position); // and add a new one var newNode = _engine.Indexer.AddNode(index, key); // point my index to data object newNode.DataBlock = dataBlock.Position; // point my dataBlock dataBlock.IndexRef[i] = newNode.Position; dataBlock.Page.IsDirty = true; } } } _engine.Transaction.Commit(); return(true); } catch (Exception ex) { _engine.Transaction.Rollback(); throw ex; } }
/// <summary> /// Insert a new document to this collection. Document Id must be a new value in collection /// </summary> public virtual void Insert(T doc) { if (doc == null) { throw new ArgumentNullException("doc"); } // gets document Id var id = BsonSerializer.GetIdValue(doc); if (id == null) { throw new LiteException("Document Id can't be null"); } // serialize object var bytes = BsonSerializer.Serialize(doc); _engine.Transaction.Begin(); try { var col = this.GetCollectionPage(true); // storage in data pages - returns dataBlock address var dataBlock = _engine.Data.Insert(col, new IndexKey(id), bytes); // store id in a PK index [0 array] var pk = _engine.Indexer.AddNode(col.PK, id); // do links between index <-> data block pk.DataBlock = dataBlock.Position; dataBlock.IndexRef[0] = pk.Position; // for each index, insert new IndexNode for (byte i = 1; i < col.Indexes.Length; i++) { var index = col.Indexes[i]; if (!index.IsEmpty) { var key = BsonSerializer.GetFieldValue(doc, index.Field); var node = _engine.Indexer.AddNode(index, key); // point my index to data object node.DataBlock = dataBlock.Position; // point my dataBlock dataBlock.IndexRef[i] = node.Position; } } _engine.Transaction.Commit(); } catch (Exception ex) { _engine.Transaction.Rollback(); throw ex; } }