diff options
author | adam <adamansky@gmail.com> | 2013-06-23 22:46:37 +0700 |
---|---|---|
committer | adam <adamansky@gmail.com> | 2013-06-23 22:46:37 +0700 |
commit | 5c1baa36792507c4d8e0cc14f550650e3df69fcc (patch) | |
tree | ab9b9a56c5afca56af804aae1803cffc6857f2a4 /nejdb | |
parent | f94f6b183ca9332598c3de532751bee6a6b244b1 (diff) | |
download | ejdb-5c1baa36792507c4d8e0cc14f550650e3df69fcc.tar.gz ejdb-5c1baa36792507c4d8e0cc14f550650e3df69fcc.tar.bz2 ejdb-5c1baa36792507c4d8e0cc14f550650e3df69fcc.zip |
Fixed #24
Diffstat (limited to 'nejdb')
-rw-r--r-- | nejdb/Ejdb.BSON/BSONDocument.cs | 76 | ||||
-rw-r--r-- | nejdb/Ejdb.BSON/BSONIterator.cs | 30 | ||||
-rw-r--r-- | nejdb/Ejdb.DB/EJDB.cs | 209 | ||||
-rw-r--r-- | nejdb/Ejdb.Tests/TestBSON.cs | 30 | ||||
-rw-r--r-- | nejdb/Ejdb.Tests/TestEJDB.cs | 2 | ||||
-rw-r--r-- | nejdb/README.md | 90 | ||||
-rw-r--r-- | nejdb/nejdb.csproj | 4 | ||||
-rw-r--r-- | nejdb/nejdb.iml | 10 | ||||
-rw-r--r-- | nejdb/nejdb.sln | 12 | ||||
-rw-r--r-- | nejdb/nejdb.userprefs | 37 | ||||
-rw-r--r-- | nejdb/sample/Program.cs | 69 | ||||
-rw-r--r-- | nejdb/sample/Properties/AssemblyInfo.cs | 42 | ||||
-rw-r--r-- | nejdb/sample/sample.csproj | 64 |
13 files changed, 613 insertions, 62 deletions
diff --git a/nejdb/Ejdb.BSON/BSONDocument.cs b/nejdb/Ejdb.BSON/BSONDocument.cs index 249ad1c..0778961 100644 --- a/nejdb/Ejdb.BSON/BSONDocument.cs +++ b/nejdb/Ejdb.BSON/BSONDocument.cs @@ -130,6 +130,55 @@ namespace Ejdb.BSON { } } + public BSONDocument(BSONIterator it, string[] fields) : this() { + Array.Sort(fields); + BSONType bt; + int ind = -1; + int nfc = 0; + foreach (string f in fields) { + if (f != null) { + nfc++; + } + } + while ((bt = it.Next()) != BSONType.EOO) { + if (nfc < 1) { + continue; + } + string kk = it.CurrentKey; + if ((ind = Array.IndexOf(fields, kk)) != -1) { + Add(it.FetchCurrentValue()); + fields[ind] = null; + nfc--; + } else if (bt == BSONType.OBJECT || bt == BSONType.ARRAY) { + string[] narr = null; + for (var i = 0; i < fields.Length; ++i) { + var f = fields[i]; + if (f == null) { + continue; + } + if (f.IndexOf(kk, StringComparison.Ordinal) == 0 && + f.Length > kk.Length + 1 && + f[kk.Length] == '.') { + if (narr == null) { + narr = new string[fields.Length]; + } + narr[i] = f.Substring(kk.Length + 1); + fields[i] = null; + nfc--; + } + } + if (narr != null) { + BSONIterator nit = new BSONIterator(it); + BSONDocument ndoc = new BSONDocument(nit, narr); + if (ndoc.KeysCount > 0) { + Add(new BSONValue(bt, kk, ndoc)); + } + } + } + } + it.Dispose(); + } + public BSONDocument(byte[] bsdata) : this() { using (BSONIterator it = new BSONIterator(bsdata)) { while (it.Next() != BSONType.EOO) { @@ -196,11 +245,11 @@ namespace Ejdb.BSON { /// <summary> /// Gets the field value object. /// </summary> - /// <returns>The object value.</returns> + /// <remarks> + /// Hierarchical field paths are NOT supported. Use <c>[]</c> operator instead. + /// </remarks> + /// <param name="key">BSON document key</param> /// <see cref="Ejdb.BSON.BSONValue"/> - /// <param name="key">Key.</param> - /// <returns>Key object </c> or <c>null</c> if the key is not exists or value type is either - /// <see cref="Ejdb.BSON.BSONType.NULL"/> or <see cref="Ejdb.BSON.BSONType.UNDEFINED"/></returns> public object GetObjectValue(string key) { var bv = GetBSONValue(key); return bv != null ? bv.Value : null; @@ -209,7 +258,7 @@ namespace Ejdb.BSON { /// <summary> /// Determines whether this document has the specified key. /// </summary> - /// <returns><c>true</c> if this document hasthe specified key; otherwise, <c>false</c>.</returns> + /// <returns><c>true</c> if this document has the specified key; otherwise, <c>false</c>.</returns> /// <param name="key">Key.</param> public bool HasKey(string key) { return (GetBSONValue(key) != null); @@ -218,12 +267,25 @@ namespace Ejdb.BSON { /// <summary> /// Gets the <see cref="Ejdb.BSON.BSONDocument"/> with the specified key. /// </summary> + /// <remarks> + /// Hierarchical field paths are supported. + /// </remarks> /// <param name="key">Key.</param> /// <returns>Key object </c> or <c>null</c> if the key is not exists or value type is either /// <see cref="Ejdb.BSON.BSONType.NULL"/> or <see cref="Ejdb.BSON.BSONType.UNDEFINED"/></returns> public object this[string key] { get { - return GetObjectValue(key); + int ind; + if ((ind = key.IndexOf(".", StringComparison.Ordinal)) == -1) { + return GetObjectValue(key); + } else { + string prefix = key.Substring(0, ind); + BSONDocument doc = GetObjectValue(prefix) as BSONDocument; + if (doc == null || key.Length < ind + 2) { + return null; + } + return doc[key.Substring(ind + 1)]; + } } set { object v = value; @@ -450,7 +512,7 @@ namespace Ejdb.BSON { } Type vtype = val.GetType(); if (val is BSONDocument) { - return new BSONDocument((BSONDocument) val); + return (BSONDocument) val; } else if (vtype == typeof(byte[])) { return new BSONDocument((byte[]) val); } else if (vtype.IsAnonymousType()) { diff --git a/nejdb/Ejdb.BSON/BSONIterator.cs b/nejdb/Ejdb.BSON/BSONIterator.cs index 261f4b6..68c69f8 100644 --- a/nejdb/Ejdb.BSON/BSONIterator.cs +++ b/nejdb/Ejdb.BSON/BSONIterator.cs @@ -41,23 +41,35 @@ namespace Ejdb.BSON { BSONValue _entryDataValue; + /// <summary> + /// Returns <c>true</c> if this <see cref="Ejdb.BSON.BSONIterator"/> is disposed. + /// </summary> public bool Disposed { get { return _disposed; } } + /// <summary> + /// Gets a value indicating whether this <see cref="Ejdb.BSON.BSONIterator"/> is empty. + /// </summary> public bool Empty { get { return (_ctype == BSONType.EOO); } } + /// <summary> + /// Gets the length of the document in bytes represented by this iterator. + /// </summary> public int DocumentLength { get { return _doclen; } private set { _doclen = value; } } + /// <summary> + /// Gets the current document key pointed by this iterator. + /// </summary> public string CurrentKey { get { return _entryKey; } } @@ -99,6 +111,11 @@ namespace Ejdb.BSON { } } + internal BSONIterator(BSONIterator it) : this(it._input, it._entryLen + 4) { + _closeOnDispose = false; + it._entryDataSkipped = true; + } + ~BSONIterator() { Dispose(); } @@ -133,8 +150,12 @@ namespace Ejdb.BSON { } } - public BSONDocument ToBSONDocument() { - return new BSONDocument(this); + public BSONDocument ToBSONDocument(params string[] fields) { + if (fields.Length > 0) { + return new BSONDocument(this, fields); + } else { + return new BSONDocument(this); + } } public BSONType Next() { @@ -247,8 +268,7 @@ namespace Ejdb.BSON { case BSONType.ARRAY: { BSONDocument doc = (_ctype == BSONType.OBJECT ? new BSONDocument() : new BSONArray()); - BSONIterator sit = new BSONIterator(this._input, _entryLen + 4); - sit._closeOnDispose = false; + BSONIterator sit = new BSONIterator(this); while (sit.Next() != BSONType.EOO) { doc.Add(sit.FetchCurrentValue()); } @@ -315,7 +335,7 @@ namespace Ejdb.BSON { //.////////////////////////////////////////////////////////////////// // Private staff //.////////////////////////////////////////////////////////////////// - void SkipData(bool force = false) { + internal void SkipData(bool force = false) { if (_entryDataSkipped && !force) { return; } diff --git a/nejdb/Ejdb.DB/EJDB.cs b/nejdb/Ejdb.DB/EJDB.cs index be03d99..ed173e0 100644 --- a/nejdb/Ejdb.DB/EJDB.cs +++ b/nejdb/Ejdb.DB/EJDB.cs @@ -138,15 +138,17 @@ namespace Ejdb.DB { /// <summary> /// Name if EJDB library /// </summary> -#if EJDBDLL + #if EJDBDLL public const string EJDB_LIB_NAME = "tcejdbdll"; #else public const string EJDB_LIB_NAME = "tcejdb"; -#endif + #endif /// <summary> /// Pointer to the native EJDB instance. /// </summary> IntPtr _db = IntPtr.Zero; + + bool _throwonfail = true; //.////////////////////////////////////////////////////////////////// // Native functions refs //.////////////////////////////////////////////////////////////////// @@ -277,6 +279,20 @@ namespace Ejdb.DB { } #endregion /// <summary> + /// If true <see cref="Ejdb.DB.EJDBException"/> will be thrown in the case of failed operation + /// otherwise method will return boolean status flag. Default value: <c>true</c> + /// </summary> + /// <value><c>true</c> if throw exception on fail; otherwise, <c>false</c>.</value> + public bool ThrowExceptionOnFail { + get { + return _throwonfail; + } + set { + _throwonfail = value; + } + } + + /// <summary> /// Gets the last DB error code or <c>null</c> if underlying native database object does not exist. /// </summary> /// <value>The last DB error code.</value> @@ -430,7 +446,11 @@ namespace Ejdb.DB { public bool EnsureCollection(string cname, EJDBCollectionOptionsN? copts = null) { CheckDisposed(); IntPtr cptr = _ejdbcreatecoll(_db, cname, copts); - return (cptr != IntPtr.Zero); + bool rv = (cptr != IntPtr.Zero); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -441,7 +461,11 @@ namespace Ejdb.DB { /// <param name="unlink">If set to <c>true</c> then the collection data file will be removed.</param> public bool DropCollection(string cname, bool unlink = false) { CheckDisposed(); - return _ejdbrmcoll(_db, cname, unlink); + bool rv = _ejdbrmcoll(_db, cname, unlink); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -452,7 +476,11 @@ namespace Ejdb.DB { public bool Sync() { CheckDisposed(); //internal static extern bool _ejdbsyncdb([In] IntPtr db); - return _ejdbsyncdb(_db); + bool rv = _ejdbsyncdb(_db); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -467,7 +495,11 @@ namespace Ejdb.DB { return true; } //internal static extern bool _ejdbsyncoll([In] IntPtr coll); - return _ejdbsyncoll(cptr); + bool rv = _ejdbsyncoll(cptr); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -477,7 +509,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool DropIndexes(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXDROPALL); + bool rv = IndexOperation(cname, ipath, JBIDXDROPALL); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -490,7 +526,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool OptimizeIndexes(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXOP); + bool rv = IndexOperation(cname, ipath, JBIDXOP); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -500,7 +540,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool EnsureStringIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXSTR); + bool rv = IndexOperation(cname, ipath, JBIDXSTR); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -510,7 +554,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool RebuildStringIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXSTR | JBIDXREBLD); + bool rv = IndexOperation(cname, ipath, JBIDXSTR | JBIDXREBLD); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -520,7 +568,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool DropStringIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXSTR | JBIDXDROP); + bool rv = IndexOperation(cname, ipath, JBIDXSTR | JBIDXDROP); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -530,7 +582,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool EnsureIStringIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXISTR); + bool rv = IndexOperation(cname, ipath, JBIDXISTR); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -540,7 +596,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool RebuildIStringIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXISTR | JBIDXREBLD); + bool rv = IndexOperation(cname, ipath, JBIDXISTR | JBIDXREBLD); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -550,7 +610,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool DropIStringIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXISTR | JBIDXDROP); + bool rv = IndexOperation(cname, ipath, JBIDXISTR | JBIDXDROP); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -560,7 +624,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool EnsureNumberIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXNUM); + bool rv = IndexOperation(cname, ipath, JBIDXNUM); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -570,7 +638,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool RebuildNumberIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXNUM | JBIDXREBLD); + bool rv = IndexOperation(cname, ipath, JBIDXNUM | JBIDXREBLD); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -580,7 +652,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool DropNumberIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXNUM | JBIDXDROP); + bool rv = IndexOperation(cname, ipath, JBIDXNUM | JBIDXDROP); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -590,7 +666,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool EnsureArrayIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXARR); + bool rv = IndexOperation(cname, ipath, JBIDXARR); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -600,7 +680,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool RebuildArrayIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXARR | JBIDXREBLD); + bool rv = IndexOperation(cname, ipath, JBIDXARR | JBIDXREBLD); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -610,7 +694,11 @@ namespace Ejdb.DB { /// <param name="cname">Name of collection.</param> /// <param name="ipath">JSON indexed field path</param> public bool DropArrayIndex(string cname, string ipath) { - return IndexOperation(cname, ipath, JBIDXARR | JBIDXDROP); + bool rv = IndexOperation(cname, ipath, JBIDXARR | JBIDXDROP); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -625,7 +713,11 @@ namespace Ejdb.DB { return true; } //internal static extern bool _ejdbtranbegin([In] IntPtr coll); - return _ejdbtranbegin(cptr); + bool rv = _ejdbtranbegin(cptr); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -639,7 +731,11 @@ namespace Ejdb.DB { return true; } //internal static extern bool _ejdbtrancommit([In] IntPtr coll); - return _ejdbtrancommit(cptr); + bool rv = _ejdbtrancommit(cptr); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -653,7 +749,11 @@ namespace Ejdb.DB { return true; } //internal static extern bool _ejdbtranabort([In] IntPtr coll); - return _ejdbtranabort(cptr); + bool rv = _ejdbtranabort(cptr); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; } /// <summary> @@ -667,9 +767,35 @@ namespace Ejdb.DB { IntPtr cptr = _ejdbgetcoll(_db, cname); if (cptr == IntPtr.Zero) { active = false; - return false; + return true; + } + bool rv = _ejdbtranstatus(cptr, out active); + if (_throwonfail && !rv) { + throw new EJDBException(this); + } + return rv; + } + + public bool Save(string cname, params object[] docs) { + CheckDisposed(); + IntPtr cptr = _ejdbcreatecoll(_db, cname, null); + if (cptr == IntPtr.Zero) { + if (_throwonfail) { + throw new EJDBException(this); + } else { + return false; + } } - return _ejdbtranstatus(cptr, out active); + foreach (var doc in docs) { + if (!Save(cptr, BSONDocument.ValueOf(doc), false)) { + if (_throwonfail) { + throw new EJDBException(this); + } else { + return false; + } + } + } + return true; } /// <summary> @@ -682,11 +808,19 @@ namespace Ejdb.DB { CheckDisposed(); IntPtr cptr = _ejdbcreatecoll(_db, cname, null); if (cptr == IntPtr.Zero) { - return false; + if (_throwonfail) { + throw new EJDBException(this); + } else { + return false; + } } foreach (var doc in docs) { if (!Save(cptr, doc, false)) { - return false; + if (_throwonfail) { + throw new EJDBException(this); + } else { + return false; + } } } return true; @@ -703,11 +837,19 @@ namespace Ejdb.DB { CheckDisposed(); IntPtr cptr = _ejdbcreatecoll(_db, cname, null); if (cptr == IntPtr.Zero) { - return false; + if (_throwonfail) { + throw new EJDBException(this); + } else { + return false; + } } foreach (var doc in docs) { if (!Save(cptr, doc, true)) { - return false; + if (_throwonfail) { + throw new EJDBException(this); + } else { + return false; + } } } return true; @@ -723,6 +865,9 @@ namespace Ejdb.DB { if (rv && bv == null) { doc.SetOID("_id", new BSONOid(oiddata)); } + if (_throwonfail && !rv) { + throw new EJDBException(this); + } return rv; } @@ -762,7 +907,11 @@ namespace Ejdb.DB { //internal static extern bool _ejdbrmbson([In] IntPtr cptr, [In] byte[] oid); foreach (var oid in oids) { if (!_ejdbrmbson(cptr, oid.ToBytes())) { - return false; + if (_throwonfail) { + throw new EJDBException(this); + } else { + return false; + } } } return true; diff --git a/nejdb/Ejdb.Tests/TestBSON.cs b/nejdb/Ejdb.Tests/TestBSON.cs index 19c6b00..5d07983 100644 --- a/nejdb/Ejdb.Tests/TestBSON.cs +++ b/nejdb/Ejdb.Tests/TestBSON.cs @@ -252,6 +252,36 @@ namespace Ejdb.Tests { } Assert.AreEqual("bc1[BSONDocument: [BSONValue: BSONType=REGEX, Key=f, Value=[BSONRegexp: re=g, opts=]]]2", cs); } + + [Test] + public void TestFilteredDoc() { + var doc = new BSONDocument(); + doc["c"] = "d"; + doc["aaa"] = 11; + doc["ndoc"] = BSONDocument.ValueOf(new { + aaaa = "nv1", + d = "nv2", + nnd = BSONDocument.ValueOf(new { + nnv = true, + nns = "s" + }) + }); + doc["ndoc2"] = BSONDocument.ValueOf(new { + n = "v" + }); + doc["f"] = "f"; + BSONIterator it = new BSONIterator(doc); + BSONDocument doc2 = it.ToBSONDocument("c", "ndoc.d", "ndoc.nnd.nns", "f"); + Assert.AreEqual(3, doc2.KeysCount); + Assert.AreEqual("d", doc2["c"]); + Assert.AreEqual(2, ((BSONDocument) doc2["ndoc"]).KeysCount); + Assert.AreEqual("nv2", ((BSONDocument) doc2["ndoc"])["d"]); + Assert.AreEqual("s", ((BSONDocument) ((BSONDocument) doc2["ndoc"])["nnd"])["nns"]); + Assert.AreEqual("s", doc2["ndoc.nnd.nns"]); + Assert.AreEqual("f", "f"); + //Console.WriteLine("doc2=" + doc2); + + } } } diff --git a/nejdb/Ejdb.Tests/TestEJDB.cs b/nejdb/Ejdb.Tests/TestEJDB.cs index 652baf0..bfaae53 100644 --- a/nejdb/Ejdb.Tests/TestEJDB.cs +++ b/nejdb/Ejdb.Tests/TestEJDB.cs @@ -177,7 +177,7 @@ namespace Ejdb.Tests { }); Assert.AreEqual(2, q.Count()); - Console.WriteLine(jb.DBMeta); + //Console.WriteLine(jb.DBMeta); //[BSONDocument: [BSONValue: BSONType=STRING, Key=file, Value=testdb1], //[BSONValue: BSONType=ARRAY, Key=collections, Value=[BSONArray: [BSONValue: BSONType=OBJECT, Key=0, Value=[BSONDocument: [BSONValue: BSONType=STRING, Key=name, Value=parrots], [BSONValue: BSONType=STRING, Key=file, Value=testdb1_parrots], [BSONValue: BSONType=LONG, Key=records, Value=2], [BSONValue: BSONType=OBJECT, Key=options, Value=[BSONDocument: [BSONValue: BSONType=LONG, Key=buckets, Value=131071], [BSONValue: BSONType=LONG, Key=cachedrecords, Value=0], [BSONValue: BSONType=BOOL, Key=large, Value=False], [BSONValue: BSONType=BOOL, Key=compressed, Value=False]]], [BSONValue: BSONType=ARRAY, Key=indexes, Value=[BSONArray: ]]]]]]] diff --git a/nejdb/README.md b/nejdb/README.md index e69de29..7bccfc0 100644 --- a/nejdb/README.md +++ b/nejdb/README.md @@ -0,0 +1,90 @@ + +EJDB .Net Binding +=========================================== + + +**Note: The .Net EJDB binding designed for .Net 4.0/4.5 and tested on Mono 3 for Unix and Windows.** + + +Prerequisites +-------------------------------- + + * EJDB C library >= v1.1.13 + * Mono 3.0 + * Monodevelop 4.x + + +Unix +--------------------------------- + +Install the tcejdb >= 1.1.13 as system-wide library. +The `tcejdb.so` shared library should be visible to the system linker. +Use the following solution configs to debug and test: `DebugUnix`, `ReleaseUnix` + + +Windows +-------------------------------- +Download appropriate [EJDB binary distribution](https://github.com/Softmotions/ejdb/blob/master/tcejdb/WIN32.md). +Then add the `tcejdbdll.dll` directory into search `PATH`. +Use the following solution configs to debug and test: `DebugWindows`, `ReleaseWindows` + + +One snippet intro +--------------------------------- + +```c# +using System; +using Ejdb.DB; +using Ejdb.BSON; + +namespace sample { + + class MainClass { + + public static void Main(string[] args) { + var jb = new EJDB("zoo", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC); + jb.ThrowExceptionOnFail = true; + + var parrot1 = BSONDocument.ValueOf(new { + name = "Grenny", + type = "African Grey", + male = true, + age = 1, + birthdate = DateTime.Now, + likes = new string[] { "green color", "night", "toys" }, + extra = BSONull.VALUE + }); + + var parrot2 = BSONDocument.ValueOf(new { + name = "Bounty", + type = "Cockatoo", + male = false, + age = 15, + birthdate = DateTime.Now, + likes = new string[] { "sugar cane" } + }); + + jb.Save("parrots", parrot1, parrot2); + + Console.WriteLine("Grenny OID: " + parrot1["_id"]); + Console.WriteLine("Bounty OID: " + parrot2["_id"]); + + var q = jb.CreateQuery(new { + likes = "toys" + }, "parrots").OrderBy("name"); + + using (var cur = q.Find()) { + Console.WriteLine("Found " + cur.Length + " parrots"); + foreach (var e in cur) { + //fetch `name` and the first element of likes array from current BSON iterator. + //alternatively you can fetch whole document from the iterator: `e.ToBSONDocument()` + BSONDocument rdoc = e.ToBSONDocument("name", "likes.0"); + Console.WriteLine(string.Format("{0} likes the '{1}'", rdoc["name"], rdoc["likes.0"])); + } + } + q.Dispose(); + jb.Dispose(); + } + } +} +``` diff --git a/nejdb/nejdb.csproj b/nejdb/nejdb.csproj index 15b1787..9158371 100644 --- a/nejdb/nejdb.csproj +++ b/nejdb/nejdb.csproj @@ -57,9 +57,7 @@ <SpecificVersion>False</SpecificVersion> </Reference> <Reference Include="System.Core" /> - <Reference Include="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756"> - <Private>False</Private> - </Reference> + <Reference Include="Mono.Posix" /> </ItemGroup> <ItemGroup> <Compile Include="AssemblyInfo.cs" /> diff --git a/nejdb/nejdb.iml b/nejdb/nejdb.iml new file mode 100644 index 0000000..ef582b1 --- /dev/null +++ b/nejdb/nejdb.iml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$" /> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> + diff --git a/nejdb/nejdb.sln b/nejdb/nejdb.sln index 45aba2d..ff96e43 100644 --- a/nejdb/nejdb.sln +++ b/nejdb/nejdb.sln @@ -8,6 +8,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sample", "sample\sample.csproj", "{93049FBC-650E-432C-9F1D-156E621DB96C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution DebugUnix|Any CPU = DebugUnix|Any CPU @@ -16,6 +18,14 @@ Global ReleaseWindows|Any CPU = ReleaseWindows|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {93049FBC-650E-432C-9F1D-156E621DB96C}.DebugUnix|Any CPU.ActiveCfg = DebugUnix|Any CPU + {93049FBC-650E-432C-9F1D-156E621DB96C}.DebugUnix|Any CPU.Build.0 = DebugUnix|Any CPU + {93049FBC-650E-432C-9F1D-156E621DB96C}.DebugWindows|Any CPU.ActiveCfg = DebugWindows|Any CPU + {93049FBC-650E-432C-9F1D-156E621DB96C}.DebugWindows|Any CPU.Build.0 = DebugWindows|Any CPU + {93049FBC-650E-432C-9F1D-156E621DB96C}.ReleaseUnix|Any CPU.ActiveCfg = ReleaseUnix|Any CPU + {93049FBC-650E-432C-9F1D-156E621DB96C}.ReleaseUnix|Any CPU.Build.0 = ReleaseUnix|Any CPU + {93049FBC-650E-432C-9F1D-156E621DB96C}.ReleaseWindows|Any CPU.ActiveCfg = ReleaseWindows|Any CPU + {93049FBC-650E-432C-9F1D-156E621DB96C}.ReleaseWindows|Any CPU.Build.0 = ReleaseWindows|Any CPU {A24C964C-771F-4359-8C93-4BFCBE451D8B}.DebugUnix|Any CPU.ActiveCfg = DebugUnix|Any CPU {A24C964C-771F-4359-8C93-4BFCBE451D8B}.DebugUnix|Any CPU.Build.0 = DebugUnix|Any CPU {A24C964C-771F-4359-8C93-4BFCBE451D8B}.DebugWindows|Any CPU.ActiveCfg = DebugWindows|Any CPU @@ -28,7 +38,7 @@ Global GlobalSection(NestedProjects) = preSolution EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = nejdb.csproj + StartupItem = sample\sample.csproj Policies = $0 $0.StandardHeader = $1 $1.Text = @============================================================================================\n .NET API for EJDB database library http://ejdb.org\n Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com>\n\n This file is part of EJDB.\n EJDB is free software; you can redistribute it and/or modify it under the terms of\n the GNU Lesser General Public License as published by the Free Software Foundation; either\n version 2.1 of the License or any later version. EJDB is distributed in the hope\n that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public\n License for more details.\n You should have received a copy of the GNU Lesser General Public License along with EJDB;\n if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,\n Boston, MA 02111-1307 USA.\n============================================================================================ diff --git a/nejdb/nejdb.userprefs b/nejdb/nejdb.userprefs index 8605c13..d7e4d87 100644 --- a/nejdb/nejdb.userprefs +++ b/nejdb/nejdb.userprefs @@ -1,24 +1,20 @@ <Properties> - <MonoDevelop.Ide.Workspace ActiveConfiguration="DebugWindows" /> - <MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.DB\EJDB.cs"> + <MonoDevelop.Ide.Workspace ActiveConfiguration="DebugUnix" /> + <MonoDevelop.Ide.Workbench ActiveDocument="sample/Program.cs"> <Files> - <File FileName="Ejdb.DB\EJDB.cs" Line="199" Column="58" /> - <File FileName="c:\Projects\ejdb\nejdb\Ejdb.Tests\TestBSON.cs" Line="1" Column="1" /> - <File FileName="Ejdb.Tests\TestEJDB.cs" Line="1" Column="1" /> + <File FileName="sample/Program.cs" Line="59" Column="70" /> + <File FileName="Ejdb.BSON/BSONIterator.cs" Line="155" Column="24" /> + <File FileName="Ejdb.BSON/BSONDocument.cs" Line="148" Column="27" /> </Files> <Pads> <Pad Id="ProjectPad"> <State expanded="True"> - <Node name="Solution Items" expanded="True" /> <Node name="nejdb" expanded="True"> - <Node name="References" expanded="True" /> <Node name="Ejdb.BSON" expanded="True" /> - <Node name="Ejdb.DB" expanded="True"> - <Node name="EJDB.cs" selected="True" /> - </Node> - <Node name="Ejdb.IO" expanded="True" /> - <Node name="Ejdb.Tests" expanded="True" /> - <Node name="Ejdb.Utils" expanded="True" /> + </Node> + <Node name="sample" expanded="True"> + <Node name="Properties" expanded="True" /> + <Node name="Program.cs" selected="True" /> </Node> </State> </Pad> @@ -28,13 +24,22 @@ </State> </Pad> <Pad Id="MonoDevelop.Debugger.WatchPad"> - <State /> + <State> + <Value>it.CurrentKey</Value> + <Value>fields</Value> + <Value>fields[i]</Value> + <Value>nfc</Value> + </State> + </Pad> + <Pad Id="ConnectionManagerPad"> + <State selected="True" /> </Pad> <Pad Id="MonoDevelop.NUnit.TestPad"> <State expanded="True" selected="True"> <Node name="nejdb" expanded="True"> <Node name="Ejdb" expanded="True"> <Node name="Tests" expanded="True"> + <Node name="TestBSON" expanded="True" /> <Node name="TestEJDB" expanded="True" /> </Node> </Node> @@ -44,7 +49,9 @@ </Pads> </MonoDevelop.Ide.Workbench> <MonoDevelop.Ide.DebuggingService.Breakpoints> - <BreakpointStore /> + <BreakpointStore> + <Breakpoint file="/home/adam/Projects/softmotions/ejdb/nejdb/Ejdb.Tests/TestBSON.cs" line="279" column="1" /> + </BreakpointStore> </MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.PinnedWatches /> </Properties>
\ No newline at end of file diff --git a/nejdb/sample/Program.cs b/nejdb/sample/Program.cs new file mode 100644 index 0000000..fc88525 --- /dev/null +++ b/nejdb/sample/Program.cs @@ -0,0 +1,69 @@ +// ============================================================================================ +// .NET API for EJDB database library http://ejdb.org +// Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com> +// +// This file is part of EJDB. +// EJDB is free software; you can redistribute it and/or modify it under the terms of +// the GNU Lesser General Public License as published by the Free Software Foundation; either +// version 2.1 of the License or any later version. EJDB is distributed in the hope +// that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +// License for more details. +// You should have received a copy of the GNU Lesser General Public License along with EJDB; +// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, +// Boston, MA 02111-1307 USA. +// ============================================================================================ +using System; +using Ejdb.DB; +using Ejdb.BSON; + +namespace sample { + + class MainClass { + + public static void Main(string[] args) { + var jb = new EJDB("zoo", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC); + jb.ThrowExceptionOnFail = true; + + var parrot1 = BSONDocument.ValueOf(new { + name = "Grenny", + type = "African Grey", + male = true, + age = 1, + birthdate = DateTime.Now, + likes = new string[] { "green color", "night", "toys" }, + extra = BSONull.VALUE + }); + + var parrot2 = BSONDocument.ValueOf(new { + name = "Bounty", + type = "Cockatoo", + male = false, + age = 15, + birthdate = DateTime.Now, + likes = new string[] { "sugar cane" } + }); + + jb.Save("parrots", parrot1, parrot2); + + Console.WriteLine("Grenny OID: " + parrot1["_id"]); + Console.WriteLine("Bounty OID: " + parrot2["_id"]); + + var q = jb.CreateQuery(new { + likes = "toys" + }, "parrots").OrderBy("name"); + + using (var cur = q.Find()) { + Console.WriteLine("Found " + cur.Length + " parrots"); + foreach (var e in cur) { + //fetch `name` and the first element of likes array from current BSON iterator. + //alternatively you can fetch whole document from the iterator: `e.ToBSONDocument()` + BSONDocument rdoc = e.ToBSONDocument("name", "likes.0"); + Console.WriteLine(string.Format("{0} likes the '{1}'", rdoc["name"], rdoc["likes.0"])); + } + } + q.Dispose(); + jb.Dispose(); + } + } +} diff --git a/nejdb/sample/Properties/AssemblyInfo.cs b/nejdb/sample/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e5ae253 --- /dev/null +++ b/nejdb/sample/Properties/AssemblyInfo.cs @@ -0,0 +1,42 @@ +// ============================================================================================ +// .NET API for EJDB database library http://ejdb.org +// Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com> +// +// This file is part of EJDB. +// EJDB is free software; you can redistribute it and/or modify it under the terms of +// the GNU Lesser General Public License as published by the Free Software Foundation; either +// version 2.1 of the License or any later version. EJDB is distributed in the hope +// that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +// License for more details. +// You should have received a copy of the GNU Lesser General Public License along with EJDB; +// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, +// Boston, MA 02111-1307 USA. +// ============================================================================================ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("sample")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Softmotions Ltd.")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com>")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/nejdb/sample/sample.csproj b/nejdb/sample/sample.csproj new file mode 100644 index 0000000..9647966 --- /dev/null +++ b/nejdb/sample/sample.csproj @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">DebugWindows</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>10.0.0</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{93049FBC-650E-432C-9F1D-156E621DB96C}</ProjectGuid> + <OutputType>Exe</OutputType> + <RootNamespace>sample</RootNamespace> + <AssemblyName>sample</AssemblyName> + <ReleaseVersion>1.0.0</ReleaseVersion> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugWindows|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug</OutputPath> + <DefineConstants>DEBUG;</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseWindows|AnyCPU' "> + <DebugType>full</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release</OutputPath> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugUnix|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug</OutputPath> + <DefineConstants>DEBUG;</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseUnix|AnyCPU' "> + <DebugType>full</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release</OutputPath> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Program.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> + <ItemGroup> + <ProjectReference Include="..\nejdb.csproj"> + <Project>{A24C964C-771F-4359-8C93-4BFCBE451D8B}</Project> + <Name>nejdb</Name> + </ProjectReference> + </ItemGroup> +</Project>
\ No newline at end of file |