3
dL                 @   s   d dl mZ d dl mZ d dl mZ d dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZ dd	gZd ZZG d
d deZG dd	 d	eZejejdfddZ[dS )    )absolute_import)division)print_functionN)BNode)StoreNO_STOREVALID_STORE)	iteritemsMemoryIOMemoryc                   sr   e Zd ZdZd fdd	ZdddZddd	Zdd
dZdddZdd Z	dd Z
dd Zdd Zdd Z  ZS )r
   a      An in memory implementation of a triple store.

    This triple store uses nested dictionaries to store triples. Each
    triple is stored in two such indices as follows spo[s][p][o] = 1 and
    pos[p][o][s] = 1.

    Authors: Michel Pelletier, Daniel Krech, Stefan Niederhauser
    Nc                s8   t t| j| || _i | _i | _i | _i | _i | _d S )N)	superr
   __init__
identifier_Memory__spo_Memory__pos_Memory__osp_Memory__namespace_Memory__prefix)selfconfigurationr   )	__class__ 7/tmp/pip-build-7vycvbft/rdflib/rdflib/plugins/memory.pyr      s    zMemory.__init__Fc          '   C   s  |\}}}| j }y|| }W n   i  }||< Y nX y|| }	W n   i  }	||< Y nX d|	|< | j}
y|
| }W n   i  }|
|< Y nX y|| }W n   i  }||< Y nX d||< | j}y|| }W n   i  }||< Y nX y|| }W n   i  }||< Y nX d||< dS )z6        Add a triple to the store of triples.
           N)r   r   r   )r   triplecontextquotedsubject	predicateobjectspoZpooposossospsppr   r   r   add+   s>    
z
Memory.addc             C   sR   xL| j |D ]>\\}}}}| j| | |= | j| | |= | j| | |= qW d S )N)triplesr   r   r   )r   triple_patternr   r   r   r   cr   r   r   removeT   s    zMemory.removec             c   st  |\}}}|t kr| j}||kr|| }|t kr||kr|t krf||| kr|||f| j fV  qqx*|| j D ]}|||f| j fV  qtW qqxh|j D ]Z}	|t kr|||	 kr||	|f| j fV  qqx(||	 j D ]}||	|f| j fV  qW qW n nn|t kr| j}
||
krp|
| }|t krh||krx,|| j D ]}|||f| j fV  qFW n n@x>|j D ]2}x*|| j D ]}|||f| j fV  qW qrW n|t kr| j}||krp|| }x|j D ]2}x*|| j D ]}	||	|f| j fV  qW qW nb| j}xZ|j D ]N}|| }x>|j D ]2}	x*||	 j D ]}||	|f| j fV  qFW q4W qW dS )z*A generator over all the triples matching N)ANYr   _Memory__contextskeysr   r   )r   r*   r   r   r   r   r    ZsubjectDictionaryr!   r'   r"   ZpredicateDictionaryr$   r%   ZobjectDictionaryr   r   r   r)   Z   sb    





"

"zMemory.triplesc             C   s$   d}x| j dD ]}|d7 }qW |S )Nr   r   )NNN)r)   )r   r   ir   r   r   r   __len__   s    zMemory.__len__c             C   s   || j |< || j|< d S )N)r   r   )r   prefix	namespacer   r   r   bind   s    
zMemory.bindc             C   s   | j j|d S )N)r   get)r   r2   r   r   r   r3      s    zMemory.namespacec             C   s   | j j|d S )N)r   r5   )r   r3   r   r   r   r2      s    zMemory.prefixc             c   s&   x t | jD ]\}}||fV  qW d S )N)r	   r   )r   r2   r3   r   r   r   
namespaces   s    zMemory.namespacesc             C   s   dd g D S )Nc             s   s   | ]
}|V  qd S )Nr   ).0r+   r   r   r   	<genexpr>   s    z$Memory.__contexts.<locals>.<genexpr>r   )r   r   r   r   
__contexts   s    zMemory.__contexts)NN)F)N)N)N)__name__
__module____qualname____doc__r   r(   r,   r)   r1   r4   r3   r2   r6   r.   __classcell__r   r   )r   r   r
      s   	
)

>
c                   s   e Zd ZdZdZdZdZd1 fdd	Zdd Zdd	 Z	d
d Z
dd Zd2ddZd3ddZd4ddZd5ddZd6ddZdd Zdd Zdd Zd7dd Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Z  ZS )8r   ah      An integer-key-optimized context-aware in-memory store.

    Uses three dict indices (for subjects, objects and predicates) holding
    sets of triples. Context information is tracked in a separate dict, with
    the triple as key and a dict of {context: quoted} items as value. The
    context information is used to filter triple query results.

    Memory usage is low due to several optimizations. RDF nodes are not
    stored directly in the indices; instead, the indices hold integer keys
    and the actual nodes are only stored once in int-to-object and
    object-to-int mapping dictionaries. A default context is determined
    based on the first triple that is added to the store, and no context
    information is actually stored for subsequent other triples with the
    same context information.

    Most operations should be quite fast, but a triples() query with two
    bound parts requires a set intersection operation, which may be slow in
    some cases. When multiple contexts are used in the same store, filtering
    based on context has to be done after each query, which may also be
    slow.

    TNc                sd   t t| j  i | _i | _d d i| _d d i| _i | _i | _i | _	i | _
d t i| _t | _d | _d S )N)r   r   r   _IOMemory__namespace_IOMemory__prefix_IOMemory__int2obj_IOMemory__obj2int_IOMemory__subjectIndex_IOMemory__predicateIndex_IOMemory__objectIndex_IOMemory__tripleContextsset_IOMemory__contextTriples_IOMemory__all_contexts_IOMemory__defaultContexts)r   r   r   )r   r   r   r      s    

zIOMemory.__init__c             C   s   || j |< || j|< d S )N)r@   r?   )r   r2   r3   r   r   r   r4      s    
zIOMemory.bindc             C   s   | j j|d S )N)r?   r5   )r   r2   r   r   r   r3      s    zIOMemory.namespacec             C   s   | j j|d S )N)r@   r5   )r   r3   r   r   r   r2      s    zIOMemory.prefixc             c   s&   x t | jD ]\}}||fV  qW d S )N)r	   r?   )r   r2   r3   r   r   r   r6      s    zIOMemory.namespacesFc             C   s   t j| ||| |d k	r$| jj| | j|}|\}}}| j||| || jkrb| j| j| nt|g| j|< || jkr| j| j| nt|g| j|< || jkr| j| j| nt|g| j|< d S )N)	r   r(   rI   _IOMemory__encodeTriple_IOMemory__addTripleContextrC   rG   rD   rE   )r   r   r   r   	enctriplesidpidoidr   r   r   r(      s    




zIOMemory.addc             C   sL  | j |}x| j||D ]\}}| j|}x0| j|D ]"}|d k	rL||krLq6| j|| q6W | j|dd}d |kr|d kst|dkr| j|d  t| j|dkr|\}	}
}| j|	 j| | j|
 j| | j	| j| | j
|= qW |d k	r|| jkrt| j| dkr| j|= |dkrH|| jkrH| j rH| jj| d S )NT)
skipQuotedr   r   )NNN)_IOMemory__obj2idr)   rK   _IOMemory__getTripleContexts_IOMemory__removeTripleContextlenrC   r,   rD   rE   rF   rH   rI   graph_aware)r   Z	triplepatr   Zreq_cidr   contextsrM   cidctxsrN   rO   rP   r   r   r   r,     s0    





zIOMemory.removec                s  |d k	r|krd }j | j\}}}|d krT|d krT|d krTj S |d k	r|d k	r|d k	r|jkrj| krj rfdddD S j S g }|d k	r|jkr|jj|  nj S |d k	r|jkr|jj|  nj S |d k	r>|jkr6|jj|  nj S t	|dkrd|d j
|dd   }n|d j } fdd|D S )Nc             3   s   | ]}j  fV  qd S )N)_IOMemory__contexts)r7   r0   )rM   r   tripleinr   r   r8   J  s    z#IOMemory.triples.<locals>.<genexpr>r   r   c             3   s.   | ]&}j | rj|j|fV  qd S )N)_IOMemory__tripleHasContext_IOMemory__decodeTriplerZ   )r7   rM   )rX   r   r   r   r8   f  s   )r   )rR   rK   _IOMemory__all_triplesrC   r\   _IOMemory__emptygenappendrD   rE   rU   intersectioncopy)r   r[   r   rN   rO   rP   ZsetsZ
enctriplesr   )rX   rM   r   r[   r   r)   8  s@    







zIOMemory.triplesc             C   sb   |d ks|dkr dd | j D S | j|}|\}}}|| jkrV|| j| krV| j|S | j S d S )Nc             s   s   | ]
}|V  qd S )Nr   )r7   r   r   r   r   r8   l  s    z$IOMemory.contexts.<locals>.<genexpr>)NNN)rI   rK   rC   rZ   r_   )r   r   rM   rN   rO   rP   r   r   r   rW   j  s    


zIOMemory.contextsc             C   s&   | j |}|| jkrdS t| j| S )Nr   )rR   rH   rU   )r   r   rX   r   r   r   r1   u  s    

zIOMemory.__len__c             C   s$   | j stj| | n| jj| d S )N)rV   r   	add_graphrI   r(   )r   graphr   r   r   rc   {  s    zIOMemory.add_graphc             C   sJ   | j stj| | n2| jd| y| jj| W n tk
rD   Y nX d S )N)NNN)rV   r   remove_graphr,   rI   KeyError)r   rd   r   r   r   re     s    zIOMemory.remove_graphc             C   s   | j |}|\}}}|| jkrh|| j| krh|| jkrF| jj | j|< || j| |< |s|| j| d< n&|r|||i| j|< n||d|i| j|< |s| jd j| || jkrt | j|< | j| j| | jdkr| j| | _| j| | jkr| j|= dS )z;add the given context to the set of contexts for the tripleN)rR   rC   rF   rJ   rb   rH   r(   rG   )r   rM   r   r   rX   rN   rO   rP   r   r   r   Z__addTripleContext  s(    




zIOMemory.__addTripleContextc             C   s.   | j j|| j}|s|j S dd |j D S )zkreturn a list of (encoded) contexts for the triple, skipping
           quoted contexts if skipQuoted==Truec             S   s   g | ]\}}|s|qS r   r   )r7   rX   r   r   r   r   
<listcomp>  s    z0IOMemory.__getTripleContexts.<locals>.<listcomp>)rF   r5   rJ   r/   items)r   rM   rQ   rY   r   r   r   Z__getTripleContexts  s    zIOMemory.__getTripleContextsc             C   s   | j j|| j}||kS )z6return True iff the triple exists in the given context)rF   r5   rJ   )r   rM   rX   rY   r   r   r   Z__tripleHasContext  s    zIOMemory.__tripleHasContextc             C   sL   | j j|| jj }||= || jkr.| j |= n
|| j |< | j| j| dS )z"remove the context from the tripleN)rF   r5   rJ   rb   rH   r,   )r   rM   rX   rY   r   r   r   Z__removeTripleContext  s    


zIOMemory.__removeTripleContextc             C   sH   || j kr>t }x|| jkr$t }qW || j |< || j|< |S | j | S )zaencode object, storing it in the encoding map if necessary,
           and return the integer key)rB   randidrA   )r   objidr   r   r   Z__obj2id  s    



zIOMemory.__obj2idc             C   s   t t| j|S )z3encode a whole triple, returning the encoded triple)tuplemaprR   )r   r   r   r   r   Z__encodeTriple  s    zIOMemory.__encodeTriplec             C   s   t t| jj|S )zDdecode a whole encoded triple, returning the original
        triple)rl   rm   rA   r5   )r   rM   r   r   r   Z__decodeTriple  s    zIOMemory.__decodeTriplec             c   s@   || j krdS x,| j | j D ]}| j|| j|fV  qW dS )z[return a generator which yields all the triples (unencoded)
           of the given contextN)rH   rb   r]   rZ   )r   rX   rM   r   r   r   Z__all_triples  s    
zIOMemory.__all_triplesc                s    fdd j |ddD S )zgreturn a generator for all the non-quoted contexts
           (unencoded) the encoded triple appears inc             3   s"   | ]}|d k	r j j|V  qd S )N)rA   r5   )r7   rX   )r   r   r   r8     s    z&IOMemory.__contexts.<locals>.<genexpr>T)rQ   )rS   )r   rM   r   )r   r   r9     s    zIOMemory.__contextsc             c   s   dS )zreturn an empty generatorNr   )r   r   r   r   Z
__emptygen  s    zIOMemory.__emptygen)NN)F)N)N)N)N)F)r:   r;   r<   r=   Zcontext_awareZformula_awarerV   r   r4   r3   r2   r6   r(   r,   r)   rW   r1   rc   re   rL   rS   r\   rT   rR   rK   r]   r^   rZ   r_   r>   r   r   )r   r   r      s4   

!
2

(
r   c             C   s   ||| dd S )Nr   i 5wr   )randintchoiceZsignsr   r   r   ri     s    ri   )rp   r   )
__future__r   r   r   randomZrdflib.termr   Zrdflib.storer   r   r   sixr	   __all__r-   ZAnyr
   r   rn   ro   ri   r   r   r   r   <module>   s    "  I