Example #1
0
        private static EJDB_OPTS MapOptions(EJDB2Options options, Utf8StringPool pool)
        {
            var result = new EJDB_OPTS();

            result.no_wal                         = !options.UseWAL;
            result.sort_buffer_sz                 = options.SortBufferSize;
            result.document_buffer_sz             = options.DocumentBufferSize;
            result.kv.random_seed                 = options.IWKVOptions.RandomSeed;
            result.kv.oflags                      = options.IWKVOptions.OpenFlags;
            result.kv.file_lock_fail_fast         = options.IWKVOptions.FileLockFailFast;
            result.kv.path                        = pool.GetString(options.IWKVOptions.Path);
            result.kv.wal.check_crc_on_checkpoint = options.IWKVOptions.WALOptions.CheckCRCOnCheckpoint;
            result.kv.wal.savepoint_timeout_sec   = options.IWKVOptions.WALOptions.SavePointTimeoutSec;
            result.kv.wal.checkpoint_timeout_sec  = options.IWKVOptions.WALOptions.CheckPointTimeoutSec;
            result.kv.wal.wal_buffer_sz           = new UIntPtr(options.IWKVOptions.WALOptions.BufferSize);
            result.kv.wal.checkpoint_buffer_sz    = options.IWKVOptions.WALOptions.CheckpointBufferSize;
            result.http.enabled                   = options.HttpOptions.Enabled;
            result.http.port                      = options.HttpOptions.Port;
            result.http.bind                      = options.HttpOptions.Bind;
            result.http.access_token              = options.HttpOptions.AccessToken;
            result.http.access_token_len          = new UIntPtr((uint)(options.HttpOptions.AccessToken?.Length ?? 0));
            result.http.read_anon                 = options.HttpOptions.ReadAnon;
            result.http.max_body_size             = new UIntPtr(options.HttpOptions.MaxBodySize);

            return(result);
        }
Example #2
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);
            }
        }
Example #3
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);
            }
        }
Example #4
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);
                }
            }
        }
Example #5
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;
            }
        }
Example #6
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);
            }
        }
Example #7
0
        public EJDB2Handle Open(EJDB2Options options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            IntPtr handle;
            ulong  rc;

            using (var pool = new Utf8StringPool())
            {
                EJDB_OPTS opts = MapOptions(options, pool);
                rc = _helper.ejdb_open(ref opts, out handle);
            }

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

            return(new EJDB2Handle(handle));
        }
Example #8
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);
                }
            }
        }