Skip to content

Commit d70f71d

Browse files
authored
Merge pull request #4972 from nextcloud/backport/4970/stable26
2 parents 1766df5 + 4b09933 commit d70f71d

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

cypress/e2e/SessionApi.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('The session Api', function() {
115115
const version = 0
116116
cy.pushSteps({ connection, steps, version })
117117
.its('version')
118-
.should('eql', 1)
118+
.should('be.at.least', 1)
119119
cy.syncSteps(connection)
120120
.its('steps[0].data')
121121
.should('eql', steps)
@@ -170,7 +170,7 @@ describe('The session Api', function() {
170170
it('saves', function() {
171171
cy.pushSteps({ connection, steps: [messages.update], version })
172172
.its('version')
173-
.should('eql', 1)
173+
.should('be.at.least', 1)
174174
cy.syncSteps(connection, { version: 1, autosaveContent: '# Heading 1', manualSave: true })
175175
cy.downloadFile(filePath)
176176
.its('data')
@@ -181,7 +181,7 @@ describe('The session Api', function() {
181181
const documentState = 'Base64 encoded string'
182182
cy.pushSteps({ connection, steps: [messages.update], version })
183183
.its('version')
184-
.should('eql', 1)
184+
.should('be.at.least', 1)
185185
cy.syncSteps(connection, {
186186
version: 1,
187187
autosaveContent: '# Heading 1',
@@ -244,7 +244,7 @@ describe('The session Api', function() {
244244
it('saves public', function() {
245245
cy.pushSteps({ connection, steps: [messages.update], version })
246246
.its('version')
247-
.should('eql', 1)
247+
.should('be.at.least', 1)
248248
cy.syncSteps(connection, { version: 1, autosaveContent: '# Heading 1', manualSave: true })
249249
cy.login(user)
250250
cy.prepareSessionApi()
@@ -257,7 +257,7 @@ describe('The session Api', function() {
257257
const documentState = 'Base64 encoded string'
258258
cy.pushSteps({ connection, steps: [messages.update], version })
259259
.its('version')
260-
.should('eql', 1)
260+
.should('be.at.least', 1)
261261
cy.syncSteps(connection, {
262262
version: 1,
263263
autosaveContent: '# Heading 1',
@@ -328,7 +328,7 @@ describe('The session Api', function() {
328328
let joining
329329
cy.pushSteps({ connection, steps: [messages.update], version })
330330
.its('version')
331-
.should('eql', 1)
331+
.should('be.at.least', 1)
332332
cy.createTextSession(undefined, { filePath: '', shareToken })
333333
.then(con => {
334334
joining = con
@@ -345,7 +345,7 @@ describe('The session Api', function() {
345345
cy.log('Initial user pushes steps')
346346
cy.pushSteps({ connection, steps: [messages.update], version })
347347
.its('version')
348-
.should('eql', 1)
348+
.should('be.at.least', 1)
349349
cy.log('Other user creates session')
350350
cy.createTextSession(undefined, { filePath: '', shareToken })
351351
.then(con => {

lib/Db/Step.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
* @method setDocumentId(int $documentId): void
3838
*/
3939
class Step extends Entity implements JsonSerializable {
40+
41+
/*
42+
* Transition: We now use the auto-incrementing id as the version.
43+
* To ensure that new steps always have a larger version than those that
44+
* used the version field, use the largest possible 32-bit integer value.
45+
*/
46+
public const VERSION_STORED_IN_ID = 2147483647;
47+
48+
public $id = null;
4049
protected string $data = '';
4150
protected int $version = 0;
4251
protected int $sessionId = 0;
@@ -54,10 +63,13 @@ public function jsonSerialize(): array {
5463
if (\json_last_error() !== JSON_ERROR_NONE) {
5564
throw new \InvalidArgumentException('Failed to parse step data');
5665
}
66+
$version = $this->version === self::VERSION_STORED_IN_ID
67+
? $this->id
68+
: $this->getVersion();
5769
return [
5870
'id' => $this->id,
5971
'data' => $jsonData,
60-
'version' => $this->version,
72+
'version' => $version,
6173
'sessionId' => $this->sessionId
6274
];
6375
}

lib/Db/StepMapper.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,16 @@ public function __construct(IDBConnection $db) {
3333
parent::__construct($db, 'text_steps', Step::class);
3434
}
3535

36-
public function find($documentId, $fromVersion, $lastAckedVersion = null) {
36+
public function find($documentId, $fromVersion) {
3737
/* @var $qb IQueryBuilder */
3838
$qb = $this->db->getQueryBuilder();
3939
$qb->select('*')
4040
->from($this->getTableName())
4141
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
42-
->andWhere($qb->expr()->gt('version', $qb->createNamedParameter($fromVersion)));
43-
if ($lastAckedVersion) {
44-
$qb->andWhere($qb->expr()->lte('version', $qb->createNamedParameter($lastAckedVersion)));
45-
}
42+
->andWhere($qb->expr()->gt('version', $qb->createNamedParameter($fromVersion)))
43+
->andWhere($qb->expr()->gt('id', $qb->createNamedParameter($fromVersion)));
4644
$qb
4745
->setMaxResults(1000)
48-
->orderBy('version')
4946
->orderBy('id');
5047

5148
return $this->findEntities($qb);
@@ -54,19 +51,19 @@ public function find($documentId, $fromVersion, $lastAckedVersion = null) {
5451
public function getLatestVersion($documentId): ?int {
5552
/* @var $qb IQueryBuilder */
5653
$qb = $this->db->getQueryBuilder();
57-
$result = $qb->select('version')
54+
$result = $qb->select('id')
5855
->from($this->getTableName())
5956
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
6057
->setMaxResults(1)
61-
->orderBy('version', 'DESC')
58+
->orderBy('id', 'DESC')
6259
->execute();
6360

6461
$data = $result->fetch();
6562
if ($data === false) {
6663
return null;
6764
}
6865

69-
return $data['version'];
66+
return $data['id'];
7067
}
7168

7269
public function deleteAll($documentId): void {
@@ -76,11 +73,12 @@ public function deleteAll($documentId): void {
7673
->executeStatement();
7774
}
7875

76+
// not in use right now
7977
public function deleteBeforeVersion($documentId, $version): void {
8078
$qb = $this->db->getQueryBuilder();
8179
$qb->delete($this->getTableName())
8280
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
83-
->andWhere($qb->expr()->lte('version', $qb->createNamedParameter($version)))
81+
->andWhere($qb->expr()->lte('id', $qb->createNamedParameter($version)))
8482
->executeStatement();
8583
}
8684

@@ -89,6 +87,7 @@ public function deleteAfterVersion($documentId, $version): int {
8987
return $qb->delete($this->getTableName())
9088
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
9189
->andWhere($qb->expr()->gt('version', $qb->createNamedParameter($version)))
90+
->andWhere($qb->expr()->gt('id', $qb->createNamedParameter($version)))
9291
->executeStatement();
9392
}
9493
}

lib/Service/DocumentService.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,15 @@ private function insertSteps($documentId, $sessionId, $steps, $version): int {
274274
throw new InvalidArgumentException('Failed to encode steps');
275275
}
276276
$stepsVersion = $this->stepMapper->getLatestVersion($document->getId());
277-
$newVersion = $stepsVersion + count($steps);
278-
$this->logger->debug("Adding steps to $documentId: bumping version from $stepsVersion to $newVersion");
279-
$this->cache->set('document-version-' . $document->getId(), $newVersion);
280277
$step = new Step();
281278
$step->setData($stepsJson);
282279
$step->setSessionId($sessionId);
283280
$step->setDocumentId($documentId);
284-
$step->setVersion($newVersion);
285-
$this->stepMapper->insert($step);
281+
$step->setVersion(Step::VERSION_STORED_IN_ID);
282+
$step = $this->stepMapper->insert($step);
283+
$newVersion = $step->getId();
284+
$this->logger->debug("Adding steps to " . $documentId . ": bumping version from $stepsVersion to $newVersion");
285+
$this->cache->set('document-version-' . $documentId, $newVersion);
286286
// TODO write steps to cache for quicker reading
287287
return $newVersion;
288288
} catch (DoesNotExistException $e) {

0 commit comments

Comments
 (0)