How to read global via Stream interface?
I have global with binary data, structured like:
^a(1)=<binary>
^a(2)=<binary>
^a(n)=<binary>
What could be easiest way to read it via Stream interface? I.e. to pass an adapter to a function expecting Stream?
look at this exercise, it may help you to get how to achieve it
Nice, thanks. But it seems that this way will work only if the global has an ID as first index.
If you have your binary or character data in (sequential) global nodes, then:
- create your own stream clas
-in the %OnNew() method fetch the data from your global
and voila, you are ready to use your data with stream methods.
set ^mydata(1)="abcd"
set ^mydata(2)="efgh"
set ^yourdata("id",1)="1234"
set ^yourdata("id",2)="5678"
Class Test.Stream Extends %Stream.TmpBinary
{
Method %OnNew(Global, initval As %String = "") As %Status [ Private ]
{
Set i%%Location=$select(initval="":$$$streamGlobal,1:initval)
do ..Clear()
for set Global=$query(@Global,1,data) quit:Global="" do ..Write(data)
do ..Rewind()
Quit $$$OK
}
}
set stream=##class(Test.Stream).%New($na(^mydata))
while 'stream.AtEnd { write stream.Read(5) }
abcdefgh
set stream=##class(Test.Stream).%New($na(^yourdata("id")))
while 'stream.AtEnd { write stream.Read(5) }
12345678
This way involves copying of the data but thanks, can be useful in some cases