示例#1
0
 public void Erase()
 {
     Cursor c = new Cursor(db);
     byte[] k1 = new byte[5]; k1[0] = 5;
     byte[] r1 = new byte[5]; r1[0] = 1;
     c.Insert(k1, r1);
     c.Erase();
 }
示例#2
0
 public void AutoCleanupCursors()
 {
     Database db = new Database();
     db.Create("ntest.db");
     Cursor cursor = new Cursor(db);
     // let gc do the cleanup
     db.Close(HamConst.HAM_AUTO_CLEANUP);
 }
示例#3
0
 public void EraseNegative()
 {
     Cursor c = new Cursor(db);
     try {
         c.Erase();
     }
     catch (DatabaseException e) {
         Assert.AreEqual(HamConst.HAM_CURSOR_IS_NIL, e.ErrorCode);
     }
 }
示例#4
0
 public void CursorTest()
 {
     Transaction t=env.Begin();
     Cursor c = new Cursor(db, t);
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     c.Insert(k, r);
     db.Find(t, k);
     c.Close();
     t.Commit();
     db.Find(k);
 }
示例#5
0
 public void AutoCleanupCursors4()
 {
     Database db = new Database();
     db.Create("ntest.db");
     Cursor cursor1 = new Cursor(db);
     Cursor cursor2 = cursor1.Clone();
     Cursor cursor3 = cursor1.Clone();
     Cursor cursor4 = cursor1.Clone();
     Cursor cursor5 = cursor1.Clone();
     cursor3.Close();
     cursor5.Close();
     // let gc do the cleanup
     db.Close(HamConst.HAM_AUTO_CLEANUP);
 }
        public override IEnumerable<KeyValuePair<long, Tick>> Read()
        {
            Cursor cursor = new Cursor(database);

            try
            {
                foreach (var item in cursor.Forward())
                    yield return new KeyValuePair<long, Tick>(Reverse(item.Key), ToTick(item.Value));
            }
            finally
            {
                cursor.Close();
            }
        }
示例#7
0
 public void Find()
 {
     Cursor c = new Cursor(db);
     byte[] k1 = new byte[5]; k1[0] = 5;
     byte[] k2 = new byte[5]; k2[0] = 6;
     byte[] r1 = new byte[5]; r1[0] = 1;
     byte[] r2 = new byte[5]; r2[0] = 2;
     db.Insert(k1, r1);
     db.Insert(k2, r2);
     c.Find(k1);
     byte[] f = c.GetRecord();
     checkEqual(r1, f);
     c.Find(k2);
     byte[] g = c.GetRecord();
     checkEqual(r2, g);
 }
示例#8
0
 public void MovePrevious()
 {
     Cursor c = new Cursor(db);
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     db.Insert(k, r);
     c.MovePrevious();
 }
示例#9
0
 public void Overwrite()
 {
     Cursor c = new Cursor(db);
     byte[] k = new byte[5];
     byte[] r1 = new byte[5]; r1[0] = 1;
     byte[] r2 = new byte[5]; r2[0] = 2;
     db.Insert(k, r1);
     c.MoveFirst();
     byte[] f = c.GetRecord();
     checkEqual(r1, f);
     c.Overwrite(r2);
     byte[] g = c.GetRecord();
     checkEqual(r2, g);
 }
示例#10
0
        public void Move()
        {
            Cursor c = new Cursor(db);
            byte[] k = new byte[5];
            byte[] r = new byte[5];

            k[0] = 0;
            db.Insert(k, r);
            k[0] = 1;
            db.Insert(k, r);
            k[0] = 2;
            db.Insert(k, r);
            k[0] = 3;
            db.Insert(k, r);
            k[0] = 4;
            db.Insert(k, r);

            c.Move(HamConst.HAM_CURSOR_NEXT);
            c.Move(HamConst.HAM_CURSOR_NEXT);
            c.Move(HamConst.HAM_CURSOR_PREVIOUS);
            c.Move(HamConst.HAM_CURSOR_LAST);
            c.Move(HamConst.HAM_CURSOR_FIRST);
        }
示例#11
0
 public void MoveNegative()
 {
     Cursor c = new Cursor(db);
     try {
         c.Move(HamConst.HAM_CURSOR_NEXT);
     }
     catch (DatabaseException e) {
         Assert.AreEqual(HamConst.HAM_KEY_NOT_FOUND, e.ErrorCode);
     }
 }
示例#12
0
        public void InsertDuplicate()
        {
            Cursor c = new Cursor(db);
            byte[] q;
            byte[] k1 = new byte[5]; k1[0] = 5;
            byte[] r1 = new byte[5]; r1[0] = 1;
            byte[] r2 = new byte[5]; r2[0] = 2;
            c.Insert(k1, r1);
            q = c.GetRecord();
            checkEqual(r1, q);
            q = c.GetKey();
            checkEqual(k1, q);

            c.Insert(k1, r2, HamConst.HAM_DUPLICATE);
            q = c.GetRecord();
            checkEqual(r2, q);
            q = c.GetKey();
            checkEqual(k1, q);
        }
示例#13
0
        public void InsertNegative()
        {
            Cursor c = new Cursor(db);
            byte[] q;
            byte[] k1 = new byte[5]; k1[0] = 5;
            byte[] r1 = new byte[5]; r1[0] = 1;
            byte[] r2 = new byte[5]; r2[0] = 2;
            c.Insert(k1, r1);
            q = c.GetRecord();
            checkEqual(r1, q);
            q = c.GetKey();
            checkEqual(k1, q);

            try {
                c.Insert(k1, r2);
            }
            catch (DatabaseException e) {
                Assert.AreEqual(HamConst.HAM_DUPLICATE_KEY, e.ErrorCode);
            }
        }
示例#14
0
        private void Cursor10000Test()
        {
            //create database
            Hamster.Environment env = new Hamster.Environment();
            env.Create("ntest.db");

            Parameter[] param = new Parameter[1];
            param[0] = new Parameter();
            param[0].name = HamConst.HAM_PARAM_KEY_TYPE;
            param[0].value = HamConst.HAM_TYPE_UINT64;
            Database db = env.CreateDatabase(1, 0, param);

            //insert records
            for (ulong i = 0; i < 10000; i++)
            {
                byte[] key = BitConverter.GetBytes(i);
                byte[] record = new byte[20];
                db.Insert(key, record);
            }

            //close database
            db.Close();

            //reopen again
            db = env.OpenDatabase(1);
            Cursor cursor = new Cursor(db);

            cursor.MoveFirst();
            ulong firstKey = BitConverter.ToUInt64(cursor.GetKey(), 0);
            Assert.AreEqual((ulong)0, firstKey);

            cursor.MoveLast();
            ulong lastKey = BitConverter.ToUInt64(cursor.GetKey(), 0);
            Assert.AreEqual((ulong)9999, lastKey);

            //close database
            cursor.Close();
            db.Close();
            env.Close();
        }
示例#15
0
 private void Create()
 {
     Cursor c = new Cursor(db);
     c.Close();
 }
示例#16
0
 /// <summary>
 /// Adds a Cursor to the Cursor list
 /// </summary>
 public void AddCursor(Cursor c)
 {
     cursors.Add(c);
 }
示例#17
0
        private void ApproxMatching()
        {
            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            byte[] k1 = new byte[5];
            byte[] r1 = new byte[5];
            k1[0] = 1; r1[0] = 1;
            byte[] k2 = new byte[5];
            byte[] r2 = new byte[5];
            k2[0] = 2; r2[0] = 2;
            byte[] k3 = new byte[5];
            byte[] r3 = new byte[5];
            k3[0] = 3; r3[0] = 3;
            try
            {
                env.Create("ntest.db");
                db = env.CreateDatabase(1);
                db.Insert(k1, r1);
                db.Insert(k2, r2);
                db.Insert(k3, r3);

                Cursor c = new Cursor(db);
                byte[] r = c.Find(k2, HamConst.HAM_FIND_GT_MATCH);
                checkEqual(r, r3);
                checkEqual(k2, k3);
                k2[0] = 2;
                r = c.Find(k2, HamConst.HAM_FIND_GT_MATCH);
                checkEqual(r, r1);
                checkEqual(k2, k1);
                db.Close();
                env.Close();
            }
            catch (DatabaseException e)
            {
                Assert.Fail("unexpected exception " + e);
            }
        }
示例#18
0
 private void TryFind()
 {
     Cursor c = new Cursor(db);
     byte[] k1 = new byte[5]; k1[0] = 5;
     byte[] k2 = new byte[5]; k2[0] = 6;
     byte[] k3 = new byte[5]; k3[0] = 7;
     byte[] r1 = new byte[5]; r1[0] = 1;
     byte[] r2 = new byte[5]; r2[0] = 2;
     db.Insert(k1, r1);
     db.Insert(k2, r2);
     var f = c.TryFind(k1);
     checkEqual(r1, f);
     var g = c.TryFind(k2);
     checkEqual(r2, g);
     var h = c.TryFind(k3);
     Assert.IsNull(h);
 }
示例#19
0
 private void MoveNext()
 {
     Cursor c = new Cursor(db);
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     db.Insert(k, r);
     c.MoveNext();
 }
示例#20
0
 private void GetKey()
 {
     Cursor c = new Cursor(db);
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     db.Insert(k, r);
     c.MovePrevious();
     byte[] f = c.GetKey();
     checkEqual(k, f);
 }
示例#21
0
 public void Clone()
 {
     Cursor c1 = new Cursor(db);
     Cursor c2 = c1.Clone();
 }
示例#22
0
 private void TryMove()
 {
     Cursor c = new Cursor(db);
     byte[] k1 = BitConverter.GetBytes(1UL);
     byte[] r1 = BitConverter.GetBytes(2UL);
     db.Insert(k1, r1);
     byte[] k2 = null, r2 = null;
     Assert.IsTrue(c.TryMove(ref k2, ref r2, HamConst.HAM_CURSOR_NEXT));
     checkEqual(k1, k2);
     checkEqual(r1, r2);
     Assert.IsFalse(c.TryMove(ref k2, ref r2, HamConst.HAM_CURSOR_NEXT));
     Assert.IsNull(k2);
     Assert.IsNull(r2);
 }
示例#23
0
 public void Create()
 {
     Cursor c = new Cursor(db);
     c.Close();
 }
示例#24
0
 private void Clone()
 {
     Cursor c1 = new Cursor(db);
     Cursor c2 = c1.Clone();
 }
示例#25
0
 /// <summary>
 /// Removes a Cursor from the Cursor list
 /// </summary>
 public void RemoveCursor(Cursor c)
 {
     cursors.Remove(c);
 }
示例#26
0
 /// <summary>
 /// Clones a Database Cursor
 /// </summary>
 /// <remarks>
 /// This method wraps the native ham_cursor_clone function.
 /// <br />
 /// Clones an existing Cursor. The new Cursor will point to exactly the
 /// same item as the old Cursor. If the old Cursor did not point
 /// to any item, so will the new Cursor.
 ///
 /// If the old Cursor is bound to a Transaction, then the new
 /// Cursor will also be bound to this Transaction.
 /// </remarks>
 /// <returns>The new Cursor object</returns>
 /// <exception cref="DatabaseException">
 ///   <list type="bullet">
 ///   <item><see cref="HamConst.HAM_OUT_OF_MEMORY"/>
 ///     if the new structure could not be allocated</item>
 ///   </list>
 /// </exception>
 public Cursor Clone()
 {
     IntPtr newHandle = new IntPtr(0);
       lock (this.db) {
     int st = NativeMethods.CursorClone(handle, out newHandle);
     if (st != 0)
       throw new DatabaseException(st);
     Cursor c = new Cursor(db, newHandle);
     db.AddCursor(c);
     return c;
       }
 }
示例#27
0
        public void GetDuplicateCount()
        {
            Cursor c = new Cursor(db);
            byte[] k1 = new byte[5]; k1[0] = 5;
            byte[] r1 = new byte[5]; r1[0] = 1;
            byte[] r2 = new byte[5]; r2[0] = 2;
            byte[] r3 = new byte[5]; r2[0] = 2;
            c.Insert(k1, r1);
            Assert.AreEqual(1, c.GetDuplicateCount());

            c.Insert(k1, r2, HamConst.HAM_DUPLICATE);
            Assert.AreEqual(2, c.GetDuplicateCount());

            c.Insert(k1, r3, HamConst.HAM_DUPLICATE);
            Assert.AreEqual(3, c.GetDuplicateCount());

            c.Erase();
            c.MoveFirst();
            Assert.AreEqual(2, c.GetDuplicateCount());
        }
示例#28
0
 public void GetRecord()
 {
     Cursor c = new Cursor(db);
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     db.Insert(k, r);
     c.MovePrevious();
     byte[] f = c.GetRecord();
     checkEqual(r, f);
 }
示例#29
0
        static void Main(string[] args)
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            Hamster.Environment env = new Hamster.Environment();
            Database[] db=new Database[3];
            Cursor[] cursor=new Cursor[3];

            /*
             * set up the customer and order data - these arrays will later
             * be inserted into the Databases
             */
            Customer[] customers=new Customer[4];
            customers[0] = new Customer(1, "Alan Antonov Corp.");
            customers[1] = new Customer(2, "Barry Broke Inc.");
            customers[2] = new Customer(3, "Carl Caesar Lat.");
            customers[3] = new Customer(4, "Doris Dove Brd.");

            Order[] orders = new Order[8];
            orders[0] = new Order(1, 1, "Joe");
            orders[1] = new Order(2, 1, "Tom");
            orders[2] = new Order(3, 3, "Joe");
            orders[3] = new Order(4, 4, "Tom");
            orders[4] = new Order(5, 3, "Ben");
            orders[5] = new Order(6, 3, "Ben");
            orders[6] = new Order(7, 4, "Chris");
            orders[7] = new Order(8, 1, "Ben");

            /*
             * Create a new Environment
             */
            env.Create("test.db");

            /*
             * then create the three Databases in this Environment; each Database
             * has a name - the first is our "customer" Database, the second
             * is for the "orders"; the third manages our 1:n relation and
             * therefore needs to enable duplicate keys
             */
            db[DBIDX_CUSTOMER] = env.CreateDatabase(DBNAME_CUSTOMER);
            db[DBIDX_ORDER]    = env.CreateDatabase(DBNAME_ORDER);
            db[DBIDX_C2O]      = env.CreateDatabase(DBNAME_C2O,
                    HamConst.HAM_ENABLE_DUPLICATES);

            /*
             * create a Cursor for each Database
             */
            cursor[DBIDX_CUSTOMER] = new Cursor(db[DBIDX_CUSTOMER]);
            cursor[DBIDX_ORDER] = new Cursor(db[DBIDX_ORDER]);
            cursor[DBIDX_C2O] = new Cursor(db[DBIDX_C2O]);

            /*
             * Insert the customers in the customer Database
             *
             * INSERT INTO customers VALUES (1, "Alan Antonov Corp.");
             * INSERT INTO customers VALUES (2, "Barry Broke Inc.");
             * etc.
             */
            for (int i = 0; i < customers.GetLength(0); i++) {
                byte[] key = customers[i].GetKey();
                byte[] rec = customers[i].GetRecord();

                db[DBIDX_CUSTOMER].Insert(key, rec);
            }

            /*
             * Insert the orders in the order Database
             *
             * INSERT INTO orders VALUES (1, "Joe");
             * INSERT INTO orders VALUES (2, "Tom");
             * etc.
             */
            for (int i = 0; i < orders.GetLength(0); i++) {
                byte[] key = orders[i].GetKey();
                byte[] rec = orders[i].GetRecord();

                db[DBIDX_ORDER].Insert(key, rec);
            }

            /*
             * and now the 1:n relationships; the flag HAM_DUPLICATE creates
             * a duplicate key, if the key already exists
             *
             * INSERT INTO c2o VALUES (1, 1);
             * INSERT INTO c2o VALUES (2, 1);
             * etc
             */
            for (int i = 0; i < orders.GetLength(0); i++) {
                byte[] key = orders[i].GetCustomerKey();
                byte[] rec = orders[i].GetKey();

                db[DBIDX_C2O].Insert(key, rec, HamConst.HAM_DUPLICATE);
            }

            /*
             * now start the queries - we want to dump each customer and
             * his orders
             *
             * loop over the customer; for each customer, loop over the
             * 1:n table and pick those orders with the customer id.
             * then load the order and print it
             *
             * the outer loop is similar to
             * SELECT * FROM customers WHERE 1;
             */
            while (1 == 1) {
                Customer c;

                try {
                    cursor[DBIDX_CUSTOMER].MoveNext();
                }
                catch (DatabaseException e) {
                    // reached end of Database?
                    if (e.ErrorCode == HamConst.HAM_KEY_NOT_FOUND)
                        break;
                    Console.Out.WriteLine("cursor.MoveNext failed: " + e);
                    return;
                }

                // load the customer
                c = new Customer(cursor[DBIDX_CUSTOMER].GetKey(),
                        cursor[DBIDX_CUSTOMER].GetRecord());

                // print information about this customer
                Console.Out.WriteLine("customer " + c.id + " ('" + c.name + "')");

                /*
                 * loop over the 1:n table
                 *
                 * SELECT * FROM customers, orders, c2o
                 *      WHERE c2o.customer_id=customers.id AND
                 *          c2o.order_id=orders.id;
                 */
                try {
                    cursor[DBIDX_C2O].Find(c.GetKey());
                }
                catch (DatabaseException e) {
                    // no order for this customer?
                    if (e.ErrorCode == HamConst.HAM_KEY_NOT_FOUND)
                        continue;
                    Console.Out.WriteLine("cursor.Find failed: " + e);
                    return;
                }

                do {
                    /*
                     * load the order; orderId is a byteArray with the ID of the
                     * Order; the record of the item is a byteArray with the
                     * name of the assigned employee
                     *
                     * SELECT * FROM orders WHERE id = order_id;
                     */
                    byte[] orderId = cursor[DBIDX_C2O].GetRecord();
                    cursor[DBIDX_ORDER].Find(orderId);
                    String assignee = enc.GetString(cursor[DBIDX_ORDER].GetRecord());

                    Console.Out.WriteLine("  order: " + BitConverter.ToInt32(orderId, 0) +
                        " (assigned to " + assignee + ")");

                    /*
                     * move to the next order of this customer
                     *
                     * the flag HAM_ONLY_DUPLICATES restricts the cursor
                     * movement to the duplicates of the current key.
                     */
                    try {
                        cursor[DBIDX_C2O].MoveNext(HamConst.HAM_ONLY_DUPLICATES);
                    }
                    catch (DatabaseException e) {
                        // no more orders for this customer?
                        if (e.ErrorCode == HamConst.HAM_KEY_NOT_FOUND)
                            break;
                        Console.Out.WriteLine("cursor.MoveNext failed: " + e);
                        return;
                    }
                } while (1==1);
            }
        }
示例#30
0
        public void Insert()
        {
            Cursor c = new Cursor(db);
            byte[] q;
            byte[] k1 = new byte[5]; k1[0] = 5;
            byte[] k2 = new byte[5]; k2[0] = 6;
            byte[] r1 = new byte[5]; r1[0] = 1;
            byte[] r2 = new byte[5]; r2[0] = 2;
            c.Insert(k1, r1);
            q = c.GetRecord();
            checkEqual(r1, q);
            q = c.GetKey();
            checkEqual(k1, q);

            c.Insert(k2, r2);
            q = c.GetRecord();
            checkEqual(r2, q);
            q = c.GetKey();
            checkEqual(k2, q);
        }