Skip to content

Commit 7efd375

Browse files
Merge pull request #11 from genealogiawebsite/optimse
Changes for bulk insert
2 parents d40977f + 7ce261a commit 7efd375

File tree

9 files changed

+858
-491
lines changed

9 files changed

+858
-491
lines changed

src/Utils/FamilyData.php

Lines changed: 33 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class FamilyData
1818
/**
1919
* Array of persons ID
2020
* key - old GEDCOM ID
21-
* value - new autoincrement ID.
21+
* value - new cyrus_authenticate(connection) sincrement ID.
2222
*
2323
* @var string
2424
*/
@@ -30,105 +30,47 @@ class FamilyData
3030
protected $repo_ids = [];
3131
protected $conn = '';
3232

33-
public static function getFamily($conn, $family, $obje_ids)
33+
public static function getFamily($conn, $families, $obje_ids)
3434
{
35-
$g_id = $family->getId();
36-
$resn = $family->getResn();
37-
$husb = $family->getHusb();
38-
$wife = $family->getWife();
35+
$familyData = [];
36+
foreach($families as $family){
37+
$g_id = $family->getId();
38+
$resn = $family->getResn();
39+
$husb = $family->getHusb();
40+
$wife = $family->getWife();
3941

40-
// string
41-
$nchi = $family->getNchi();
42-
$rin = $family->getRin();
42+
// string
43+
$nchi = $family->getNchi();
44+
$rin = $family->getRin();
4345

44-
// array
45-
$subm = $family->getSubm();
46-
$_slgs = $family->getSlgs();
46+
// array
47+
$subm = $family->getSubm();
48+
$_slgs = $family->getSlgs();
4749

48-
$description = null;
49-
$type_id = 0;
50+
$description = null;
51+
$type_id = 0;
5052

51-
$children = $family->getChil();
52-
$events = $family->getAllEven();
53-
$_note = $family->getNote();
54-
$_obje = $family->getObje();
55-
$_sour = $family->getSour();
56-
$_refn = $family->getRefn();
53+
$children = $family->getChil();
54+
$events = $family->getAllEven();
55+
$_note = $family->getNote();
56+
$_obje = $family->getObje();
57+
$_sour = $family->getSour();
58+
$_refn = $family->getRefn();
5759

58-
$chan = $family->getChan();
60+
$chan = $family->getChan();
5961

60-
$husband_id = (isset($persons_id[$husb])) ? $persons_id[$husb] : 0;
61-
$wife_id = (isset($persons_id[$wife])) ? $persons_id[$wife] : 0;
62+
$husband_id = (isset($persons_id[$husb])) ? $persons_id[$husb] : 0;
63+
$wife_id = (isset($persons_id[$wife])) ? $persons_id[$wife] : 0;
6264

63-
$family = Family::on($conn)->updateOrCreate(
64-
compact('husband_id', 'wife_id'),
65-
compact('husband_id', 'wife_id', 'description', 'type_id', 'nchi', 'rin')
66-
);
65+
$family = Family::on($conn)->updateOrCreate(
66+
compact('husband_id', 'wife_id'),
67+
compact('husband_id', 'wife_id', 'description', 'type_id', 'nchi', 'rin')
68+
);
6769

68-
if ($children !== null) {
69-
foreach ($children as $child) {
70-
if (isset($persons_id[$child])) {
71-
$person = Person::on($conn)->find($persons_id[$child]);
72-
$person->child_in_family_id = $family->id;
73-
$person->save();
74-
}
75-
}
76-
}
77-
78-
if ($events !== null && count($events) > 0) {
79-
foreach ($events as $item) {
80-
if ($item) {
81-
Even::read($conn, $item, $family, $obje_ids);
82-
}
83-
// $date = $getDate($item->getDate());
84-
// $place = $getPlace($item->getPlac());
85-
// $family->addEvent($item->getType(), $date, $place);
86-
}
87-
}
88-
$_group = 'fam';
89-
$_gid = $family->id;
90-
if ($_note != null && count($_note) > 0) {
91-
foreach ($_note as $item) {
92-
NoteRef::read($conn, $item, $_group, $_gid);
93-
}
94-
}
95-
if ($_obje && count($_obje) > 0) {
96-
foreach ($_obje as $item) {
97-
if ($item) {
98-
ObjeRef::read($conn, $item, $_group, $_gid, $obje_ids);
99-
}
100-
}
101-
}
102-
if ($_refn && count($_refn) > 0) {
103-
foreach ($_refn as $item) {
104-
if ($item) {
105-
Refn::read($conn, $item, $_group, $_gid);
106-
}
107-
}
108-
}
109-
if ($_sour && count($_sour) > 0) {
110-
foreach ($_sour as $item) {
111-
if ($item) {
112-
SourRef::read($conn, $item, $_group, $_gid, $sour_ids, $obje_ids);
113-
}
114-
}
115-
}
116-
if ($_slgs && count($_slgs) > 0) {
117-
foreach ($_slgs as $item) {
118-
if ($item) {
119-
Slgs::read($conn, $item, $family);
120-
}
121-
}
122-
}
123-
if ($subm && count($subm) > 0) {
124-
foreach ($subm as $item) {
125-
if ($item) {
126-
Subm::read($conn, $item, $_group, $_gid, $obje_ids);
127-
}
128-
}
129-
}
130-
if ($chan) {
131-
Chan::read($conn, $chan, 'family', $family->id);
70+
// $value = ['husband_id'=>$husband_id, 'wife_id'=>$wife_id, 'description'=>$description, 'type_id'=>$type_id, 'nchi'=>$nchi, 'rin'=>$rin];
71+
// $familydata [] = $value;
13272
}
73+
// Family::insert($familyData);
74+
otherFamRecord::insertFamilyData($conn,$families,$obje_ids);
13375
}
13476
}

src/Utils/GedcomParser.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ public function parse($conn, string $filename, string $slug, bool $progressBar =
4444
$this->conn = $conn;
4545
//start calculating the memory - https://www.php.net/manual/en/function.memory-get-usage.php
4646
$startMemoryUse = round(memory_get_usage() / 1048576, 2);
47-
error_log("\nMemory Usage: ".$startMemoryUse.''.' MB');
47+
48+
error_log("\n Memory Usage: ".$startMemoryUse.' MB');
4849
error_log('PARSE LOG : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'.$conn);
4950
$parser = new Parser();
5051
$gedcom = @$parser->parse($filename);
52+
5153
/**
5254
* work.
5355
*/
56+
5457
$head = $gedcom->getHead();
5558
$subn = $gedcom->getSubn();
5659
$subm = $gedcom->getSubm();
@@ -71,17 +74,20 @@ public function parse($conn, string $filename, string $slug, bool $progressBar =
7174
if ($subn != null) {
7275
$c_subn = 1;
7376
}
77+
$beforeInsert = round(memory_get_usage() / 1048576, 2);
7478

7579
$individuals = $gedcom->getIndi();
7680
$families = $gedcom->getFam();
77-
$total = count($individuals) + count($families) + $c_subn + $c_subm + $c_sour + $c_note + $c_repo + $c_obje;
81+
$indCount = count($individuals);
82+
$famCount = count($families);
83+
$total = $indCount + $famCount + $c_subn + $c_subm + $c_sour + $c_note + $c_repo + $c_obje;
7884
$complete = 0;
7985
if ($progressBar === true) {
80-
$bar = $this->getProgressBar(count($individuals) + count($families));
81-
event(new GedComProgressSent($slug, $total, $complete));
86+
$bar = $this->getProgressBar($indCount + $famCount);
87+
event(new GedComProgressSent($slug, $total, $complete));
8288
}
83-
Log::info('Individual:'.count($individuals));
84-
Log::info('Families:'.count($families));
89+
Log::info('Individual:'.$indCount);
90+
Log::info('Families:'.$famCount);
8591
Log::info('Subn:'.$c_subn);
8692
Log::info('Subm:'.$c_subm);
8793
Log::info('Sour:'.$c_sour);
@@ -177,9 +183,10 @@ public function parse($conn, string $filename, string $slug, bool $progressBar =
177183
event(new GedComProgressSent($slug, $total, $complete));
178184
}
179185
}
186+
ParentData::getPerson($this->conn, $individuals, $this->obje_ids);
180187

181188
foreach ($individuals as $individual) {
182-
ParentData::getPerson($this->conn, $individual, $this->obje_ids);
189+
// ParentData::getPerson($this->conn, $individual, $this->obje_ids);
183190
if ($progressBar === true) {
184191
$bar->advance();
185192
$complete++;
@@ -211,10 +218,12 @@ public function parse($conn, string $filename, string $slug, bool $progressBar =
211218
$item->delete();
212219
}
213220
}
214-
221+
222+
FamilyData::getFamily($this->conn, $families, $this->obje_ids);
223+
215224
foreach ($families as $family) {
216-
// $this->getFamily($family);
217-
FamilyData::getFamily($this->conn, $family, $this->obje_ids);
225+
// // $this->getFamily($family);
226+
// FamilyData::getFamily($this->conn, $family, $this->obje_ids);
218227
if ($progressBar === true) {
219228
$bar->advance();
220229
$complete++;
@@ -226,6 +235,7 @@ public function parse($conn, string $filename, string $slug, bool $progressBar =
226235
$time_end = microtime(true);
227236
$endMemoryUse = round(memory_get_usage() / 1048576, 2);
228237
$execution_time = ($time_end - $time_start);
238+
$beform_insert_memory = $beforeInsert - $startMemoryUse;
229239
$memory_usage = $endMemoryUse - $startMemoryUse;
230240
error_log("\nTotal Execution Time: ".round($execution_time).' Seconds');
231241
error_log("\nMemory Usage: ".$memory_usage.''.' MB');
@@ -240,4 +250,5 @@ private function getProgressBar(int $max)
240250
new StreamOutput(fopen('php://stdout', 'w'))
241251
))->createProgressBar($max);
242252
}
253+
243254
}

src/Utils/Importer/Indi/Alia.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ class Alia
1111
* String $group
1212
* Integer $group_id.
1313
*/
14-
public static function read($conn, string $alia, $group = '', $group_id = 0)
15-
{
16-
// store alia
17-
$key = ['group'=>$group, 'gid'=>$group_id, 'alia'=>$alia];
18-
$data = ['group'=>$group, 'gid'=>$group_id, 'alia'=>$alia];
19-
$record = PersonAlia::on($conn)->updateOrCreate($key, $data);
14+
public static function read($conn, $item, $group = '', $group_id = 0)
15+
{
16+
$aliaData = [];
17+
foreach ($item as $alia) {
18+
if ($alia) {
19+
$data = ['group'=>$group, 'gid'=>$group_id, 'alia'=>$alia];
20+
$aliaData [] = $data;
21+
}
22+
}
23+
PersonAlia::insert($aliaData);
2024
}
2125
}

src/Utils/Importer/Indi/Anci.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ class Anci
1111
* String $group
1212
* Integer $group_id.
1313
*/
14-
public static function read($conn, string $anci, $group = '', $group_id = 0, $subm_ids)
14+
public static function read($conn, $item, $group = '', $group_id = 0, $subm_ids)
1515
{
16-
// store alia
17-
if (isset($subm_ids[$anci])) {
18-
$subm_id = $subm_ids[$anci];
19-
$key = ['group'=>$group, 'gid'=>$group_id, 'anci'=>$subm_id];
20-
$data = ['group'=>$group, 'gid'=>$group_id, 'anci'=>$subm_id];
21-
$record = PersonAnci::on($conn)->updateOrCreate($key, $data);
16+
$record = [];
17+
foreach ($item as $anci) {
18+
if ($anci) {
19+
// store alia
20+
if (isset($subm_ids[$anci])) {
21+
$subm_id = $subm_ids[$anci];
22+
$key = ['group'=>$group, 'gid'=>$group_id, 'anci'=>$subm_id];
23+
$data = ['group'=>$group, 'gid'=>$group_id, 'anci'=>$subm_id];
24+
$record [] = $data;
25+
}
26+
}
2227
}
28+
PersonAnci::insert($record);
2329
}
2430
}

0 commit comments

Comments
 (0)