public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine) { var col = this.ReadCollection(engine, s); var value = JsonSerializer.Deserialize(s); var sid = s.Scan(@"\s+_?id:(int32|int64|int|long|objectid|datetime|date|guid)", 1).Trim().ToLower(); var autoId = sid == "int32" || sid == "int" ? BsonType.Int32 : sid == "int64" || sid == "long" ? BsonType.Int64 : sid == "date" || sid == "datetime" ? BsonType.DateTime : sid == "guid" ? BsonType.Guid : BsonType.ObjectId; s.ThrowIfNotFinish(); if (value.IsArray) { var count = engine.InsertBulk(col, value.AsArray.RawValue.Select(x => x.AsDocument), autoId: autoId); yield return(count); } else if (value.IsDocument) { engine.Insert(col, new BsonDocument[] { value.AsDocument }, autoId); yield return(value.AsDocument["_id"]); } else { throw LiteException.SyntaxError(s, "Invalid JSON value (must be a document or an array)"); } }
public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine) { var col = this.ReadCollection(engine, s); // try read any kind of expression var expression = BsonExpression.ReadExpression(s, false, false); // if not found a valid one, try read only as path (will add $. before) if (expression == null) { expression = BsonExpression.ReadExpression(s, true, true); } var query = Query.All(); // support into new_collection var into = s.Scan(@"\s*into\s+([\w-]+)", 1); var autoId = BsonType.ObjectId; // checks for autoId if (into.Length > 0) { var sid = s.Scan(@"\s+_?id:(int32|int64|int|long|objectid|datetime|date|guid)", 1).Trim().ToLower(); autoId = sid == "int32" || sid == "int" ? BsonType.Int32 : sid == "int64" || sid == "long" ? BsonType.Int64 : sid == "date" || sid == "datetime" ? BsonType.DateTime : sid == "guid" ? BsonType.Guid : BsonType.ObjectId; } if (s.Scan(@"\s*where\s*").Length > 0) { query = this.ReadQuery(s, true); } var skipLimit = this.ReadSkipLimit(s); var includes = this.ReadIncludes(s); s.ThrowIfNotFinish(); var docs = engine.Find(col, query, includes, skipLimit.Key, skipLimit.Value); if (into.Length > 0) { // insert into results to other collection collection var count = engine.InsertBulk(into, this.Execute(docs, expression), autoId: autoId); // return inserted documents return(new BsonValue[] { count }); } else { return(this.Execute(docs, expression).Select(x => x as BsonValue)); } }
public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine) { var col = this.ReadCollection(engine, s); var filename = s.Scan(@".*"); using (var sr = new StreamReader(new FileStream(filename, System.IO.FileMode.Open))) { var docs = JsonSerializer.DeserializeArray(sr); yield return(engine.InsertBulk(col, docs.Select(x => x.AsDocument))); } }
public void BulkInsert_Test() { using (var file = new TempFile()) using (var db = new LiteEngine(file.Filename)) { // let's bulk 500.000 documents db.InsertBulk("col", GetDocs(1, 500000)); // and assert if all are inserted (based on collection header only) Assert.AreEqual(500000, db.Count("col")); // and now count all Assert.AreEqual(500000, db.Count("col", Query.All())); } }
public void Shrink_After_DropCollection() { using (var file = new TempFile()) using (var db = new LiteEngine(file.Filename)) { db.InsertBulk("col", GetDocs(1, 10000)); db.DropCollection("col"); // full disk usage var size = file.Size; var r = db.Shrink(); // only header page + reserved lock page Assert.AreEqual(8192, size - r); } }