File: javaboulder.txt

package info (click to toggle)
libboulder-perl 1.30-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 404 kB
  • ctags: 306
  • sloc: perl: 4,286; makefile: 41
file content (496 lines) | stat: -rw-r--r-- 16,799 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
	    PARTIAL SPECIFICATION OF THE JAVA BOULDER API

class Boulder.Stone

public class Stone extends Object;

In order to work around Java's strong typing and to emulate the
behavior of Perl Boulder, the Stone object becomes a wrapper around
simple types (strings and numerics) as well as a more complex object
that contains tag/value pairs.  This allows the retrieval and storage
methods to work on Stones rather than the more generic Objects, and
should reduce the amount of upcasting necessary.

Constructors:
-------------

* Stone()

Create new Stone object containing no initial tags.

* Stone(Dictionary d)

Create new Stone object initialized from a Dictionary object.  Each
key in the dictionary becomes a tag in the Stone.  Certain values are
treated specially:
	
	-Dictionary values are recursively turned into sub-Stone
	objects.
	-Array and Vector values are turned into multivalued entries
	that share the same tag.

*Stone(String s) 

Create a Stone wrapped around a string.

*Stone(Number n)

Create a Stone wrapped around any of the numeric objects (Integer,
Long, Float, Double).


Static (class) Methods:
-----------------------

* static public Stone fromString(String s)

Create a Stone from its string representation.


Object Methods:
---------------

*public String toString()

Return the string representation of the Stone in Boulder format.
Simple Stones are cast to the string representation of their scalar
value.  Complex Stones are represented (recursively) in boulderio
form.  This serializes a Stone in a form suitable for reconstitution
with fromString().

*public Integer toInt() throws NumberFormatException

Converts a simple Stone to an Integer object.  Complex Stones and
simple Stones that do not contain parseable contents throw a
NumberFormatException.

*public Float toFloat() throws NumberFormatException

Converts a simple Stone to a Float object.  Complex Stones and simple
Stones that do not contain parseable contents throw a
NumberFormatException.

*public Double toDouble() throws NumberFormatException

Converts a simple Stone to a Double object.  Complex Stones and simple
Stones that do not contain parseable contents throw a
NumberFormatException.

*public void insert(Stone s)

Insert Stone s into the current Stone, merging their keys and values.

*public void insert(Dictionary d)

Insert the key/value pairs contained in dictionary into the Stone.
Similarly-named keys are appended to, making them multi-valued.  On
simple Stones this, and the other insertion methods, silently converts
the simple Stone into a complex Stone, deleting its simple value.
[This behavior is open for discussion; I think it's better than making
the method a no-op]

*public void insert(String tag,Object value)

Insert an Object into the Stone at the indicated tag.  Similarly-named
keys are appended to, making them multi-valued.

*public void replace(Dictionary d)

Insert the key/value pairs contained in dictionary into the Stone.
Similarly-named keys are replaced, overwriting their previous
contents.

*public void replace(Stone s)

Merge Stone s into the current Stone, overwriting any keys in common.

*public void replace(String tag, Stone value)

Insert a sub-Stone (complex or scalar) into the Stone at the indicated
tag.  Similarly-named keys are replaced, overwriting their previous
contents.

*public void subtract(Stone s)

Remove from this Stone all the tags and associated values present in
Stone s.  Only the top level of tags are affected (no need to do
recursive subtraction).

*public void intersect(Stone s)

Remove from this Stone any tags and associated values not in common
with Stone s.

*public Stone[] get(String tag)

Return an array of the Stone at the indicated tag.  Returns null if
the tag is not found.  Also returns null if called on a scalar Stone.

*public Stone get(String tag, integer n)

Return the nth Stone at the given tag.  Uses zero-based indexing.
Returns null if the tag is not found or if the index is out of bounds
(? should it raise an array exception).  Negative numbers count in
from the right end of the array.  The last item is index -1.

*public Stone getFirst(String tag)

Return the first Stone at the indicated tag.  Returns null if the tag
is not found.  This is the same as get(tag,0).

*public Stone getLast(String tag)

Return the first Stone at the indicated tag.  Returns null if the tag
is not found.  This is the same as get(tag,-1).

*public Stone getAny(String tag)

Return a random value from the indicated tag.  This has never been
used, to my knowledge, but it is a feature of the Perl implementation.

*public String[] tags()

Return all the tags available in this Stone.  If it is a scalar Stone, 
an empty (not null) array is returned.

*public Boolean exists(String tag)

Returns true if the indicated tag exists.

*public void delete(String tag)

Delete the tag and its associated subtree from the Stone.

*public Stone[] search(String tag)

Recursively searches through the Stone and its subtrees for the first
tag that matches the argument and returns its contents.  The search
method is depth-first (top-level tags returned preferentially).

*public Stone[] index(String index_string)

Follows a path through the Stone, returning the value.  The path is of 
the form:

   tag1[index].tag2[index].tag3[index]

Indexes can be omitted, in which case the path follows the first value 
of the tag.  Indexes match any of the following expressions:

	[0-9]+  index leftward from first value
        -[0-9]+ index rightward from last value
	#       last item
	\?      random item (question mark)

*public Stone[] path(String index_string)

This is a better name for index(), but unfortunately not part of the
Perl Boulder API.  Maybe index() should be phased out.

*public Enumeration cursor()

Return an Enumeration over the Stone object.  Each call to
nextElement() takes a step in a breadth-first traversal of the Stone.
The elements of the Enumeration are Stones with three tags:

	tag name   interpretation
        --------   --------------
	"tag"      String representing the name of the current
                       element's tag
        "path"     String representing full path to current element
        "value"    The value pointed to by the tag.

===========================================================================
public interface Boulder.Filter;

Boulder.Filter can prefilter a BoulderIO stream so that only certain
Stones are passed to higher layers.  Its filter() method is presented
with each candidate Stone in turn.  It returns a boolean True to
accept the Stone, or False to filter it.

*public abstract Boolean filter(Stone s)

Return True if this Stone should be passed up to higher layers.

===========================================================================

public interface Boulder.IO;

This interface defines everything that a generic Boulder IO class
should be able to do.  Both Boulder.Stream (serial input/output) and
Boulder.Store (record-oriented input/output) implement this interface.

Note that Boulder.IO has an intrinsic cursor behavior, in that it
returns Stones in some defined order.

* public abstract Stone read_record() throws IOException

Reads a new Stone from input and returns it.  If no further stones can 
be read returns NULL.  If an I/O error occurs returns IOException.
Returns EOFException if the caller makes additional calls to
read_record() after it has returned NULL.

* public abstract Stone read_record(String[] f) throws IOException

Reads a new Stone from input and returns it, filtering tag(s) based on
an array of tag filter patterns. The argument is an array of strings
of the form "tag1.tag2.tag3....", corresponding to a set of tag paths.
For example, if the current Stone has the structure:

  NAME=Fred
  DEMOGRAPHICS={
    AGE=62
    GENDER=Male
    PHYSICAL_ATTRIBUTES={
      BALDING=Y
      OVERWEIGHT=N
    }
  }
  ADDRESS={
    STREET=1313 Mockingbird Lane
    TOWN=Port Washington
    STATE=NY
  }

then the stone returned by
read_record(["NAME","DEMOGRAPHICS.AGE","ADDRESS"]) will return the
Stone:

  NAME=Fred
  DEMOGRAPHICS={
    AGE=62
  }
  ADDRESS={
    STREET=1313 Mockingbird Lane
    TOWN=Port Washington
    STATE=NY
  }
  
If no tags match the filter specification, returns an empty (but not
NULL) Stone.  If no further stones can be read returns NULL.  If an
I/O error occurs, returns IOException.  Returns EOFException if the
caller makes additional calls to read_record() after it has returned
NULL.

* public abstract Stone get() throws IOException
* public abstract Stone get(String[] f) throws IOException

The Perl version of Boulder uses get() as a synonum for read_record(),
because some people requested it.  Now I use get() in preference to
the longer form and am open to entirely replacing read_record() with
get().

* public abstract void write_record(Stone s) throws IOException
* public abstract void write_record(Stone s, String[] f) throws IOException

These two methods write a Stone to an output device or file.  In the
first form, the Stone is written out with all its fields intact.  In
the second form, the Stone's tags are first filtered on the specified
array of filtering rules.  The format of filtering rules is identical
to the read_record() method.  If the Stone cannot be successfully
written, this method throws an IOException.

* public abstract void put(Stone s) throws IOException
* public abstract void put(Stone s, String[] f) throws IOException

These methods are synonyms for write_record() in the Perl
implementation.  They are shorter, and if you like them better maybe
they should be the canonical names.

* public void filter(Boulder.Filter s)

This method adds a filter to the Boulder.IO object.  The filter is
presented with each Stone in turn and selects whether to accept the
Stone or reject it.  See the Boulder.Filter interface for details.

===========================================================================
public class Boulder.Stream implements Boulder.IO;

This class defines a Boulder class that reads and writes to a type of
I/O that behaves in a serial fashion.

Constructors:
-------------

* public Stream()

Create a new Boulder.Stream object attached to standard in and
standard output.  The get_record() method will read Stones from
standard input one at a time until standard input is exhausted.
The write_record() method will emit Stones to standard output.

* public Stream(InputStream in, OutputStream out)

Create a Boulder.Stream, tieing it to the specified InputStream and
OutputStream objects.

* public void passthru(Boolean pass)

A Boulder.Stream object can behave in either of two ways.  It can
gobble up the Stone objects that are read via get_record() completely,
in which case it emits nothing unless write_record() is called, or it
can pass the unwanted components of the object through to its output
stream.  In pass through mode, a program that repeatedly calls
read_record(["NAME","DEMOGRAPHICS.AGE","ADDRESS"]) on an input stream
containing the Stone given in the example above, would emit the
following Stone automatically even if it doesn't make a call to
write_record():

  DEMOGRAPHICS={
    GENDER=Male
    PHYSICAL_ATTRIBUTES={
      BALDING=Y
      OVERWEIGHT=N
    }
  }

In pass through mode, any calls to write_record() performed before the
next read_record() will merge the contents of the Stone specified by
write_record() with the passed through portion of the Stone.  If,
after calling read_record(), the program were to call write_record()
with a Stone with this structure:

  NAME=Andrew
  DEMOGRAPHICS={
    FAVORITE_COLOR=blue
  }

Then the resulting stone would be:

  NAME=Andrew
  DEMOGRAPHICS={
    GENDER=Male
    FAVORITE_COLOR=blue
    PHYSICAL_ATTRIBUTES={
      BALDING=Y
      OVERWEIGHT=N
    }
  }

Note that this behavior is slightly different than the Perl
implementation, in which only top-level tags are merged.  That
behavior has always seemed a bit bogus, and this is more logical
(but perhaps not more useful in practice).

The passthru() method accepts a boolean indicating whether passthru
behavior should be activated or not.  The default is "false", for no
passthru.

* public Boolean passthru()

This method returns the state of the passthru flag.

===========================================================================

public interface Boulder.Store extends Boulder.IO;

Boulder.Store adds unique record IDs to the basic Boulder.IO scheme,
turning it into a database of sorts.  The record IDs can be used to
fetch and store Stones in a non-linear fashion, and provides simple
indexing and querying services.  The record ID is a declared tag in
the Stone that must be present and unique.  The record ID can be
generated automatically if desired.

The serial access methods behave as they do in the parent interface.
Stones are returned one at a time (in some implementation-specific
order).  The set of Stones returned and their order can be affected by
the query() method, however.  The behavior of the write_record()
method is dependent on flags that control whether missing record IDs
are automatically generated, and whether Stones can overwrite objects
with identical record IDs that are already in the database.

* public abstract void setRecordID (String tag)

Declare that this tag will be used as the unique record ID in the
Stone.  It is assumed that classes that implement the Boulder.Store
interface will store this tag name somewhere in the associated
database.  The default is "ID".

* public abstract String getRecordID()

Return the special tag.

* public abstract Boolean setIndex (String tag)

Declare that a tag (or tag path) is an index.  Returns a true value if 
the database supports indexing on this tag.

* public abstract String[] getIndex()

Returns an array of all the tags that are indexes.

* public abstract void setAutoID(Boolean doAuto)

Sets a flag that allows write_record() to automatically add a
record ID tag to Stones that do not already contain the designated
tag.  The default is false.

* public abstract Boolean getAutoID()

Returns the state of the auto record generation flag.

* public abstract void setClobber(Boolean clobber)

Sets a flag that allows write_record() to clobber (overwrite) any Stone
in the database that already has the ID of the Stone being written.
The default is false.

* public abstract Boolean getClobber()

Returns the state of the clobber flag.

* public abstract Stone read_record(String ID)

Read the Stone with the indicated record ID from the database.
Returns NULL if not present.

* public abstract Stone read_record(String[] f,String ID)

Reads the Stone with the indicated record ID from the database,
filtering its tags on the provided array of tag patterns.

* public abstract void write_record(Stone s) throws IOException;

Writes the Stone to the database, using the value of its record ID tag 
to put the Stone in the right place.  If no record ID tag is present,
and autoID is true, then write_record() generates a new unique record
ID tag and adds it to the Stone.  If the record ID is not unique, and
clobber is false, then throws an InvalidObjectException.

* public abstract void write_record(Stone s,String ID) throws IOException;

First replaces the Stone's record ID tag with the indicated ID and
then calls write_record(Stone s) to insert the Stone into the
database.

* public abstract Boolean query(String s)

Add a query to the BoulderIO object.  The query is a string whose
syntax and semantics are implementation-specific.  Once the query is
in force, repeated calls to read_record() will return the set of
Stone objects that match the query.  The order of Stones returned may
be affected by the query as well.  The query remains in force until
all selected Stones are exhausted (read_record() returns null).
Calling query() before exhausting the Stones resets the state of the
BoulderIO object.  The boolean result from query() indicates whether
the query string is syntactically acceptable, not whether the query
will return a non-empty set.

Possibly it would be better to raise an exception for queries that
fail the syntax check. What do you think?

============================================================

public class Boulder.Recno extends Boulder.Store;

Boulder.Store implements a record-oriented/random-access type of
retrieval.  Each Stone is associated with a unique record number that
can be used to fetch and retrieve Stones in a non-linear fashion.  The
record number is an integer greater than or equal to zero.  In the
Perl implementation, the record numbers are continuous.  When a record
is deleted, the remaining Stones are renumbered to fill in the gap.
(This is a decision that we might want to reconsidered.)

= unfinished =