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?

 

  • + 1
  • 0
  • 141
  • 2
  • 2

Answers

look at this exercise, it may help you to get how to achieve it

  Set id = 1
  Set streamGN = $Name(^IRIS.TempStream)
  Kill @streamGN
  Set @streamGN@(id, 1) = "some binary data chunk 1"
  Set @streamGN@(id, 2) = "some binary data chunk 1"

  Set lastChunk = $Order(@streamGN@(id, ""), -1)
  Set @streamGN@(id) = lastChunk
  Set size = 0
  For chunk=1:1:lastChunk {
    Set size = size + $Length(@streamGN@(id, chunk))
  }
  Set @streamGN@(id, 0) = size

  Set stream = ##class(%Stream.GlobalBinary).%Open($Listbuild(id, , streamGN))
  While 'stream.AtEnd {
    Write !,stream.Read()
  }
  Quit

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