Skip to content

DOCSP-39689 Network Compression standardization #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions snooty.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "java-rs"
title = "Java Reactive Streams Driver"

intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
"https://www.mongodb.com/docs/atlas/objects.inv"
]
intersphinx = [
"https://www.mongodb.com/docs/manual/objects.inv",
"https://www.mongodb.com/docs/atlas/objects.inv",
]

sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

Expand All @@ -12,8 +13,8 @@ toc_landing_pages = [
"/read-data-from-mongo/",
"/write-data-to-mongo/",
"/getting-started/",
"/secure-your-data/"
]
"/secure-your-data/",
]

[constants]
driver-short = "Java Reactive Streams driver"
Expand All @@ -27,4 +28,6 @@ java-rs = "Java Reactive Streams"
string-data-type = "``String``"
bool = "``boolean``"
pr = "Project Reactor"
mdb-server = "MongoDB Server"
mdb-server = "MongoDB Server"
snappyVersion = "org.xerial.snappy:snappy-java:1.1.10.3"
zstdVersion = "com.github.luben:zstd-jni:1.5.5-3"
147 changes: 0 additions & 147 deletions source/connect-to-mongo/compression.txt

This file was deleted.

98 changes: 98 additions & 0 deletions source/connect-to-mongo/network-compression.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
.. _java-rs-compression:
.. _java-rs-network-compression:

========================
Compress Network Traffic
========================

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: zlib, zstandard, zstd, snappy

The {+driver-short+} provides a connection option to compress messages, which reduces the amount
of data passed over the network between MongoDB and your application.

The driver supports the following algorithms:

- `Snappy <https://google.github.io/snappy/>`__: available in MongoDB 3.4 and later.
- `Zlib <https://zlib.net/>`__: available in MongoDB 3.6 and later.
- `Zstandard <https://github.com/facebook/zstd/>`__: available in MongoDB 4.2 and later.

The driver tests against the following versions of these libraries:

- ``{+snappyVersion+}``
- ``{+zstdVersion+}``

If you specify multiple compression algorithms, the driver selects the first one
in the list supported by your MongoDB instance.

.. note::

Applications that require Snappy or Zstandard compression must
:ref:`add explicit dependencies <java-rs-compression-dependencies>` for those
algorithms.

.. _enable-compression:

Specify Compression Algorithms
------------------------------

You can enable compression for the connection to your MongoDB instance
by specifying the algorithms in one of two ways. Select the
:guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to
see the corresponding syntax:

- In the ``compressors`` parameter of your connection string
- In the ``compressorList`` method chained to the ``MongoClientSettings.builder()`` method

The following example shows how to specify all compression algorithms:

.. tabs::

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/connect/network-compression.java
:start-after: start-specify-connection-string
:end-before: end-specify-connection-string
:language: java

.. tab:: MongoClientSettings
:tabid: mongoclientsettings

.. literalinclude:: /includes/connect/network-compression.java
:start-after: start-specify-uri
:end-before: end-specify-uri
:language: java

.. _java-rs-compression-dependencies:

Compression Algorithm Dependencies
----------------------------------

The JDK natively supports `Zlib <https://zlib.net/>`__ compression. However,
Snappy and Zstandard depend on open source Java implementations. To learn more
about these implementations, see the following Github pages:

- `snappy-java <https://github.com/xerial/snappy-java>`__
- `zstd-java <https://github.com/luben/zstd-jni>`__

API Documentation
-----------------

To learn more about any of the methods or types discussed in this
guide, see the following API documentation:

- `MongoClient <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClient.html>`__
- `createSnappyCompressor() <{+api+}/mongodb-driver-core/com/mongodb/MongoCompressor.html#createSnappyCompressor()>`__
- `createZlibCompressor() <{+api+}//mongodb-driver-core/com/mongodb/MongoCompressor.html#createZlibCompressor()>`__
- `createZstdCompressor() <{+api+}/mongodb-driver-core/com/mongodb/MongoCompressor.html#createZstdCompressor()>`__
2 changes: 1 addition & 1 deletion source/connect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Connect to MongoDB
/connect-to-mongo/choose-connection-target/
/connect-to-mongo/connection-options/
/connect-to-mongo/tls/
/connect-to-mongo/compression/
/connect-to-mongo/network-compression

Overview
--------
Expand Down
19 changes: 19 additions & 0 deletions source/includes/connect/network-compression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// start-specify-connection-string

import java.util.Arrays;
Copy link
Member

@vbabanin vbabanin Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We specify the inclusion of Arrays as an import here, but it is never used in this example tab. It seems to be related to the next tab, where the compressorList() method is used.

However, even if we move the import of Arrays to the next tab, why do we only include this import and omit the imports like MongoClient and MongoClientSettings?

The previous page, 'TLS/SSL', includes a statement before examples that lists these import statements:

import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoClient;

Perhaps it is assumed that users have read the previous page, 'TLS/SSL' and are aware of these imports. However, I don't think we should make this assumption.

I noticed in another PR that we followed the approach of duplicating the same information to ensure users have everything they need within the section they’re reading, regardless of prior sections. This is highlighted in the discussion here: #48 (comment).

Therefore, it seems that we should add not only the Arrays import but also MongoClients and MongoClientSettings imports to ensure completeness and consistency.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this! That import was added by mistake (I think my editor automatically added it without me noticing for the following code snippet in that same file). For these pages that focus on a specific topic and don't provide "fully runnable code examples," we typically don't show the imports, as these code examples are intended to show only the relevant lines of code for the operation itself (See the pages nested under "Write Data to MongoDB" for more examples of this). The TLS/SSL page has not yet been standardized so it doesn't follow this convention yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, thank you for clarification!


ConnectionString connectionString = new ConnectionString(
"mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd");

MongoClient client = MongoClients.create(connectionString);
// end-specify-connection-string

// start-specify-uri
MongoClientSettings settings = MongoClientSettings.builder()
.compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(),
MongoCompressor.createZlibCompressor(),
MongoCompressor.createZstdCompressor()))
.build();

MongoClient client = MongoClients.create(settings);
// end-specify-uri