コード例 #1
0
        public override void Init(int flowCount, long flowRecordCount)
        {
            string path = Path.Combine(DataDirectory, String.Format("{0}..hamster", CollectionName));

            enviroment = new Hamster.Environment();
            enviroment.Create(path, 0, 0664, new Parameter[] { GetCacheParam(CACHE_SIZE) });
            database = enviroment.CreateDatabase((short)1, 0, new Parameter[] { GetKeySizeParam(8) });
        }
コード例 #2
0
        public override void Init(int flowCount, long flowRecordCount)
        {
            string path = Path.Combine(DataDirectory, String.Format("{0}..hamster", CollectionName));

            enviroment = new Hamster.Environment();
            enviroment.Create(path, 0, 0664, new Parameter[] { GetCacheParam(CACHE_SIZE) });
            database = enviroment.CreateDatabase((short)1, 0, new Parameter[] { GetKeySizeParam(8) });
        }
コード例 #3
0
ファイル: DatabaseTest.cs プロジェクト: GerHobbelt/hamsterdb
 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);
 }
コード例 #4
0
ファイル: DatabaseTest.cs プロジェクト: GerHobbelt/hamsterdb
 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);
 }
コード例 #5
0
ファイル: Cursor.cs プロジェクト: mathieurodic/hamsterdb
 /// <summary>
 /// Constructor which creates a new Cursor in a Transaction
 /// </summary>
 public Cursor(Database db, Transaction txn)
 {
     Create(db, txn);
 }
コード例 #6
0
ファイル: Cursor.cs プロジェクト: mathieurodic/hamsterdb
 /// <summary>
 /// Creates a new Cursor in a Transaction
 /// </summary>
 /// <remarks>
 /// This method wraps the native ham_cursor_create function.
 /// <br />
 /// Creates a new Database Cursor. Cursors can be used to traverse
 /// the Database from start to end or vice versa. Cursors can also
 /// be used to insert, delete or search Database items.
 ///
 /// A newly created Cursor does not point to any item in the Database.
 ///
 /// The application should close all Database Cursors before closing
 /// the Database.
 /// </remarks>
 /// <param name="db">The Database object</param>
 /// <param name="txn">The optional Transaction</param>
 /// <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 void Create(Database db, Transaction txn)
 {
     this.db = db;
       lock (this.db) {
     int st = NativeMethods.CursorCreate(out handle, db.Handle,
         txn != null ? txn.Handle : IntPtr.Zero, 0);
     if (st != 0)
       throw new DatabaseException(st);
     db.AddCursor(this);
       }
 }
コード例 #7
0
ファイル: Cursor.cs プロジェクト: mathieurodic/hamsterdb
 /// <summary>
 /// Closes the Cursor
 /// </summary>
 /// <remarks>
 /// This method wraps the native ham_cursor_close function.
 /// <br />
 /// Closes this Cursor and frees allocated memory. All Cursors should
 /// be closed before closing the Database.
 /// </remarks>
 public void Close()
 {
     if (handle == IntPtr.Zero)
     return;
       int st;
       lock (db) {
     st = NativeMethods.CursorClose(handle);
       }
       if (st != 0)
     throw new DatabaseException(st);
       handle = IntPtr.Zero;
       db = null;
 }
コード例 #8
0
ファイル: CursorTest.cs プロジェクト: GerHobbelt/hamsterdb
 public void SetUp()
 {
     db = new Database();
     db.Create("ntest.db", HamConst.HAM_ENABLE_DUPLICATES);
 }
コード例 #9
0
ファイル: CursorTest.cs プロジェクト: mathieurodic/hamsterdb
        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);
            }
        }
コード例 #10
0
        private void Recovery()
        {
            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            env.Create("ntest.db", HamConst.HAM_ENABLE_RECOVERY);
            db = env.CreateDatabase(1);

            byte[] k = new byte[5];
            byte[] r = new byte[5];
            db.Insert(k, r);
            db.Close();
            env.Close();
        }
コード例 #11
0
        private void EraseKeyTwice()
        {
            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            byte[] k = new byte[5];
            byte[] r = new byte[5];

            env.Create("ntest.db");
            db = env.CreateDatabase(1);
            db.Insert(k, r);
            byte[] r2 = db.Find(k);
            checkEqual(r, r2);
            db.Erase(k);

            try {
                db.Erase(k);
            }
            catch (DatabaseException e) {
                Assert.AreEqual(HamConst.HAM_KEY_NOT_FOUND, e.ErrorCode);
            }
            db.Close();
            env.Close();
        }
コード例 #12
0
 private void EraseKeyNegative()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     byte[] k = new byte[5];
     env.Create("ntest.db");
     db = env.CreateDatabase(1);
     try {
         db.Erase(null);
     }
     catch (NullReferenceException) {
     }
     db.Close();
     env.Close();
 }
コード例 #13
0
 private void DatabaseClose()
 {
     Database db = new Database();
     try {
         db.Close();
     }
     catch (DatabaseException e) {
         Assert.Fail("Unexpected exception " + e);
     }
 }
コード例 #14
0
 private void CreateStringIntIntParameterNeg()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     Parameter[] param = new Parameter[1];
     param[0] = new Parameter();
     param[0].name = HamConst.HAM_PARAM_CACHESIZE;
     param[0].value = 1024;
     try {
         env.Create("ntest.db", HamConst.HAM_IN_MEMORY, 0644, param);
         env.Close();
     }
     catch (DatabaseException e) {
         Assert.AreEqual(HamConst.HAM_INV_PARAMETER, e.ErrorCode);
     }
 }
コード例 #15
0
 private void CreateStringIntIntParameter()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     Parameter[] param = new Parameter[1];
     param[0] = new Parameter();
     param[0].name = HamConst.HAM_PARAM_CACHESIZE;
     param[0].value = 1024;
     try {
         env.Create("ntest.db", 0, 0644, param);
         env.Close();
     }
     catch (DatabaseException e) {
         Assert.Fail("Unexpected exception " + e);
     }
 }
コード例 #16
0
        private Database OpenDatabase(string file)
        {
            List<Parameter> list = new List<Parameter>();

            Parameter param1 = new Parameter();
            param1.name = HamConst.HAM_PARAM_CACHESIZE;
            param1.value = 768 * 1024 * 1024;
            list.Add(param1);

            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            env.Open(file, 0, list.ToArray());
            db = env.OpenDatabase(1);
            db.SetCompareFunc(new CompareFunc(NumericalCompareFunc));
            return db;
        }
コード例 #17
0
 private void FindKeyNull()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     try {
         env.Create("ntest.db");
         db = env.CreateDatabase(1);
         byte[] r = db.Find(null);
     }
     catch (NullReferenceException) {
     }
     db.Close();
     env.Close();
 }
コード例 #18
0
        private void SetComparator()
        {
            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            byte[] k = new byte[5];
            byte[] r = new byte[5];
            Parameter[] param = new Parameter[1];
            param[0] = new Parameter();
            param[0].name = HamConst.HAM_PARAM_KEY_TYPE;
            param[0].value = HamConst.HAM_TYPE_CUSTOM;

            compareCounter = 0;
            try {
                env.Create("ntest.db");
                db = env.CreateDatabase(1, 0, param);
                db.SetCompareFunc(new CompareFunc(MyCompareFunc));
                db.Insert(k, r);
                k[0] = 1;
                db.Insert(k, r);
                db.Close();
                env.Close();
            }
            catch (DatabaseException e) {
                Assert.Fail("unexpected exception " + e);
            }
            Assert.AreEqual(1, compareCounter);
        }
コード例 #19
0
 private void FindUnknownKey()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     byte[] k = new byte[5];
     try {
         env.Create("ntest.db");
         db = env.CreateDatabase(1);
         byte[] r = db.Find(k);
     }
     catch (DatabaseException e) {
         Assert.AreEqual(HamConst.HAM_KEY_NOT_FOUND, e.ErrorCode);
     }
     db.Close();
     env.Close();
 }
コード例 #20
0
ファイル: CursorTest.cs プロジェクト: mathieurodic/hamsterdb
 private void SetUp()
 {
     env = new Hamster.Environment();
     db = new Database();
     env.Create("ntest.db");
     db = env.CreateDatabase(1, HamConst.HAM_ENABLE_DUPLICATE_KEYS);
 }
コード例 #21
0
        private void GetKeyCount()
        {
            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            env.Create("ntest.db");
            db = env.CreateDatabase(1);

            byte[] k = new byte[5];
            byte[] r = new byte[5];
            Assert.AreEqual(0, db.GetKeyCount());
            db.Insert(k, r);
            Assert.AreEqual(1, db.GetKeyCount());
            k[0] = 1;
            db.Insert(k, r);
            Assert.AreEqual(2, db.GetKeyCount());
            db.Close();
            env.Close();
        }
コード例 #22
0
ファイル: Program.cs プロジェクト: alepharchives/hamsterdb
        static void Main(string[] args)
        {
            byte[] key = new byte[5];
            byte[] record = new byte[5];
            Database db = new Database();

            /*
             * first, create a new Database
             */
            db.Create("test.db");

            /*
             * now we can insert, delete or lookup values in the Database
             *
             * for our test program, we just insert a few values, then look them
             * up, then delete them and try to look them up again (which will fail).
             */
            for (int i = 0; i < LOOP; i++) {
                key[0] = (byte)i;
                record[0] = (byte)i;
                db.Insert(key, record);
            }

            /*
             * now look up all values
             */
            for (int i = 0; i < LOOP; i++) {
                key[0] = (byte)i;
                byte[] r = db.Find(key);

                /*
                 * check if the value is ok
                 */
                if (r[0] != (byte)i) {
                    Console.Out.WriteLine("db.Find() returned bad value");
                    return;
                }
            }

            /*
             * close the Database handle, then re-open it (just to demonstrate how
             * to open a Database file)
             */
            db.Close();
            db.Open("test.db");

            /*
             * now erase all values
             */
            for (int i = 0; i < LOOP; i++) {
                key[0] = (byte)i;
                db.Erase(key);
            }

            /*
             * once more we try to find all values... every db.Find() call must
             * now fail with HAM_KEY_NOT_FOUND
             */
            for (int i = 0; i < LOOP; i++) {
                key[0] = (byte)i;

                try {
                    byte[] r = db.Find(key);
                }
                catch (DatabaseException e) {
                    if (e.ErrorCode != HamConst.HAM_KEY_NOT_FOUND) {
                        Console.Out.WriteLine("db.Find() returned error " + e);
                        return;
                    }
                }
            }

            /*
             * We're done! No need to close the Database handle - it's closed automatically
             */
            Console.Out.WriteLine("Success!");
        }
コード例 #23
0
        private void InsertKey()
        {
            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            byte[] k = new byte[5];
            byte[] r1 = new byte[5];
            byte[] r2;
            try {
                env.Create("ntest.db");
                db = env.CreateDatabase(1);
                k[0] = 1;
                r1[0] = 1;
                db.Insert(k, r1);
                r2 = db.Find(k);
                checkEqual(r1, r2);

                k[0] = 2;
                r1[0] = 2;
                db.Insert(k, r1);
                r2 = db.Find(k);
                checkEqual(r1, r2);

                k[0] = 3;
                r1[0] = 3;
                db.Insert(k, r1);
                r2 = db.Find(k);
                checkEqual(r1, r2);
                db.Close();
                env.Close();
            }
            catch (DatabaseException e) {
                Assert.Fail("unexpected exception " + e);
            }
        }
コード例 #24
0
ファイル: Cursor.cs プロジェクト: mathieurodic/hamsterdb
 /// <summary>
 /// Creates a new Cursor
 /// </summary>
 ///
 /// <see cref="Cursor.Create(Database, Transaction)" />
 public void Create(Database db)
 {
     Create(db, null);
 }
コード例 #25
0
 private void InsertKeyInvalidParam()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     env.Create("ntest.db");
     db = env.CreateDatabase(1);
     try {
         db.Insert(null, r);
     }
     catch (NullReferenceException) {
     }
     try {
         db.Insert(k, null);
     }
     catch (NullReferenceException) {
     }
     try {
         db.Insert(k, r, 9999);
     }
     catch (DatabaseException e) {
         Assert.AreEqual(HamConst.HAM_INV_PARAMETER, e.ErrorCode);
     }
     db.Close();
     env.Close();
 }
コード例 #26
0
ファイル: Cursor.cs プロジェクト: mathieurodic/hamsterdb
 /// <summary>
 /// Constructor which creates a new Cursor
 /// </summary>
 /// <param name="db">The Database of this Cursor</param>
 public Cursor(Database db)
 {
     Create(db);
 }
コード例 #27
0
 private void InsertKeyNegative()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     try {
         env.Create("ntest.db");
         db = env.CreateDatabase(1);
         db.Insert(k, r);
         db.Insert(k, r);
     }
     catch (DatabaseException e) {
         Assert.AreEqual(HamConst.HAM_DUPLICATE_KEY, e.ErrorCode);
     }
     db.Close();
     env.Close();
 }
コード例 #28
0
ファイル: Cursor.cs プロジェクト: mathieurodic/hamsterdb
 /// <summary>
 /// Constructor which creates a new Cursor
 /// </summary>
 /// <param name="db">The Database of this Cursor</param>
 /// <param name="handle">The handle of this Cursor</param>
 internal Cursor(Database db, IntPtr handle)
 {
     this.db = db;
       this.handle = handle;
 }
コード例 #29
0
 private void InsertKeyOverwrite()
 {
     Hamster.Environment env = new Hamster.Environment();
     Database db = new Database();
     byte[] k = new byte[5];
     byte[] r = new byte[5];
     try {
         env.Create("ntest.db");
         db = env.CreateDatabase(1);
         db.Insert(k, r);
         r[0] = 1;
         db.Insert(k, r, HamConst.HAM_OVERWRITE);
         byte[] r2 = db.Find(k);
         checkEqual(r, r2);
         db.Close();
         env.Close();
     }
     catch (DatabaseException e) {
         Assert.Fail("unexpected exception " + e);
     }
 }
コード例 #30
0
ファイル: Program.cs プロジェクト: GerHobbelt/hamsterdb
        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);
            }
        }
コード例 #31
0
        private void InsertRecNo()
        {
            Hamster.Environment env = new Hamster.Environment();
            Database db = new Database();
            byte[] r1 = new byte[5];
            byte[] r2;
            try
            {
                env.Create("ntest.db");
                db = env.CreateDatabase(1, HamConst.HAM_RECORD_NUMBER);
                r1[0] = 1;
                var k = db.InsertRecNo(r1);
                r2 = db.Find(k);
                checkEqual(r1, r2);

                r1[0] = 2;
                k = db.InsertRecNo(r1);
                r2 = db.Find(k);
                checkEqual(r1, r2);

                r1[0] = 3;
                k = db.InsertRecNo(r1);
                r2 = db.Find(k);
                checkEqual(r1, r2);
            }
            catch (DatabaseException e)
            {
                Assert.Fail("unexpected exception " + e);
            }
            db.Close();
            env.Close();
        }