Contents
This topic explains how we built the Lock and Unlock operators sample.
This sample uses paired Lock and Unlock operators to protect shared data against concurrent writes. The sample is intended only to demonstrate how to protect the data, and does not include other concurrent operations which might access the data. Other concurrent operations could derive from data on different streams sharing the query table, or they could be generated by tuples on the same stream, which would have to wait for the tuple being protected by the Lock clearing the Unlock (presumably there would be additional operators between the Lock and Unlock to process the data.)
This example uses the same key for both the key into the Query Table and the key on which to lock for exclusive access.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click → .
-
Select operator from the Applications list.
-
Click OK.
StreamBase Studio creates a single project for the operator samples.
By default, the sample files are installed in:
-
On Windows:
C:\Program Files\StreamBase Systems\StreamBase.n.m\sample\operator -
On UNIX:
/opt/streambase/sample/operator
This sample uses paired Lock and Unlock operators to protect shared data against concurrent writes. The sample is intended only to demonstrate how to protect the data, and does not include other concurrent operations which might access the data. Other concurrent operations could derive from data on different streams sharing the Query Table, or they could be generated by tuples on the same stream, which would have to wait for the tuple being protected by the Lock clearing the Unlock.
Note: Normally there would be additional operators between the Lock and Unlock operators to process the protected data. This sample is kept deliberately simple to demonstrate the basic features.
-
Launched StreamBase Studio
-
Created (or subsequently used) the "sample_operator" project.
-
From the top menu, in the Authoring perspective, selected → → . Selected the
sample_operatorproject and enteredLockUnlockfor the diagram name. -
Dragged an input stream from the palette to the canvas.
-
Clicked it on the canvas, which invoked the Input Stream Properties view.
-
On the General tab, Name
WriteProtectedData -
On the Edit Schema tab, added:
-
Field Name:
protectedDataKey, Type:int -
Field Name:
protectedDataValueInit, Type:string, Size:8
-
-
Created a Query Table:
-
Dragged a Query Table from the palette to the canvas.
-
Clicked it on the canvas, which invoked the Query Table Properties view.
-
On the General tab, Name:
SharedDataQueryTable -
On the Table Settings tab, Type:
Memory -
On the Edit Schema tab, added:
-
Field Name:
sharedDataLockKey, Type:int -
Field Name:
protectedDataValue, Type:string, Size:8
-
-
On the Primary Index tab:
-
Select
sharedDataLockKeyfield and click the ">" icon to move this field into the Selected Fields window.
-
-
On the Secondary Index tab:
-
We did not use a secondary index in this sample.
-
-
-
Created a Lock Set:
-
Dragged a Lock Set from the palette to the canvas.
-
Clicked it on the canvas, which invoked the Lock Set Properties view.
-
On the General tab, Name:
SharedDataLock -
On the Edit Schema tab, added:
-
Field Name:
sharedDataLockKey, Type:int
-
-
-
Created a Lock operator:
-
Dragged a Lock operator from the palette to the canvas.
-
Connected the
WriteProtectedDatainput stream to the Lock operator. -
Connected the
SharedDataLockLock Set to the Lock operator. -
Clicked on Lock operator.
-
On the General tab, Name:
LockSharedData -
On the Lock Settings tab:
-
Expression:
protectedDataKey
-
-
-
Created a Query operator for write operations:
-
Dragged a Query operator from the palette to the canvas.
-
Connected the
SharedDataQueryTabledata construct to the Query operator (this will help fill in some symbol names). -
Connected the
LockSharedDataoutput to the Query operator -
Clicked on Query operator.
-
On the General tab, Name:
WriteSharedData -
On the Query Settings tab:
-
Operation:
Write -
Where:
Primary Index -
Matches:
-
Field Name:
sharedDataLockKey(the value comes from the Query Table) -
Expression:
protectedDataKey
-
-
-
On the Operation Settings tab:
-
Type of Write:
insert -
Values to insert:
-
Expression:
protectedDataValueInit -
If insert fails because no row was found:
Update existing row using value above
-
-
-
On the Output Settings tab:
-
Fields Available from Input Stream: unchecked both boxes
-
Fields Available from Query Table:
-
checked both boxes
-
Rename the outputs to
sharedDataLockKeyandprotectedDataValue, respectively.
We indicated that we want this Query operator to emit both query table fields as output (to the next downstream operator, the
UnlockSharedDataoperator). Notice thenew.<field-name>convention for the new values of these fields (as compared to the way the input fields are qualified:input.<field-name>). -
-
-
-
Created an Unlock operator:
-
Dragged an Unlock operator from the palette to the canvas.
-
Connected the
SharedDataLockdata construct to the Unlock operator. -
Connected the
WriteSharedDataQuery operator to the Unlock operator. -
Clicked on Unlock operator.
-
On the General tab, Name:
UnlockSharedData -
On the Unlock Settings tab:
-
Expression:
sharedDataLockKey
-
-
-
Created an output stream:
-
Dragged an output stream from the palette to the canvas.
-
Clicked on the output stream
-
On the General tab, Name:
ReturnProtectedData -
Connected the
UnlockSharedDataUnlock operator to theReturnProtectedDataoutput stream.
-
-
In the Package Explorer, double-click to open the
LockUnlock.sbappapplication. Make sure the application is the currently active tab in the EventFlow Editor. -
Click the
Run StreamBase Application (Default Launch)
button. (Not the Run button.) This
opens the Test/Debug perspective and starts the application.
-
In the Application Output view, select the ReturnProtectedData output stream. No output is displayed at this point, but the dequeuer is prepared to receive output. This view will eventually show the output of the application.
-
In the Manual Input view, select the WriteProtectedData input stream.
-
Enter
1andxyzin the protectedDataKey and protectedDataValueInit fields, respectively. -
Click Send Data, and observe this data appears in the Application Output view:
sharedDataLockKey=1, protectedDataLockKey=xyz -
Click Run > Stop StreamBase Application (F9).
Open three terminal windows. In all windows, first change your working directory to where the samples are installed.
-
In window 1, type:
sbd LockUnlock.sbappThe window shows
notice[StreamBaseServer] listening on port 10000. -
In window 2, type:
sbc dequeueThere is no output immediately, but the dequeuer is primed to display subsequent output of the application.
-
In window 3, type:
sbc enqueue WriteProtectedDataThe sbc command is now awaiting keyboard input. Then type:
1,xyz. Type Ctrl-Z (Windows) or Ctrl-D (UNIX), and sbc will exit.Observe these lines in the dequeue window:
ReturnProtectedData,1,xyz. -
In window 3, type:
sbadmin shutdownThe sbadmin shutdown command will terminate the daemon and dequeuer.
