Skip to content

Commit 5c710bd

Browse files
authored
Merge pull request #51 from alxsabo/v5.2
rdbc-711
2 parents 085478c + 2f814c5 commit 5c710bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1848
-232
lines changed

src/Documents/Conventions/DocumentConventions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function getDefaultForServerConventions(): DocumentConventions
5555
//
5656

5757
// private static final Map<Class, String> _cachedDefaultTypeCollectionNames = new HashMap<>();
58-
protected array $cachedDefaultTypeCollectionNames = []; // @todo: check if this is ok
58+
protected static array $cachedDefaultTypeCollectionNames = [];
5959

6060
// private final List<Tuple<Class, IValueForQueryConverter<Object>>> _listOfQueryValueToObjectConverters = new ArrayList<>();
6161

@@ -734,11 +734,11 @@ public function setIdentityPartsSeparator(string $identityPartsSeparator): void
734734
*
735735
* @throws IllegalStateException|ReflectionException
736736
*/
737-
private function defaultGetCollectionName(string $className): string
737+
public static function defaultGetCollectionName(string $className): string
738738
{
739739
$result = null;
740-
if (array_key_exists($className, $this->cachedDefaultTypeCollectionNames)) {
741-
$result = $this->cachedDefaultTypeCollectionNames[$className];
740+
if (array_key_exists($className, self::$cachedDefaultTypeCollectionNames)) {
741+
$result = self::$cachedDefaultTypeCollectionNames[$className];
742742

743743
if ($result != null) {
744744
return $result;
@@ -767,7 +767,7 @@ private function defaultGetCollectionName(string $className): string
767767

768768
$result = StringUtils::pluralize(ClassUtils::getSimpleClassName($className));
769769

770-
$this->cachedDefaultTypeCollectionNames[$className] = $result;
770+
self::$cachedDefaultTypeCollectionNames[$className] = $result;
771771

772772
return $result;
773773
}

src/Documents/DocumentStore.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
use RavenDB\Extensions\JsonExtensions;
1717
use RavenDB\Http\RequestExecutor;
1818
use RavenDB\Http\RequestExecutorMap;
19+
use RavenDB\Primitives\CleanCloseable;
1920
use RavenDB\Primitives\ClosureArray;
2021
use RavenDB\Primitives\EventArgs;
2122
use RavenDB\Primitives\EventHelper;
2223
use RavenDB\Primitives\ExceptionsUtils;
24+
use RavenDB\Primitives\SimpleClosable;
25+
use RavenDB\Type\Duration;
2326
use RavenDB\Type\Url;
2427
use RavenDB\Type\UrlArray;
2528

@@ -136,13 +139,13 @@ public function close(): void
136139
/**
137140
* Opens the session for a particular database
138141
*
139-
* @param string|SessionOptions $dbNameOrOptions Database to use
142+
* @param null|string|SessionOptions $dbNameOrOptions Database to use
140143
*
141144
* @return DocumentSessionInterface Document session
142145
*/
143-
public function openSession($dbNameOrOptions = ''): DocumentSessionInterface
146+
public function openSession(null|string|SessionOptions $dbNameOrOptions = null): DocumentSessionInterface
144147
{
145-
if (is_string($dbNameOrOptions)) {
148+
if (is_string($dbNameOrOptions) || is_null($dbNameOrOptions)) {
146149
return $this->openSessionWithDatabase($dbNameOrOptions);
147150
}
148151

@@ -157,12 +160,13 @@ public function openSession($dbNameOrOptions = ''): DocumentSessionInterface
157160
* @throws IllegalStateException
158161
* @throws InvalidArgumentException
159162
*/
160-
private function openSessionWithDatabase(string $database = ''): DocumentSessionInterface
163+
private function openSessionWithDatabase(?string $database = null): DocumentSessionInterface
161164
{
162165
$sessionOptions = new SessionOptions();
163166
$sessionOptions->setDisableAtomicDocumentWritesInClusterWideTransaction(
164167
$this->getConventions()->getDisableAtomicDocumentWritesInClusterWideTransaction()
165168
);
169+
166170
if ($database) {
167171
$sessionOptions->setDatabase($database);
168172
}
@@ -226,23 +230,22 @@ public function getRequestExecutor(?string $database = null): RequestExecutor
226230
return $executor;
227231
}
228232

233+
public function setRequestTimeout(?Duration $timeout, ?string $database = null): CleanCloseable
234+
{
235+
$this->assertInitialized();
229236

230-
// public CleanCloseable setRequestTimeout(Duration timeout) {
231-
// return setRequestTimeout(timeout, null);
232-
// }
233-
//
234-
// @Override
235-
// public CleanCloseable setRequestTimeout(Duration timeout, String database) {
236-
// assertInitialized();
237-
//
238-
// database = this.getEffectiveDatabase(database);
239-
//
240-
// RequestExecutor requestExecutor = getRequestExecutor(database);
241-
// Duration oldTimeout = requestExecutor.getDefaultTimeout();
242-
// requestExecutor.setDefaultTimeout(timeout);
243-
//
244-
// return () -> requestExecutor.setDefaultTimeout(oldTimeout);
245-
// }
237+
$database = $this->getEffectiveDatabase($database);
238+
239+
$requestExecutor = $this->getRequestExecutor($database);
240+
$oldTimeout = $requestExecutor->getDefaultTimeout();
241+
$requestExecutor->setDefaultTimeout($timeout);
242+
243+
$closable = new SimpleClosable();
244+
$closable->setCloseMethod(function() use ($requestExecutor, $oldTimeout) : void {
245+
$requestExecutor->setDefaultTimeout($oldTimeout);
246+
});
247+
return $closable;
248+
}
246249

247250

248251
/**

src/Documents/DocumentStoreInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use RavenDB\Documents\Session\DocumentSessionInterface;
1414
use RavenDB\Documents\Session\SessionOptions;
1515
use RavenDB\Documents\TimeSeries\TimeSeriesOperations;
16+
use RavenDB\Primitives\CleanCloseable;
17+
use RavenDB\Type\Duration;
1618
use RavenDB\Type\UrlArray;
1719
use RavenDB\Http\RequestExecutor;
1820
use RavenDB\Documents\Conventions\DocumentConventions;
@@ -93,11 +95,11 @@ public function initialize(): DocumentStoreInterface;
9395
/**
9496
* Opens the session for a particular database
9597
*
96-
* @param string|SessionOptions $dbNameOrOptions Database to use
98+
* @param null|string|SessionOptions $dbNameOrOptions Database to use
9799
*
98100
* @return DocumentSessionInterface Document session
99101
*/
100-
public function openSession($dbNameOrOptions = ''): DocumentSessionInterface;
102+
public function openSession(null|string|SessionOptions $dbNameOrOptions = null): DocumentSessionInterface;
101103

102104
/**
103105
* Executes the index creation
@@ -143,10 +145,8 @@ public function maintenance(): MaintenanceOperationExecutor;
143145
public function operations(): OperationExecutor;
144146

145147
// DatabaseSmuggler smuggler();
146-
//
147-
// CleanCloseable setRequestTimeout(Duration timeout);
148-
//
149-
// CleanCloseable setRequestTimeout(Duration timeout, String database);
148+
149+
public function setRequestTimeout(?Duration $timeout, ?string $database = null): CleanCloseable;
150150

151151
public function close(): void;
152152
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace RavenDB\Documents\Operations\Expiration;
4+
5+
use RavenDB\Exceptions\IllegalArgumentException;
6+
use RavenDB\Http\HttpRequest;
7+
use RavenDB\Http\HttpRequestInterface;
8+
use RavenDB\Http\RaftCommandInterface;
9+
use RavenDB\Http\RavenCommand;
10+
use RavenDB\Http\ServerNode;
11+
use RavenDB\Utils\RaftIdGenerator;
12+
13+
class ConfigureExpirationCommand extends RavenCommand implements RaftCommandInterface
14+
{
15+
private ?ExpirationConfiguration $configuration = null;
16+
17+
public function __construct(?ExpirationConfiguration $configuration)
18+
{
19+
parent::__construct(ConfigureExpirationOperationResult::class);
20+
21+
if ($configuration == null) {
22+
throw new IllegalArgumentException("Configuration cannot be null");
23+
}
24+
25+
$this->configuration = $configuration;
26+
}
27+
28+
public function isReadRequest(): bool
29+
{
30+
return false;
31+
}
32+
33+
public function createUrl(ServerNode $serverNode): string
34+
{
35+
return $serverNode->getUrl() . "/databases/" . $serverNode->getDatabase() . "/admin/expiration/config";
36+
}
37+
38+
public function createRequest(ServerNode $serverNode): HttpRequestInterface
39+
{
40+
$options = [
41+
'json' => $this->getMapper()->normalize($this->configuration),
42+
'headers' => [
43+
'Content-Type' => 'application/json'
44+
]
45+
];
46+
47+
return new HttpRequest($this->createUrl($serverNode), HttpRequest::POST, $options);
48+
}
49+
50+
public function setResponse(?string $response, bool $fromCache): void
51+
{
52+
if ($response ==null) {
53+
self::throwInvalidResponse();
54+
}
55+
56+
$this->result = $this->getMapper()->deserialize($response, $this->resultClass, 'json');
57+
}
58+
59+
public function getRaftUniqueRequestId(): string
60+
{
61+
return RaftIdGenerator::newId();
62+
}
63+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace RavenDB\Documents\Operations\Expiration;
4+
5+
use RavenDB\Documents\Conventions\DocumentConventions;
6+
use RavenDB\Documents\Operations\MaintenanceOperationInterface;
7+
use RavenDB\Http\RavenCommand;
8+
9+
class ConfigureExpirationOperation implements MaintenanceOperationInterface
10+
{
11+
private ?ExpirationConfiguration $configuration = null;
12+
13+
public function __construct(?ExpirationConfiguration $configuration)
14+
{
15+
$this->configuration = $configuration;
16+
}
17+
18+
public function getCommand(DocumentConventions $conventions): RavenCommand
19+
{
20+
return new ConfigureExpirationCommand($this->configuration);
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RavenDB\Documents\Operations\Expiration;
4+
5+
class ConfigureExpirationOperationResult
6+
{
7+
private ?int $raftCommandIndex = null;
8+
9+
public function getRaftCommandIndex(): ?int
10+
{
11+
return $this->raftCommandIndex;
12+
}
13+
14+
public function setRaftCommandIndex(?int $raftCommandIndex): void
15+
{
16+
$this->raftCommandIndex = $raftCommandIndex;
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace RavenDB\Documents\Operations\Expiration;
4+
5+
class ExpirationConfiguration
6+
{
7+
private bool $disabled = false;
8+
private ?int $deleteFrequencyInSec = null;
9+
10+
public function isDisabled(): bool
11+
{
12+
return $this->disabled;
13+
}
14+
15+
public function setDisabled(bool $disabled): void
16+
{
17+
$this->disabled = $disabled;
18+
}
19+
20+
public function getDeleteFrequencyInSec(): ?int
21+
{
22+
return $this->deleteFrequencyInSec;
23+
}
24+
25+
public function setDeleteFrequencyInSec(?int $deleteFrequencyInSec): void
26+
{
27+
$this->deleteFrequencyInSec = $deleteFrequencyInSec;
28+
}
29+
}

src/Documents/Queries/Facets/RangeFacetList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class RangeFacetList extends TypedList
88
{
99
public function __construct()
1010
{
11-
parent::__construct(RangeFacetList::class);
11+
parent::__construct(RangeFacet::class);
1212
}
1313
}

src/Documents/Session/AfterConversionToDocumentEventArgs.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public function getDocument(): array
3535
return $this->document;
3636
}
3737

38+
public function setDocument(array $document): void
39+
{
40+
$this->document = $document;
41+
}
42+
3843
public function getSession(): InMemoryDocumentSessionOperations
3944
{
4045
return $this->session;

src/Documents/Session/Operations/QueryOperation.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ private function completeInternal(?string $className, QueryResult $queryResult):
151151

152152
$resultItems = [];
153153

154-
try {
154+
// try {
155155
foreach ($queryResult->getResults() as $document) {
156156

157157
$metadata = array_key_exists(DocumentsMetadata::KEY, $document) ? $document[DocumentsMetadata::KEY] : null;
158158
try {
159-
$idNode = array_key_exists(DocumentsMetadata::ID, $metadata) ? $metadata[DocumentsMetadata::ID] : NULL;
159+
$idNode = array_key_exists(DocumentsMetadata::ID, $metadata) ? $metadata[DocumentsMetadata::ID] : null;
160160

161161
$id = null;
162162
if ($idNode != null && is_string($idNode)) {
@@ -179,9 +179,9 @@ private function completeInternal(?string $className, QueryResult $queryResult):
179179
throw new IllegalArgumentException("Raw query with aggregation by facet should be called by executeAggregation method." . $e->getMessage());
180180
}
181181
}
182-
} catch (Throwable $e) {
183-
throw new RuntimeException("Unable to read json: " . $e->getMessage(), 0, $e);
184-
}
182+
// } catch (Throwable $e) { // we don't need this because in PHP implementation json is already deserialized when arrives to this method
183+
// throw new RuntimeException("Unable to read json: " . $e->getMessage(), 0, $e);
184+
// }
185185

186186
if (!$this->noTracking) {
187187
$this->session->registerMissingIncludes($queryResult->getResults(), $queryResult->getIncludes(), $queryResult->getIncludedPaths());

0 commit comments

Comments
 (0)