diff --git a/src/methods/create.ts b/src/methods/create.ts index 3d04bb536..cc7c0436a 100644 --- a/src/methods/create.ts +++ b/src/methods/create.ts @@ -32,6 +32,7 @@ export async function create(properties: Configurati defaultLanguage, schema: properties.schema, docs: {}, + docsCount: 0, index: {}, hooks: properties.hooks || {}, edge: properties.edge ?? false, diff --git a/src/methods/insert.ts b/src/methods/insert.ts index dfac11feb..87d860f54 100644 --- a/src/methods/insert.ts +++ b/src/methods/insert.ts @@ -45,6 +45,7 @@ export async function insert( assertDocSchema(doc, lyra.schema); lyra.docs[id] = doc; + lyra.docsCount++; recursiveradixInsertion(lyra, doc, id, config, undefined, lyra.components?.tokenizer as TokenizerConfigExec); trackInsertion(lyra); diff --git a/src/methods/load.ts b/src/methods/load.ts index af4b5cc39..0fce8be88 100644 --- a/src/methods/load.ts +++ b/src/methods/load.ts @@ -11,6 +11,7 @@ export async function load( lyra.index = index; lyra.docs = docs; + lyra.docsCount = Object.keys(docs).length; lyra.schema = schema; lyra.frequencies = frequencies; lyra.tokenOccurrencies = tokenOccurrencies; diff --git a/src/methods/remove.ts b/src/methods/remove.ts index 15f752b3e..acf645f7d 100644 --- a/src/methods/remove.ts +++ b/src/methods/remove.ts @@ -53,6 +53,7 @@ export async function remove(lyra: Lyra, docID: s } lyra.docs[docID] = undefined; + lyra.docsCount--; return true; } diff --git a/src/methods/search.ts b/src/methods/search.ts index ee1554a31..f71255281 100644 --- a/src/methods/search.ts +++ b/src/methods/search.ts @@ -100,6 +100,7 @@ export async function search( const results: RetrievedDoc[] = Array.from({ length: limit, }); + const N = lyra.docsCount; const timeStart = getNanosecondsTime(); // uniqueDocsIDs contains unique document IDs for all the tokens in all the indices. @@ -139,8 +140,6 @@ export async function search( docsIntersection[index] = []; } - const N = Object.keys(lyra.docs).length; - // Now it's time to loop over all the indices and get the documents IDs for every single term const indexesLength = indices.length; for (let i = 0; i < indexesLength; i++) { diff --git a/src/types.ts b/src/types.ts index c430e10d8..588791eee 100644 --- a/src/types.ts +++ b/src/types.ts @@ -64,6 +64,7 @@ export interface Lyra extends Data { hooks: Hooks; components?: Components; frequencies: FrequencyMap; + docsCount: number; } type ResolveTypes = TType extends "string" diff --git a/tap-snapshots/tests/lyra.dataset.test.ts.test.cjs b/tap-snapshots/tests/lyra.dataset.test.ts.test.cjs index 96a880ce6..8201640db 100644 --- a/tap-snapshots/tests/lyra.dataset.test.ts.test.cjs +++ b/tap-snapshots/tests/lyra.dataset.test.ts.test.cjs @@ -16,12 +16,12 @@ Object { "first": "By place", "second": "Roman Republic", }, - "date": "-89", - "description": "Social War:", + "date": "-299", + "description": "The Samnites, seizing their chance when Rome is engaged on the Lombard plain, start the third Samnite War with a collection of mercenaries from Gaul, Sabine, and Etruscan allies to help them.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { @@ -29,12 +29,12 @@ Object { "first": "By place", "second": "Roman Republic", }, - "date": "-57", - "description": "Gallic Wars:", + "date": "-298", + "description": "The Samnites defeat the Romans under Lucius Cornelius Scipio Barbatus in the Battle of Camerinum, first battle of the Third Samnite War.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { @@ -42,25 +42,25 @@ Object { "first": "By place", "second": "Roman Republic", }, - "date": "-55", - "description": "Gallic War", + "date": "-290", + "description": "Roman general and consul, Manius Curius Dentatus, gains a decisive victory over the Samnites, thereby ending a war that has lasted 50 years. He also reduces the Sabine insurgents to submission, their territory is annexed and they are granted civitas sine suffragio (ampquotcitizenship without the right to voteampquot). The Samnites are recognised by the Romans as autonomous allies. The Samnites are forced to give up some of their land to the Romans as compensation.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "Greece", }, - "date": "-54", - "description": "Gallic Wars", + "date": "-287", + "description": "Demetrius decides to leave Antigonus in charge of the war in Greece, assembles all his ships and embarks with his troops to attack Caria and Lydia, provinces in Asia Minor controlled by Lysimachus.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { @@ -68,77 +68,77 @@ Object { "first": "By place", "second": "Roman Republic", }, - "date": "-53", - "description": "Parthian war:", + "date": "-282", + "description": "Rome declares war on Tarentum. King Pyrrhus of Epirus declares his willingness to come to the aid of Tarentum. Tarentum also looks for support from the Samnites and other Italian tribes in southern Italy.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "Seleucid Empire", }, - "date": "-53", - "description": "Gallic War:", + "date": "-280", + "description": "Nicomedes, King of Bithynia, is threatened with an invasion from Antiochus who has already made war upon his father, Zipoites. Antiochus actually invades Bithynia but withdraws again without risking a battle.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "Seleucid Empire", }, - "date": "-48", - "description": "Civil War:", + "date": "-280", + "description": "Antiochus is defeated by Egypt's Ptolemy II in the Damascene War.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "Egypt", }, - "date": "-47", - "description": "Civil War:", + "date": "-276", + "description": "The first of the Syrian Wars starts between Egypt's Ptolemy II and Seleucid emperor Antiochus I Soter. The Egyptians invade northern Syria, but Antiochus defeats and repels his opponent's army.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "Seleucid Empire", }, - "date": "-46", - "description": "Civil War:", + "date": "-272", + "description": "The Seleucid king Antiochus I Soter is defeated by Egypt's Ptolemy II during the First Syrian War. Ptolemy II annexes Miletus, Phoenicia and western Cilicia from Antiochus. As a result, Ptolemy II extends Egyptian rule as far as Caria and into most of Cilicia.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "January/March", - "second": "", + "first": "By place", + "second": "Roman Republic", }, - "date": "1809/03/13", - "description": "Peninsular War", + "date": "-264", + "description": "The tyrant of Syracuse, Hiero II, once more attacks the Mamertines. They ally themselves with a nearby Carthaginian fleet and hold off the Syracusans. However, when the Carthaginians do not leave, the Mamertines appeal to Rome for an alliance, hoping for more reliable protection. Although initially reluctant to assist lest it encourage other mercenary groups to mutiny, Rome is unwilling to see Carthaginian power spread further over Sicily and encroach on Italy. Rome therefore enters into an alliance with the Mamertines. By this action, the First Punic War begins and will embroil Rome in a conflict with Carthage that will continue for 23 years.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, ], } @@ -152,132 +152,132 @@ Object { Object { "document": Object { "categories": Object { - "first": "Ongoing", - "second": "", + "first": "By place", + "second": "Greece", }, - "date": "1867/12/02", - "description": "Paraguayan War.", + "date": "-263", + "description": "The Athenians and Spartans, worn down by several years of war and the devastation of their lands, make peace with Antigonus II of Macedonia who thus retains his hold on Greece.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "February", - "second": "", + "first": "By place", + "second": "Greece", }, - "date": "1950/02/14", - "description": " Cold War:", + "date": "-262", + "description": "After Athens surrenders following a long siege by Macedonian forces, Antigonus II Gonatas re-garrisons Athens and forbids the city from making war. Otherwise, he leaves Athens alone as the seat of philosophy and learning in Greece.", "granularity": "year", }, "id": "", - "score": 0.6028126688266209, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "China", }, - "date": "-86", - "description": "First Mithridatic War", + "date": "-260", + "description": "In the Battle of Changping, the army of the Qin state routs the army of Zhao, establishing its military superiority over all other Chinese states during the Warring States Period. The battle, in which Zhao forces are led by Lian Po and Zhao Kuo, while Qin is led by Wang He and Bai Qi, takes place near modern-day Gaoping in Shanxi and hundreds of thousands of soldiers from Zhao are executed after the battle.", "granularity": "year", }, "id": "", - "score": 0.40187511255108055, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Asia", + "second": "Seleucid Empire", }, - "date": "632/01/27", - "description": "Ridda Wars begins", + "date": "-259", + "description": "The Seleucid king Antiochus II starts the Second Syrian War against Ptolemy to avenge his father's losses. Antiochus II finds a willing ally in Antigonus II Gonatas, the king of Macedonia, who has been dealing with Ptolemy II's attempts to destabilize Macedonia.", "granularity": "year", }, "id": "", - "score": 0.40187511255108055, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Europe", + "second": "Roman Republic", }, - "date": "988", - "description": "Rus'–Byzantine War", + "date": "-256", + "description": "Rome aims for a quick end to hostilities in the First Punic War and decides to invade the Carthaginian colonies in Northern Africa to force the enemy to accept terms. A major fleet is built, including transports for the army and its equipment, and warships for their protection. Carthage under Hamilcar tries to intervene but a force under the Roman general and consul Marcus Atilius Regulus and his colleague Lucius Manlius Vulso Longus defeat the Carthaginian fleet in the Battle of Cape Ecnomus off the southern coast of Sicily.", "granularity": "year", }, "id": "", - "score": 0.40187511255108055, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "", - "second": "", + "first": "By place", + "second": "Roman Republic", }, - "date": "1043/10/31", - "description": "Rus'-Byzantine War (1043).", + "date": "-256", + "description": "Following the Battle of Cape Ecnomus, the Romans land an army near Carthage and begin ravaging the Carthaginian countryside. The Roman army soon forces the capitulation of Clupea, a town 40 miles (64 kilometres) east of Carthage. After setting up Roman defenses for the city, the two consuls receive instructions from Rome that Vulso is to set sail for Rome, taking most of the fleet with him. Regulus, on the other hand, is to stay with the infantry and cavalry to finish the war.", "granularity": "year", }, "id": "", - "score": 0.40187511255108055, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "April/June", - "second": "", + "first": "By place", + "second": "Egypt", }, - "date": "1861/04/27", - "description": " American Civil War:", + "date": "-255", + "description": "In the Second Syrian War, Ptolemy II loses ground in Cilicia, Pamphylia, and Ionia, while Antiochus II regains Miletus and Ephesus. A peace is then concluded between Antiochus and Ptolemy under which Antiochus is to marry Ptolemy's daughter Berenice Syra.", "granularity": "year", }, "id": "", - "score": 0.40187511255108055, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "December", - "second": "", + "first": "By place", + "second": "Seleucid Empire", }, - "date": "1914/12/24", - "description": " World War I:", + "date": "-253", + "description": "The second Syrian War between the Seleucids and the Ptolemies ends. Antiochus II regains much of Anatolia from Ptolemy II, including the cities of Miletus and Ephesus, and also the Phoenician coast.", "granularity": "year", }, "id": "", - "score": 0.40187511255108055, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Byzantine Empire", + "second": "Seleucid Empire", }, - "date": "630", - "description": "The Byzantine-Arab Wars begin.", + "date": "-253", + "description": "The war is concluded with the marriage of Antiochus to Ptolemy II's daughter, Berenice Syra. Antiochus divorces his previous wife, Laodice, and transfers the succession to Berenice's children.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "Date unknown", - "second": "", + "first": "By place", + "second": "Roman Republic", }, - "date": "1522/12/20", - "description": "The Habsburg-Valois Wars begin.", + "date": "-253", + "description": "A second Roman war fleet of 150 ships is wrecked on the voyage from Lilybaeum (in Sicily) to Rome.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, ], } @@ -291,93 +291,93 @@ Object { Object { "document": Object { "categories": Object { - "first": "In fiction", - "second": "", + "first": "By place", + "second": "Greece", }, - "date": "1728/10/20", - "description": "The Meerkat–Mongoose war.", + "date": "-253", + "description": "Macedonia's involvement in the second Syrian War ceases when Antigonus becomes preoccupied with the rebellion of Corinth and Chalcis, as well as an increase in enemy activity along Macedon's northern frontier.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "Date unknown", - "second": "", + "first": "By place", + "second": "Roman Republic", }, - "date": "1830/12/20", - "description": "The Java War ends.", + "date": "-250", + "description": "In the Punic War, the Romans shift their attention to the southwest of Sicily. They send a naval expedition toward the Carthaginian city of Lilybaeum. En route, the Romans seize and burn the Carthaginian held cities of Selinous and Heraclea Minoa. The Romans then begin the siege of Lilybaeum.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "July/September", - "second": "", + "first": "By place", + "second": "Carthage", }, - "date": "1862/09/17", - "description": " American Civil War ampndash", + "date": "-247", + "description": "By this stage in the Punic War, Carthage has lost to Rome all its Sicilian possessions except Lilybaeum (now Marsala) and Drepanum (now Trapani). Hamilcar Barca takes over the chief command of the Carthaginian forces in Sicily at a time when the island is almost completely in the hands of the Romans. Landing on the north-west of the island with a small mercenary force, he seizes a strong position on Mount Ercte (Monte Pellegrino, near Palermo), and not only successfully defends himself against all attacks, but also carries his raids as far as the coast of southern Italy.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "January/March", - "second": "", + "first": "By place", + "second": "Seleucid Empire", }, - "date": "1879/01/11", - "description": " The Anglo-Zulu War begins.", + "date": "-246", + "description": "Berenice's brother, Ptolemy III, sets about to avenge his sister's murder by invading Syria which begins the Third Syrian War (also known as the Laodicean War). Ptolemy III's navy, perhaps with the aid of rebels in the cities, advances against Seleucus II's forces as far as Thrace, across the Hellespont, and also captures some islands off the Anatolian coast.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "February", - "second": "", + "first": "By place", + "second": "Seleucid Empire", }, - "date": "1919/02/14", - "description": " The Polish-Soviet War begins.", + "date": "-246", + "description": "Seleucus II Callinicus' mother, Laodice attempts to take control over the Seleucid Empire by insisting that Seleucus II make his younger brother, Antiochus Hierax, co-regent and give him all the Seleucid territory in Anatolia. Antiochus promptly declares independence and begins fighting a war with his brother.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "March", - "second": "", + "first": "By place", + "second": "Roman Republic", }, - "date": "1939/03/23", - "description": " The Slovak-Hungarian War begins.", + "date": "-246", + "description": "In Rome, the number of praetors is increased from one to two. The second praetor is appointed to relieve the backlog of judicial business and to give the Republic a magistrate with Imperium who can field an army in an emergency when both consuls are away fighting a war.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { "categories": Object { - "first": "February", - "second": "", + "first": "By place", + "second": "Greece", }, - "date": "2003/02/05", - "description": "War in Darfur begins.", + "date": "-244", + "description": "The war in Asia Minor and the Aegean Sea intensifies as the Achaean League allies itself to Ptolemy III of Egypt, while Seleucus II secures two allies in the Black Sea region. Ptolemy III's armies reach as far as Bactria and the borders of India in their attacks on the Seleucid Empire.", "granularity": "year", }, "id": "", - "score": 0.30140633441331044, + "score": null, }, Object { "document": Object { @@ -385,38 +385,38 @@ Object { "first": "By place", "second": "Roman Republic", }, - "date": "-156", - "description": "The first Dalmatian war begins.", + "date": "-241/03/10", + "description": " The Carthaginian fleet sent to relieve the Roman blockade of the Sicilian cities of Lilybaeum and Drepanum is totally defeated near the Aegates Islands off western Sicily by the Roman fleet led by Roman consul and commander, Gaius Lutatius Catulus. The result is a decisive Roman victory which forces an end to the protracted First Punic War, to Rome's distinct advantage.", "granularity": "year", }, "id": "", - "score": 0.24112506753064836, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "Greece", }, - "date": "-119", - "description": "The second Dalmatian war begins.", + "date": "-241", + "description": "The Eurypontid King of Sparta, Agis IV, is called away from Sparta when Aratus of Sicyon, temporarily Sparta's ally, requests Agis' aid in his war against the Aetolians. Upon his return, Agis finds that his supporters are discontented with the rule of his uncle, Agesilaus, and are disillusioned by the delay in implementing the Agis IV's reforms. As a result, the Agiad king of Sparta, Leonidas II, gains power, supported by mercenaries. Rather than engage in a war with Leonidas, Agis takes sanctuary in a temple, but is enticed out, summarily tried and then executed, along with his mother and grandmother.", "granularity": "year", }, "id": "", - "score": 0.24112506753064836, + "score": null, }, Object { "document": Object { "categories": Object { "first": "By place", - "second": "Roman Republic", + "second": "Carthage", }, - "date": "-78", - "description": "The Third Dalmatian war begins.", + "date": "-240", + "description": "Two of Carthage's mercenary commanders ampampndash Spendius and Mathos ampampndash convince the Libyan conscripts in the mercenary army, that is currently occupying the Carthaginian city of Tunis, to accept their leadership. They persuade the native Libyans that Carthage will take revenge against them for their part in the conflict once the foreign mercenaries are paid and sent home. They then convince the combined mercenary armies to revolt against Carthage and convince the various native Libyan towns and cities to back the revolt. Spendius and Mathos then take the Carthaginian commander Gesco as a hostage. What has started as an argument over pay owed to soldiers by the Carthaginian Government, explodes into a full-scale revolution, known as the Mercenary War.", "granularity": "year", }, "id": "", - "score": 0.24112506753064836, + "score": null, }, ], }