106 for eid in range(last_eid - self.eids_seq_range + 1, last_eid + 1): |
106 for eid in range(last_eid - self.eids_seq_range + 1, last_eid + 1): |
107 yield eid |
107 yield eid |
108 |
108 |
109 # master/slaves specific API |
109 # master/slaves specific API |
110 |
110 |
111 def master_init(self): |
111 def master_init(self, commit=True): |
112 """Initialize database for massive insertion. |
112 """Initialize database for massive insertion. |
113 |
113 |
114 This is expected to be called once, by the master store in master/slaves configuration. |
114 This is expected to be called once, by the master store in master/slaves configuration. |
115 """ |
115 """ |
116 assert not self.slave_mode |
116 assert not self.slave_mode |
117 if self not in self._initialized: |
117 if self not in self._initialized: |
118 self.sql('CREATE TABLE cwmassive_initialized' |
118 self.sql('CREATE TABLE cwmassive_initialized' |
119 '(retype text, type varchar(128), uuid varchar(32))') |
119 '(retype text, type varchar(128), uuid varchar(32))') |
120 self._initialized[self] = None |
120 self._initialized[self] = None |
|
121 if commit: |
|
122 self.commit() |
121 |
123 |
122 # SQL utilities ######################################################### |
124 # SQL utilities ######################################################### |
123 |
125 |
124 def _drop_metadata_constraints(self): |
126 def _drop_metadata_constraints(self): |
125 """Drop constraints and indexes for the metadata tables. |
127 """Drop constraints and indexes for the metadata tables. |
147 """Given an entity type, attributes and inlined relations, returns the inserted entity's |
149 """Given an entity type, attributes and inlined relations, returns the inserted entity's |
148 eid. |
150 eid. |
149 """ |
151 """ |
150 if etype not in self._initialized: |
152 if etype not in self._initialized: |
151 if not self.slave_mode: |
153 if not self.slave_mode: |
152 self.master_init() |
154 self.master_init(commit=False) |
153 tablename = 'cw_%s' % etype.lower() |
155 tablename = 'cw_%s' % etype.lower() |
154 tmp_tablename = '%s_%s' % (tablename, self.uuid) |
156 tmp_tablename = '%s_%s' % (tablename, self.uuid) |
155 self.sql("INSERT INTO cwmassive_initialized VALUES (%(e)s, 'etype', %(uuid)s)", |
157 self.sql("INSERT INTO cwmassive_initialized VALUES (%(e)s, 'etype', %(uuid)s)", |
156 {'e': etype, 'uuid': self.uuid}) |
158 {'e': etype, 'uuid': self.uuid}) |
157 attr_defs = eschema_sql_def(self._source_dbhelper, self.schema[etype]) |
159 attr_defs = eschema_sql_def(self._source_dbhelper, self.schema[etype]) |
173 |
175 |
174 Relation must not be inlined. |
176 Relation must not be inlined. |
175 """ |
177 """ |
176 if rtype not in self._initialized: |
178 if rtype not in self._initialized: |
177 if not self.slave_mode: |
179 if not self.slave_mode: |
178 self.master_init() |
180 self.master_init(commit=False) |
179 assert not self._cnx.vreg.schema.rschema(rtype).inlined |
181 assert not self._cnx.vreg.schema.rschema(rtype).inlined |
180 self._initialized[rtype] = None |
182 self._initialized[rtype] = None |
181 tablename = '%s_relation' % rtype.lower() |
183 tablename = '%s_relation' % rtype.lower() |
182 tmp_tablename = '%s_%s' % (tablename, self.uuid) |
184 tmp_tablename = '%s_%s' % (tablename, self.uuid) |
183 self.sql("INSERT INTO cwmassive_initialized VALUES (%(r)s, 'rtype', %(uuid)s)", |
185 self.sql("INSERT INTO cwmassive_initialized VALUES (%(r)s, 'rtype', %(uuid)s)", |