Ejemplo n.º 1
0
        private List <StockItem> ReadStockItems(Vendor vendor)
        {
            Databases        dbs    = Databases.Instance;
            List <StockItem> items  = new List <StockItem>();
            DbBTreeCursor    cursor = dbs.VendorNameIdx.OpenCursor(null, DbFileCursor.CreateFlags.None);

            try {
                DbEntry secKeyEntry = dbs.Fmt.ToDbEntry <string>(vendor.Name);

#if false
                // same as below, but using standard cursor methods
                DbEntry    keyEntry  = DbEntry.Out(keyBuffer);
                DbEntry    dataEntry = DbEntry.Out(dataBuffer);
                ReadStatus status;
                status = cursor.PGetAt(ref secKeyEntry, ref keyEntry, ref dataEntry,
                                       DbcFile.GetAtMode.Set, DbcFile.ReadFlags.None);
                // we are not checking for ReadStatus.BufferSmall, but maybe we should
                while (status == ReadStatus.Success)
                {
                    StockItem item = null;
                    index = dataEntry.Start;
                    dbs.Formatter.Deserialize <StockItem>(ref item, dataEntry.Buffer, ref index);
                    items.Add(item);
                    status = cursor.PGet(ref secKeyEntry, ref keyEntry, ref dataEntry,
                                         DbcFile.GetMode.NextDup, DbcFile.ReadFlags.None);
                }
#endif
#if true
                // same as above, but using a foreach loop (which closes the cursor automatically)
                foreach (KeyDataPair entry in cursor.ItemsAt(ref secKeyEntry, DbFileCursor.ReadFlags.None))
                {
                    StockItem item = null;
                    dbs.Fmt.FromDbEntry <StockItem>(ref item, ref entry.Data);
                    items.Add(item);
                }
#endif
            }
            finally {
                cursor.Close();
            }

            inventoryView.DataSource = null;
            DataGridViewColumn col = new DataGridViewTextBoxColumn();
            col.Name             = "NameCol";
            col.HeaderText       = "Name";
            col.DataPropertyName = "Name";
            inventoryView.Columns.Add(col);
            inventoryView.DataSource = items;
            return(items);
        }
Ejemplo n.º 2
0
        private bool GetNextRecord(DbBTreeCursor cursor, ref Customer cust)
        {
            ReadStatus status;

            // If we need to call SetLength further down, and that causes its value to
            // increase, then it will clear all buffer content between the old and the
            // new length, overwriting some of our data. So we set it to the maximum here.
            keyStream.SetLength(keyStream.Capacity);
            dataStream.SetLength(dataStream.Capacity);
            // we base our DbEntry instances on the serialization stream buffers
            DbEntry key  = DbEntry.Out(keyStream.GetBuffer());
            DbEntry data = DbEntry.Out(dataStream.GetBuffer());

            do
            {
                status = cursor.Get(ref key, ref data, DbFileCursor.GetMode.Next, DbFileCursor.ReadFlags.None);
                switch (status)
                {
                case ReadStatus.NotFound: return(false);

                case ReadStatus.KeyEmpty: continue; // skip deleted records

                case ReadStatus.BufferSmall:
                    if (key.Buffer.Length < key.Size)
                    {
                        keyStream.SetLength(key.Size);
                        key = DbEntry.Out(keyStream.GetBuffer());
                    }
                    if (data.Buffer.Length < data.Size)
                    {
                        dataStream.SetLength(data.Size);
                        data = DbEntry.Out(dataStream.GetBuffer());
                    }
                    continue;

                case ReadStatus.Success:
                    dataStream.Position = 0;
                    dataStream.SetLength(data.Size);
                    cust = (Customer)formatter.Deserialize(dataStream);
                    return(true);

                default:
                    return(false);
                }
            } while (true);
        }
Ejemplo n.º 3
0
        public IList <T> Lookup <T>(ICondition condition) where T : class
        {
            dbName = typeof(T).FullName;
            initDatabase();
            List <T> result = new List <T>( );

            Txn trans = env.TxnBegin(null, Txn.BeginFlags.None);

            switch (condition.Name)
            {
            case "SELECT_ALL":
                using (DbBTreeCursor cursor = dbInstance.OpenCursor(null, DbFileCursor.CreateFlags.None)) {
                    T cust = null;

                    IEnumerable <KeyDataPair> lst = cursor.ItemsForward(false, DbFileCursor.ReadFlags.None);

                    foreach (KeyDataPair pair in lst)
                    {
                        using (MemoryStream mem = new MemoryStream(pair.Data.Buffer)) {
                            T t = (T)formatter.Deserialize(mem);

                            result.Add(t);
                        }
                    }
                }
                break;

            case "SELECT_BY_CODE":

                SelectByCodeCondition cond = condition as SelectByCodeCondition;

                if (cond == null)
                {
                    throw new InvalidDataException(String.Format("Invalid {0} condition", condition.Name));
                }
                foreach (int i in cond.HashCodes)
                {
                    T            t     = null;
                    byte[]       buf   = new byte[1024];
                    MemoryStream key   = new MemoryStream( );
                    MemoryStream value = new MemoryStream();
                    formatter.Serialize(key, i);

                    DbEntry dbKey = DbEntry.InOut(key.ToArray());
                    DbEntry dbVal = DbEntry.Out(buf);
                    while (true)
                    {
                        ReadStatus status =
                            dbInstance.GetExact(trans, ref dbKey, ref dbVal, DbFile.ReadFlags.None);


                        switch (status)
                        {
                        case ReadStatus.BufferSmall:
                            if (dbVal.Buffer.Length < dbVal.Size)
                            {
                                value.SetLength(dbVal.Size);
                                dbVal = DbEntry.Out(value.GetBuffer());
                            }
                            continue;

                        case ReadStatus.KeyEmpty:
                            goto brk;

                        case ReadStatus.NotFound:
                            goto brk;

                        case ReadStatus.Success:
                            value.Position = 0;
                            value.SetLength(dbVal.Size);
                            value.Write(dbVal.Buffer, 0, dbVal.Size);
                            value.Flush();
                            value.Seek(0, SeekOrigin.Begin);
                            t = (T)formatter.Deserialize(value);

                            result.Add(t);

                            key.Dispose();
                            value.Dispose();
                            break;
                        }
                    }
                    brk :;
                }

                break;

            default:
                throw new NotImplementedException(String.Format("Condition {0} is not implemented", condition.Name));
            }
            trans.Commit(Txn.CommitMode.Sync);
            env.Close();
            return(result);
        }