本文整理匯總了Java中com.google.common.hash.Hashing類的典型用法代碼示例。如果您正苦於以下問題:Java Hashing類的具體用法?Java Hashing怎麽用?Java Hashing使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Hashing類屬於com.google.common.hash包,在下文中一共展示了Hashing類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: genSignature
import com.google.common.hash.Hashing; //導入依賴的package包/類
public static String genSignature(HttpServletRequestEx requestEx) {
Hasher hasher = Hashing.sha256().newHasher();
hasher.putString(requestEx.getRequestURI(), StandardCharsets.UTF_8);
for (String paramName : paramNames) {
String paramValue = requestEx.getHeader(paramName);
if (paramValue != null) {
hasher.putString(paramName, StandardCharsets.UTF_8);
hasher.putString(paramValue, StandardCharsets.UTF_8);
System.out.printf("%s %s\n", paramName, paramValue);
}
}
byte[] bytes = requestEx.getBodyBytes();
if (bytes != null) {
hasher.putBytes(bytes, 0, requestEx.getBodyBytesLength());
}
return hasher.hash().toString();
}
示例2: shouldReload
import com.google.common.hash.Hashing; //導入依賴的package包/類
public boolean shouldReload() {
if(context == null) return true;
if(!configuration.autoReload()) return false;
if(context.loadedFiles().isEmpty()) return configuration.reloadWhenError();
try {
for(Map.Entry<Path, HashCode> loaded : context.loadedFiles().entrySet()) {
HashCode latest = Files.hash(loaded.getKey().toFile(), Hashing.sha256());
if(!latest.equals(loaded.getValue())) return true;
}
return false;
} catch (IOException e) {
return true;
}
}
示例3: choose
import com.google.common.hash.Hashing; //導入依賴的package包/類
@Override
public Server choose(Object key) {
//使用一致性哈希進行分發
//TODO 此處使用了GUAVA中簡單的一致性哈希算法選擇服務,但這裏存在性能缺陷:當reachableServers中間某一個服務節點失效了
//那麽後續節點的一致性哈希結果將會不匹配,後續需要使用更完善的哈希環 加上 虛擬節點 的形式解決本問題
List<Server> reachableServers = getLoadBalancer().getReachableServers();
if(reachableServers != null && reachableServers.size() != 0){
int serverSeq = Hashing.consistentHash(Thread.currentThread().getId(), reachableServers.size());
return reachableServers.get(serverSeq);
} else {
return super.choose(key);
}
}
示例4: isFullDuplicate
import com.google.common.hash.Hashing; //導入依賴的package包/類
public boolean isFullDuplicate(IHttpRequestResponse messageInfo) {
PrintWriter stdout = new PrintWriter(callbacks.getStdout(), true);
IResponseInfo respInfo = helpers.analyzeResponse(messageInfo.getResponse());
if (dubBloomFilter == null) return false;
HashFunction m_hash = Hashing.murmur3_32();
if (helpers.bytesToString(messageInfo.getResponse()).length() > respInfo.getBodyOffset()) {
String body = helpers.bytesToString(messageInfo.getResponse()).substring(respInfo.getBodyOffset());
/* full-dub detection */
String dedupHashValue = m_hash.hashBytes(helpers.stringToBytes(body)).toString();
if (dubBloomFilter.mightContain(dedupHashValue)) {
return true;
}
dubBloomFilter.put(dedupHashValue);
}
return false;
}
示例5: makeSign
import com.google.common.hash.Hashing; //導入依賴的package包/類
private static String makeSign(long userId, long sid, long expireTime) {
StringBuilder stringBuilder = new StringBuilder();
long type = (userId + sid + expireTime) % 3;
if (type == 0) {
stringBuilder.append(expireTime);
stringBuilder.append(userId);
stringBuilder.append(sid);
stringBuilder.append(type);
} else if (type == 1) {
stringBuilder.append(userId);
stringBuilder.append(expireTime);
stringBuilder.append(sid);
stringBuilder.append(type);
} else {
stringBuilder.append(sid);
stringBuilder.append(expireTime);
stringBuilder.append(userId);
stringBuilder.append(type);
}
return Hashing.sha256().hashBytes(stringBuilder.toString().getBytes()).toString();
}
示例6: calcBitPositions
import com.google.common.hash.Hashing; //導入依賴的package包/類
/**
* Calculate bit positions of {@code str}.
* <p>
* See "Less Hashing, Same Performance: Building a Better Bloom Filter" by Adam Kirsch and Michael
* Mitzenmacher.
* </p>
*
* @param str
* @return
*/
public int[] calcBitPositions(String str) {
int[] bitPositions = new int[this.k];
long hash64 = Hashing.murmur3_128().hashString(str, UTF_8).asLong();
int hash1 = (int) hash64;
int hash2 = (int) (hash64 >>> 32);
for (int i = 1; i <= this.k; i++) {
int combinedHash = hash1 + (i * hash2);
// Flip all the bits if it's negative (guaranteed positive number)
if (combinedHash < 0) {
combinedHash = ~combinedHash;
}
bitPositions[i - 1] = combinedHash % this.m;
}
return bitPositions;
}
示例7: testImageTransform
import com.google.common.hash.Hashing; //導入依賴的package包/類
@Test
public void testImageTransform() throws Exception {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String origPath = loader.getResource("com/filestack/sample_image.jpg").getPath();
File origFile = new File(origPath);
FileLink fileLink = client.upload(origPath, false);
HANDLES.add(fileLink.getHandle());
ImageTransform transform = fileLink.imageTransform();
transform.addTask(new CropTask(0, 0, 500, 500));
String cropPath = loader.getResource("com/filestack/sample_image_cropped.jpg").getPath();
File cropFile = new File(cropPath);
String correct = Files.asByteSource(cropFile).hash(Hashing.sha256()).toString();
byte[] bytes = transform.getContent().bytes();
String output = Hashing.sha256().hashBytes(bytes).toString();
Assert.assertEquals(correct, output);
}
示例8: processRegions
import com.google.common.hash.Hashing; //導入依賴的package包/類
private void processRegions(File file, RegionProcessor regionProcessor, String randomAccessFileMode, FileChannel.MapMode mapMode) throws IOException {
try (
RandomAccessFile randomAccessFile = new RandomAccessFile(file.getAbsolutePath().toFile(), randomAccessFileMode);
FileChannel channel = randomAccessFile.getChannel()
) {
RegionCalculator.calculateForSize(file, file.getSize());
for (Region region : file.getRegions().values()) {
Hasher hasher = Hashing.sha256().newHasher();
MappedByteBuffer mappedByteBuffer = channel.map(mapMode, region.getOffset(), region.getSize());
int sum = regionProcessor.processRegion(region, hasher, mappedByteBuffer);
region.setQuickDigest(sum);
byte[] slowDigest = hasher.hash().asBytes();
region.setSlowDigest(slowDigest);
clientMessageHandler.submitClientRegionMessage(clientId, file, region.getOffset(), region.getSize(), sum, slowDigest);
}
}
}
示例9: getJarFilePrefix
import com.google.common.hash.Hashing; //導入依賴的package包/類
/**
* Files exported from jars are exported into a certain folder so that we can rebuild them
* when the related jar file changes.
*/
@NonNull
private static String getJarFilePrefix(@NonNull File inputFile) {
// get the filename
String name = inputFile.getName();
// remove the extension
int pos = name.lastIndexOf('.');
if (pos != -1) {
name = name.substring(0, pos);
}
// add a hash of the original file path.
String input = inputFile.getAbsolutePath();
HashFunction hashFunction = Hashing.sha1();
HashCode hashCode = hashFunction.hashString(input, Charsets.UTF_16LE);
return name + "-" + hashCode.toString();
}
示例10: put
import com.google.common.hash.Hashing; //導入依賴的package包/類
public boolean put(String url) {
byte[] bytes = Hashing.murmur3_128().hashObject(url, funnel).asBytes();
long hash1 = this.lowerEight(bytes);
long hash2 = this.upperEight(bytes);
boolean bitsChanged = false;
long combinedHash = hash1;
try {
for (int i = 0; i < numHashFunctions; ++i) {
/* mask combinedHash with 0x7FFFFFFFFFFFFFFF */
bitsChanged |= bits.set((combinedHash & 9223372036854775807L) % bitsSize);
combinedHash += hash2;
}
} catch (IOException e) {
e.printStackTrace();
}
return bitsChanged;
}
示例11: mightContain
import com.google.common.hash.Hashing; //導入依賴的package包/類
public boolean mightContain(String url) throws IOException {
byte[] bytes = Hashing.murmur3_128().hashObject(url, funnel).asBytes();
long hash1 = this.lowerEight(bytes);
long hash2 = this.upperEight(bytes);
long combinedHash = hash1;
for (int i = 0; i < numHashFunctions; ++i) {
if (!bits.get((combinedHash & 9223372036854775807L) % bitsSize)) {
return false;
}
combinedHash += hash2;
}
return true;
}
示例12: process
import com.google.common.hash.Hashing; //導入依賴的package包/類
@Override
public void process(ResultItems resultItems, Task task) {
SpiderInfo spiderInfo = resultItems.get("spiderInfo");
Webpage webpage = convertResultItems2Webpage(resultItems);
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
.setTypes(TYPE_NAME)
.setQuery(QueryBuilders.matchQuery("url", webpage.getUrl()));
SearchResponse response = searchRequestBuilder.execute().actionGet();
if (response.getHits().totalHits() == 0) {
try {
client.prepareIndex(INDEX_NAME, TYPE_NAME)
.setId(Hashing.md5().hashString(webpage.getUrl(), Charset.forName("utf-8")).toString())
.setSource(gson.toJson(webpage))
.get();
} catch (Exception e) {
LOG.error("索引 Webpage 出錯," + e.getLocalizedMessage());
}
}
}
示例13: add
import com.google.common.hash.Hashing; //導入依賴的package包/類
/**
* add
*
* @param element
* @return
*/
public boolean add(CharSequence element) {
if (element == null) {
return false;
}
initCharset();
BloomFilter<CharSequence> bf = this.bloomfilters.get(0);
if (getBloomfilterBucketLength() > 1) {
byte[] datas = element.toString().getBytes(this.charset);
int bfIndex = Math.abs(Hashing.murmur3_128().hashBytes(datas).asInt()) % getBloomfilterBucketLength();
bf = this.bloomfilters.get(bfIndex);
}
synchronized (bf) {
bf.put(element);
numberOfAddedElements++;
return true;
}
}
示例14: convertToWebpage
import com.google.common.hash.Hashing; //導入依賴的package包/類
/**
* 將Webmagic中的ResultItems轉換為Webpage
* @param items
* @return
*/
public static Webpage convertToWebpage(ResultItems items) {
if(items.toString().indexOf("ResultItems{fields={}") > 0) {
return null;
}
Webpage page = new Webpage().setContent(items.get("content")).setTitle(items.get("title"))
.setUrl(items.get("url")).setDomain(items.get("domain")).setSpiderInfoId(items.get("spiderInfoId"))
.setGathertime(items.get("gathertime")).setSpiderUUID(items.get("spiderUUID"))
.setKeywords(items.get("keywords")).setSummary(items.get("summary"))
.setNamedEntity(items.get("namedEntity")).setPublishTime(items.get("publishTime"))
.setCategory(items.get("category")).setRawHTML(items.get("rawHTML"))
.setDynamicFields(items.get(DYNAMIC_FIELD)).setStaticFields(items.get("staticField"))
.setAttachmentList(items.get("attachmentList")).setImageList(items.get("imageList"))
.setProcessTime(items.get("processTime"));
return page.setId(Hashing.sha256().hashString(page.getUrl(), Charset.forName("utf-8")).toString());
}
示例15: process
import com.google.common.hash.Hashing; //導入依賴的package包/類
@Override
public void process(ResultItems resultItems, Task task) {
SpiderInfo info = resultItems.get("spiderInfo");
Webpage page = convertToWebpage(resultItems);
/*
* guava22.0不再對MD5()提供支持,
* 如果想更安全,使用sha256(),
* 如果想更快,使用goodFastHash()
* */
try {
client.prepareIndex(INDEX_NAME, TYPE_NAME)
.setId(Hashing.sha256().hashString(page.getUrl(), Charset.forName("utf-8")).toString())
.setSource(GSON.toJson(page), XContentType.JSON)
.get();
} catch(Exception e) {
LOG.error("索引Webpage出錯, 由於 " + e.getLocalizedMessage());
}
}