Search Rocket site

Improve Program Performance by up to 300 Times Faster Using the Rocket U2 Toolkit Driver

Paul Chang

November 22, 2017

If you are trying to improve performance on a client-server application accessing UniVerse or UniData data files, then this blog is written for you. Minor code changes are required, but these changes can dramatically improve program performance, making it 100 to 300 times faster.

Historically, most client-server or web applications accessing a chunk of information from UniVerse or UniData files will use the SELECT command to generate an active list. After the result lists have been created, the simple program logic will retrieve data based on the key value from the active list one by one.

The following VB.NET sample program demonstrates this function.

Dim u2connect As U2.Data.Client.U2Connection
Dim u2session As U2.Data.Client.UO.UniSession
Dim u2Select As U2.Data.Client.UO.UniSelectList
Dim u2cmd As U2.Data.Client.UO.UniCommand
Dim u2file As U2.Data.Client.UO.UniFile

— Create a new u2session with native mode —

U2file = u2session.CreateUniFile(“U2FILE”)
u2Select = u2session.CreateUniSelectList(0)

u2cmd = u2session.CreateUniCommand()
u2cmd.Command = “CLEARSELECT”
u2cmd.Execute()
u2cmd.Command = “SELECT U2FILE BY @ID”
u2cmd.Execute()

u2file.RecordID = u2Select.Next
Do While Not u2Select.LastRecordRead
u2DArray = u2file.Read()
— Process data —

u2file.RecordID = u2Select.Next
Loop

This simple program logic works with all UniVerse and UniData files, even if the file contains 9M records.

In the example below, we want to retrieve 1,000 records at a time and improve the program performance significantly. The U2 Toolkit driver provides a new ReadRecords function to read a large chunk of data using a key list.

The following VB.NET program demonstrates this program logic to improve performance.

Dim u2connect As U2.Data.Client.U2Connection
Dim u2session As U2.Data.Client.UO.UniSession
Dim u2Select As U2.Data.Client.UO.UniSelectList
Dim u2cmd As U2.Data.Client.UO.UniCommand
Dim u2file As U2.Data.Client.UO.UniFile

Dim u2Set As UniDataSet
Dim items() As String
Dim TotalRecords As Integer = 0
Dim i As Integer = 0
Dim nox As Integer = 0
Dim count_record As Integer = 0

— Create a new u2session with native mode —

U2file = u2session.CreateUniFile(“U2FILE”)
u2Select = u2session.CreateUniSelectList(0)

u2cmd = u2session.CreateUniCommand()
u2cmd.Command = “CLEARSELECT”
u2cmd.Execute()
u2cmd.Command = “SELECT U2FILE BY @ID”
u2cmd.Execute()

TotalRecords = u2cmd.CommandAtSelected

items = u2Select.ReadListAsStringArray()
Dim list As New List(Of String)
For i = 0 To items.GetUpperBound(0)
list.Add(items(i))
nox = nox + 1
count_record = count_record + 1

— Read data every 1,000 records —
If nox = 1000 Or count_record = totalrecords Then
u2Set = u2file.ReadRecords(list.ToArray())

For Each item As UniRecord In u2Set
u2DArray = item.Record
— Process data —

Next

nox = 0

list.Clear()

End If

Next

The above program logic will work for most UniVerse and UniData files with less than 1M records. When the U2 file contains more than 1M records, then the ReadListAsStringArray() function might cause an out-of-memory error. We need another way to handle large UniVerse and UniData files containing more than 1M record IDs. First, we can use the SELECT and SAVE.LIST commands or a UniBasic program on a UniVerse or UniData server to create a key file that can be copied or download to the client machine.

Here are the two simple UniVerse/UniData Commands to create a key file – namely getting a key file and downloading.

: SELECT U2FILE BY @ID
: SAVE.LIST U2FILE

The following VB.NET sample program accesses UniVerse and UniData file data using a key file provided by the previous step to improve the program performance. It will work with any UniVerse or UniData file.

Dim u2connect As U2.Data.Client.U2Connection
Dim u2session As U2.Data.Client.UO.UniSession
Dim u2Select As U2.Data.Client.UO.UniSelectList
Dim u2cmd As U2.Data.Client.UO.UniCommand
Dim u2file As U2.Data.Client.UO.UniFile

Dim u2Set As UniDataSet
Dim TotalRecords As Integer = 0
Dim nox As Integer = 0
Dim count_record As Integer = 0

Dim oRead As System.IO.StreamReader

— Create a new u2session with native mode —

— Set the TotalRecords value from a key file or other information —

U2file = u2session.CreateUniFile(“U2FILE”)

oRead = System.IO.File.OpenText(“U2_Key_File”)

Dim list As New List(Of String)

While oRead.Peek <> -1
keyx = oRead.ReadLine()
list.Add(items(i))
nox = nox + 1
count_record = count_record + 1

— Read data every 1,000 records —
If nox = 1000 Or count_record = totalrecords Then
u2Set = u2file.ReadRecords(list.ToArray())

For Each item As UniRecord In u2Set
u2DArray = item.
— Process data —

Next

nox = 0

list.Clear()

End If

End While

oRead.Close()

When working on a small UniVerse or UniData file with less than 10,000 records, you should be able to read the whole file of records at once. We have done some program logic changes on some tools to improve the performance, making it run 100 to 300 times faster. Hopefully, UniVerse and UniData customers can use similar functions to make their applications faster.