Пример #1
0
        public ulong OnlineBackup(EJDB2Handle handle, string targetFile)
        {
            if (handle.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (handle.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (targetFile == null)
            {
                throw new ArgumentNullException(nameof(targetFile));
            }

            ulong rc, ts;

            using (var tf = new Utf8String(targetFile))
            {
                rc = _helper.ejdb_online_backup(handle.DangerousGetHandle(), out ts, tf);
            }

            if (rc != 0)
            {
                throw _e.CreateException(rc);
            }

            return(ts);
        }
Пример #2
0
        public void Delete(EJDB2Handle handle, string collection, long id)
        {
            if (handle.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (handle.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            ulong rc;

            using (var col = new Utf8String(collection))
            {
                rc = _helper.ejdb_del(handle.DangerousGetHandle(), col, id);
            }

            if (rc != 0)
            {
                throw _e.CreateException(rc);
            }
        }
Пример #3
0
        public void EnsureIndex(EJDB2Handle handle, string collection, string path, ejdb_idx_mode_t mode)
        {
            if (handle.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (handle.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            ulong rc;

            using (var pool = new Utf8StringPool())
            {
                rc = _helper.ejdb_ensure_index(handle.DangerousGetHandle(),
                                               pool.GetString(collection), pool.GetString(path), mode);
            }

            if (rc != 0)
            {
                throw _e.CreateException(rc);
            }
        }
Пример #4
0
        public void RenameCollection(EJDB2Handle handle, string oldCollectionName, string newCollectionName)
        {
            if (handle.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (handle.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (oldCollectionName == null)
            {
                throw new ArgumentNullException(nameof(oldCollectionName));
            }

            if (newCollectionName == null)
            {
                throw new ArgumentNullException(nameof(newCollectionName));
            }

            ulong rc;

            using (var pool = new Utf8StringPool())
            {
                rc = _helper.ejdb_rename_collection(handle.DangerousGetHandle(),
                                                    pool.GetString(oldCollectionName), pool.GetString(newCollectionName));
            }

            if (rc != 0)
            {
                throw _e.CreateException(rc);
            }
        }
Пример #5
0
 public void Destroy(EJDB2Handle handle)
 {
     if (!handle.IsInvalid)
     {
         IntPtr jql = handle.DangerousGetHandle();
         _helper.jql_destroy(ref jql);
     }
 }
Пример #6
0
        public ulong Close(EJDB2Handle handle)
        {
            if (!handle.IsInvalid)
            {
                IntPtr db = handle.DangerousGetHandle();
                return(_helper.ejdb_close(ref db));
            }

            return(0);
        }
Пример #7
0
        public long Put(EJDB2Handle handle, string collection, string json, long id)
        {
            if (handle.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (handle.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (json == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            long   ret = id;
            IntPtr db = handle.DangerousGetHandle(), jbl = IntPtr.Zero;

            try
            {
                using (var pool = new Utf8StringPool())
                {
                    ulong rc = _helper.jbl_from_json(out jbl, pool.GetString(json));
                    if (rc != 0)
                    {
                        throw _e.CreateException(rc);
                    }

                    rc = id > 0
                        ? _helper.ejdb_put(db, pool.GetString(collection), jbl, id)
                        : _helper.ejdb_put_new(db, pool.GetString(collection), jbl, out ret);

                    if (rc != 0)
                    {
                        throw _e.CreateException(rc);
                    }
                }

                return(ret);
            }
            finally
            {
                if (jbl != IntPtr.Zero)
                {
                    _helper.jbl_destroy(ref jbl);
                }
            }
        }
Пример #8
0
        public EJDB2Handle Init(EJDB2Handle db, string query, ref string collection)
        {
            if (db == null)
            {
                throw new ArgumentNullException(nameof(db));
            }

            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            IntPtr q = IntPtr.Zero;

            try
            {
                ulong rc;
                using (var pool = new Utf8StringPool())
                {
                    rc = _helper.jql_create2(out q, pool.GetString(collection), pool.GetString(query),
                                             jql_create_mode_t.JQL_KEEP_QUERY_ON_PARSE_ERROR | jql_create_mode_t.JQL_SILENT_ON_PARSE_ERROR);
                }

                if (rc != 0)
                {
                    string message = null;
                    if (rc == (ulong)jql_ecode_t.JQL_ERROR_QUERY_PARSE)
                    {
                        message = $"Query parse error: {Utf8String.FromIntPtr(_helper.jql_error(q))}";
                    }

                    throw _e.CreateException(rc, message);
                }

                if (collection == null)
                {
                    IntPtr col = _helper.jql_collection(q);
                    collection = Utf8String.FromIntPtr(col);
                }

                return(new EJDB2Handle(q));
            }
            catch (Exception)
            {
                if (q != IntPtr.Zero)
                {
                    _helper.jql_destroy(ref q);
                }

                throw;
            }
        }
Пример #9
0
        public void Get(EJDB2Handle handle, string collection, long id, Stream stream, bool pretty)
        {
            if (handle.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (handle.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            IntPtr db = handle.DangerousGetHandle(), jbl = IntPtr.Zero;

            try
            {
                ulong rc;
                using (var col = new Utf8String(collection))
                {
                    rc = _helper.ejdb_get(db, col, id, out jbl);
                }

                if (rc != 0)
                {
                    throw _e.CreateException(rc);
                }

                var printer = new Printer(stream);

                rc = _helper.jbl_as_json(jbl, printer.JbnJsonPrinter, IntPtr.Zero,
                                         pretty ? jbl_print_flags_t.JBL_PRINT_PRETTY : jbl_print_flags_t.JBL_PRINT_NONE);

                if (rc != 0)
                {
                    throw _e.CreateException(rc);
                }

                stream.Flush();
            }
            finally
            {
                if (jbl != IntPtr.Zero)
                {
                    _helper.jbl_destroy(ref jbl);
                }
            }
        }
Пример #10
0
        public void Reset(EJDB2Handle jql)
        {
            if (jql.IsInvalid)
            {
                throw new ArgumentException("Invalid JQL handle.");
            }

            if (jql.IsClosed)
            {
                throw new ArgumentException("JQL handle is closed.");
            }

            _helper.jql_reset(jql.DangerousGetHandle(), true, true);
        }
Пример #11
0
        public long GetLimit(EJDB2Handle jql)
        {
            if (jql.IsInvalid)
            {
                throw new ArgumentException("Invalid JQL handle.");
            }

            if (jql.IsClosed)
            {
                throw new ArgumentException("JQL handle is closed.");
            }

            ulong rc = _helper.jql_get_limit(jql.DangerousGetHandle(), out long limit);

            if (rc != 0)
            {
                throw _e.CreateException(rc);
            }

            return(limit);
        }
Пример #12
0
        public void Patch(EJDB2Handle handle, string collection, string patch, long id, bool upsert)
        {
            if (handle.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (handle.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (patch == null)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            IntPtr db = handle.DangerousGetHandle();

            ulong rc;

            using (var pool = new Utf8StringPool())
            {
                rc = upsert
                    ? _helper.ejdb_merge_or_put(db, pool.GetString(collection), pool.GetString(patch), id)
                    : _helper.ejdb_patch(db, pool.GetString(collection), pool.GetString(patch), id);
            }

            if (rc != 0)
            {
                throw _e.CreateException(rc);
            }
        }
Пример #13
0
        public void SetNull(EJDB2Handle jql, int pos, string placeholder)
        {
            if (jql.IsInvalid)
            {
                throw new ArgumentException("Invalid JQL handle.");
            }

            if (jql.IsClosed)
            {
                throw new ArgumentException("JQL handle is closed.");
            }

            ulong rc;

            using (var ph = new Utf8String(placeholder))
            {
                rc = _helper.jql_set_null(jql.DangerousGetHandle(), ph, pos);
            }

            if (rc != 0)
            {
                throw _e.CreateException(rc);
            }
        }
Пример #14
0
        public long ExecuteScalarInt64(EJDB2Handle db, EJDB2Handle jql,
                                       long skip, long limit, StringWriter explain)
        {
            if (db.IsInvalid)
            {
                throw new ArgumentException("Invalid DB handle.");
            }

            if (db.IsClosed)
            {
                throw new ArgumentException("DB handle is closed.");
            }

            if (jql.IsInvalid)
            {
                throw new ArgumentException("Invalid JQL handle.");
            }

            if (jql.IsClosed)
            {
                throw new ArgumentException("JQL handle is closed.");
            }

            IntPtr log = IntPtr.Zero;

            try
            {
                if (explain != null)
                {
                    log = _helper.iwxstr_new();
                    if (log == IntPtr.Zero)
                    {
                        throw new InvalidOperationException("iwxstr_new failed.");
                    }
                }

                var ux = new EJDB_EXEC
                {
                    db      = db.DangerousGetHandle(),
                    q       = jql.DangerousGetHandle(),
                    skip    = skip > 0 ? skip : 0,
                    limit   = limit > 0 ? limit : 0,
                    opaque  = IntPtr.Zero,
                    visitor = null,
                    log     = log,
                };

                ulong rc = _helper.ejdb_exec(ref ux);
                if (rc != 0)
                {
                    throw _e.CreateException(rc);
                }

                if (log != IntPtr.Zero)
                {
                    string slog = Utf8String.FromIntPtr(_helper.iwxstr_ptr(log));
                    explain.Write(slog);
                }

                return(ux.cnt);
            }
            finally
            {
                if (log != IntPtr.Zero)
                {
                    _helper.iwxstr_destroy(log);
                }
            }
        }
Пример #15
0
        public void SetString(EJDB2Handle jql, int pos, string placeholder, string val, SetStringType type)
        {
            if (jql.IsInvalid)
            {
                throw new ArgumentException("Invalid JQL handle.");
            }

            if (jql.IsClosed)
            {
                throw new ArgumentException("JQL handle is closed.");
            }

            if (val == null)
            {
                throw new ArgumentNullException(nameof(val));
            }

            IntPtr q = jql.DangerousGetHandle();

            if (type == SetStringType.Json)
            {
                IntPtr pool = IntPtr.Zero;

                try
                {
                    pool = _helper.iwpool_create((UIntPtr)1024);
                    if (pool == IntPtr.Zero)
                    {
                        throw new InvalidOperationException("iwpool_create failed.");
                    }

                    using (var spool = new Utf8StringPool())
                    {
                        ulong rc = _helper.jbn_from_json(spool.GetString(val), out JBL_NODE node, ref pool);
                        if (rc != 0)
                        {
                            throw _e.CreateException(rc);
                        }

                        rc = _helper.jql_set_json2(q, spool.GetString(placeholder), pos, ref node, FreePool, pool);
                        if (rc != 0)
                        {
                            throw _e.CreateException(rc);
                        }
                    }
                }
                catch (Exception)
                {
                    FreePool(pool, IntPtr.Zero);
                }
            }
            else if (type == SetStringType.Regexp)
            {
                IntPtr str = IntPtr.Zero;

                try
                {
                    ulong rc;
                    using (var ph = new Utf8String(placeholder))
                    {
                        str = Utf8String.CreateUnmanaged(val);
                        rc  = _helper.jql_set_regexp2(q, ph, pos, str, FreeStringMem, IntPtr.Zero);
                    }

                    if (rc != 0)
                    {
                        throw _e.CreateException(rc);
                    }
                }
                catch (Exception)
                {
                    FreeStringMem(str, IntPtr.Zero);
                }
            }
            else
            {
                IntPtr str = IntPtr.Zero;

                try
                {
                    ulong rc;
                    using (var ph = new Utf8String(placeholder))
                    {
                        str = Utf8String.CreateUnmanaged(val);
                        rc  = _helper.jql_set_str2(q, ph, pos, str, FreeStringMem, IntPtr.Zero);
                    }

                    if (rc != 0)
                    {
                        throw _e.CreateException(rc);
                    }
                }
                catch (Exception)
                {
                    FreeStringMem(str, IntPtr.Zero);
                }
            }
        }