Example usage for com.google.common.hash BloomFilter mightContain

List of usage examples for com.google.common.hash BloomFilter mightContain

Introduction

In this page you can find the example usage for com.google.common.hash BloomFilter mightContain.

Prototype

@CheckReturnValue
public boolean mightContain(T object) 

Source Link

Document

Returns true if the element might have been put in this Bloom filter, false if this is definitely not the case.

Usage

From source file:org.apache.niolex.common.guava.GuavaHashing.java

/**
 * @param args/*from   w  w  w.java 2  s . com*/
 */
public static void main(String[] args) {
    // Common Hash
    HashFunction hf = Hashing.goodFastHash(32);
    HashCode code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE);
    System.out.println("Code1 => " + code.asInt());
    code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1985), PersonFunnel.INSTANCE);
    System.out.println("Code2 => " + code.asInt());
    // Consistent Hashing
    HashFunction hf2 = Hashing.goodFastHash(64);
    code = hf2.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE);
    System.out.println("Code3 => " + code.asLong());
    long hash = code.asLong();
    int bucket = Hashing.consistentHash(code, 100);
    System.out.println("Bucket1 => " + bucket);
    bucket = Hashing.consistentHash(hash, 101);
    System.out.println("Bucket2 => " + bucket);
    for (int i = 0; i < 10; ++i) {
        System.out.println("HashTo5 => " + Hashing.consistentHash(i, 5));
        System.out.println("HashTo6 => " + Hashing.consistentHash(i, 6));
    }
    // BloomFilter
    BloomFilter<Person> friends = BloomFilter.create(PersonFunnel.INSTANCE, 500, 0.02);
    for (int i = 0; i < 500; ++i) {
        friends.put(new Person(i, "Jiyun", "Xie", 1984 + i));
    }
    int k = 0;
    for (int i = 0; i < 500; ++i) {
        if (!friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) {
            System.out.println("Error1 => " + i);
            ++k;
        }
    }
    System.out.println("fnp => (should be 0)" + ((double) k / 500));
    k = 0;
    for (int i = 0; i < 1000; i += 2) {
        if (friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) {
            //System.out.println("Error2 => " + i);
            ++k;
        }
    }
    System.out.println("fpp => " + ((double) k / 500));
}

From source file:com.jordanwilliams.heftydb.test.performance.table.file.TableBloomFilterPerformance.java

public static void main(String[] args) throws Exception {
    TestFileHelper.createTestDirectory();
    KeyValueGenerator keyValueGenerator = new KeyValueGenerator();
    Value value = new Value(keyValueGenerator.testValue(100));

    System.out.println("Writing bloom filter");

    Paths paths = ConfigGenerator.testPaths();
    TableBloomFilterWriter filterWriter = TableBloomFilterWriter.open(1, paths, RECORD_COUNT);
    BloomFilter<Key> guavaFilter = BloomFilter.create(new Funnel<Key>() {
        @Override//from w w w.  ja  v  a 2  s.co m
        public void funnel(Key key, PrimitiveSink primitiveSink) {
            primitiveSink.putBytes(key.data().array());
        }
    }, RECORD_COUNT, 0.01);

    for (int i = 0; i < RECORD_COUNT; i++) {
        value.data().rewind();
        filterWriter.write(new Key(ByteBuffers.fromString(i + ""), i));
        guavaFilter.put(new Key(ByteBuffers.fromString(i + ""), i));
    }

    filterWriter.finish();

    System.out.println("Reading bloom filter");

    TableBloomFilter tableBloomFilter = TableBloomFilter.read(1, paths);

    double hits = 0;
    double misses = 0;

    double ghits = 0;
    double gmisses = 0;

    for (int i = RECORD_COUNT * 2; i > RECORD_COUNT; i--) {
        if (tableBloomFilter.mightContain(new Key(ByteBuffers.fromString(i + ""), i))) {
            hits++;
        } else {
            misses++;
        }

        if (guavaFilter.mightContain(new Key(ByteBuffers.fromString(i + ""), i))) {
            ghits++;
        } else {
            gmisses++;
        }
    }

    System.out.println("False positive rate: " + hits / (hits + misses));
    System.out.println("Guava positive rate: " + ghits / (ghits + gmisses));

    TestFileHelper.cleanUpTestFiles();
}

From source file:graphene.ingest.batchoptimizers.BasicBatchOptimizer.java

/**
 * Checks to see if the relationship is unique, and if it is, adds it to the
 * bloom filter.//from www  .j a  va2s. c om
 * 
 * @param from
 * @param to
 * @param rel
 * @return
 */
public boolean is_unique_rel(BloomFilter<String> bf, Long from, String rel, Long to) {
    String key = "" + from + to + rel;
    if (bf.mightContain(key)) {
        return false;
    } else {
        bf.put(key);
        return true;
    }
}

From source file:cc.gospy.core.util.bloomfilter.ScalableBloomFilter.java

public boolean mightContain(Task task) {
    for (BloomFilter<Task> filter : bloomFilters) {
        if (filter.mightContain(task)) {
            return true;
        }//from  w w w .j ava 2  s  . c o m
    }
    return false;
}

From source file:etri.sdn.controller.module.netfailover.LDUpdateProcessor.java

void removeOldRoutesOnLink(long src, OFPort srcPort, long dst, OFPort dstPort) {
    NodePortTuple srcNpt = new NodePortTuple(src, srcPort);
    NodePortTuple dstNpt = new NodePortTuple(dst, dstPort);

    Set<Long> asws = getAccessSwitches();

    // now, for every pair of access switches,
    for (long s : asws) {
        for (long d : asws) {
            // we don't do the removal for the reverse direction.
            // can this be some source of evil?
            if (s <= d)
                continue;

            Route r = this.routingService.getOldRoute(s, d);
            if (r == null)
                // topology is not ready right now.
                continue;

            BloomFilter<NodePortTuple> bf = r.getBloomFilter();
            if (bf.mightContain(srcNpt) || bf.mightContain(dstNpt)) {
                removeRouteFromNetwork(r);
            }/*from  w w  w.j  a  v a2 s  .  c om*/
        }
    }
}

From source file:etri.sdn.controller.module.netfailover.LDUpdateProcessor.java

void removeRoutesOnAnyLink(long src, OFPort srcPort, long dst, OFPort dstPort) {
    NodePortTuple srcNpt = new NodePortTuple(src, srcPort);
    NodePortTuple dstNpt = new NodePortTuple(dst, dstPort);

    Set<Long> asws = getAccessSwitches();

    // now, for every pair of access switches,
    for (long s : asws) {
        for (long d : asws) {
            // we don't do the removal for the reverse direction.
            // can this be some source of evil?
            if (s <= d)
                continue;

            Route r = this.routingService.getRoute(s, d);
            if (r == null)
                // topology is not ready right now.
                continue;

            BloomFilter<NodePortTuple> bf = r.getBloomFilter();
            if (bf.mightContain(srcNpt) || bf.mightContain(dstNpt)) {
                removeAnyRouteFromNetwork(r);
            }/*ww  w  . j  av  a2  s.  co m*/
        }
    }
}

From source file:edu.ucla.discoverfriend.DeviceDetailFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mContentView = inflater.inflate(R.layout.device_detail, null);
    mContentView.findViewById(R.id.btn_connect).setOnClickListener(new View.OnClickListener() {

        @Override//from w  ww . java2  s  . c om
        public void onClick(View v) {

            WifiP2pConfig config = new WifiP2pConfig();
            config.deviceAddress = device.deviceAddress;
            config.wps.setup = WpsInfo.PBC;
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel",
                    "Connecting to :" + device.deviceAddress, true, true
            //                        new DialogInterface.OnCancelListener() {
            //
            //                            @Override
            //                            public void onCancel(DialogInterface dialog) {
            //                                ((DeviceActionListener) getActivity()).cancelDisconnect();
            //                            }
            //                        }
            );
            ((DeviceActionListener) getActivity()).connect(config);

        }
    });

    mContentView.findViewById(R.id.btn_disconnect).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ((DeviceActionListener) getActivity()).disconnect();
        }
    });

    mContentView.findViewById(R.id.btn_start_server).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Server feature that prepares the CustomNetworkPacket
            // intent to send to the client.
            TextView statusText = (TextView) mContentView.findViewById(R.id.status_text);
            statusText.setText("Sending (to device): CNP");
            Log.d(MainActivity.TAG, "Intent----------- ");

            Intent serviceIntent = new Intent(getActivity(), DataTransferService.class);
            Bundle extras = new Bundle();
            extras.putSerializable(DataTransferService.EXTRAS_DATA, ((MainActivity) getActivity()).getCnp());
            serviceIntent.setAction(DataTransferService.ACTION_SEND_DATA);
            serviceIntent.putExtras(extras);
            getActivity().startService(serviceIntent);
        }
    });

    mContentView.findViewById(R.id.btn_yes).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (receivedCNP != null) {
                // Extract the initiator ID from BFc+ by
                // exhaustively trying all entries in their friends
                // list.
                FacebookFragment fragment = (FacebookFragment) getFragmentManager()
                        .findFragmentById(R.id.frag_facebook);
                String ids[] = fragment.getFriendId();
                BloomFilter<String> bf = receivedCNP.getBf();
                BloomFilter<String> bfc = receivedCNP.getBfc();
                for (int i = 0; i < ids.length; i++) {
                    if (bfc.mightContain(ids[i]) && !bf.mightContain(ids[i])) {
                        // The current friend is the initiator.
                    }
                }
            }
        }
    });

    mContentView.findViewById(R.id.btn_no).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Don't do anything and reset view.
        }
    });

    return mContentView;
}