public class SizeOfVisitor extends Object implements Visitor
Caching is accomplished via a simple singly linked list of the cached
documents. This works since the document will be written in the same in-order
traversal of the document tree that this visitor follows. As each level of
the tree is written this visitor should have rewind()
called to set
the size back to zero and also remove the head from the document cache list.
If the next document written is the expected in-order traversal there is a
cache hit and the size is simply read from the cache's head node. Rinse,
repeat.
A custom list node type
is used instead of a generic
linked list to remove the overhead of extra object allocations for the node
and the value.
Modifier and Type | Class and Description |
---|---|
protected static class |
SizeOfVisitor.CachedSizeNode
CachedSizeNode provides a node in a singly linked list that forms the
cache for the sizes of lists of elements.
|
Modifier and Type | Field and Description |
---|---|
static Charset |
UTF8
UTF-8 Character set for encoding strings.
|
Constructor and Description |
---|
SizeOfVisitor()
Creates a new SizeOfVisitor.
|
Modifier and Type | Method and Description |
---|---|
int |
computeCStringSize(String string)
Returns the visitor's output buffer.
|
int |
computeStringSize(String string)
Returns the visitor's output buffer.
|
int |
getSize()
Return the current Size of the written document.
|
void |
reset()
Resets the size to zero and clears the cached documents.
|
void |
rewind()
Resets the size to zero but does not clear the cached documents.
|
int |
utf8Size(String string)
Computes the size of the encoded UTF8 String based on the table below.
|
void |
visit(List<Element> elements)
Visit the root document.
|
void |
visitArray(String name,
List<Element> elements)
Visits an array of elements.
|
void |
visitBinary(String name,
byte subType,
byte[] data)
Visits a binary element.
|
void |
visitBoolean(String name,
boolean value)
Visits a boolean element.
|
void |
visitDBPointer(String name,
String databaseName,
String collectionName,
ObjectId id)
Visits a deprecated DBPointer element.
|
void |
visitDocument(String name,
List<Element> elements)
Visits a sub-document element.
|
void |
visitDouble(String name,
double value)
Visits a double element.
|
void |
visitInteger(String name,
int value)
Visits a integer (32-bit signed) element.
|
void |
visitJavaScript(String name,
String code)
Visits a JavaScript element.
|
void |
visitJavaScript(String name,
String code,
Document scope)
Visits a JavaScript with Scope element.
|
void |
visitLong(String name,
long value)
Visits a long (64-bit signed) element.
|
void |
visitMaxKey(String name)
Visits a minimum key value element.
|
void |
visitMinKey(String name)
Visits a minimum key value element.
|
void |
visitMongoTimestamp(String name,
long value)
Visits a MongoDB Timestamp element.
|
void |
visitNull(String name)
Visits a
null valued element. |
void |
visitObjectId(String name,
ObjectId id)
Visits an ObjectId element.
|
void |
visitRegularExpression(String name,
String pattern,
String options)
Visits a regular expression element.
|
void |
visitString(String name,
String value)
Visits a string element.
|
void |
visitSymbol(String name,
String symbol)
Visits a symbol element.
|
void |
visitTimestamp(String name,
long timestamp)
Visits a timestamp element.
|
public static final Charset UTF8
public int computeCStringSize(String string)
string
- The 'C' string to determine the size of.public int computeStringSize(String string)
string
- The 'UTF8' string to determine the size of.public int getSize()
public void reset()
rewind()
to just set the size to zero and not clear the cached
documents.public void rewind()
reset()
to set the size to zero and clear the cached documents.public int utf8Size(String string)
# Code Points Bytes 1 U+0000..U+007F 1 2 U+0080..U+07FF 2 3 U+0800..U+0FFF 3 U+1000..U+FFFF 4 U+10000..U+3FFFF 4 U+40000..U+FFFFF 4 U+100000..U10FFFF 4
string
- The string to determine the length of.public void visit(List<Element> elements)
Implementations of Document
may see a significant performance
enhancement if they ensure that the list of elements is the same list.
(Identify check instead of Object.equals(Object)
.
public void visitArray(String name, List<Element> elements)
The ArrayElement
implementation ensures that the list of elements
is always the same list. Visitors may use this fact to cache intermediate
results.
visitArray
in interface Visitor
name
- The name of the element.elements
- The elements in the array.public void visitBinary(String name, byte subType, byte[] data)
visitBinary
in interface Visitor
name
- The name of the element.subType
- The binary data sub type.data
- The binary data.public void visitBoolean(String name, boolean value)
visitBoolean
in interface Visitor
name
- The name of the element.value
- The boolean value.public void visitDBPointer(String name, String databaseName, String collectionName, ObjectId id)
visitDBPointer
in interface Visitor
name
- The name of the element.databaseName
- The name of the database containing the document.collectionName
- The name of the collection containing the document.id
- The id for the document.public void visitDocument(String name, List<Element> elements)
The DocumentElement
implementation ensures that the list of
elements is always the same list. Visitors may use this fact to cache
intermediate results.
visitDocument
in interface Visitor
name
- The name of the element.elements
- The sub elements of the document.public void visitDouble(String name, double value)
visitDouble
in interface Visitor
name
- The name of the element.value
- The double value.public void visitInteger(String name, int value)
visitInteger
in interface Visitor
name
- The name of the element.value
- The integer value.public void visitJavaScript(String name, String code)
visitJavaScript
in interface Visitor
name
- The name of the element.code
- The java script code.public void visitJavaScript(String name, String code, Document scope)
visitJavaScript
in interface Visitor
name
- The name of the element.code
- The java script code.scope
- The scope for the JacaScript code.public void visitLong(String name, long value)
public void visitMaxKey(String name)
visitMaxKey
in interface Visitor
name
- The name of the element.public void visitMinKey(String name)
visitMinKey
in interface Visitor
name
- The name of the element.public void visitMongoTimestamp(String name, long value)
visitMongoTimestamp
in interface Visitor
name
- The name of the element.value
- The mongoDB timstamp value.public void visitNull(String name)
null
valued element.public void visitObjectId(String name, ObjectId id)
visitObjectId
in interface Visitor
name
- The name of the element.id
- The object id.public void visitRegularExpression(String name, String pattern, String options)
visitRegularExpression
in interface Visitor
name
- The name of the element.pattern
- The pattern for the regular expression.options
- The regular expression options. See the BSON specification for
details.public void visitString(String name, String value)
visitString
in interface Visitor
name
- The name of the element.value
- The string value.public void visitSymbol(String name, String symbol)
visitSymbol
in interface Visitor
name
- The name of the element.symbol
- The symbol value.public void visitTimestamp(String name, long timestamp)
visitTimestamp
in interface Visitor
name
- The name of the element.timestamp
- The number of milliseconds since the Unix epoch.Copyright © 2011-2013 Allanbank Consulting, Inc.. All Rights Reserved.