author | Martin Kobetic |
Sun, 17 Nov 2013 00:23:18 -0500 | |
changeset 147 | bd6be28aa924 |
parent 111 | 44ac233b2f83 |
permissions | -rw-r--r-- |
10 | 1 |
"{ Package: 'stx:goodies/xtreams/transforms' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
WriteStream subclass:#CollectWriteStream |
|
6 |
instanceVariableNames:'block cache' |
|
7 |
classVariableNames:'' |
|
111
44ac233b2f83
* removed namespace from pool references and stray extension methods
joe
parents:
97
diff
changeset
|
8 |
poolDictionaries:'XtreamsPool' |
27
2cc5a8a3ca14
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
10
diff
changeset
|
9 |
category:'Xtreams-Transforms' |
10 | 10 |
! |
11 |
||
12 |
CollectWriteStream comment:'Converts written elements using the provided conversion block. Optimizes bulk writes by batching the conversion results, allowing the destination stream to perform bulk writes as well. |
|
13 |
||
14 |
Instance Variables |
|
15 |
block <BlockClosure> collecting block (same style as collect: blocks) |
|
16 |
cache <SequenceableCollection> caches results after transformation so that they can be bulk written too |
|
17 |
||
18 |
' |
|
19 |
! |
|
20 |
||
21 |
||
22 |
!CollectWriteStream class methodsFor:'instance creation'! |
|
23 |
||
24 |
on: aWriteStream block: block |
|
25 |
^self new on: aWriteStream block: block |
|
26 |
! ! |
|
27 |
||
28 |
!CollectWriteStream methodsFor:'accessing'! |
|
29 |
||
30 |
block |
|
31 |
^block |
|
32 |
! |
|
33 |
||
34 |
insert: anInteger from: aSequenceableCollection at: startIndex |
|
35 |
| count amount written offset | |
|
36 |
count := 0. |
|
37 |
offset := startIndex. |
|
38 |
[count < anInteger] whileTrue: |
|
39 |
[amount := anInteger min: cache size. |
|
40 |
1 to: amount do: [:index | cache at: index put: (block value: (aSequenceableCollection at: offset+index))]. |
|
41 |
written := [destination insert: anInteger from: cache. amount] on: Incomplete do: [ :ex | ex count ]. |
|
42 |
count := count + written. |
|
43 |
written < amount ifTrue: [(Incomplete on: aSequenceableCollection count: count at: startIndex) raise]. |
|
44 |
offset := offset + amount ]. |
|
45 |
^anInteger |
|
46 |
! |
|
47 |
||
48 |
put: anObject |
|
49 |
destination put: (block value: anObject). |
|
50 |
^anObject |
|
51 |
! |
|
52 |
||
53 |
write: anInteger from: aSequenceableCollection at: startIndex |
|
54 |
| count amount written offset | |
|
55 |
count := 0. |
|
56 |
offset := startIndex - 1. |
|
57 |
[count < anInteger] whileTrue: |
|
58 |
[amount := anInteger - count min: cache size. |
|
59 |
1 to: amount do: [:index | cache at: index put: (block value: (aSequenceableCollection at: offset + index))]. |
|
60 |
written := [destination write: amount from: cache. amount] on: Incomplete do: [ :ex | ex count ]. |
|
61 |
count := count + written. |
|
62 |
written < amount ifTrue: [(Incomplete on: aSequenceableCollection count: count at: startIndex) raise]. |
|
63 |
offset := offset + amount ]. |
|
64 |
^anInteger |
|
65 |
! ! |
|
66 |
||
67 |
!CollectWriteStream methodsFor:'initialize-release'! |
|
68 |
||
69 |
close |
|
70 |
super close. |
|
71 |
cache recycle. |
|
72 |
cache := nil |
|
73 |
! |
|
74 |
||
75 |
contentsSpecies |
|
76 |
||
77 |
^cache species |
|
78 |
! |
|
79 |
||
80 |
contentsSpecies: aClass |
|
81 |
||
40 | 82 |
cache ifNotNil: [ cache recycle ]. |
83 |
cache := aClass newRecycled: ( |
|
84 |
(cache notNil and: [ cache size > 0 ]) |
|
85 |
ifTrue: [cache size] |
|
86 |
ifFalse: [DefaultBufferSize]) |
|
10 | 87 |
! |
88 |
||
89 |
on: aDestination block: aBlock |
|
90 |
self on: aDestination. |
|
91 |
block := aBlock. |
|
92 |
self contentsSpecies: aDestination contentsSpecies |
|
93 |
! ! |
|
94 |
||
95 |
!CollectWriteStream methodsFor:'seeking'! |
|
96 |
||
97 |
++ anInteger |
|
98 |
^destination ++ anInteger |
|
99 |
! |
|
100 |
||
101 |
-- anInteger |
|
102 |
^destination -- anInteger |
|
103 |
! |
|
104 |
||
105 |
length |
|
106 |
^destination length |
|
107 |
! |
|
108 |
||
109 |
position |
|
110 |
^destination position |
|
111 |
! |
|
112 |
||
113 |
position: anInteger |
|
114 |
^destination position: anInteger |
|
115 |
! ! |
|
116 |
||
117 |
!CollectWriteStream methodsFor:'testing'! |
|
118 |
||
119 |
isPositionable |
|
120 |
^destination isPositionable |
|
121 |
! ! |
|
122 |
||
123 |
!CollectWriteStream class methodsFor:'documentation'! |
|
124 |
||
111
44ac233b2f83
* removed namespace from pool references and stray extension methods
joe
parents:
97
diff
changeset
|
125 |
version_HG |
44ac233b2f83
* removed namespace from pool references and stray extension methods
joe
parents:
97
diff
changeset
|
126 |
|
44ac233b2f83
* removed namespace from pool references and stray extension methods
joe
parents:
97
diff
changeset
|
127 |
^ '$Changeset: <not expanded> $' |
44ac233b2f83
* removed namespace from pool references and stray extension methods
joe
parents:
97
diff
changeset
|
128 |
! |
44ac233b2f83
* removed namespace from pool references and stray extension methods
joe
parents:
97
diff
changeset
|
129 |
|
10 | 130 |
version_SVN |
131 |
^ '$Id$' |
|
132 |
! ! |
|
111
44ac233b2f83
* removed namespace from pool references and stray extension methods
joe
parents:
97
diff
changeset
|
133 |