KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > DBBulkUpdateHandler


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: DBBulkUpdateHandler.java,v 1.19 2005/02/21 12:02:42 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.db.impl;
8
9 import java.util.*;
10
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.util.IteratorCollection;
13 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
14 import com.hp.hpl.jena.graph.impl.*;
15 import com.hp.hpl.jena.db.*;
16
17 /**
18     An implementation of the bulk update interface. Updated by kers to permit event
19     handling for bulk updates.
20     
21     @author csayers based on SimpleBulkUpdateHandler by kers
22     @version $Revision: 1.19 $
23 */

24
25 public class DBBulkUpdateHandler implements BulkUpdateHandler {
26     private GraphRDB graph;
27     private GraphEventManager manager;
28     
29     protected static int CHUNK_SIZE = 50;
30
31     public DBBulkUpdateHandler(GraphRDB graph) {
32         this.graph = graph;
33         this.manager = graph.getEventManager();
34     }
35
36     /**
37         add a list of triples to the graph; the add is done as a list with notify off,
38         and then the array-notify invoked.
39     */

40     public void add(Triple[] triples) {
41         add( Arrays.asList(triples), false );
42         manager.notifyAddArray( graph, triples );
43     }
44
45     public void add( List triples )
46         { add( triples, true ); }
47         
48     /**
49         add a list of triples to the graph, notifying only if requested.
50     */

51     protected void add( List triples, boolean notify ) {
52         graph.add(triples);
53         if (notify) manager.notifyAddList( graph, triples );
54     }
55
56     /**
57         Add the [elements of the] iterator to the graph. Complications arise because
58         we wish to avoid duplicating the iterator if there are no listeners; otherwise
59         we have to read the entire iterator into a list and use add(List) with notification
60         turned off.
61         @see com.hp.hpl.jena.graph.BulkUpdateHandler#add(java.util.Iterator)
62      */

63     public void add(Iterator it)
64         {
65         if (manager.listening())
66             {
67             List L = IteratorCollection.iteratorToList( it );
68             add( L, false );
69             manager.notifyAddIterator( graph, L );
70             }
71         else
72             addIterator( it );
73         }
74     
75     protected void addIterator( Iterator it )
76     {
77         ArrayList list = new ArrayList(CHUNK_SIZE);
78         while (it.hasNext()) {
79             while (it.hasNext() && list.size() < CHUNK_SIZE) {
80                 list.add( it.next() );
81             }
82             graph.add(list);
83             list.clear();
84         }
85     }
86         
87     public void add( Graph g )
88         { add( g, false ); }
89         
90     public void add( Graph g, boolean withReifications ) {
91         ExtendedIterator triplesToAdd = GraphUtil.findAll( g );
92         try { addIterator( triplesToAdd ); } finally { triplesToAdd.close(); }
93         if (withReifications) SimpleBulkUpdateHandler.addReifications( graph, g );
94         manager.notifyAddGraph( graph, g );
95     }
96
97     /**
98         remove a list of triples from the graph; the remove is done as a list with notify off,
99         and then the array-notify invoked.
100     */

101     public void delete( Triple[] triples ) {
102         delete( Arrays.asList(triples), false );
103         manager.notifyDeleteArray( graph, triples );
104     }
105
106     public void delete( List triples )
107         { delete( triples, true ); }
108         
109     /**
110         Add a list of triples to the graph, notifying only if requested.
111     */

112     protected void delete(List triples, boolean notify ) {
113         graph.delete( triples );
114         if (notify) manager.notifyDeleteList( graph, triples );
115     }
116     
117     /**
118         Delete the [elements of the] iterator from the graph. Complications arise
119         because we wish to avoid duplicating the iterator if there are no listeners;
120         otherwise we have to read the entire iterator into a list and use delete(List)
121         with notification turned off.
122         @see com.hp.hpl.jena.graph.BulkUpdateHandler#add(java.util.Iterator)
123      */

124     public void delete(Iterator it)
125         {
126         if (manager.listening())
127             {
128             List L = IteratorCollection.iteratorToList( it );
129             delete( L, false );
130             manager.notifyDeleteIterator( graph, L );
131             }
132         else
133             deleteIterator( it );
134         }
135     
136     protected void deleteIterator(Iterator it) {
137         ArrayList list = new ArrayList(CHUNK_SIZE);
138         while (it.hasNext()) {
139             while (it.hasNext() && list.size() < CHUNK_SIZE) {
140                 list.add(it.next());
141             }
142             graph.delete(list);
143             list.clear();
144         }
145     }
146
147     public void delete(Graph g)
148         { delete( g, false ); }
149         
150     public void delete( Graph g, boolean withReifications ) {
151         ExtendedIterator triplesToDelete = GraphUtil.findAll( g );
152         try { deleteIterator( triplesToDelete ); } finally { triplesToDelete.close(); }
153         if (withReifications) SimpleBulkUpdateHandler.deleteReifications( graph, g );
154         manager.notifyDeleteGraph( graph, g );
155     }
156     
157     public void removeAll()
158         { SimpleBulkUpdateHandler.removeAll( graph );
159         manager.notifyEvent( graph, GraphEvents.removeAll ); }
160     
161     public void remove( Node s, Node p, Node o )
162         { SimpleBulkUpdateHandler.removeAll( graph, s, p, o );
163         manager.notifyEvent( graph, GraphEvents.remove( s, p, o ) ); }
164 }
165
166
167 /*
168     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
169     All rights reserved.
170
171     Redistribution and use in source and binary forms, with or without
172     modification, are permitted provided that the following conditions
173     are met:
174
175     1. Redistributions of source code must retain the above copyright
176        notice, this list of conditions and the following disclaimer.
177
178     2. Redistributions in binary form must reproduce the above copyright
179        notice, this list of conditions and the following disclaimer in the
180        documentation and/or other materials provided with the distribution.
181
182     3. The name of the author may not be used to endorse or promote products
183        derived from this software without specific prior written permission.
184
185     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195 */
Popular Tags