Menu

Possible to Do Get with Unknown Buffer Size?

Help
belsokar
2009-02-04
2013-04-09
  • belsokar

    belsokar - 2009-02-04

    I am currently doing the following:

    I have a LinkedList object of a custom type.

    Example:

                LinkedList<TransactionType> transactions = new LinkedList<TransactionType>();

    I continuously grow that list of transactions, and then I serialize that data using a BinaryFormatter and then write to DB doing the following:

               //Write out transactions to database? memory?
                DbEntry keyEntry;
                DbEntry dataEntry;

                keyEntry = dbs.Fmt.ToDbEntry<string>(key);

                MemoryStream ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, transactions);

                dataEntry = DbEntry.InOut(ms.GetBuffer());

                WriteStatus status = dbs.TransactionsDB.Put(null, ref keyEntry, ref dataEntry);

    When it comes time to read, I'm doing the following:

    LinkedList<Transaction> results = null;

                DbEntry keyEntry;
                DbEntry dataEntry = DbEntry.Out(new byte[1024]);

                BinaryFormatter bf = new BinaryFormatter();

                BerkeleyDb.ReadStatus result;

                keyEntry = dbs.Fmt.ToDbEntry<string>(key);

                result = dbs.TransactionsDB.Get(null, ref keyEntry, ref dataEntry, DbFile.ReadFlags.None);

                if (result == ReadStatus.Success)
                {
                    byte[] buffer = dataEntry.Buffer;
                    MemoryStream ms = new MemoryStream(buffer);
                    results = (LinkedList<Transaction>)bf.Deserialize(ms);

                }

    This works fine as long as the dataEntry.Buffer size is not too small of course.  So my main question is whether or not there is a way to pull that data back without specifying an initial buffer size?  I could size the buffer appropriately, but in most cases, I would be over allocating, which wouldn't be a huge deal, but ideally I wouldn't allocate too much memory when I didn't have to.

    Thanks!

     
    • Karl Waclawek

      Karl Waclawek - 2009-02-05

      In a way, yes, but it requires two calls to the Get() function.
      Have a look at the Overview demo app. There is a method GetNextRecord() in MainFrm.cs, and it deals with ReadStatus.BufferSmall.

      Karl

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.