Skip to content

Commit 3d3caf9

Browse files
authored
Merge pull request #63 from xp-forge/refactor/use-compression
Use compression library
2 parents 25e62da + 76abd90 commit 3d3caf9

File tree

8 files changed

+47
-74
lines changed

8 files changed

+47
-74
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"xp-framework/core": "^12.0 | ^11.0 | ^10.0",
1010
"xp-framework/networking": "^10.0",
1111
"xp-framework/math": "^10.0 | ^9.1",
12+
"xp-forge/compression": "^2.0",
1213
"php" : ">=7.4.0"
1314
},
1415
"require-dev" : {

src/main/php/com/mongodb/io/Compression.class.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace com\mongodb\io;
22

3+
use io\streams\compress\{Gzip, ZStandard, Snappy};
34
use lang\Value;
45
use util\Comparison;
56

@@ -16,8 +17,21 @@ class Compression implements Value {
1617
private $compressors;
1718

1819
static function __static() {
19-
extension_loaded('zlib') && self::$negotiable['zlib']= fn($options) => new Zlib($options['zlibCompressionLevel'] ?? -1);
20-
extension_loaded('zstd') && self::$negotiable['zstd']= fn($options) => new Zstd($options['zstdCompressionLevel'] ?? -1);
20+
self::$negotiable['snappy']= fn($options) => new Compressor(
21+
1,
22+
new Snappy(),
23+
null
24+
);
25+
extension_loaded('zlib') && self::$negotiable['zlib']= fn($options) => new Compressor(
26+
2,
27+
new Gzip(),
28+
$options['zlibCompressionLevel'] ?? -1
29+
);
30+
extension_loaded('zstd') && self::$negotiable['zstd']= fn($options) => new Compressor(
31+
3,
32+
new ZStandard(),
33+
$options['zstdCompressionLevel'] ?? -1
34+
);
2135
}
2236

2337
/** @param [:com.mongodb.io.Compressor] $compressors */

src/main/php/com/mongodb/io/Compressor.class.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?php namespace com\mongodb\io;
22

3+
use io\streams\compress\Algorithm;
34
use lang\Value;
45

5-
abstract class Compressor implements Value {
6-
public $id;
6+
class Compressor implements Value {
7+
public $id, $algorithm, $options;
78

8-
public abstract function compress($data);
9+
public function __construct(int $id, Algorithm $algorithm, $options= null) {
10+
$this->id= $id;
11+
$this->algorithm= $algorithm;
12+
$this->options= $options;
13+
}
914

10-
public abstract function decompress($compressed);
11-
12-
public function toString() { return nameof($this).'(id: '.$this->id.')'; }
15+
public function toString() { return nameof($this).'(id: '.$this->id.', options: '.$this->options.')'; }
1316

1417
public function hashCode() { return 'C'.$this->id; }
1518

src/main/php/com/mongodb/io/Connection.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function send($operation, $header, $sections, $readPreference= null) {
239239
$length= strlen($body);
240240

241241
if ($this->compression && $compressor= $this->compression->for($sections, $length)) {
242-
$compressed= $compressor->compress($body);
242+
$compressed= $compressor->algorithm->compress($body, $compressor->options);
243243
$this->socket->write(pack(
244244
'VVVVVVCa*',
245245
strlen($compressed) + 25,
@@ -284,7 +284,7 @@ public function send($operation, $header, $sections, $readPreference= null) {
284284
$compressed= unpack('VoriginalOpcode/VuncompressedSize/CcompressorId', $response);
285285

286286
if ($this->compression && $compressor= $this->compression->select($compressed['compressorId'] ?? null)) {
287-
$response= $compressor->decompress(substr($response, 9));
287+
$response= $compressor->algorithm->decompress(substr($response, 9));
288288
$meta['opCode']= $compressed['originalOpcode'];
289289
goto opcode;
290290
}

src/main/php/com/mongodb/io/Zlib.class.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/php/com/mongodb/io/Zstd.class.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/test/php/com/mongodb/unittest/CompressionTest.class.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace com\mongodb\unittest;
22

3-
use com\mongodb\io\{Compression, Compressor, Zlib, Zstd};
3+
use com\mongodb\io\{Compression, Compressor};
4+
use io\streams\compress\{None, Gzip, Snappy, ZStandard};
45
use test\verify\Runtime;
56
use test\{Assert, Before, Test, Values};
67

@@ -9,11 +10,7 @@ class CompressionTest {
910

1011
#[Before]
1112
public function compressor() {
12-
$this->compressor= new class() extends Compressor {
13-
public $id= 9;
14-
public function compress($data) { /** Not implemented */ }
15-
public function decompress($compressed) { /** Not implemented */ }
16-
};
13+
$this->compressor= new Compressor(0, new None());
1714
}
1815

1916

@@ -57,29 +54,34 @@ public function negotiate_unsupported() {
5754
Assert::null(Compression::negotiate(['unsupported']));
5855
}
5956

57+
#[Test]
58+
public function negotiate_snappy() {
59+
Assert::instance(Snappy::class, Compression::negotiate(['unsupported', 'snappy'])->select(1)->algorithm);
60+
}
61+
6062
#[Test, Runtime(extensions: ['zlib'])]
6163
public function negotiate_zlib() {
62-
Assert::instance(Zlib::class, Compression::negotiate(['unsupported', 'zlib'])->select(2));
64+
Assert::instance(Gzip::class, Compression::negotiate(['unsupported', 'zlib'])->select(2)->algorithm);
6365
}
6466

6567
#[Test, Runtime(extensions: ['zlib']), Values([[[], -1], [['zlibCompressionLevel' => 6], 6]])]
6668
public function negotiate_zlib_with($options, $level) {
6769
$compressor= Compression::negotiate(['zlib'], $options)->select(2);
6870

69-
Assert::instance(Zlib::class, $compressor);
70-
Assert::equals($level, $compressor->level);
71+
Assert::instance(Gzip::class, $compressor->algorithm);
72+
Assert::equals($level, $compressor->options);
7173
}
7274

7375
#[Test, Runtime(extensions: ['zstd'])]
7476
public function negotiate_zstd() {
75-
Assert::instance(Zstd::class, Compression::negotiate(['unsupported', 'zstd'])->select(3));
77+
Assert::instance(ZStandard::class, Compression::negotiate(['unsupported', 'zstd'])->select(3)->algorithm);
7678
}
7779

7880
#[Test, Runtime(extensions: ['zstd']), Values([[[], -1], [['zstdCompressionLevel' => 6], 6]])]
7981
public function negotiate_zstd_with($options, $level) {
8082
$compressor= Compression::negotiate(['zstd'], $options)->select(3);
8183

82-
Assert::instance(Zstd::class, $compressor);
83-
Assert::equals($level, $compressor->level);
84+
Assert::instance(ZStandard::class, $compressor->algorithm);
85+
Assert::equals($level, $compressor->options);
8486
}
8587
}

src/test/php/com/mongodb/unittest/ConnectionTest.class.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php namespace com\mongodb\unittest;
22

33
use com\mongodb\Int64;
4-
use com\mongodb\io\{BSON, Connection, Compression, Compressor, Zlib, Zstd};
4+
use com\mongodb\io\{BSON, Connection, Compression, Compressor};
5+
use io\streams\compress\{Gzip, ZStandard};
56
use peer\ConnectException;
67
use test\verify\Runtime;
78
use test\{Assert, Before, Expect, Test, Values};
@@ -31,7 +32,7 @@ private function msg(array $document): array {
3132
/** Creates an OP_COMPRESSED message with an embedded OP_MSG opcode */
3233
private function compressed(Compressor $compressor, array $document): array {
3334
$payload= pack('VC', 0, 0).$this->bson->sections($document);
34-
$compressed= $compressor->compress($payload);
35+
$compressed= $compressor->algorithm->compress($payload, $compressor->options);
3536
return [
3637
pack('VVVV', strlen($compressed) + 25, 0, 0, Connection::OP_COMPRESSED),
3738
pack('VVC', Connection::OP_MSG, strlen($payload), $compressor->id).$compressed
@@ -147,7 +148,7 @@ public function send_and_receive_zlib() {
147148
$documents= [['_id' => 'one']];
148149
$c= new Connection(new TestingSocket([
149150
...$this->reply(['ok' => 1.0, 'compression' => ['zlib']]),
150-
...$this->compressed(new Zlib(), [
151+
...$this->compressed(new Compressor(2, new Gzip()), [
151152
'cursor' => ['firstBatch' => $documents, 'id' => new Int64(0), 'ns' => 'test.entries'],
152153
'ok' => 1,
153154
]),
@@ -163,7 +164,7 @@ public function send_and_receive_zstd() {
163164
$documents= [['_id' => 'one']];
164165
$c= new Connection(new TestingSocket([
165166
...$this->reply(['ok' => 1.0, 'compression' => ['zstd']]),
166-
...$this->compressed(new Zstd(), [
167+
...$this->compressed(new Compressor(3, new ZStandard()), [
167168
'cursor' => ['firstBatch' => $documents, 'id' => new Int64(0), 'ns' => 'test.entries'],
168169
'ok' => 1,
169170
]),

0 commit comments

Comments
 (0)