Index: resourcedata.cpp
===================================================================
--- resourcedata.cpp	(revision 1126675)
+++ resourcedata.cpp	(working copy)
@@ -60,7 +60,6 @@ Nepomuk::ResourceData::ResourceData( con
       m_kickoffUri( uri ),
       m_mainType( type ),
       m_modificationMutex(QMutex::Recursive),
-      m_proxyData(0),
       m_cacheDirty(true),
       m_pimoThing(0),
       m_groundingOccurence(0),
@@ -103,16 +102,12 @@ bool Nepomuk::ResourceData::isFile()
 
 QUrl Nepomuk::ResourceData::uri() const
 {
-    if( m_proxyData )
-        return m_proxyData->uri();
     return m_uri;
 }
 
 
 QUrl Nepomuk::ResourceData::type()
 {
-    if( m_proxyData )
-        return m_proxyData->type();
     load();
     return m_mainType;
 }
@@ -120,8 +115,6 @@ QUrl Nepomuk::ResourceData::type()
 
 QList<QUrl> Nepomuk::ResourceData::allTypes()
 {
-    if( m_proxyData )
-        return m_proxyData->allTypes();
     load();
     return m_types;
 }
@@ -129,10 +122,7 @@ QList<QUrl> Nepomuk::ResourceData::allTy
 
 void Nepomuk::ResourceData::setTypes( const QList<QUrl>& types )
 {
-    if( m_proxyData ) {
-        m_proxyData->setTypes( types );
-    }
-    else if ( store() ) {
+    if ( store() ) {
         QMutexLocker lock(&m_modificationMutex);
 
         // reset types
@@ -155,19 +145,9 @@ void Nepomuk::ResourceData::setTypes( co
 
 void Nepomuk::ResourceData::resetAll( bool isDelete )
 {
-    // reset proxy
-    bool hadProxy = false;
-    if( m_proxyData ) {
-        hadProxy = true;
-        if( !m_proxyData->deref() &&
-            rm()->dataCacheFull() )
-            delete m_proxyData;
-        m_proxyData = 0;
-    }
-
     // remove us from all caches (store() will re-insert us later if necessary)
     m_rm->mutex.lock();
-    if( !m_uri.isEmpty() && !hadProxy ) // if we had a proxy we were not in m_initializedData ourselves
+    if( !m_uri.isEmpty() )
         m_rm->m_initializedData.remove( m_uri );
     if( !m_kickoffUri.isEmpty() )
         m_rm->m_uriKickoffData.remove( m_kickoffUri );
@@ -198,20 +178,13 @@ void Nepomuk::ResourceData::resetAll( bo
 
 QHash<QUrl, Nepomuk::Variant> Nepomuk::ResourceData::allProperties()
 {
-    if( m_proxyData )
-        return m_proxyData->allProperties();
-
     load();
-
     return m_cache;
 }
 
 
 bool Nepomuk::ResourceData::hasProperty( const QUrl& uri )
 {
-    if( m_proxyData )
-        return m_proxyData->hasProperty( uri );
-
     if( !load() )
         return false;
 
@@ -225,9 +198,6 @@ bool Nepomuk::ResourceData::hasProperty(
 
 bool Nepomuk::ResourceData::hasProperty( const QUrl& p, const Variant& v )
 {
-    if( m_proxyData )
-        return m_proxyData->hasProperty( p, v );
-
     if( !load() )
         return false;
 
@@ -247,9 +217,6 @@ bool Nepomuk::ResourceData::hasProperty(
 
 bool Nepomuk::ResourceData::hasType( const QUrl& uri )
 {
-    if( m_proxyData )
-        return m_proxyData->hasType( uri );
-
     load();
     return constHasType( uri );
 }
@@ -275,9 +242,6 @@ bool Nepomuk::ResourceData::constHasType
 
 Nepomuk::Variant Nepomuk::ResourceData::property( const QUrl& uri )
 {
-    if( m_proxyData )
-        return m_proxyData->property( uri );
-
     if ( load() ) {
         // we need to protect the reading, too. load my be triggered from another thread's
         // connection to a Soprano statement signal
@@ -477,9 +441,6 @@ void Nepomuk::ResourceData::setProperty(
 {
     Q_ASSERT( uri.isValid() );
 
-    if( m_proxyData )
-        return m_proxyData->setProperty( uri, value );
-
     // step 0: make sure this resource is in the store
     if ( store() ) {
         QMutexLocker lock(&m_modificationMutex);
@@ -532,9 +493,6 @@ void Nepomuk::ResourceData::removeProper
 {
     Q_ASSERT( uri.isValid() );
 
-    if( m_proxyData )
-        return m_proxyData->removeProperty( uri );
-
     QMutexLocker lock(&m_modificationMutex);
 
     if ( determineUri() ) {
@@ -558,20 +516,14 @@ void Nepomuk::ResourceData::remove( bool
     // That way the proxy handling will take care of the rest.
     m_rm->determineAllUris();
 
-    if( m_proxyData ) {
-        m_proxyData->remove( recursive );
-    }
-    else {
         QMutexLocker lock(&m_modificationMutex);
 
-
         if ( determineUri() ) {
             MAINMODEL->removeAllStatements( Statement( m_uri, Node(), Node() ) );
             if ( recursive ) {
                 MAINMODEL->removeAllStatements( Statement( Node(), Node(), m_uri ) );
             }
         }
-    }
 
     resetAll();
 }
@@ -579,9 +531,6 @@ void Nepomuk::ResourceData::remove( bool
 
 bool Nepomuk::ResourceData::exists()
 {
-    if( m_proxyData )
-        return m_proxyData->exists();
-
     if( determineUri() ) {
         return MAINMODEL->containsAnyStatement( Statement( m_uri, Node(), Node() ) );
     }
@@ -592,19 +541,12 @@ bool Nepomuk::ResourceData::exists()
 
 bool Nepomuk::ResourceData::isValid() const
 {
-    if( m_proxyData )
-        return m_proxyData->isValid();
-
     return( !m_mainType.isEmpty() && ( !m_uri.isEmpty() || !m_kickoffUri.isEmpty() || !m_kickoffId.isEmpty() ) );
 }
 
 
 bool Nepomuk::ResourceData::determineUri()
 {
-    if( m_proxyData )
-        return m_proxyData->determineUri();
-
-    else {
         //
         // Preconditions:
         // 1. m_kickoffId is not a local file path or URL (use m_kickoffUri for that)
@@ -744,14 +686,33 @@ bool Nepomuk::ResourceData::determineUri
                     m_rm->m_initializedData.insert( m_uri, this );
                 }
                 else {
-                    m_proxyData = it.value();
-                    m_proxyData->ref();
+                // As the ResourceData already exists in m_initializedData, we simply
+                // make all the the Resources which contain "this" ResourceData, point
+                // to the initialized ResourceData instead.
+                replaceWith( it.value() );
                 }
             }
         }
 
         return !m_uri.isEmpty();
+}
+
+
+void Nepomuk::ResourceData::replaceWith(Nepomuk::ResourceData* rd)
+{
+    foreach( Resource * res, m_resources) {
+        
+        ResourceData * oldData = res->m_data;
+        if ( oldData && !oldData->deref( res ) && oldData->shouldBeDeleted() )
+            delete oldData;
+        
+        res->m_data = rd;
+        
+        // vHanda : Can reference counting be eliminated totally? 
+        // It could be replaced with m_resources.size() 
+        rd->ref( res );
     }
+    delete this;
 }
 
 
@@ -796,17 +757,43 @@ Nepomuk::Thing Nepomuk::ResourceData::pi
 
 bool Nepomuk::ResourceData::operator==( const ResourceData& other ) const
 {
-    const ResourceData* that = this;
-    if( m_proxyData )
-        that = m_proxyData;
-
-    if( that == &other )
+    if( this == &other )
         return true;
 
-    return( that->m_uri == other.m_uri &&
-            that->m_mainType == other.m_mainType &&
-            that->m_kickoffUri == other.m_kickoffUri &&
-            that->m_kickoffId == other.m_kickoffId );
+    return( m_uri == other.m_uri &&
+            m_mainType == other.m_mainType &&
+            m_kickoffUri == other.m_kickoffUri &&
+            m_kickoffId == other.m_kickoffId );
+}
+
+
+bool Nepomuk::ResourceData::ref(Nepomuk::Resource* res)
+{
+    m_resources.push_back( res );
+    return m_ref.ref();
+}
+
+
+bool Nepomuk::ResourceData::deref(Nepomuk::Resource* res)
+{
+    m_resources.removeAll( res );
+    return m_ref.deref();
+}
+
+
+bool Nepomuk::ResourceData::shouldBeDeleted() const
+{
+    if ( cnt() )
+        return false;
+
+    //
+    // We delete data objects in one of two cases:
+    // 1. They are not valid and as such not in one of the ResourceManagerPrivate kickoff lists
+    // 2. The cache is already full and we need to clean up
+    //
+    if ( !isValid() || rm()->dataCacheFull() ) {
+        return true;
+    }
 }
 
 
@@ -818,8 +805,7 @@ QDebug Nepomuk::ResourceData::operator<<
              m_uri.url(),
              m_mainType.toString())
         .arg(m_ref);
-    if(m_proxyData)
-        dbg << QLatin1String(", proxy: " ) << *m_proxyData;
+    
     return dbg << QLatin1String("]");
 }
 
Index: resourcedata.h
===================================================================
--- resourcedata.h	(revision 1126675)
+++ resourcedata.h	(working copy)
@@ -37,6 +37,7 @@
 
 namespace Nepomuk {
 
+    class Resource;
     class ResourceManagerPrivate;
 
     class ResourceData
@@ -45,13 +46,8 @@ namespace Nepomuk {
         explicit ResourceData( const QUrl& uri, const QString& kickoffId_, const QUrl& type_, ResourceManagerPrivate* rm );
         ~ResourceData();
 
-        inline bool ref() {
-            return m_ref.ref();
-        }
-
-        inline bool deref() {
-            return m_ref.deref();
-        }
+        bool ref(Resource * res);
+        bool deref(Resource * res);
 
         inline int cnt() const {
             return m_ref;
@@ -155,7 +151,7 @@ namespace Nepomuk {
 
         ResourceManagerPrivate* rm() const { return m_rm; }
 
-        ResourceData* proxy() const { return m_proxyData; }
+        bool shouldBeDeleted() const;
 
     private:
         void loadType( const QUrl& type );
@@ -187,13 +183,14 @@ namespace Nepomuk {
         QMutex m_determineUriMutex;
         mutable QMutex m_modificationMutex;
 
+        /// Contains a list of resources which contain this ResourceData
+        QList<Resource *> m_resources;
+        
         /**
-         * Used to virtually merge two data objects representing the same
-         * resource. This happens if the resource was once created using its
-         * actual URI and once via its ID. To prevent early loading we allow
-         * this scenario.
+         * Makes the m_data pointer of all the Resources in m_resources point to
+         * rd, instead of 'this'. Handles refernce counting as well.
          */
-        ResourceData* m_proxyData;
+        void replaceWith( ResourceData * rd );
 
         QHash<QUrl, Variant> m_cache;
         bool m_cacheDirty;
Index: resource.cpp
===================================================================
--- resource.cpp	(revision 1126675)
+++ resource.cpp	(working copy)
@@ -47,14 +47,16 @@
 Nepomuk::Resource::Resource()
 {
     m_data = ResourceManager::instance()->d->data( QUrl(), QUrl() );
-    m_data->ref();
+    if ( m_data )
+        m_data->ref( this );
 }
 
 
 Nepomuk::Resource::Resource( ResourceManager* manager )
 {
     m_data = manager->d->data( QUrl(), QUrl() );
-    m_data->ref();
+    if ( m_data )
+        m_data->ref( this );
 }
 
 
@@ -62,7 +64,7 @@ Nepomuk::Resource::Resource( const Nepom
 {
     m_data = res.m_data;
     if ( m_data )
-        m_data->ref();
+        m_data->ref( this );
 }
 
 
@@ -70,7 +72,7 @@ Nepomuk::Resource::Resource( const QStri
 {
     m_data = ResourceManager::instance()->d->data( uri, type );
     if ( m_data )
-        m_data->ref();
+        m_data->ref( this );
 }
 
 
@@ -78,7 +80,7 @@ Nepomuk::Resource::Resource( const QStri
 {
     m_data = manager->d->data( uri, type );
     if ( m_data )
-        m_data->ref();
+        m_data->ref( this );
 }
 
 
@@ -86,7 +88,7 @@ Nepomuk::Resource::Resource( const QStri
 {
     m_data = ResourceManager::instance()->d->data( uri, type );
     if ( m_data )
-        m_data->ref();
+        m_data->ref( this );
 }
 
 
@@ -94,7 +96,7 @@ Nepomuk::Resource::Resource( const QUrl&
 {
     m_data = ResourceManager::instance()->d->data( uri, type );
     if ( m_data )
-        m_data->ref();
+        m_data->ref( this );
 }
 
 
@@ -102,7 +104,7 @@ Nepomuk::Resource::Resource( const QUrl&
 {
     m_data = manager->d->data( uri, type );
     if ( m_data )
-        m_data->ref();
+        m_data->ref( this );
 }
 
 
@@ -110,39 +112,29 @@ Nepomuk::Resource::Resource( Nepomuk::Re
 {
     m_data = data;
     if ( m_data )
-        data->ref();
+        m_data->ref( this );
 }
 
 
 Nepomuk::Resource::~Resource()
 {
     if ( m_data ) {
-        if ( !m_data->deref() ) {
-            //
-            // We delete data objects in one of three cases:
-            // 1. They are not valid and as such not in one of the ResourceManagerPrivate kickoff lists
-            // 2. They have a proxy which is the actual thing to reuse later on
-            // 3. The cache is already full and we need to clean up
-            //
-            if ( !m_data->isValid() ||
-                 m_data->proxy() ||
-                 m_data->rm()->dataCacheFull() ) {
+        m_data->deref( this );
+        if ( m_data->shouldBeDeleted() )
                 delete m_data;
             }
-        }
-    }
 }
 
 
 Nepomuk::Resource& Nepomuk::Resource::operator=( const Resource& res )
 {
     if( m_data != res.m_data ) {
-        if ( m_data && !m_data->deref() && !m_data->isValid() ) {
+        if ( m_data && !m_data->deref( this ) && m_data->shouldBeDeleted() ) {
             delete m_data;
         }
         m_data = res.m_data;
         if ( m_data )
-            m_data->ref();
+            m_data->ref( this );
     }
 
     return *this;