forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsInMemoryDataSource.cpp
2261 lines (1879 loc) · 67.2 KB
/
nsInMemoryDataSource.cpp
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Waterson <waterson@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****
*
*
* This Original Code has been modified by IBM Corporation.
* Modifications made by IBM described herein are
* Copyright (c) International Business Machines
* Corporation, 2000
*
* Modifications to Mozilla code or documentation
* identified per MPL Section 3.3
*
* Date Modified by Description of modification
* 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink
* use in OS2
*/
/*
Implementation for an in-memory RDF data store.
TO DO
1) Instrument this code to gather space and time performance
characteristics.
2) Optimize lookups for datasources which have a small number
of properties + fanning out to a large number of targets.
3) Complete implementation of thread-safety; specifically, make
assertions be reference counted objects (so that a cursor can
still refer to an assertion that gets removed from the graph).
*/
#include "nsAgg.h"
#include "nsCOMPtr.h"
#include "nscore.h"
#include "nsIOutputStream.h"
#include "nsIRDFDataSource.h"
#include "nsIRDFLiteral.h"
#include "nsIRDFNode.h"
#include "nsIRDFObserver.h"
#include "nsIRDFInMemoryDataSource.h"
#include "nsIRDFPropagatableDataSource.h"
#include "nsIRDFPurgeableDataSource.h"
#include "nsIRDFService.h"
#include "nsIServiceManager.h"
#include "nsISupportsArray.h"
#include "nsCOMArray.h"
#include "nsEnumeratorUtils.h"
#include "nsTArray.h"
#include "nsCRT.h"
#include "nsRDFCID.h"
#include "nsRDFBaseDataSources.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsXPIDLString.h"
#include "nsFixedSizeAllocator.h"
#include "rdfutil.h"
#include "pldhash.h"
#include "plstr.h"
#include "prlog.h"
#include "rdf.h"
#include "rdfIDataSource.h"
#include "rdfITripleVisitor.h"
#ifdef PR_LOGGING
static PRLogModuleInfo* gLog = nsnull;
#endif
// This struct is used as the slot value in the forward and reverse
// arcs hash tables.
//
// Assertion objects are reference counted, because each Assertion's
// ownership is shared between the datasource and any enumerators that
// are currently iterating over the datasource.
//
class Assertion
{
public:
static Assertion*
Create(nsFixedSizeAllocator& aAllocator,
nsIRDFResource* aSource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget,
PRBool aTruthValue) {
void* place = aAllocator.Alloc(sizeof(Assertion));
return place
? ::new (place) Assertion(aSource, aProperty, aTarget, aTruthValue)
: nsnull; }
static Assertion*
Create(nsFixedSizeAllocator& aAllocator, nsIRDFResource* aSource) {
void* place = aAllocator.Alloc(sizeof(Assertion));
return place
? ::new (place) Assertion(aSource)
: nsnull; }
static void
Destroy(nsFixedSizeAllocator& aAllocator, Assertion* aAssertion) {
if (aAssertion->mHashEntry && aAssertion->u.hash.mPropertyHash) {
PL_DHashTableEnumerate(aAssertion->u.hash.mPropertyHash,
DeletePropertyHashEntry, &aAllocator);
PL_DHashTableDestroy(aAssertion->u.hash.mPropertyHash);
aAssertion->u.hash.mPropertyHash = nsnull;
}
aAssertion->~Assertion();
aAllocator.Free(aAssertion, sizeof(*aAssertion)); }
static PLDHashOperator
DeletePropertyHashEntry(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg);
Assertion(nsIRDFResource* aSource, // normal assertion
nsIRDFResource* aProperty,
nsIRDFNode* aTarget,
PRBool aTruthValue);
Assertion(nsIRDFResource* aSource); // PLDHashTable assertion variant
~Assertion();
void AddRef() {
if (mRefCnt == PR_UINT16_MAX) {
NS_WARNING("refcount overflow, leaking Assertion");
return;
}
++mRefCnt;
}
void Release(nsFixedSizeAllocator& aAllocator) {
if (mRefCnt == PR_UINT16_MAX) {
NS_WARNING("refcount overflow, leaking Assertion");
return;
}
if (--mRefCnt == 0)
Destroy(aAllocator, this);
}
// For nsIRDFPurgeableDataSource
inline void Mark() { u.as.mMarked = PR_TRUE; }
inline PRBool IsMarked() { return u.as.mMarked; }
inline void Unmark() { u.as.mMarked = PR_FALSE; }
// public for now, because I'm too lazy to go thru and clean this up.
// These are shared between hash/as (see the union below)
nsIRDFResource* mSource;
Assertion* mNext;
union
{
struct hash
{
PLDHashTable* mPropertyHash;
} hash;
struct as
{
nsIRDFResource* mProperty;
nsIRDFNode* mTarget;
Assertion* mInvNext;
// make sure PRPackedBool are final elements
PRPackedBool mTruthValue;
PRPackedBool mMarked;
} as;
} u;
// also shared between hash/as (see the union above)
// but placed after union definition to ensure that
// all 32-bit entries are long aligned
PRUint16 mRefCnt;
PRPackedBool mHashEntry;
private:
// Hide so that only Create() and Destroy() can be used to
// allocate and deallocate from the heap
static void* operator new(size_t) CPP_THROW_NEW { return 0; }
static void operator delete(void*, size_t) {}
};
struct Entry {
PLDHashEntryHdr mHdr;
nsIRDFNode* mNode;
Assertion* mAssertions;
};
Assertion::Assertion(nsIRDFResource* aSource)
: mSource(aSource),
mNext(nsnull),
mRefCnt(0),
mHashEntry(PR_TRUE)
{
MOZ_COUNT_CTOR(RDF_Assertion);
NS_ADDREF(mSource);
u.hash.mPropertyHash = PL_NewDHashTable(PL_DHashGetStubOps(),
nsnull, sizeof(Entry), PL_DHASH_MIN_SIZE);
}
Assertion::Assertion(nsIRDFResource* aSource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget,
PRBool aTruthValue)
: mSource(aSource),
mNext(nsnull),
mRefCnt(0),
mHashEntry(PR_FALSE)
{
MOZ_COUNT_CTOR(RDF_Assertion);
u.as.mProperty = aProperty;
u.as.mTarget = aTarget;
NS_ADDREF(mSource);
NS_ADDREF(u.as.mProperty);
NS_ADDREF(u.as.mTarget);
u.as.mInvNext = nsnull;
u.as.mTruthValue = aTruthValue;
u.as.mMarked = PR_FALSE;
}
Assertion::~Assertion()
{
MOZ_COUNT_DTOR(RDF_Assertion);
#ifdef DEBUG_REFS
--gInstanceCount;
fprintf(stdout, "%d - RDF: Assertion\n", gInstanceCount);
#endif
NS_RELEASE(mSource);
if (!mHashEntry)
{
NS_RELEASE(u.as.mProperty);
NS_RELEASE(u.as.mTarget);
}
}
PLDHashOperator
Assertion::DeletePropertyHashEntry(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg)
{
Entry* entry = reinterpret_cast<Entry*>(aHdr);
nsFixedSizeAllocator* allocator = static_cast<nsFixedSizeAllocator*>(aArg);
Assertion* as = entry->mAssertions;
while (as) {
Assertion* doomed = as;
as = as->mNext;
// Unlink, and release the datasource's reference.
doomed->mNext = doomed->u.as.mInvNext = nsnull;
doomed->Release(*allocator);
}
return PL_DHASH_NEXT;
}
////////////////////////////////////////////////////////////////////////
// InMemoryDataSource
class InMemoryArcsEnumeratorImpl;
class InMemoryAssertionEnumeratorImpl;
class InMemoryResourceEnumeratorImpl;
class InMemoryDataSource : public nsIRDFDataSource,
public nsIRDFInMemoryDataSource,
public nsIRDFPropagatableDataSource,
public nsIRDFPurgeableDataSource,
public rdfIDataSource
{
protected:
nsFixedSizeAllocator mAllocator;
// These hash tables are keyed on pointers to nsIRDFResource
// objects (the nsIRDFService ensures that there is only ever one
// nsIRDFResource object per unique URI). The value of an entry is
// an Assertion struct, which is a linked list of (subject
// predicate object) triples.
PLDHashTable mForwardArcs;
PLDHashTable mReverseArcs;
nsCOMArray<nsIRDFObserver> mObservers;
PRUint32 mNumObservers;
// VisitFoo needs to block writes, [Un]Assert only allowed
// during mReadCount == 0
PRUint32 mReadCount;
static PLDHashOperator
DeleteForwardArcsEntry(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg);
static PLDHashOperator
ResourceEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg);
friend class InMemoryArcsEnumeratorImpl;
friend class InMemoryAssertionEnumeratorImpl;
friend class InMemoryResourceEnumeratorImpl; // b/c it needs to enumerate mForwardArcs
// Thread-safe writer implementation methods.
nsresult
LockedAssert(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv);
nsresult
LockedUnassert(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target);
InMemoryDataSource(nsISupports* aOuter);
virtual ~InMemoryDataSource();
nsresult Init();
friend nsresult
NS_NewRDFInMemoryDataSource(nsISupports* aOuter, const nsIID& aIID, void** aResult);
public:
NS_DECL_CYCLE_COLLECTING_AGGREGATED
NS_DECL_AGGREGATED_CYCLE_COLLECTION_CLASS(InMemoryDataSource)
// nsIRDFDataSource methods
NS_DECL_NSIRDFDATASOURCE
// nsIRDFInMemoryDataSource methods
NS_DECL_NSIRDFINMEMORYDATASOURCE
// nsIRDFPropagatableDataSource methods
NS_DECL_NSIRDFPROPAGATABLEDATASOURCE
// nsIRDFPurgeableDataSource methods
NS_DECL_NSIRDFPURGEABLEDATASOURCE
// rdfIDataSource methods
NS_DECL_RDFIDATASOURCE
protected:
static PLDHashOperator
SweepForwardArcsEntries(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg);
public:
// Implementation methods
Assertion*
GetForwardArcs(nsIRDFResource* u) {
PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mForwardArcs, u, PL_DHASH_LOOKUP);
return PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
: nsnull; }
Assertion*
GetReverseArcs(nsIRDFNode* v) {
PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mReverseArcs, v, PL_DHASH_LOOKUP);
return PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
: nsnull; }
void
SetForwardArcs(nsIRDFResource* u, Assertion* as) {
PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mForwardArcs, u,
as ? PL_DHASH_ADD : PL_DHASH_REMOVE);
if (as && hdr) {
Entry* entry = reinterpret_cast<Entry*>(hdr);
entry->mNode = u;
entry->mAssertions = as;
} }
void
SetReverseArcs(nsIRDFNode* v, Assertion* as) {
PLDHashEntryHdr* hdr = PL_DHashTableOperate(&mReverseArcs, v,
as ? PL_DHASH_ADD : PL_DHASH_REMOVE);
if (as && hdr) {
Entry* entry = reinterpret_cast<Entry*>(hdr);
entry->mNode = v;
entry->mAssertions = as;
} }
#ifdef PR_LOGGING
void
LogOperation(const char* aOperation,
nsIRDFResource* asource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget,
PRBool aTruthValue = PR_TRUE);
#endif
PRBool mPropagateChanges;
};
//----------------------------------------------------------------------
//
// InMemoryAssertionEnumeratorImpl
//
/**
* InMemoryAssertionEnumeratorImpl
*/
class InMemoryAssertionEnumeratorImpl : public nsISimpleEnumerator
{
private:
InMemoryDataSource* mDataSource;
nsIRDFResource* mSource;
nsIRDFResource* mProperty;
nsIRDFNode* mTarget;
nsIRDFNode* mValue;
PRInt32 mCount;
PRBool mTruthValue;
Assertion* mNextAssertion;
nsCOMPtr<nsISupportsArray> mHashArcs;
// Hide so that only Create() and Destroy() can be used to
// allocate and deallocate from the heap
static void* operator new(size_t) CPP_THROW_NEW { return 0; }
static void operator delete(void*, size_t) {}
InMemoryAssertionEnumeratorImpl(InMemoryDataSource* aDataSource,
nsIRDFResource* aSource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget,
PRBool aTruthValue);
virtual ~InMemoryAssertionEnumeratorImpl();
public:
static InMemoryAssertionEnumeratorImpl*
Create(InMemoryDataSource* aDataSource,
nsIRDFResource* aSource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget,
PRBool aTruthValue) {
void* place = aDataSource->mAllocator.Alloc(sizeof(InMemoryAssertionEnumeratorImpl));
return place
? ::new (place) InMemoryAssertionEnumeratorImpl(aDataSource,
aSource, aProperty, aTarget,
aTruthValue)
: nsnull; }
static void
Destroy(InMemoryAssertionEnumeratorImpl* aEnumerator) {
// Keep the datasource alive for the duration of the stack
// frame so its allocator stays valid.
nsCOMPtr<nsIRDFDataSource> kungFuDeathGrip = aEnumerator->mDataSource;
// Grab the pool from the datasource; since we keep the
// datasource alive, this has to be safe.
nsFixedSizeAllocator& pool = aEnumerator->mDataSource->mAllocator;
aEnumerator->~InMemoryAssertionEnumeratorImpl();
pool.Free(aEnumerator, sizeof(*aEnumerator)); }
// nsISupports interface
NS_DECL_ISUPPORTS
// nsISimpleEnumerator interface
NS_DECL_NSISIMPLEENUMERATOR
};
////////////////////////////////////////////////////////////////////////
InMemoryAssertionEnumeratorImpl::InMemoryAssertionEnumeratorImpl(
InMemoryDataSource* aDataSource,
nsIRDFResource* aSource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget,
PRBool aTruthValue)
: mDataSource(aDataSource),
mSource(aSource),
mProperty(aProperty),
mTarget(aTarget),
mValue(nsnull),
mCount(0),
mTruthValue(aTruthValue),
mNextAssertion(nsnull)
{
NS_ADDREF(mDataSource);
NS_IF_ADDREF(mSource);
NS_ADDREF(mProperty);
NS_IF_ADDREF(mTarget);
if (mSource) {
mNextAssertion = mDataSource->GetForwardArcs(mSource);
if (mNextAssertion && mNextAssertion->mHashEntry) {
// its our magical HASH_ENTRY forward hash for assertions
PLDHashEntryHdr* hdr = PL_DHashTableOperate(mNextAssertion->u.hash.mPropertyHash,
aProperty, PL_DHASH_LOOKUP);
mNextAssertion = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
: nsnull;
}
}
else {
mNextAssertion = mDataSource->GetReverseArcs(mTarget);
}
// Add an owning reference from the enumerator
if (mNextAssertion)
mNextAssertion->AddRef();
}
InMemoryAssertionEnumeratorImpl::~InMemoryAssertionEnumeratorImpl()
{
#ifdef DEBUG_REFS
--gInstanceCount;
fprintf(stdout, "%d - RDF: InMemoryAssertionEnumeratorImpl\n", gInstanceCount);
#endif
if (mNextAssertion)
mNextAssertion->Release(mDataSource->mAllocator);
NS_IF_RELEASE(mDataSource);
NS_IF_RELEASE(mSource);
NS_IF_RELEASE(mProperty);
NS_IF_RELEASE(mTarget);
NS_IF_RELEASE(mValue);
}
NS_IMPL_ADDREF(InMemoryAssertionEnumeratorImpl)
NS_IMPL_RELEASE_WITH_DESTROY(InMemoryAssertionEnumeratorImpl, Destroy(this))
NS_IMPL_QUERY_INTERFACE1(InMemoryAssertionEnumeratorImpl, nsISimpleEnumerator)
NS_IMETHODIMP
InMemoryAssertionEnumeratorImpl::HasMoreElements(PRBool* aResult)
{
if (mValue) {
*aResult = PR_TRUE;
return NS_OK;
}
while (mNextAssertion) {
PRBool foundIt = PR_FALSE;
if ((mProperty == mNextAssertion->u.as.mProperty) &&
(mTruthValue == mNextAssertion->u.as.mTruthValue)) {
if (mSource) {
mValue = mNextAssertion->u.as.mTarget;
NS_ADDREF(mValue);
}
else {
mValue = mNextAssertion->mSource;
NS_ADDREF(mValue);
}
foundIt = PR_TRUE;
}
// Remember the last assertion we were holding on to
Assertion* as = mNextAssertion;
// iterate
mNextAssertion = (mSource) ? mNextAssertion->mNext : mNextAssertion->u.as.mInvNext;
// grab an owning reference from the enumerator to the next assertion
if (mNextAssertion)
mNextAssertion->AddRef();
// ...and release the reference from the enumerator to the old one.
as->Release(mDataSource->mAllocator);
if (foundIt) {
*aResult = PR_TRUE;
return NS_OK;
}
}
*aResult = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
InMemoryAssertionEnumeratorImpl::GetNext(nsISupports** aResult)
{
nsresult rv;
PRBool hasMore;
rv = HasMoreElements(&hasMore);
if (NS_FAILED(rv)) return rv;
if (! hasMore)
return NS_ERROR_UNEXPECTED;
// Don't AddRef: we "transfer" ownership to the caller
*aResult = mValue;
mValue = nsnull;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
//
/**
* This class is a little bit bizarre in that it implements both the
* <tt>nsIRDFArcsOutCursor</tt> and <tt>nsIRDFArcsInCursor</tt> interfaces.
* Because the structure of the in-memory graph is pretty flexible, it's
* fairly easy to parameterize this class. The only funky thing to watch
* out for is the multiple inheritance clashes.
*/
class InMemoryArcsEnumeratorImpl : public nsISimpleEnumerator
{
private:
// Hide so that only Create() and Destroy() can be used to
// allocate and deallocate from the heap
static void* operator new(size_t) CPP_THROW_NEW { return 0; }
static void operator delete(void*, size_t) {}
InMemoryDataSource* mDataSource;
nsIRDFResource* mSource;
nsIRDFNode* mTarget;
nsAutoTArray<nsCOMPtr<nsIRDFResource>, 8> mAlreadyReturned;
nsIRDFResource* mCurrent;
Assertion* mAssertion;
nsCOMPtr<nsISupportsArray> mHashArcs;
InMemoryArcsEnumeratorImpl(InMemoryDataSource* aDataSource,
nsIRDFResource* aSource,
nsIRDFNode* aTarget);
virtual ~InMemoryArcsEnumeratorImpl();
static PLDHashOperator
ArcEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg);
public:
// nsISupports interface
NS_DECL_ISUPPORTS
// nsISimpleEnumerator interface
NS_DECL_NSISIMPLEENUMERATOR
static InMemoryArcsEnumeratorImpl*
Create(InMemoryDataSource* aDataSource,
nsIRDFResource* aSource,
nsIRDFNode* aTarget) {
void* place = aDataSource->mAllocator.Alloc(sizeof(InMemoryArcsEnumeratorImpl));
return place
? ::new (place) InMemoryArcsEnumeratorImpl(aDataSource, aSource, aTarget)
: nsnull; }
static void
Destroy(InMemoryArcsEnumeratorImpl* aEnumerator) {
// Keep the datasource alive for the duration of the stack
// frame so its allocator stays valid.
nsCOMPtr<nsIRDFDataSource> kungFuDeathGrip = aEnumerator->mDataSource;
// Grab the pool from the datasource; since we keep the
// datasource alive, this has to be safe.
nsFixedSizeAllocator& pool = aEnumerator->mDataSource->mAllocator;
aEnumerator->~InMemoryArcsEnumeratorImpl();
pool.Free(aEnumerator, sizeof(*aEnumerator)); }
};
PLDHashOperator
InMemoryArcsEnumeratorImpl::ArcEnumerator(PLDHashTable* aTable,
PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg)
{
Entry* entry = reinterpret_cast<Entry*>(aHdr);
nsISupportsArray* resources = static_cast<nsISupportsArray*>(aArg);
resources->AppendElement(entry->mNode);
return PL_DHASH_NEXT;
}
InMemoryArcsEnumeratorImpl::InMemoryArcsEnumeratorImpl(InMemoryDataSource* aDataSource,
nsIRDFResource* aSource,
nsIRDFNode* aTarget)
: mDataSource(aDataSource),
mSource(aSource),
mTarget(aTarget),
mCurrent(nsnull)
{
NS_ADDREF(mDataSource);
NS_IF_ADDREF(mSource);
NS_IF_ADDREF(mTarget);
if (mSource) {
// cast okay because it's a closed system
mAssertion = mDataSource->GetForwardArcs(mSource);
if (mAssertion && mAssertion->mHashEntry) {
// its our magical HASH_ENTRY forward hash for assertions
nsresult rv = NS_NewISupportsArray(getter_AddRefs(mHashArcs));
if (NS_SUCCEEDED(rv)) {
PL_DHashTableEnumerate(mAssertion->u.hash.mPropertyHash,
ArcEnumerator, mHashArcs.get());
}
mAssertion = nsnull;
}
}
else {
mAssertion = mDataSource->GetReverseArcs(mTarget);
}
}
InMemoryArcsEnumeratorImpl::~InMemoryArcsEnumeratorImpl()
{
#ifdef DEBUG_REFS
--gInstanceCount;
fprintf(stdout, "%d - RDF: InMemoryArcsEnumeratorImpl\n", gInstanceCount);
#endif
NS_RELEASE(mDataSource);
NS_IF_RELEASE(mSource);
NS_IF_RELEASE(mTarget);
NS_IF_RELEASE(mCurrent);
}
NS_IMPL_ADDREF(InMemoryArcsEnumeratorImpl)
NS_IMPL_RELEASE_WITH_DESTROY(InMemoryArcsEnumeratorImpl, Destroy(this))
NS_IMPL_QUERY_INTERFACE1(InMemoryArcsEnumeratorImpl, nsISimpleEnumerator)
NS_IMETHODIMP
InMemoryArcsEnumeratorImpl::HasMoreElements(PRBool* aResult)
{
NS_PRECONDITION(aResult != nsnull, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
if (mCurrent) {
*aResult = PR_TRUE;
return NS_OK;
}
if (mHashArcs) {
PRUint32 itemCount;
nsresult rv;
if (NS_FAILED(rv = mHashArcs->Count(&itemCount))) return(rv);
if (itemCount > 0) {
--itemCount;
mCurrent = static_cast<nsIRDFResource *>
(mHashArcs->ElementAt(itemCount));
mHashArcs->RemoveElementAt(itemCount);
*aResult = PR_TRUE;
return NS_OK;
}
}
else
while (mAssertion) {
nsIRDFResource* next = mAssertion->u.as.mProperty;
// "next" is the property arc we are tentatively going to return
// in a subsequent GetNext() call. It is important to do two
// things, however, before that can happen:
// 1) Make sure it's not an arc we've already returned.
// 2) Make sure that |mAssertion| is not left pointing to
// another assertion that has the same property as this one.
// The first is a practical concern; the second a defense against
// an obscure crash and other erratic behavior. To ensure the
// second condition, skip down the chain until we find the next
// assertion with a property that doesn't match the current one.
// (All these assertions would be skipped via mAlreadyReturned
// checks anyways; this is even a bit faster.)
do {
mAssertion = (mSource ? mAssertion->mNext :
mAssertion->u.as.mInvNext);
}
while (mAssertion && (next == mAssertion->u.as.mProperty));
PRBool alreadyReturned = PR_FALSE;
for (PRInt32 i = mAlreadyReturned.Length() - 1; i >= 0; --i) {
if (mAlreadyReturned[i] == next) {
alreadyReturned = PR_TRUE;
break;
}
}
if (! alreadyReturned) {
mCurrent = next;
NS_ADDREF(mCurrent);
*aResult = PR_TRUE;
return NS_OK;
}
}
*aResult = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
InMemoryArcsEnumeratorImpl::GetNext(nsISupports** aResult)
{
nsresult rv;
PRBool hasMore;
rv = HasMoreElements(&hasMore);
if (NS_FAILED(rv)) return rv;
if (! hasMore)
return NS_ERROR_UNEXPECTED;
// Add this to the set of things we've already returned so that we
// can ensure uniqueness
mAlreadyReturned.AppendElement(mCurrent);
// Don't AddRef: we "transfer" ownership to the caller
*aResult = mCurrent;
mCurrent = nsnull;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
// InMemoryDataSource
nsresult
NS_NewRDFInMemoryDataSource(nsISupports* aOuter, const nsIID& aIID, void** aResult)
{
NS_PRECONDITION(aResult != nsnull, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
*aResult = nsnull;
if (aOuter && !aIID.Equals(NS_GET_IID(nsISupports))) {
NS_ERROR("aggregation requires nsISupports");
return NS_ERROR_ILLEGAL_VALUE;
}
InMemoryDataSource* datasource = new InMemoryDataSource(aOuter);
if (! datasource)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(datasource);
nsresult rv = datasource->Init();
if (NS_SUCCEEDED(rv)) {
datasource->fAggregated.AddRef();
rv = datasource->AggregatedQueryInterface(aIID, aResult); // This'll AddRef()
datasource->fAggregated.Release();
}
NS_RELEASE(datasource);
return rv;
}
InMemoryDataSource::InMemoryDataSource(nsISupports* aOuter)
: mNumObservers(0), mReadCount(0)
{
NS_INIT_AGGREGATED(aOuter);
static const size_t kBucketSizes[] = {
sizeof(Assertion),
sizeof(Entry),
sizeof(InMemoryArcsEnumeratorImpl),
sizeof(InMemoryAssertionEnumeratorImpl) };
static const PRInt32 kNumBuckets = sizeof(kBucketSizes) / sizeof(size_t);
// Per news://news.mozilla.org/39BEC105.5090206%40netscape.com
static const PRInt32 kInitialSize = 1024;
mAllocator.Init("nsInMemoryDataSource", kBucketSizes, kNumBuckets, kInitialSize);
mForwardArcs.ops = nsnull;
mReverseArcs.ops = nsnull;
mPropagateChanges = PR_TRUE;
}
nsresult
InMemoryDataSource::Init()
{
if (!PL_DHashTableInit(&mForwardArcs,
PL_DHashGetStubOps(),
nsnull,
sizeof(Entry),
PL_DHASH_MIN_SIZE)) {
mForwardArcs.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
if (!PL_DHashTableInit(&mReverseArcs,
PL_DHashGetStubOps(),
nsnull,
sizeof(Entry),
PL_DHASH_MIN_SIZE)) {
mReverseArcs.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
#ifdef PR_LOGGING
if (! gLog)
gLog = PR_NewLogModule("InMemoryDataSource");
#endif
return NS_OK;
}
InMemoryDataSource::~InMemoryDataSource()
{
#ifdef DEBUG_REFS
--gInstanceCount;
fprintf(stdout, "%d - RDF: InMemoryDataSource\n", gInstanceCount);
#endif
if (mForwardArcs.ops) {
// This'll release all of the Assertion objects that are
// associated with this data source. We only need to do this
// for the forward arcs, because the reverse arcs table
// indexes the exact same set of resources.
PL_DHashTableEnumerate(&mForwardArcs, DeleteForwardArcsEntry, &mAllocator);
PL_DHashTableFinish(&mForwardArcs);
}
if (mReverseArcs.ops)
PL_DHashTableFinish(&mReverseArcs);
PR_LOG(gLog, PR_LOG_NOTICE,
("InMemoryDataSource(%p): destroyed.", this));
}
PLDHashOperator
InMemoryDataSource::DeleteForwardArcsEntry(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PRUint32 aNumber, void* aArg)
{
Entry* entry = reinterpret_cast<Entry*>(aHdr);
nsFixedSizeAllocator* allocator = static_cast<nsFixedSizeAllocator*>(aArg);
Assertion* as = entry->mAssertions;
while (as) {
Assertion* doomed = as;
as = as->mNext;
// Unlink, and release the datasource's reference.
doomed->mNext = doomed->u.as.mInvNext = nsnull;
doomed->Release(*allocator);
}
return PL_DHASH_NEXT;
}
////////////////////////////////////////////////////////////////////////
NS_IMPL_CYCLE_COLLECTION_CLASS(InMemoryDataSource)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(InMemoryDataSource)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mObservers)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_AGGREGATED(InMemoryDataSource)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mObservers)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTING_AGGREGATED(InMemoryDataSource)
NS_INTERFACE_MAP_BEGIN_AGGREGATED(InMemoryDataSource)
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION_AGGREGATED(InMemoryDataSource)
NS_INTERFACE_MAP_ENTRY(nsIRDFDataSource)
NS_INTERFACE_MAP_ENTRY(nsIRDFInMemoryDataSource)
NS_INTERFACE_MAP_ENTRY(nsIRDFPropagatableDataSource)
NS_INTERFACE_MAP_ENTRY(nsIRDFPurgeableDataSource)
NS_INTERFACE_MAP_ENTRY(rdfIDataSource)
NS_INTERFACE_MAP_END