The Structure of Apache Lucene
Join the DZone community and get the full member experience.Join For Free
the inestimably noble apache software foundation produces many of the blockbuster products (ant, couchdb, hadoop, jmeter, maven, openoffice, subversion, etc.) that help build our digital universe. one perhaps less well-known gem is lucene , which, " ... provides java-based indexing and search technology, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities." despite its shying from headlines, lucene forms a quiet but integral component of many apache (and third-party) projects.
let us take a look at the structure the underlies this wonderful and highly successful product.
before we begin, the usual four caveats.
- being a syntactic structural analysis, this review cares little for either program semantics or, no matter the exquisiteness, delivered user-experience .
- structure merits investigation in and of itself because it governs the predictability of the potential cost of change. poorly-structured systems exhibit excessive interconnectedness in which ripple effects drastically blunt the precision of change-cost estimations. well-structured systems may not necessarily be cheaper to maintain and upgrade but they generally spring fewer nasty surprises.
- this analysis depicts a package-structure as a spoiklin diagram in which a circle represents a package, a straight line represents a dependency from a package drawn above to one drawn below and a curved line represents a dependency from a package drawn below to one drawn above. the colour of a package indicates the relative number of transitive package dependencies of which it partakes: the redder, the more transitive dependencies.
- no diagram can prove structural value or cost. high-level analyses only ever prompt questions whose answers lie buried deep under geological layers of code.
and so, to business ...
figure 1: package structure of lucene version 1.4.3.
figure 1 shows one of the earliest version of lucene still archived, version 1.4.3. recall that a simple test of structure suggests the selection of a package at random and asks, "if this package changes, which other packages will it most likely impact?"
take index for example. clearly both queryparser and spans depend on it and hence might be impacted by any change to index , and that curved line shows that search depends on it too. this ease of dependency identification characterizes the entire figure, making this a well-structured design.
bravo, lucene, you're off to a good start.
figure 2: package structure of lucene version 2.0.
figure 2 shows version 2.0 (note that we shall not investigate every release, but evenly spaced milestones along the entire release path), and the simplicity of interconnectedness continues. despite the number of methods rising from version 1.4.3's 1,637 to version 2.0's 2,085, the number of packages has fallen from 11 to 10. this has prompted a slight fall in potent coupling efficiency - from 41% to 37% - but nonetheless good design principles clearly master this system.
figure 3: package structure of lucene version 2.4.
presented above in figure 3, version 2.4 - although far from an obviously bad structure - shows the first signs of distress.
true, many of the packages stand in clear relationship to their neighbours; but now some do not. in particular, search and index seem to have become embroiled in one another's affairs.
this mild degradation of structure, however, belies the tumultuous changes that have taken place behind the scenes. where version 2.0 had 2,085 methods, version 2.4 has more than doubled in size to 4,176 methods. and where version 2.0 had just 9,767 transitive dependencies, version 2.4 sags beneath a burdensome 48,370 transitive dependencies. some structural crack has opened deep down on method-level to trigger this five-fold increase in dependencies, a crack which lucene's programmers never detect or seal, and which plagues later revisions, as we shall see.
not only has the number of dependencies dramatically increased, but the depth of the program - the average length of its transitive dependencies - has increased, too, jumping from version 2.0's 7 to version 2.4's 8.6, not only laying more tracks over which ripple effects may trundle, but extending those tracks to shunt spurious impacts further afield.
still, this structure presents no unsolvable problems. focused design could reinstate the simplicity enjoyed by the earlier versions.
figure 4: package structure of lucene version 3.0.
alas, version 3.0 - shown above in figure 4 - seems to continue, ever so slightly, the downward trend. again, figure 4 does not present an irredeemable structure: we can tease apart the packages to see how most connect with one another. the task, however, has become harder.
both analysis and spans have been sucked into the tangle sparked by search and index . predicting the impact of changing any of these four packages would now seem to require an automatic investigation all others.
contributing to this increase in interconnectedness is the addition of 800 methods to this revision; and even though the number of transitive dependencies has admirably fallen to 46,917, nevertheless the average length has again risen, this time to 9.3.
is the system's structure beyond hope? not at all: many of the packages enjoy clear dependency relations with their colleagues. just around the corner, however, lies version 3.5 and a surge of transitive dependencies which, though not immediately fatal, proves a disease resistant to all medicines.
...and the fall...
figure 5: package structure of lucene version 3.5.
on positive note, version 3.5, shown in figure 5 above, introduces an extra three packages - bringing the total to 18 - in an attempt to distribute and separate the system's functionality. the generous might also offer that, although the package structure has clearly decayed again from the previous revision's, that decay remains somewhat localized: bad-boys analysis , spans , search and index continue to terrorize the rest of lucene-town's largely well-behaved population.
but the generosity ends there.
for despite adding only another 1,800 methods, the number of revision 3.5's transitive dependencies has soared to 109,357, and the average length of those dependencies hits 11 methods long, a sad maximum for the entire evolution. given this phenomenal rise in structural complexity, we wonder how the package design seems as good as it does - and indeed any such harmony proves short-lived, as the strain finally destroys all semblance of control in the next revision milestone.
figure 5: package structure of lucene version 4.0.
revision 4.0, shown in figure 5, adds 1,600 methods to the previous revision, bringing the total to 8,474 and raising the number of transitive dependencies relatively modestly to 116,211, but as can be seen from the figure, something terrible has happened.
the burgeoning interconnectedness of the previous revisions has suddenly systematized, causing the structure to implode into the dreaded ball of tangled dependencies that makes code-impact prediction wildly unreliable.
true, this revision adds another two packages - raising the potential coupling efficiency to 43% - and reduces (slightly) transitive dependency length to 10.4, but the sheer effort of controlling this vast number of transitive dependencies has simply broken the system. it will not recover.
figure 6: package structure of lucene version 4.5.
in revision 4.5, shown in figure 6, some heroic action has reduced the number of transitive dependencies to 106,242 while still raising the number of methods to 9,562, and perhaps some packages have managed to distance themselves from the ravenous black hole spinning manically at the system's core. but the work is too little, too late.
figure 7: package structure of lucene version 5.0.
revision 5.0, shown in figure 7, attempts to tame the beast by removing 200 methods, yet this curiously results in again raising the number of transitive dependencies to 113,556.
does revision 5.0 look as bad as revision 4.5? well, perhaps not. something looks a little cleaner. we should not, however, allow this to blind us to the grand dis-structure on display in figure 7: this system weeps in pain. predicting the costs of changing any of those central packages has become foolhardy.
to understand what happened to destroy this system's initial structural integrity, we must examine revision 3.5. again, this may not look like the worst structure, but this revision heralded the changes that lead to eventual ruin.
the main change was not just one of size: bigger systems need not necessarily fall to poor structure. revision 3.5 increased the number of methods by 35% - but revision 2.4 increased the number of methods by more than 100% without wrecking the overall organization.
instead, the primary culprits were the number of transitive dependencies and their distribution across the system.
the sheer number of new transitive dependencies introduced in revision 3.5 is astounding, rising from 46,917 to 109,357. this brought the dependency-to-method ratio to an artery-hardening 16.
figure 8: comparing lucene's transitive-dependencies-per-method ratio.
the dependency-to-method ratio had already been too high. in previous revisions, however, these transitive dependencies largely confined themselves to just one or two packages. in revision 3.0, 95% of all transitive method dependencies either terminated in their originating package or in a package just one dependency away. this gave hope that changes might in some sense limit themselves to a region close to the origin point, leaving few changes to spill out all over the system and to defy cost prediction.
revision 3.5, however, saw that figure plummet to just 75%. this means that 25% of all revision 3.5's transitive dependencies spill into three or more packages. combining both these factors reveals that more than 33,000 dependencies lie in wait to catapult changes far from their origins. more than anything else, this dooms the product to further structural decay.
figure 9: percentage of lucene transitive dependencies spanning fewer than 3 packages.
this, then, concludes the examination of the lucene's package-level structure. should we delve below package level? should we comb through individual packages to examine various class constellations? no. according to the blighttown corollary , if the package-level structure is bad, we should not hope to find diamonds below. so we won't.
let us attempt an objective scoring of lucene's structure (its final revision examined here, 5.0).
we shall use the average of four factors. the first measures lucene's attempt to limit the number of dependencies that are possible to form. the second and third attempt to capture transitive dependency length, and the fourth attempts to capture the number of transitive dependencies. of course, large systems will always have, say, more dependencies than small systems, so we cannot say that system a is more well-structured than system b simply because it has fewer dependencies. instead, we must derive measurements that can be fairly compared by either normalizing for size or making the measurements in some sense self-referential.
first, we shall measure its absolute ideal efficiency: this analyzes the structure's potential coupling , and basically asks how many methods are encapsulated away from other methods, and thus how many dependencies could conceivably be created. if every method were put in one class, then every method would be visible to every other, and so the efficiency would be 0%. the value rises the more methods are made private and put in separate package-private classes, thus increasingly encapsulating methods from one another.
lucene scores 44%, indicating that it has at least attempted to encapsulate its functionality, but much more could be done.
second, we shall measure the length of lucene's transitive dependencies in a form which allows fair comparisons between programs. for this, we shall use a cdf graph showing how long lucene's transitive method dependencies are as a percentage of its longest transitive dependency.
figure 10: lucene's transitive dependency cdf.
in figure 10 above, we see that half of lucene's transitive dependencies are shorter than 45% of the length of its longest transitive dependency. this is bad. a system's resistance to ripple effects relies on most of its dependencies being short; half of junit 's transitive dependencies, for example, are only 30% of the length of its longest dependency.
as we require a figure that rises with improved structure, we shall use 100 minus this figure, so lucene will score 100 - 45 = 55, a value which should be closer to 70.
the third factor we shall use has already been discussed: the percentage of methods that span two packages or fewer, a figure found to be 75.5%. this sounds high, but with modern structuring techniques there is little reason for this value to be less than 90%.
finally, we need a factor that measures how many dependencies wriggle through a system, as the fewer the number of dependencies, the better. to normalize for size, we would like to measure the number of method-dependencies per method. here we must unfortunately estimate an industry lowest-possible score. some research suggests that 25 seems an appropriate figure: if system contains more than 25 dependencies per method then that system's structure is so bad that all other all other metrics lose their importance.
we saw earlier that lucene has a huge 12 dependencies per method; so the figure we shall use is 25-12 = 13, expressed as a percentage of 25, giving 52%. as figure 8 presented, other systems reach as low as 6 dependencies per method, a figure which yields over 70% for this metric.
this gives lucene a final score of 226.5/400 points, or 57%. with firm structural principles, modern programs easily score above 80%, so this is a poor score indicating, alas, a poor structure. lucene finds itself second to last on the leader-board of systems analyzed so far in this series.
|absolute potential couple efficiency %||44|
|100 - (% length of longest dependency that half the system is shorter than)||55|
|% method transitive dependencies spanning 2 packages or fewer||75.5|
|((25 - (number of transitive method dependencies per method) / 25) as % of 25||52|
table 1: lucene 5.0's structural evaluation.
table 2: lucene's place on the leader-board.
could do better.
Opinions expressed by DZone contributors are their own.