001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: DBBulkUpdateHandler.java,v 1.23 2008/01/02 12:08:25 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.util.IteratorCollection;
013: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
014: import com.hp.hpl.jena.graph.impl.*;
015: import com.hp.hpl.jena.db.*;
016:
017: /**
018: An implementation of the bulk update interface. Updated by kers to permit event
019: handling for bulk updates.
020:
021: @author csayers based on SimpleBulkUpdateHandler by kers
022: @version $Revision: 1.23 $
023: */
024:
025: public class DBBulkUpdateHandler implements BulkUpdateHandler {
026: private GraphRDB graph;
027: private GraphEventManager manager;
028:
029: protected static int CHUNK_SIZE = 50;
030:
031: public DBBulkUpdateHandler(GraphRDB graph) {
032: this .graph = graph;
033: this .manager = graph.getEventManager();
034: }
035:
036: /**
037: add a list of triples to the graph; the add is done as a list with notify off,
038: and then the array-notify invoked.
039: */
040: public void add(Triple[] triples) {
041: add(Arrays.asList(triples), false);
042: manager.notifyAddArray(graph, triples);
043: }
044:
045: public void add(List triples) {
046: add(triples, true);
047: }
048:
049: /**
050: add a list of triples to the graph, notifying only if requested.
051: */
052: protected void add(List triples, boolean notify) {
053: graph.add(triples);
054: if (notify)
055: manager.notifyAddList(graph, triples);
056: }
057:
058: /**
059: Add the [elements of the] iterator to the graph. Complications arise because
060: we wish to avoid duplicating the iterator if there are no listeners; otherwise
061: we have to read the entire iterator into a list and use add(List) with notification
062: turned off.
063: @see com.hp.hpl.jena.graph.BulkUpdateHandler#add(java.util.Iterator)
064: */
065: public void add(Iterator it) {
066: if (manager.listening()) {
067: List L = IteratorCollection.iteratorToList(it);
068: add(L, false);
069: manager.notifyAddIterator(graph, L);
070: } else
071: addIterator(it);
072: }
073:
074: protected void addIterator(Iterator it) {
075: ArrayList list = new ArrayList(CHUNK_SIZE);
076: while (it.hasNext()) {
077: while (it.hasNext() && list.size() < CHUNK_SIZE) {
078: list.add(it.next());
079: }
080: graph.add(list);
081: list.clear();
082: }
083: }
084:
085: public void add(Graph g) {
086: add(g, false);
087: }
088:
089: public void add(Graph g, boolean withReifications) {
090: ExtendedIterator triplesToAdd = GraphUtil.findAll(g);
091: try {
092: addIterator(triplesToAdd);
093: } finally {
094: triplesToAdd.close();
095: }
096: if (withReifications)
097: SimpleBulkUpdateHandler.addReifications(graph, g);
098: manager.notifyAddGraph(graph, g);
099: }
100:
101: /**
102: remove a list of triples from the graph; the remove is done as a list with notify off,
103: and then the array-notify invoked.
104: */
105: public void delete(Triple[] triples) {
106: delete(Arrays.asList(triples), false);
107: manager.notifyDeleteArray(graph, triples);
108: }
109:
110: public void delete(List triples) {
111: delete(triples, true);
112: }
113:
114: /**
115: Add a list of triples to the graph, notifying only if requested.
116: */
117: protected void delete(List triples, boolean notify) {
118: graph.delete(triples);
119: if (notify)
120: manager.notifyDeleteList(graph, triples);
121: }
122:
123: /**
124: Delete the [elements of the] iterator from the graph. Complications arise
125: because we wish to avoid duplicating the iterator if there are no listeners;
126: otherwise we have to read the entire iterator into a list and use delete(List)
127: with notification turned off.
128: @see com.hp.hpl.jena.graph.BulkUpdateHandler#add(java.util.Iterator)
129: */
130: public void delete(Iterator it) {
131: if (manager.listening()) {
132: List L = IteratorCollection.iteratorToList(it);
133: delete(L, false);
134: manager.notifyDeleteIterator(graph, L);
135: } else
136: deleteIterator(it);
137: }
138:
139: protected void deleteIterator(Iterator it) {
140: ArrayList list = new ArrayList(CHUNK_SIZE);
141: while (it.hasNext()) {
142: while (it.hasNext() && list.size() < CHUNK_SIZE) {
143: list.add(it.next());
144: }
145: graph.delete(list);
146: list.clear();
147: }
148: }
149:
150: public void delete(Graph g) {
151: delete(g, false);
152: }
153:
154: public void delete(Graph g, boolean withReifications) {
155: ExtendedIterator triplesToDelete = GraphUtil.findAll(g);
156: try {
157: deleteIterator(triplesToDelete);
158: } finally {
159: triplesToDelete.close();
160: }
161: if (withReifications)
162: SimpleBulkUpdateHandler.deleteReifications(graph, g);
163: manager.notifyDeleteGraph(graph, g);
164: }
165:
166: public void removeAll() {
167: graph.clear();
168: manager.notifyEvent(graph, GraphEvents.removeAll);
169: }
170:
171: public void remove(Node s, Node p, Node o) {
172: SimpleBulkUpdateHandler.removeAll(graph, s, p, o);
173: manager.notifyEvent(graph, GraphEvents.remove(s, p, o));
174: }
175: }
176:
177: /*
178: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
179: All rights reserved.
180:
181: Redistribution and use in source and binary forms, with or without
182: modification, are permitted provided that the following conditions
183: are met:
184:
185: 1. Redistributions of source code must retain the above copyright
186: notice, this list of conditions and the following disclaimer.
187:
188: 2. Redistributions in binary form must reproduce the above copyright
189: notice, this list of conditions and the following disclaimer in the
190: documentation and/or other materials provided with the distribution.
191:
192: 3. The name of the author may not be used to endorse or promote products
193: derived from this software without specific prior written permission.
194:
195: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
202: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
203: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
204: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205: */
|