1 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 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 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 51 protected void add( List triples, boolean notify ) { 52 graph.add(triples); 53 if (notify) manager.notifyAddList( graph, triples ); 54 } 55 56 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 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 112 protected void delete(List triples, boolean notify ) { 113 graph.delete( triples ); 114 if (notify) manager.notifyDeleteList( graph, triples ); 115 } 116 117 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 | Popular Tags |