/
VanillaNetworkTypeaheadInitializer.java
106 lines (91 loc) · 3.9 KB
/
VanillaNetworkTypeaheadInitializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (c) 2011 LinkedIn, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package cleo.search.tool;
import cleo.search.Element;
import cleo.search.Indexer;
import cleo.search.connection.ConnectionFilter;
import cleo.search.connection.TransitivePartitionConnectionFilter;
import cleo.search.filter.BloomFilter;
import cleo.search.filter.FnvBloomFilter;
import cleo.search.selector.PrefixSelectorFactory;
import cleo.search.selector.SelectorFactory;
import cleo.search.store.ArrayStoreConnections;
import cleo.search.store.ArrayStoreElement;
import cleo.search.store.MemoryArrayStoreElement;
import cleo.search.store.StoreFactory;
import cleo.search.typeahead.VanillaNetworkTypeahead;
import cleo.search.typeahead.NetworkTypeaheadConfig;
import cleo.search.typeahead.Typeahead;
import cleo.search.util.Range;
import krati.core.segment.WriteBufferSegmentFactory;
/**
* VanillaNetworkTypeaheadInitializer
*
* @author jwu
* @since 02/08, 2011
*
* <p>
* 02/06, 2012 - Optimize by loading connection store before element store. <br/>
*/
public class VanillaNetworkTypeaheadInitializer<E extends Element> implements TypeaheadInitializer<E>, IndexerInitializer<E> {
private final VanillaNetworkTypeahead<E> networkTypeahead;
public VanillaNetworkTypeaheadInitializer(Config<E> config) throws Exception {
this.networkTypeahead = createTypeahead(config);
}
public VanillaNetworkTypeaheadInitializer(NetworkTypeaheadConfig<E> config) throws Exception {
this.networkTypeahead = createTypeahead(config);
}
@Override
public Typeahead<E> getTypeahead() {
return networkTypeahead;
}
@Override
public Indexer<E> getIndexer() {
return networkTypeahead;
}
protected VanillaNetworkTypeahead<E> createTypeahead(NetworkTypeaheadConfig<E> config) throws Exception {
// create connectionsStore
ArrayStoreConnections connectionsStore = StoreFactory.createArrayStoreConnections(
config.getConnectionsStoreDir(),
config.getConnectionsStoreCapacity(),
config.getConnectionsStoreSegmentMB());
// create elementStore
ArrayStoreElement<E> elementStore = StoreFactory.createElementStorePartition(
config.getElementStoreDir(),
config.getElementStoreIndexStart(),
config.getElementStoreCapacity(),
new WriteBufferSegmentFactory(config.getElementStoreSegmentMB()),
config.getElementStoreSegmentMB(),
config.getElementSerializer());
// load elementStore in memory
if(config.isElementStoreCached()) {
elementStore = new MemoryArrayStoreElement<E>(elementStore);
}
// create selectorFactory
SelectorFactory<E> selectorFactory = config.getSelectorFactory();
if(selectorFactory == null) selectorFactory = new PrefixSelectorFactory<E>();
// create bloomFilter
BloomFilter<Integer> bloomFilter = new FnvBloomFilter(config.getFilterPrefixLength());
// create connectionFilter
ConnectionFilter connectionFilter = config.getConnectionFilter();
if(connectionFilter == null) {
connectionFilter = new TransitivePartitionConnectionFilter(new Range(config.getPartitionStart(), config.getPartitionCount()));
}
// create NetworkTypeahead
return new VanillaNetworkTypeahead<E>(config.getName(), elementStore, connectionsStore, selectorFactory, bloomFilter, connectionFilter);
}
public static class Config<E extends Element> extends NetworkTypeaheadConfig<E> {}
}