~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to java/src/Freeze/ObjectStore.java

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// **********************************************************************
2
2
//
3
 
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
 
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
4
4
//
5
5
// This copy of Ice is licensed to you under the terms described in the
6
6
// ICE_LICENSE file included in this distribution.
11
11
 
12
12
class ObjectStore implements IceUtil.Store
13
13
{
14
 
    ObjectStore(String facet, String facetType, boolean createDb, EvictorI evictor, 
15
 
                java.util.List indices, boolean populateEmptyIndices)
 
14
    ObjectStore(String facet, String facetType, boolean createDb, EvictorI evictor, java.util.List<Index> indices,
 
15
                boolean populateEmptyIndices)
16
16
    {
17
17
        _cache = new IceUtil.Cache(this);
18
 
        
 
18
 
19
19
        _facet = facet;
20
 
      
 
20
 
21
21
        _evictor = evictor;
22
22
        _indices = indices;
23
23
        _communicator = evictor.communicator();
24
 
        
 
24
 
25
25
        if(facet.equals(""))
26
26
        {
27
27
            _dbName = EvictorI.defaultDb;
39
39
            Ice.ObjectFactory factory = _communicator.findObjectFactory(facetType);
40
40
            if(factory == null)
41
41
            {
42
 
                throw new DatabaseException(_evictor.errorPrefix() + "No object factory registered for type-id '" 
43
 
                                            + facetType + "'");
 
42
                throw new DatabaseException(_evictor.errorPrefix() + "No object factory registered for type-id '" +
 
43
                                            facetType + "'");
44
44
            }
45
 
            
 
45
 
46
46
            _sampleServant = factory.create(facetType);
47
47
        }
48
48
 
49
 
 
50
49
        Connection connection = Util.createConnection(_communicator, evictor.dbEnv().getEnvName());
51
50
 
52
51
        try
53
52
        {
54
 
            Catalog catalog = new Catalog(connection, Util.catalogName(), true);            
55
 
            CatalogData catalogData = (CatalogData)catalog.get(evictor.filename());
56
 
            
 
53
            Catalog catalog = new Catalog(connection, Util.catalogName(), true);
 
54
            CatalogData catalogData = catalog.get(evictor.filename());
 
55
 
57
56
            if(catalogData != null && catalogData.evictor == false)
58
57
            {
59
58
                DatabaseException ex = new DatabaseException();
60
59
                ex.message = _evictor.errorPrefix() + evictor.filename() + " is not an evictor database";
61
60
                throw ex;
62
61
            }
63
 
            
 
62
 
64
63
            com.sleepycat.db.Environment dbEnv = evictor.dbEnv().getEnv();
65
 
            
 
64
 
66
65
            //
67
66
            // TODO: FREEZE_DB_MODE
68
67
            //
72
71
 
73
72
            Ice.Properties properties = _evictor.communicator().getProperties();
74
73
            String propPrefix = "Freeze.Evictor." + _evictor.filename() + ".";
75
 
                
 
74
 
76
75
            int btreeMinKey = properties.getPropertyAsInt(propPrefix + _dbName + ".BtreeMinKey");
77
76
            if(btreeMinKey > 2)
78
77
            {
79
78
                if(_evictor.trace() >= 1)
80
79
                {
81
80
                    _evictor.communicator().getLogger().trace(
82
 
                        "Freeze.Evictor", "Setting \"" + _evictor.filename() + "." + _dbName + "\"'s btree minkey to " + btreeMinKey);
 
81
                        "Freeze.Evictor", "Setting \"" + _evictor.filename() + "." + _dbName +
 
82
                        "\"'s btree minkey to " + btreeMinKey);
83
83
                }
84
84
                config.setBtreeMinKey(btreeMinKey);
85
85
            }
86
 
                
 
86
 
87
87
            boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0;
88
88
            if(checksum)
89
89
            {
92
92
                   _evictor.communicator().getLogger().trace(
93
93
                        "Freeze.Evictor", "Turning checksum on for \"" + _evictor.filename()  + "\"");
94
94
                }
95
 
                    
 
95
 
96
96
                config.setChecksum(true);
97
97
            }
98
 
                
 
98
 
99
99
            int pageSize = properties.getPropertyAsInt(propPrefix + "PageSize");
100
100
            if(pageSize > 0)
101
101
            {
107
107
                config.setPageSize(pageSize);
108
108
            }
109
109
 
110
 
 
111
110
            try
112
 
            {       
 
111
            {
113
112
                Transaction tx = connection.beginTransaction();
114
113
                com.sleepycat.db.Transaction txn = Util.getTxn(tx);
115
114
 
116
115
                _db = dbEnv.openDatabase(txn, evictor.filename(), _dbName, config);
117
116
 
118
 
                java.util.Iterator p = _indices.iterator();
119
 
                while(p.hasNext())
 
117
                for(Index index : _indices)
120
118
                {
121
 
                    Index index = (Index) p.next();
122
119
                    index.associate(this, txn, createDb, populateEmptyIndices);
123
120
                }
124
 
                
 
121
 
125
122
                if(catalogData == null)
126
123
                {
127
124
                    catalogData = new CatalogData();
172
169
        try
173
170
        {
174
171
            _db.close();
175
 
           
176
 
            java.util.Iterator p = _indices.iterator();
177
 
            while(p.hasNext())
 
172
 
 
173
            for(Index index : _indices)
178
174
            {
179
 
                Index index = (Index)p.next();
180
175
                index.close();
181
176
            }
182
177
            _indices.clear();
190
185
        }
191
186
        _db = null;
192
187
    }
193
 
    
 
188
 
194
189
    boolean
195
190
    dbHasObject(Ice.Identity ident, TransactionI transaction)
196
191
    {
205
200
            }
206
201
        }
207
202
 
208
 
 
209
203
        byte[] key = marshalKey(ident, _communicator);
210
204
        com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(key);
211
 
                
 
205
 
212
206
        //
213
207
        // Keep 0 length since we're not interested in the data
214
208
        //
215
209
        com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry();
216
210
        dbValue.setPartial(true);
217
 
        
 
211
 
218
212
        for(;;)
219
213
        {
220
214
            try
221
 
            {   
 
215
            {
222
216
                com.sleepycat.db.OperationStatus err = _db.get(tx, dbKey, dbValue, null);
223
 
                
 
217
 
224
218
                if(err == com.sleepycat.db.OperationStatus.SUCCESS)
225
219
                {
226
220
                    return true;
239
233
                if(_evictor.deadlockWarning())
240
234
                {
241
235
                    _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.dhHasObject while reading " +
242
 
                                                      "Db \"" + _evictor.filename() + "/" + _dbName +
243
 
                                                      "\"");
 
236
                                                      "Db \"" + _evictor.filename() + "/" + _dbName + "\"");
244
237
                }
245
238
 
246
239
                if(tx != null)
311
304
    static byte[]
312
305
    marshalKey(Ice.Identity v, Ice.Communicator communicator)
313
306
    {
314
 
        IceInternal.BasicStream os = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
 
307
        IceInternal.BasicStream os =
 
308
            new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), false, false);
315
309
        v.__write(os);
316
310
        IceInternal.Buffer buf = os.prepareWrite();
317
311
        byte[] r = new byte[buf.size()];
322
316
    static Ice.Identity
323
317
    unmarshalKey(byte[] b, Ice.Communicator communicator)
324
318
    {
325
 
        IceInternal.BasicStream is = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
 
319
        IceInternal.BasicStream is =
 
320
            new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), false, false);
326
321
        is.resize(b.length, true);
327
322
        IceInternal.Buffer buf = is.getBuffer();
328
323
        buf.b.position(0);
336
331
    static byte[]
337
332
    marshalValue(ObjectRecord v, Ice.Communicator communicator)
338
333
    {
339
 
        IceInternal.BasicStream os = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
 
334
        IceInternal.BasicStream os =
 
335
            new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), false, false);
340
336
        os.startWriteEncaps();
341
337
        v.__write(os);
342
338
        os.writePendingObjects();
350
346
    static ObjectRecord
351
347
    unmarshalValue(byte[] b, Ice.Communicator communicator)
352
348
    {
353
 
        IceInternal.BasicStream is = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
 
349
        IceInternal.BasicStream is =
 
350
            new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), false, false);
354
351
        is.sliceObjects(false);
355
352
        is.resize(b.length, true);
356
353
        IceInternal.Buffer buf = is.getBuffer();
376
373
    {
377
374
        return _db;
378
375
    }
379
 
    
 
376
 
380
377
    final Ice.Communicator
381
378
    communicator()
382
379
    {
383
380
        return _communicator;
384
381
    }
385
 
    
 
382
 
386
383
    final EvictorI
387
384
    evictor()
388
385
    {
389
386
        return _evictor;
390
387
    }
391
 
    
 
388
 
392
389
    final String
393
390
    facet()
394
391
    {
424
421
        for(;;)
425
422
        {
426
423
            try
427
 
            {   
 
424
            {
428
425
                com.sleepycat.db.OperationStatus rs = _db.get(null, dbKey, dbValue, null);
429
 
                
 
426
 
430
427
                if(rs == com.sleepycat.db.OperationStatus.NOTFOUND)
431
428
                {
432
429
                    return null;
461
458
 
462
459
        ObjectRecord rec = unmarshalValue(dbValue.getData(), _communicator);
463
460
        _evictor.initialize(ident, _facet, rec.servant);
464
 
     
 
461
 
465
462
        Object result = _evictor.createEvictorElement(ident, rec, this);
466
463
        return result;
467
464
    }
489
486
        com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry();
490
487
 
491
488
        try
492
 
        {   
 
489
        {
493
490
            com.sleepycat.db.OperationStatus rs = _db.get(tx, dbKey, dbValue, null);
494
 
            
 
491
 
495
492
            if(rs == com.sleepycat.db.OperationStatus.NOTFOUND)
496
493
            {
497
494
                return null;
509
506
                _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.load while reading Db \"" +
510
507
                                                  _evictor.filename() + "/" + _dbName + "\"");
511
508
            }
512
 
            
513
 
            DeadlockException ex = new DeadlockException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(),
514
 
                                                         transaction);
 
509
 
 
510
            DeadlockException ex = new DeadlockException(
 
511
                _evictor.errorPrefix() + "Db.get: " + dx.getMessage(), transaction);
515
512
            ex.initCause(dx);
516
513
            throw ex;
517
514
        }
528
525
        return rec;
529
526
    }
530
527
 
531
 
 
532
528
    void
533
529
    update(Ice.Identity ident, ObjectRecord objectRecord, TransactionI transaction)
534
530
    {
546
542
        {
547
543
            String msg = _evictor.errorPrefix() + "Attempting to save a '" + objectRecord.servant.ice_id()
548
544
                + "' servant in a database of '" + _sampleServant.ice_id() + "' servants";
549
 
            
 
545
 
550
546
            throw new DatabaseException(msg);
551
547
        }
552
548
 
553
549
        com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(marshalKey(ident, _communicator));
554
 
        com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(marshalValue(objectRecord, _communicator));
 
550
        com.sleepycat.db.DatabaseEntry dbValue =
 
551
            new com.sleepycat.db.DatabaseEntry(marshalValue(objectRecord, _communicator));
555
552
 
556
553
        try
557
554
        {
568
565
                _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.update while updating Db \"" +
569
566
                                                  _evictor.filename() + "/" + _dbName + "\"");
570
567
            }
571
 
            
 
568
 
572
569
            DeadlockException ex = new DeadlockException(
573
570
                _evictor.errorPrefix() + "Db.put: " + dx.getMessage(), transaction);
574
571
            ex.initCause(dx);
598
595
        }
599
596
 
600
597
        com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(marshalKey(ident, _communicator));
601
 
        com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(marshalValue(objectRecord, _communicator));
 
598
        com.sleepycat.db.DatabaseEntry dbValue =
 
599
            new com.sleepycat.db.DatabaseEntry(marshalValue(objectRecord, _communicator));
602
600
 
603
601
        if(_sampleServant != null && !objectRecord.servant.ice_id().equals(_sampleServant.ice_id()))
604
602
        {
605
 
            String msg = _evictor.errorPrefix() + "Attempting to save a '" + objectRecord.servant.ice_id()
606
 
                + "' servant in a database of '" + _sampleServant.ice_id() + "' servants";
607
 
            
 
603
            String msg = _evictor.errorPrefix() + "Attempting to save a '" + objectRecord.servant.ice_id() +
 
604
                "' servant in a database of '" + _sampleServant.ice_id() + "' servants";
 
605
 
608
606
            throw new DatabaseException(msg);
609
607
        }
610
608
 
621
619
                    _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.update while updating Db \"" +
622
620
                                                      _evictor.filename() + "/" + _dbName + "\"");
623
621
                }
624
 
                
 
622
 
625
623
                if(tx != null)
626
624
                {
627
625
                    DeadlockException ex = new DeadlockException(
645
643
 
646
644
    boolean
647
645
    remove(Ice.Identity ident, TransactionI transaction)
648
 
    { 
 
646
    {
649
647
        com.sleepycat.db.Transaction tx = null;
650
648
 
651
649
        if(transaction != null)
675
673
 
676
674
                if(tx != null)
677
675
                {
678
 
                    DeadlockException ex = new DeadlockException(_evictor.errorPrefix() + "Db.delete: " + dx.getMessage(),
679
 
                                                                 transaction);
 
676
                    DeadlockException ex = new DeadlockException(
 
677
                        _evictor.errorPrefix() + "Db.delete: " + dx.getMessage(), transaction);
680
678
                    ex.initCause(dx);
681
679
                    throw ex;
682
680
                }
700
698
    private final String _facet;
701
699
    private final String _dbName;
702
700
    private final EvictorI _evictor;
703
 
    private final java.util.List _indices;
 
701
    private final java.util.List<Index> _indices;
704
702
    private final Ice.Communicator _communicator;
705
703
 
706
704
    private com.sleepycat.db.Database _db;