4D v16

BLOBs

Home

 
4D v16
BLOBs

BLOBs    


 

 

In addition to the manipulations explained in the video, you should also note the following:

When starting out, we always ask the same questions:

  • What good can this serve and under what circumstances?
  • I’m fine with putting “things” inside, but how? and in what order will I get them back out?

Let’s start by tackling the second question:

Even though we can access any byte of the BLOB, in most cases we’ll use what we call FIFO (First In First Out). This means that you retrieve the content of the BLOB in the order in which you put it into the BLOB.
To make a simple analogy, let’s take a bookshelf where you put away books from left to right. You start by putting away a small book on the left and then a large book next to the first one, and then another big book and then a picture of your mother-in-law and a knick-knack from last summer’s vacation, etc. When its time to dust the bookshelf, you don’t think about how many inches from the left side a book is located. You take them in the order they are and you adjust your gaze (and hands) depending on the size of the last object picked.

It’s the same thing in a BLOB, especially because you know what you put away in the BLOB.

For example, when you put away:

  • an integer, it’s 2 bytes
  • a long integer is 4 bytes
  • while a date is 6 bytes long
  • and so on.
  • For text, you indicate how you want it put away and how the length of the text is expressed (C string, Pascal string, etc.).

When re-reading, you will take the information in the same order again and put it away in variables that are adapted to the expected content (typed correctly).

This way the BLOB is read in a consistent manner.
Here’s the answer to the first question (what is the purpose of a BLOB?) You can use a BLOB, for example, in the following cases:

  • To store variables, hierarchical lists, arrays
  • To exchange documents between client and server
  • To protect confidential data in an encrypted BLOB
  • To save variables containing plug-ins
  • To send pictures, texts, or documents to a Web browser
  • To communicate with a web service.

The VARIABLE TO BLOB command stores the data in a format internal to 4D. That’s why the required space is slightly greater than the raw data size. The advantage of this command is that it keeps you from having to manage Byte Swapping (position of the most significant byte) when working cross-platform.

When you fill a BLOB, you can use the * character instead of the $PositionInBlob variable that we used.

When you use this parameter, 4D understands that it must store the variable at the end of the BLOB and increase its size as a result.

Now let's imagine that you have a number of parameters in your database that must be taken into account for each user (or each site where your application is installed):

  • the startup screen
  • the background color
  • the font and size of characters
  • and so on.
    They are really just user preferences.

To keep them, there are several solutions (create a table, a text file, an XML file, etc.).

In our case, we will keep these preferences in a BLOB, which is stored in a file on your disk.

The read and write commands for BLOBs are BLOB TO DOCUMENT and DOCUMENT TO BLOB.

To save space and/or transfer time, you may also want to consider compressing your BLOBs.

Remember that 4D only compresses BLOBs if their size is greater than 255 bytes.

When decompressing BLOBs, remember to check to see whether the BLOB is actually compressed. Otherwise, trying to decompress it will generate an error.

In this video, we're going to learn how to program BLOBs and see how they interact with variables.

BLOBs allow you to store and retrieve all kinds of information.

Let’s use a simple example, say, storing variables. First let's create a method and then we are going to:

  • assign values to three input variables, and three output variables
  • initialize the BLOB and the reading position in the BLOB
  • assign values to the variables
  • and transfer these variables to the BLOB.

The principle is to:

  • use the VARIABLE TO BLOB command
  • indicate the variable to process
  • the BLOB where you want to store it
  • and the position where you want to store the variable in the BLOB.

The PositionInBlob variable works as both an input and output variable, in other words:

  • It indicates where you want to store the variable beforehand
  • And after the command has been executed, this variable contains the position in the BLOB just after the variable so that you can then store the following elements.
  • Once the data is stored in the BLOB, we can do a series of processing and then extract data from the BLOB by writing this:
    We redefine the starting point in the BLOB to position 0.
    We're going to extract from the BLOB the first variable that was entered.
  • Then the 2nd and the 3rd.
  • Normally, when we have finished using a BLOB, we can erase it by setting its size to 0.

We're going to trace this method to see how it fills the BLOB and how it restores the variables.

At this point:

  • All the initializations have been done.
  • The BLOB is empty.
  • When we move to the 1st row, the BLOB contains a certain number of bytes.
  • And gradually it is filled.

To extract data from the BLOB:

  • We go back to position 0 in the BLOB.
  • Then we extract the integer value
  • The date
  • And the text that were stored.
  • Then we empty the BLOB.

This simple example helps us understand the entry and removal of data in a BLOB.

We could store results from statistical arrays in a BLOB so that we can use them again later on.
So we will:

  • Create a BLOB button
    (usually you have to go put it in the XLIFF file)
  • And in this button, we'll create a BLOB
  • And transfer the 2 arrays.

Later, when we want to retrieve the contents of the arrays, we'll need to program this as follows:

  • Initializing the first array
  • Then the other one
  • Setting the position in the BLOB
  • Retrieving both arrays from the BLOB.

During execution:

  • Our arrays contain 7 elements,
  • The BLOB is empty
  • We load the BLOB with the information from the arrays.

Then when we want to retrieve the BLOB contents later on:

  • We start with 2 empty arrays
  • We go to the beginning of the BLOB
  • Then we reload the arrays (Technicians and then Interventions)

 
 

 
PROPERTIES 

Product: 4D
Theme: BLOBs

 
HISTORY 

 
ARTICLE USAGE

Self-training ( 4D v16)