Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 88b1864a3cd0689a11af1542a93c46f6f4fa471d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*
 * Copyright (c) 2009-2013, 2016, 2019 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andre Dietisheim - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.common.internal.db.cache;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchManager;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.branch.CDOBranchVersion;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfoManager;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDProvider;
import org.eclipse.emf.cdo.common.internal.db.AbstractQueryStatement;
import org.eclipse.emf.cdo.common.internal.db.AbstractUpdateStatement;
import org.eclipse.emf.cdo.common.internal.db.DBRevisionCacheUtil;
import org.eclipse.emf.cdo.common.lob.CDOLobStore;
import org.eclipse.emf.cdo.common.model.CDOPackageRegistry;
import org.eclipse.emf.cdo.common.protocol.CDODataInput;
import org.eclipse.emf.cdo.common.protocol.CDODataOutput;
import org.eclipse.emf.cdo.common.revision.CDOListFactory;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.CDORevisionCache;
import org.eclipse.emf.cdo.common.revision.CDORevisionFactory;
import org.eclipse.emf.cdo.spi.common.protocol.CDODataInputImpl;
import org.eclipse.emf.cdo.spi.common.protocol.CDODataOutputImpl;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevisionCache;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBConnectionProvider;
import org.eclipse.net4j.util.CheckUtil;
import org.eclipse.net4j.util.io.ExtendedDataInputStream;
import org.eclipse.net4j.util.io.ExtendedDataOutput;
import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
import org.eclipse.net4j.util.lifecycle.Lifecycle;

import org.eclipse.emf.ecore.EClass;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * A JDBC-based {@link CDORevisionCache}.
 *
 * @author Andre Dietisheim
 */
public class DBRevisionCache extends Lifecycle implements InternalCDORevisionCache
{
  private CDOIDProvider idProvider;

  private CDOListFactory listFactory;

  private CDOPackageRegistry packageRegistry;

  private CDORevisionFactory revisionFactory;

  private IDBAdapter dbAdapter;

  private IDBConnectionProvider dbConnectionProvider;

  public DBRevisionCache()
  {
  }

  @Override
  public InternalCDORevisionCache instantiate(CDORevision revision)
  {
    // TODO: Support branches directly
    throw new UnsupportedOperationException();
  }

  public CDOIDProvider getIDProvider()
  {
    return idProvider;
  }

  public void setIDProvider(CDOIDProvider idProvider)
  {
    this.idProvider = idProvider;
  }

  public CDOListFactory getListFactory()
  {
    return listFactory;
  }

  public void setListFactory(CDOListFactory listFactory)
  {
    this.listFactory = listFactory;
  }

  public CDOPackageRegistry getPackageRegistry()
  {
    return packageRegistry;
  }

  public void setPackageRegistry(CDOPackageRegistry packageRegistry)
  {
    this.packageRegistry = packageRegistry;
  }

  public CDORevisionFactory getRevisionFactory()
  {
    return revisionFactory;
  }

  public void setRevisionFactory(CDORevisionFactory revisionFactory)
  {
    this.revisionFactory = revisionFactory;
  }

  public IDBAdapter getDBAdapter()
  {
    return dbAdapter;
  }

  public void setDBAdapter(IDBAdapter dbAdapter)
  {
    this.dbAdapter = dbAdapter;
  }

  public IDBConnectionProvider getDBConnectionProvider()
  {
    return dbConnectionProvider;
  }

  public void setDBConnectionProvider(IDBConnectionProvider dbConnectionProvider)
  {
    this.dbConnectionProvider = dbConnectionProvider;
  }

  @Override
  public EClass getObjectType(CDOID id)
  {
    return null;
  }

  /**
   * Gets the revision with the highest version for a given {@link CDOID}.
   *
   * @param id
   *          the id to match
   * @return the revision that was found
   */
  public InternalCDORevision getRevision(final CDOID id)
  {
    Connection connection = null;
    String sql = null;

    try
    {
      connection = getConnection();
      AbstractQueryStatement<InternalCDORevision> query = createGetRevisionByIDStatement(id);
      sql = query.getSQL();
      return query.query(connection);
    }
    catch (Exception e)
    {
      throw new DBException("Error while retrieving the revision from the database", e, sql); //$NON-NLS-1$
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  private AbstractQueryStatement<InternalCDORevision> createGetRevisionByIDStatement(final CDOID id)
  {
    return new AbstractQueryStatement<InternalCDORevision>()
    {
      @Override
      public String getSQL()
      {
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_CDOREVISION);
        builder.append(", "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
        builder.append(" FROM "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS);
        builder.append(" WHERE "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_ID);
        builder.append("=? AND "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
        builder.append("="); //$NON-NLS-1$
        builder.append(CDORevision.UNSPECIFIED_DATE);
        return builder.toString();
      }

      @Override
      protected void setParameters(PreparedStatement preparedStatement) throws SQLException
      {
        preparedStatement.setString(1, id.toURIFragment());
      }

      @Override
      protected InternalCDORevision getResult(ResultSet resultSet) throws Exception
      {
        long revised = resultSet.getLong(2);
        Blob blob = resultSet.getBlob(1);
        return toRevision(blob, revised);
      }
    };
  }

  /**
   * Gets an {@link InternalCDORevision} that matches the given timestamp (it is >= created timestamp AND <= revised
   * timestamp of the revision).
   *
   * @param id
   *          the id
   * @return the revision by time
   */
  @Override
  public InternalCDORevision getRevision(final CDOID id, final CDOBranchPoint branchPoint)
  {
    Connection connection = null;
    String sql = null;

    try
    {
      connection = getConnection();
      AbstractQueryStatement<InternalCDORevision> statement = new AbstractQueryStatement<InternalCDORevision>()
      {
        @Override
        public String getSQL()
        {
          StringBuilder builder = new StringBuilder();
          builder.append("SELECT "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_CDOREVISION);
          builder.append(", "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
          builder.append(" FROM "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS);
          builder.append(" WHERE "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_ID);
          builder.append("=? AND "); //$NON-NLS-1$
          appendTimestampCondition(builder);
          return builder.toString();
        }

        @Override
        protected void setParameters(PreparedStatement preparedStatement) throws SQLException
        {
          long timeStamp = branchPoint.getTimeStamp();
          preparedStatement.setString(1, id.toURIFragment());
          preparedStatement.setLong(2, timeStamp);
          preparedStatement.setLong(3, timeStamp);
        }

        @Override
        protected InternalCDORevision getResult(ResultSet resultSet) throws Exception
        {
          long revised = resultSet.getLong(2);
          Blob blob = resultSet.getBlob(1);
          return toRevision(blob, revised);
        }
      };

      sql = statement.getSQL();
      return statement.query(connection);
    }
    catch (Exception e)
    {
      throw new DBException("Error while retrieving a revision by timestamp from the database", e, sql); //$NON-NLS-1$
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  /**
   * Gets a {@link InternalCDORevision} by a given id and version.
   *
   * @param id
   *          the id to match the revision against
   * @return the revision by version
   */
  @Override
  public InternalCDORevision getRevisionByVersion(final CDOID id, final CDOBranchVersion branchVersion)
  {
    Connection connection = null;
    String sql = null;

    try
    {
      connection = getConnection();
      AbstractQueryStatement<InternalCDORevision> statement = new AbstractQueryStatement<InternalCDORevision>()
      {
        @Override
        public String getSQL()
        {
          StringBuilder builder = new StringBuilder();
          builder.append("SELECT "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_CDOREVISION);
          builder.append(", "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
          builder.append(" FROM "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS);
          builder.append(" WHERE "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_ID);
          builder.append("=? AND "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_VERSION);
          builder.append("=?"); //$NON-NLS-1$
          return builder.toString();
        }

        @Override
        protected void setParameters(PreparedStatement preparedStatement) throws SQLException
        {
          preparedStatement.setString(1, id.toURIFragment());
          preparedStatement.setInt(2, branchVersion.getVersion());
        }

        @Override
        protected InternalCDORevision getResult(ResultSet resultSet) throws Exception
        {
          long revised = resultSet.getLong(2);
          Blob blob = resultSet.getBlob(1);
          return toRevision(blob, revised);
        }
      };

      sql = statement.getSQL();
      return statement.query(connection);
    }
    catch (Exception e)
    {
      throw new DBException("Error while retrieving a revision by version from the database", e, sql); //$NON-NLS-1$
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  /**
   * Gets the latest revisions of all persisted model versions.
   *
   * @return the revisions
   */
  @Override
  public List<CDORevision> getCurrentRevisions()
  {
    Connection connection = null;
    String sql = null;

    try
    {
      connection = getConnection();
      AbstractQueryStatement<List<CDORevision>> query = new AbstractQueryStatement<List<CDORevision>>()
      {
        @Override
        public String getSQL()
        {
          StringBuilder builder = new StringBuilder();
          builder.append("SELECT "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_CDOREVISION);
          builder.append(", "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
          builder.append(" FROM "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS);
          builder.append(" WHERE "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
          builder.append("="); //$NON-NLS-1$
          builder.append(CDORevision.UNSPECIFIED_DATE);
          return builder.toString();
        }

        @Override
        protected void setParameters(PreparedStatement preparedStatement) throws SQLException
        {
        }

        @Override
        protected List<CDORevision> getResult(ResultSet resultSet) throws Exception
        {
          final List<CDORevision> revisionList = new ArrayList<>();

          do
          {
            long revised = resultSet.getLong(2);
            Blob blob = resultSet.getBlob(1);
            revisionList.add(toRevision(blob, revised));
          } while (resultSet.next());

          return revisionList;
        }
      };

      sql = query.getSQL();
      return query.query(connection);
    }
    catch (Exception e)
    {
      throw new DBException("Error while retrieving a revision by version from the database", e, sql); //$NON-NLS-1$
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  @Override
  public void forEachCurrentRevision(Consumer<CDORevision> consumer)
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public void getAllRevisions(List<InternalCDORevision> result)
  {
    throw new UnsupportedOperationException();
  }

  /**
   * Adds a given revision to this cache. It furthermore updates the revised timestamp of the latest (before inserting
   * the new one) revision
   *
   * @param revision
   *          the revision to add to this cache
   */
  @Override
  public void addRevision(CDORevision revision)
  {
    CheckUtil.checkArg(revision, "revision");
    Connection connection = null;
    String sql = null;

    try
    {
      connection = getConnection();
      AbstractUpdateStatement update = createAddRevisionStatement((InternalCDORevision)revision);
      sql = update.getSQL();
      update.update(connection);

      if (revision.getVersion() > CDORevision.FIRST_VERSION)
      {
        // Update former latest revision
        update = createUpdateRevisedStatement((InternalCDORevision)revision);
        update.update(connection);
      }
    }
    catch (DBException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new DBException("Error while retrieving the revision from the database", e, sql); //$NON-NLS-1$
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  private AbstractUpdateStatement createUpdateRevisedStatement(final InternalCDORevision revision)
  {
    return new AbstractUpdateStatement()
    {
      @Override
      public String getSQL()
      {
        StringBuilder builder = new StringBuilder();
        builder.append("UPDATE "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS);
        builder.append(" SET "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
        builder.append(" =? WHERE "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_ID);
        builder.append(" =? AND "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_VERSION);
        builder.append(" =?"); //$NON-NLS-1$
        return builder.toString();
      }

      @Override
      protected void setParameters(PreparedStatement preparedStatement) throws SQLException
      {
        preparedStatement.setLong(1, revision.getTimeStamp() - 1);
        preparedStatement.setString(2, revision.getID().toURIFragment());
        preparedStatement.setInt(3, revision.getVersion() - 1);
      }
    };
  }

  private AbstractUpdateStatement createAddRevisionStatement(final InternalCDORevision revision)
  {
    return new AbstractUpdateStatement()
    {
      @Override
      public String getSQL()
      {
        StringBuilder builder = new StringBuilder();
        builder.append("INSERT INTO "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS);
        builder.append(" ("); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_ID);
        builder.append(", "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_VERSION);
        builder.append(", "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_CREATED);
        builder.append(", "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
        builder.append(", "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_CDOREVISION);
        builder.append(", "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_RESOURCENODE_NAME);
        builder.append(", "); //$NON-NLS-1$
        builder.append(DBRevisionCacheSchema.REVISIONS_CONTAINERID);
        builder.append(") "); //$NON-NLS-1$
        builder.append(" VALUES (?, ?, ?, ?, ?, ? ,?)"); //$NON-NLS-1$
        return builder.toString();
      }

      @Override
      protected void setParameters(PreparedStatement preparedStatement) throws Exception
      {
        preparedStatement.setString(1, revision.getID().toURIFragment());
        preparedStatement.setInt(2, revision.getVersion());
        preparedStatement.setLong(3, revision.getTimeStamp());
        preparedStatement.setLong(4, revision.getRevised());
        preparedStatement.setBytes(5, toBytes(revision));
        setResourceNodeValues(revision, preparedStatement);
      }

      /**
       * Sets the values in the prepared statment, that are related to the given revision. If the revision is a resource
       * node, the values are set otherwise the fields are set to <tt>null</tt>
       *
       * @param revision
       *          the revision
       * @param preparedStatement
       *          the prepared statement
       * @throws SQLException
       *           the SQL exception
       */
      private void setResourceNodeValues(InternalCDORevision revision, PreparedStatement preparedStatement) throws SQLException
      {
        if (revision.isResourceNode())
        {
          preparedStatement.setString(6, DBRevisionCacheUtil.getResourceNodeName(revision));
          CDOID containerID = (CDOID)revision.getContainerID();
          preparedStatement.setString(7, containerID.toURIFragment());
        }
        else
        {
          preparedStatement.setNull(6, Types.VARCHAR);
          preparedStatement.setNull(7, Types.INTEGER);
        }
      }
    };
  }

  /**
   * Removes a revision by its Id and version. If the given revision does not exist <tt>null</tt> is returned. Otherwise
   * the {@link InternalCDORevision}, that was removed is returned
   *
   * @param id
   *          the id of the revision to remove
   * @return the {@link InternalCDORevision} that was removed, <tt>null</tt> otherwise
   */
  @Override
  public InternalCDORevision removeRevision(CDOID id, CDOBranchVersion branchVersion)
  {
    Connection connection = null;
    String sql = null;

    try
    {
      final InternalCDORevision revision = getRevisionByVersion(id, branchVersion);
      if (revision != null)
      {
        connection = getConnection();
        AbstractUpdateStatement statement = new AbstractUpdateStatement()
        {
          @Override
          public String getSQL()
          {
            StringBuilder builder = new StringBuilder();
            builder.append("DELETE FROM "); //$NON-NLS-1$
            builder.append(DBRevisionCacheSchema.REVISIONS);
            builder.append(" WHERE ID =? AND VERSION =?"); //$NON-NLS-1$
            return builder.toString();
          }

          @Override
          protected void setParameters(PreparedStatement preparedStatement) throws Exception
          {
            preparedStatement.setString(1, revision.getID().toURIFragment());
            preparedStatement.setInt(2, revision.getVersion());
          }
        };

        sql = statement.getSQL();
        statement.update(connection);
      }

      return revision;
    }
    catch (Exception e)
    {
      throw new DBException("Error while removing a revision from the database", e, sql); //$NON-NLS-1$
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  /**
   * Removes all revisions from this cache (and its database).
   */
  @Override
  public void clear()
  {
    Connection connection = null;
    String sql = null;

    try
    {
      connection = getConnection();
      AbstractUpdateStatement update = new AbstractUpdateStatement()
      {
        @Override
        public String getSQL()
        {
          StringBuilder builder = new StringBuilder();
          builder.append("DELETE FROM  "); //$NON-NLS-1$
          builder.append(DBRevisionCacheSchema.REVISIONS);
          return builder.toString();
        }
      };

      sql = update.getSQL();
      update.update(connection);
    }
    catch (Exception e)
    {
      throw new DBException("Error while clearing the database", e, sql);
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  @Override
  public Map<CDOBranch, List<CDORevision>> getAllRevisions()
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public List<CDORevision> getRevisions(CDOBranchPoint branchPoint)
  {
    throw new UnsupportedOperationException();
  }

  @Override
  protected void doBeforeActivate() throws Exception
  {
    super.doBeforeActivate();
    checkState(idProvider, "idProvider"); //$NON-NLS-1$
    checkState(listFactory, "listFactory");//$NON-NLS-1$
    checkState(packageRegistry, "packageRegistry"); //$NON-NLS-1$
    checkState(revisionFactory, "revisionFactory"); //$NON-NLS-1$
    checkState(dbAdapter, "dbAdapter"); //$NON-NLS-1$
    checkState(dbConnectionProvider, "dbConnectionProvider"); //$NON-NLS-1$
  }

  @Override
  protected void doActivate() throws Exception
  {
    super.doActivate();
    createTable();
  }

  /**
   * Creates the (single) table that's used to store the cached revisions.
   *
   * @throws SQLException
   *           Signals that an error has occured while getting the connection or committing the transaction
   */
  private void createTable() throws SQLException
  {
    Connection connection = null;

    try
    {
      connection = getConnection();
      DBRevisionCacheSchema.INSTANCE.create(dbAdapter, connection);
      connection.commit();
    }
    finally
    {
      DBUtil.close(connection);
    }
  }

  private static StringBuilder appendTimestampCondition(StringBuilder builder)
  {
    builder.append(DBRevisionCacheSchema.REVISIONS_CREATED);
    builder.append("<=? AND ("); //$NON-NLS-1$
    builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
    builder.append(">=? OR "); //$NON-NLS-1$
    builder.append(DBRevisionCacheSchema.REVISIONS_REVISED);
    builder.append("="); //$NON-NLS-1$
    builder.append(CDORevision.UNSPECIFIED_DATE);
    builder.append(")"); //$NON-NLS-1$
    return builder;
  }

  /**
   * Converts the given Objects to an {@link InternalCDORevision}. The object is deserialized to an instance of the
   * correct type and the revised timestamp is set separatley. Whe you insert a new revision into this cache, the former
   * latest revision gets a new revised timestamp. This timestamp's only updated in the database column 'revised', not
   * in the blob that holds the serialized instance. Therefore the revised timestamp has to be set separately
   *
   * @param revisedTimestamp
   *          the revised timestamp to set to the revision
   * @param blob
   *          the blob that holds the revision
   * @return the revision
   * @throws IOException
   *           Signals that an error has occurred while reading the revision from the blob.
   * @throws SQLException
   *           Signals that an error hass occured while getting the binary stream from the blob
   */
  private InternalCDORevision toRevision(Blob blob, long revisedTimestamp) throws IOException, SQLException
  {
    CDODataInput dataInput = getCDODataInput(ExtendedDataInputStream.wrap(blob.getBinaryStream()));
    InternalCDORevision revision = (InternalCDORevision)dataInput.readCDORevision();
    // Revised timestamp's updated in the revised column only (not in the blob)
    revision.setRevised(revisedTimestamp);
    return revision;
  }

  private CDODataInput getCDODataInput(ExtendedDataInputStream inputStream) throws IOException
  {
    return new CDODataInputImpl(inputStream)
    {
      @Override
      public CDOPackageRegistry getPackageRegistry()
      {
        return packageRegistry;
      }

      @Override
      protected CDOBranchManager getBranchManager()
      {
        return null;
      }

      @Override
      protected CDOCommitInfoManager getCommitInfoManager()
      {
        return null;
      }

      @Override
      protected CDORevisionFactory getRevisionFactory()
      {
        return revisionFactory;
      }

      @Override
      protected CDOListFactory getListFactory()
      {
        return listFactory;
      }

      @Override
      protected CDOLobStore getLobStore()
      {
        return null;
      }
    };
  }

  /**
   * Converts a given {@link CDORevision} to a byte array.
   *
   * @param revision
   *          the revision
   * @return the array of bytes for the given revision
   * @throws IOException
   *           Signals an error has occurred while writing the revision to the byte array.
   */
  private byte[] toBytes(InternalCDORevision revision) throws IOException
  {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CDODataOutput dataOutput = getCDODataOutput(ExtendedDataOutputStream.wrap(byteArrayOutputStream));
    dataOutput.writeCDORevision(revision, CDORevision.UNCHUNKED);
    return byteArrayOutputStream.toByteArray();
  }

  private CDODataOutput getCDODataOutput(ExtendedDataOutput extendedDataOutputStream)
  {
    return new CDODataOutputImpl(extendedDataOutputStream)
    {
      @Override
      public CDOPackageRegistry getPackageRegistry()
      {
        return packageRegistry;
      }

      @Override
      public CDOIDProvider getIDProvider()
      {
        return idProvider;
      }
    };
  }

  /**
   * Gets a connection from the {@link IDBConnectionProvider} within this cache. The Connection is set not to auto
   * commit transactions.
   *
   * @return the connection
   * @throws SQLException
   *           Signals that an error occured while getting the connection from the connection provider
   */
  private Connection getConnection() throws SQLException
  {
    Connection connection = dbConnectionProvider.getConnection();
    connection.setAutoCommit(false);
    return connection;
  }
}

Back to the top