2012-08-15 00:04:19 -07:00
|
|
|
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// Originally based on Chrome sources:
|
|
|
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
#include "HashStore.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsICryptoHash.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsCheckSummedOutputStream.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "zlib.h"
|
|
|
|
|
|
|
|
// Main store for SafeBrowsing protocol data. We store
|
2012-08-15 00:06:54 -07:00
|
|
|
// known add/sub chunks, prefixes and completions in memory
|
2012-08-15 00:04:19 -07:00
|
|
|
// during an update, and serialize to disk.
|
|
|
|
// We do not store the add prefixes, those are retrieved by
|
|
|
|
// decompressing the PrefixSet cache whenever we need to apply
|
|
|
|
// an update.
|
2012-08-15 00:06:54 -07:00
|
|
|
//
|
|
|
|
// byte slicing: Many of the 4-byte values stored here are strongly
|
|
|
|
// correlated in the upper bytes, and uncorrelated in the lower
|
|
|
|
// bytes. Because zlib/DEFLATE requires match lengths of at least
|
|
|
|
// 3 to achieve good compression, and we don't get those if only
|
|
|
|
// the upper 16-bits are correlated, it is worthwhile to slice 32-bit
|
|
|
|
// values into 4 1-byte slices and compress the slices individually.
|
|
|
|
// The slices corresponding to MSBs will compress very well, and the
|
|
|
|
// slice corresponding to LSB almost nothing. Because of this, we
|
|
|
|
// only apply DEFLATE to the 3 most significant bytes, and store the
|
|
|
|
// LSB uncompressed.
|
|
|
|
//
|
|
|
|
// byte sliced (numValues) data format:
|
|
|
|
// uint32 compressed-size
|
|
|
|
// compressed-size bytes zlib DEFLATE data
|
|
|
|
// 0...numValues byte MSB of 4-byte numValues data
|
|
|
|
// uint32 compressed-size
|
|
|
|
// compressed-size bytes zlib DEFLATE data
|
|
|
|
// 0...numValues byte 2nd byte of 4-byte numValues data
|
|
|
|
// uint32 compressed-size
|
|
|
|
// compressed-size bytes zlib DEFLATE data
|
|
|
|
// 0...numValues byte 3rd byte of 4-byte numValues data
|
|
|
|
// 0...numValues byte LSB of 4-byte numValues data
|
|
|
|
//
|
|
|
|
// Store data format:
|
2012-08-15 00:04:19 -07:00
|
|
|
// uint32 magic
|
|
|
|
// uint32 version
|
|
|
|
// uint32 numAddChunks
|
|
|
|
// uint32 numSubChunks
|
|
|
|
// uint32 numAddPrefixes
|
|
|
|
// uint32 numSubPrefixes
|
|
|
|
// uint32 numAddCompletes
|
|
|
|
// uint32 numSubCompletes
|
2012-08-15 00:06:54 -07:00
|
|
|
// 0...numAddChunks uint32 addChunk
|
|
|
|
// 0...numSubChunks uint32 subChunk
|
|
|
|
// byte sliced (numAddPrefixes) uint32 add chunk of AddPrefixes
|
|
|
|
// byte sliced (numSubPrefixes) uint32 add chunk of SubPrefixes
|
2012-08-15 00:08:37 -07:00
|
|
|
// byte sliced (numSubPrefixes) uint32 sub chunk of SubPrefixes
|
2012-08-15 00:06:54 -07:00
|
|
|
// byte sliced (numSubPrefixes) uint32 SubPrefixes
|
2012-08-15 00:07:23 -07:00
|
|
|
// 0...numAddCompletes 32-byte Completions + uint32 addChunk
|
|
|
|
// 0...numSubCompletes 32-byte Completions + uint32 addChunk
|
|
|
|
// + uint32 subChunk
|
2012-08-15 00:04:19 -07:00
|
|
|
// 16-byte MD5 of all preceding data
|
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
// Name of the SafeBrowsing store
|
|
|
|
#define STORE_SUFFIX ".sbstore"
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// NSPR_LOG_MODULES=UrlClassifierDbService:5
|
|
|
|
extern PRLogModuleInfo *gUrlClassifierDbServiceLog;
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4)
|
|
|
|
#else
|
|
|
|
#define LOG(args)
|
|
|
|
#define LOG_ENABLED() (false)
|
|
|
|
#endif
|
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
// Either the return was successful or we call the Reset function.
|
|
|
|
// Used while reading in the store.
|
|
|
|
#define SUCCESS_OR_RESET(res) \
|
|
|
|
do { \
|
|
|
|
nsresult __rv = res; /* Don't evaluate |res| more than once */ \
|
|
|
|
if (NS_FAILED(__rv)) { \
|
|
|
|
NS_WARNING("SafeBrowsing store corrupted or out of date."); \
|
|
|
|
Reset(); \
|
|
|
|
return __rv; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace safebrowsing {
|
|
|
|
|
|
|
|
const uint32 STORE_MAGIC = 0x1231af3b;
|
2012-08-15 00:06:54 -07:00
|
|
|
const uint32 CURRENT_VERSION = 2;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
TableUpdate::NewAddPrefix(uint32_t aAddChunk, const Prefix& aHash)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
AddPrefix *add = mAddPrefixes.AppendElement();
|
|
|
|
add->addChunk = aAddChunk;
|
|
|
|
add->prefix = aHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
TableUpdate::NewSubPrefix(uint32_t aAddChunk, const Prefix& aHash, uint32_t aSubChunk)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
SubPrefix *sub = mSubPrefixes.AppendElement();
|
|
|
|
sub->addChunk = aAddChunk;
|
|
|
|
sub->prefix = aHash;
|
|
|
|
sub->subChunk = aSubChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
TableUpdate::NewAddComplete(uint32_t aAddChunk, const Completion& aHash)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
AddComplete *add = mAddCompletes.AppendElement();
|
|
|
|
add->addChunk = aAddChunk;
|
|
|
|
add->hash.complete = aHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
TableUpdate::NewSubComplete(uint32_t aAddChunk, const Completion& aHash, uint32_t aSubChunk)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
SubComplete *sub = mSubCompletes.AppendElement();
|
|
|
|
sub->addChunk = aAddChunk;
|
|
|
|
sub->hash.complete = aHash;
|
|
|
|
sub->subChunk = aSubChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HashStore::HashStore(const nsACString& aTableName, nsIFile* aStoreDir)
|
|
|
|
: mTableName(aTableName)
|
|
|
|
, mStoreDirectory(aStoreDir)
|
|
|
|
, mInUpdate(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HashStore::~HashStore()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::Reset()
|
|
|
|
{
|
|
|
|
LOG(("HashStore resetting"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> storeFile;
|
|
|
|
nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(STORE_SUFFIX));
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = storeFile->Remove(false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::CheckChecksum(nsIFile* aStoreFile)
|
|
|
|
{
|
|
|
|
// Check for file corruption by
|
|
|
|
// comparing the stored checksum to actual checksum of data
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hash;
|
|
|
|
nsAutoCString compareHash;
|
2012-08-15 00:04:19 -07:00
|
|
|
char *data;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t read;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t fileSize;
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult rv = aStoreFile->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (fileSize < 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
rv = CalculateChecksum(hash, fileSize, true);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
compareHash.GetMutableData(&data, hash.Length());
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekIn = do_QueryInterface(mInputStream);
|
|
|
|
rv = seekIn->Seek(nsISeekableStream::NS_SEEK_SET, fileSize-hash.Length());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mInputStream->Read(data, hash.Length(), &read);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ASSERTION(read == hash.Length(), "Could not read hash bytes");
|
|
|
|
|
|
|
|
if (!hash.Equals(compareHash)) {
|
|
|
|
NS_WARNING("Safebrowing file failed checksum.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::Open()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> storeFile;
|
|
|
|
nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(".sbstore"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> origStream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(origStream), storeFile,
|
|
|
|
PR_RDONLY);
|
|
|
|
|
|
|
|
if (rv == NS_ERROR_FILE_NOT_FOUND) {
|
|
|
|
UpdateHeader();
|
|
|
|
return NS_OK;
|
2012-09-28 09:31:19 -07:00
|
|
|
} else {
|
|
|
|
SUCCESS_OR_RESET(rv);
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t fileSize;
|
2012-08-15 00:04:31 -07:00
|
|
|
rv = storeFile->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(mInputStream), origStream,
|
2012-08-15 00:04:31 -07:00
|
|
|
fileSize);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CheckChecksum(storeFile);
|
2012-09-28 09:31:19 -07:00
|
|
|
SUCCESS_OR_RESET(rv);
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
rv = ReadHeader();
|
2012-09-28 09:31:19 -07:00
|
|
|
SUCCESS_OR_RESET(rv);
|
2012-08-15 00:04:19 -07:00
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
rv = SanityCheck();
|
|
|
|
SUCCESS_OR_RESET(rv);
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
rv = ReadChunkNumbers();
|
2012-09-28 09:31:19 -07:00
|
|
|
SUCCESS_OR_RESET(rv);
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::ReadHeader()
|
|
|
|
{
|
|
|
|
if (!mInputStream) {
|
|
|
|
UpdateHeader();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mInputStream);
|
|
|
|
nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
void *buffer = &mHeader;
|
|
|
|
rv = NS_ReadInputStreamToBuffer(mInputStream,
|
|
|
|
&buffer,
|
|
|
|
sizeof(Header));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-28 09:31:19 -07:00
|
|
|
HashStore::SanityCheck()
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
if (mHeader.magic != STORE_MAGIC || mHeader.version != CURRENT_VERSION) {
|
|
|
|
NS_WARNING("Unexpected header data in the store.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-28 09:31:19 -07:00
|
|
|
HashStore::CalculateChecksum(nsAutoCString& aChecksum,
|
|
|
|
int64_t aSize,
|
|
|
|
bool aChecksumPresent)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
aChecksum.Truncate();
|
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
// Reset mInputStream to start
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mInputStream);
|
|
|
|
nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0);
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsICryptoHash> hash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Size of MD5 hash in bytes
|
|
|
|
const uint32 CHECKSUM_SIZE = 16;
|
|
|
|
|
|
|
|
rv = hash->Init(nsICryptoHash::MD5);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!aChecksumPresent) {
|
|
|
|
// Hash entire file
|
2012-09-28 09:31:19 -07:00
|
|
|
rv = hash->UpdateFromStream(mInputStream, UINT32_MAX);
|
2012-08-15 00:04:19 -07:00
|
|
|
} else {
|
|
|
|
// Hash everything but last checksum bytes
|
2012-09-28 09:31:19 -07:00
|
|
|
rv = hash->UpdateFromStream(mInputStream, fileSize-CHECKSUM_SIZE);
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = hash->Finish(false, aChecksum);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HashStore::UpdateHeader()
|
|
|
|
{
|
|
|
|
mHeader.magic = STORE_MAGIC;
|
|
|
|
mHeader.version = CURRENT_VERSION;
|
|
|
|
|
|
|
|
mHeader.numAddChunks = mAddChunks.Length();
|
|
|
|
mHeader.numSubChunks = mSubChunks.Length();
|
|
|
|
mHeader.numAddPrefixes = mAddPrefixes.Length();
|
|
|
|
mHeader.numSubPrefixes = mSubPrefixes.Length();
|
|
|
|
mHeader.numAddCompletes = mAddCompletes.Length();
|
|
|
|
mHeader.numSubCompletes = mSubCompletes.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::ReadChunkNumbers()
|
|
|
|
{
|
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mInputStream);
|
|
|
|
nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
sizeof(Header));
|
|
|
|
|
|
|
|
rv = mAddChunks.Read(mInputStream, mHeader.numAddChunks);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ASSERTION(mAddChunks.Length() == mHeader.numAddChunks, "Read the right amount of add chunks.");
|
|
|
|
|
|
|
|
rv = mSubChunks.Read(mInputStream, mHeader.numSubChunks);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ASSERTION(mSubChunks.Length() == mHeader.numSubChunks, "Read the right amount of sub chunks.");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::ReadHashes()
|
|
|
|
{
|
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mInputStream);
|
|
|
|
|
|
|
|
uint32 offset = sizeof(Header);
|
|
|
|
offset += (mHeader.numAddChunks + mHeader.numSubChunks) * sizeof(uint32);
|
|
|
|
nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, offset);
|
|
|
|
|
|
|
|
rv = ReadAddPrefixes();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ReadSubPrefixes();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ReadTArray(mInputStream, &mAddCompletes, mHeader.numAddCompletes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ReadTArray(mInputStream, &mSubCompletes, mHeader.numSubCompletes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::BeginUpdate()
|
|
|
|
{
|
2012-09-28 09:31:19 -07:00
|
|
|
// Read the rest of the store in memory.
|
|
|
|
nsresult rv = ReadHashes();
|
|
|
|
SUCCESS_OR_RESET(rv);
|
2012-08-15 00:04:19 -07:00
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
// Close input stream, won't be needed any more and
|
|
|
|
// we will rewrite ourselves.
|
2012-09-28 09:31:18 -07:00
|
|
|
if (mInputStream) {
|
|
|
|
rv = mInputStream->Close();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2012-09-28 09:31:19 -07:00
|
|
|
mInUpdate = true;
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static nsresult
|
|
|
|
Merge(ChunkSet* aStoreChunks,
|
|
|
|
nsTArray<T>* aStorePrefixes,
|
|
|
|
ChunkSet& aUpdateChunks,
|
2012-08-15 00:10:39 -07:00
|
|
|
nsTArray<T>& aUpdatePrefixes,
|
|
|
|
bool aAllowMerging = false)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
EntrySort(aUpdatePrefixes);
|
|
|
|
|
|
|
|
T* updateIter = aUpdatePrefixes.Elements();
|
|
|
|
T* updateEnd = aUpdatePrefixes.Elements() + aUpdatePrefixes.Length();
|
|
|
|
|
|
|
|
T* storeIter = aStorePrefixes->Elements();
|
|
|
|
T* storeEnd = aStorePrefixes->Elements() + aStorePrefixes->Length();
|
|
|
|
|
|
|
|
// use a separate array so we can keep the iterators valid
|
|
|
|
// if the nsTArray grows
|
|
|
|
nsTArray<T> adds;
|
|
|
|
|
|
|
|
for (; updateIter != updateEnd; updateIter++) {
|
2012-08-15 00:10:39 -07:00
|
|
|
// skip this chunk if we already have it, unless we're
|
|
|
|
// merging completions, in which case we'll always already
|
|
|
|
// have the chunk from the original prefix
|
|
|
|
if (aStoreChunks->Has(updateIter->Chunk()))
|
|
|
|
if (!aAllowMerging)
|
|
|
|
continue;
|
2012-08-15 00:04:19 -07:00
|
|
|
// XXX: binary search for insertion point might be faster in common
|
|
|
|
// case?
|
|
|
|
while (storeIter < storeEnd && (storeIter->Compare(*updateIter) < 0)) {
|
|
|
|
// skip forward to matching element (or not...)
|
|
|
|
storeIter++;
|
|
|
|
}
|
|
|
|
// no match, add
|
|
|
|
if (storeIter == storeEnd
|
|
|
|
|| storeIter->Compare(*updateIter) != 0) {
|
|
|
|
if (!adds.AppendElement(*updateIter))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:10:39 -07:00
|
|
|
// Chunks can be empty, but we should still report we have them
|
|
|
|
// to make the chunkranges continuous.
|
2012-08-15 00:04:19 -07:00
|
|
|
aStoreChunks->Merge(aUpdateChunks);
|
|
|
|
|
|
|
|
aStorePrefixes->AppendElements(adds);
|
|
|
|
EntrySort(*aStorePrefixes);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::ApplyUpdate(TableUpdate &update)
|
|
|
|
{
|
|
|
|
nsresult rv = mAddExpirations.Merge(update.AddExpirations());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mSubExpirations.Merge(update.SubExpirations());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Expire();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Merge(&mAddChunks, &mAddPrefixes,
|
|
|
|
update.AddChunks(), update.AddPrefixes());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Merge(&mAddChunks, &mAddCompletes,
|
2012-08-15 00:10:39 -07:00
|
|
|
update.AddChunks(), update.AddCompletes(), true);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Merge(&mSubChunks, &mSubPrefixes,
|
|
|
|
update.SubChunks(), update.SubPrefixes());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Merge(&mSubChunks, &mSubCompletes,
|
2012-08-15 00:10:39 -07:00
|
|
|
update.SubChunks(), update.SubCompletes(), true);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::Rebuild()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mInUpdate, "Must be in update to rebuild.");
|
|
|
|
|
|
|
|
nsresult rv = ProcessSubs();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
UpdateHeader();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static void
|
|
|
|
ExpireEntries(nsTArray<T>* aEntries, ChunkSet& aExpirations)
|
|
|
|
{
|
|
|
|
T* addIter = aEntries->Elements();
|
|
|
|
T* end = aEntries->Elements() + aEntries->Length();
|
|
|
|
|
|
|
|
for (T *iter = addIter; iter != end; iter++) {
|
|
|
|
if (!aExpirations.Has(iter->Chunk())) {
|
|
|
|
*addIter = *iter;
|
|
|
|
addIter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aEntries->SetLength(addIter - aEntries->Elements());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::Expire()
|
|
|
|
{
|
|
|
|
ExpireEntries(&mAddPrefixes, mAddExpirations);
|
|
|
|
ExpireEntries(&mAddCompletes, mAddExpirations);
|
|
|
|
ExpireEntries(&mSubPrefixes, mSubExpirations);
|
|
|
|
ExpireEntries(&mSubCompletes, mSubExpirations);
|
|
|
|
|
|
|
|
mAddChunks.Remove(mAddExpirations);
|
|
|
|
mSubChunks.Remove(mSubExpirations);
|
|
|
|
|
|
|
|
mAddExpirations.Clear();
|
|
|
|
mSubExpirations.Clear();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
nsresult DeflateWriteTArray(nsIOutputStream* aStream, nsTArray<T>& aIn)
|
|
|
|
{
|
|
|
|
uLongf insize = aIn.Length() * sizeof(T);
|
|
|
|
uLongf outsize = compressBound(insize);
|
|
|
|
nsTArray<char> outBuff;
|
|
|
|
outBuff.SetLength(outsize);
|
|
|
|
|
|
|
|
int zerr = compress(reinterpret_cast<Bytef*>(outBuff.Elements()),
|
|
|
|
&outsize,
|
|
|
|
reinterpret_cast<const Bytef*>(aIn.Elements()),
|
|
|
|
insize);
|
|
|
|
if (zerr != Z_OK) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
LOG(("DeflateWriteTArray: %d in %d out", insize, outsize));
|
|
|
|
|
|
|
|
outBuff.TruncateLength(outsize);
|
|
|
|
|
|
|
|
// Length of compressed data stream
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t dataLen = outBuff.Length();
|
|
|
|
uint32_t written;
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult rv = aStream->Write(reinterpret_cast<char*>(&dataLen), sizeof(dataLen), &written);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(written == sizeof(dataLen), "Error writing deflate length");
|
|
|
|
|
|
|
|
// Store to stream
|
|
|
|
rv = WriteTArray(aStream, outBuff);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
nsresult InflateReadTArray(nsIInputStream* aStream, nsTArray<T>* aOut,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aExpectedSize)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t inLen;
|
|
|
|
uint32_t read;
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult rv = aStream->Read(reinterpret_cast<char*>(&inLen), sizeof(inLen), &read);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(read == sizeof(inLen), "Error reading inflate length");
|
|
|
|
|
|
|
|
nsTArray<char> inBuff;
|
|
|
|
inBuff.SetLength(inLen);
|
|
|
|
|
|
|
|
rv = ReadTArray(aStream, &inBuff, inLen);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uLongf insize = inLen;
|
|
|
|
uLongf outsize = aExpectedSize * sizeof(T);
|
|
|
|
aOut->SetLength(aExpectedSize);
|
|
|
|
|
|
|
|
int zerr = uncompress(reinterpret_cast<Bytef*>(aOut->Elements()),
|
|
|
|
&outsize,
|
|
|
|
reinterpret_cast<const Bytef*>(inBuff.Elements()),
|
|
|
|
insize);
|
|
|
|
if (zerr != Z_OK) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
LOG(("InflateReadTArray: %d in %d out", insize, outsize));
|
|
|
|
|
|
|
|
NS_ASSERTION(outsize == aExpectedSize * sizeof(T), "Decompression size mismatch");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
static nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
ByteSliceWrite(nsIOutputStream* aOut, nsTArray<uint32_t>& aData)
|
2012-08-15 00:06:54 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTArray<uint8_t> slice1;
|
|
|
|
nsTArray<uint8_t> slice2;
|
|
|
|
nsTArray<uint8_t> slice3;
|
|
|
|
nsTArray<uint8_t> slice4;
|
|
|
|
uint32_t count = aData.Length();
|
2012-08-15 00:06:54 -07:00
|
|
|
|
|
|
|
slice1.SetCapacity(count);
|
|
|
|
slice2.SetCapacity(count);
|
|
|
|
slice3.SetCapacity(count);
|
|
|
|
slice4.SetCapacity(count);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2012-08-15 00:06:54 -07:00
|
|
|
slice1.AppendElement( aData[i] >> 24);
|
|
|
|
slice2.AppendElement((aData[i] >> 16) & 0xFF);
|
|
|
|
slice3.AppendElement((aData[i] >> 8) & 0xFF);
|
|
|
|
slice4.AppendElement( aData[i] & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = DeflateWriteTArray(aOut, slice1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = DeflateWriteTArray(aOut, slice2);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = DeflateWriteTArray(aOut, slice3);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// The LSB slice is generally uncompressible, don't bother
|
|
|
|
// compressing it.
|
|
|
|
rv = WriteTArray(aOut, slice4);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
ByteSliceRead(nsIInputStream* aInStream, nsTArray<uint32_t>* aData, uint32_t count)
|
2012-08-15 00:06:54 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTArray<uint8_t> slice1;
|
|
|
|
nsTArray<uint8_t> slice2;
|
|
|
|
nsTArray<uint8_t> slice3;
|
|
|
|
nsTArray<uint8_t> slice4;
|
2012-08-15 00:06:54 -07:00
|
|
|
|
|
|
|
nsresult rv = InflateReadTArray(aInStream, &slice1, count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = InflateReadTArray(aInStream, &slice2, count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = InflateReadTArray(aInStream, &slice3, count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ReadTArray(aInStream, &slice4, count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aData->SetCapacity(count);
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < count; i++) {
|
|
|
|
aData->AppendElement((slice1[i] << 24) | (slice2[i] << 16)
|
|
|
|
| (slice3[i] << 8) | (slice4[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult
|
|
|
|
HashStore::ReadAddPrefixes()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTArray<uint32_t> chunks;
|
|
|
|
uint32_t count = mHeader.numAddPrefixes;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
nsresult rv = ByteSliceRead(mInputStream, &chunks, count);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mAddPrefixes.SetCapacity(count);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
AddPrefix *add = mAddPrefixes.AppendElement();
|
|
|
|
add->prefix.FromUint32(0);
|
|
|
|
add->addChunk = chunks[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::ReadSubPrefixes()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTArray<uint32_t> addchunks;
|
|
|
|
nsTArray<uint32_t> subchunks;
|
|
|
|
nsTArray<uint32_t> prefixes;
|
|
|
|
uint32_t count = mHeader.numSubPrefixes;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
nsresult rv = ByteSliceRead(mInputStream, &addchunks, count);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
rv = ByteSliceRead(mInputStream, &subchunks, count);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
rv = ByteSliceRead(mInputStream, &prefixes, count);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mSubPrefixes.SetCapacity(count);
|
|
|
|
for (uint32 i = 0; i < count; i++) {
|
|
|
|
SubPrefix *sub = mSubPrefixes.AppendElement();
|
|
|
|
sub->addChunk = addchunks[i];
|
2012-08-15 00:06:54 -07:00
|
|
|
sub->prefix.FromUint32(prefixes[i]);
|
2012-08-15 00:04:19 -07:00
|
|
|
sub->subChunk = subchunks[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split up PrefixArray back into the constituents
|
|
|
|
nsresult
|
|
|
|
HashStore::WriteAddPrefixes(nsIOutputStream* aOut)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTArray<uint32_t> chunks;
|
|
|
|
uint32_t count = mAddPrefixes.Length();
|
2012-08-15 00:04:19 -07:00
|
|
|
chunks.SetCapacity(count);
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < count; i++) {
|
|
|
|
chunks.AppendElement(mAddPrefixes[i].Chunk());
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
nsresult rv = ByteSliceWrite(aOut, chunks);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::WriteSubPrefixes(nsIOutputStream* aOut)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTArray<uint32_t> addchunks;
|
|
|
|
nsTArray<uint32_t> subchunks;
|
|
|
|
nsTArray<uint32_t> prefixes;
|
|
|
|
uint32_t count = mSubPrefixes.Length();
|
2012-08-15 00:04:19 -07:00
|
|
|
addchunks.SetCapacity(count);
|
|
|
|
subchunks.SetCapacity(count);
|
|
|
|
prefixes.SetCapacity(count);
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < count; i++) {
|
|
|
|
addchunks.AppendElement(mSubPrefixes[i].AddChunk());
|
2012-08-15 00:06:54 -07:00
|
|
|
prefixes.AppendElement(mSubPrefixes[i].PrefixHash().ToUint32());
|
2012-08-15 00:04:19 -07:00
|
|
|
subchunks.AppendElement(mSubPrefixes[i].Chunk());
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
nsresult rv = ByteSliceWrite(aOut, addchunks);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
rv = ByteSliceWrite(aOut, subchunks);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-15 00:06:54 -07:00
|
|
|
rv = ByteSliceWrite(aOut, prefixes);
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HashStore::WriteFile()
|
|
|
|
{
|
2012-09-28 09:31:19 -07:00
|
|
|
NS_ASSERTION(mInUpdate, "Must be in update to write database.");
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsCOMPtr<nsIFile> storeFile;
|
|
|
|
nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(".sbstore"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
rv = NS_NewCheckSummedOutputStream(getter_AddRefs(out), storeFile,
|
|
|
|
PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t written;
|
2012-08-15 00:04:19 -07:00
|
|
|
rv = out->Write(reinterpret_cast<char*>(&mHeader), sizeof(mHeader), &written);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Write chunk numbers...
|
|
|
|
rv = mAddChunks.Write(out);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mSubChunks.Write(out);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Write hashes..
|
|
|
|
rv = WriteAddPrefixes(out);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = WriteSubPrefixes(out);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = WriteTArray(out, mAddCompletes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = WriteTArray(out, mSubCompletes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISafeOutputStream> safeOut = do_QueryInterface(out, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = safeOut->Finish();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static void
|
|
|
|
Erase(nsTArray<T>* array, T* iterStart, T* iterEnd)
|
|
|
|
{
|
|
|
|
uint32 start = iterStart - array->Elements();
|
|
|
|
uint32 count = iterEnd - iterStart;
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
array->RemoveElementsAt(start, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find items matching between |subs| and |adds|, and remove them,
|
|
|
|
// recording the item from |adds| in |adds_removed|. To minimize
|
|
|
|
// copies, the inputs are processing in parallel, so |subs| and |adds|
|
|
|
|
// should be compatibly ordered (either by SBAddPrefixLess or
|
|
|
|
// SBAddPrefixHashLess).
|
|
|
|
//
|
|
|
|
// |predAS| provides add < sub, |predSA| provides sub < add, for the
|
|
|
|
// tightest compare appropriate (see calls in SBProcessSubs).
|
|
|
|
template<class TSub, class TAdd>
|
|
|
|
static void
|
|
|
|
KnockoutSubs(nsTArray<TSub>* aSubs, nsTArray<TAdd>* aAdds)
|
|
|
|
{
|
|
|
|
// Keep a pair of output iterators for writing kept items. Due to
|
|
|
|
// deletions, these may lag the main iterators. Using erase() on
|
|
|
|
// individual items would result in O(N^2) copies. Using a list
|
|
|
|
// would work around that, at double or triple the memory cost.
|
|
|
|
TAdd* addOut = aAdds->Elements();
|
|
|
|
TAdd* addIter = aAdds->Elements();
|
|
|
|
|
|
|
|
TSub* subOut = aSubs->Elements();
|
|
|
|
TSub* subIter = aSubs->Elements();
|
|
|
|
|
|
|
|
TAdd* addEnd = addIter + aAdds->Length();
|
|
|
|
TSub* subEnd = subIter + aSubs->Length();
|
|
|
|
|
|
|
|
while (addIter != addEnd && subIter != subEnd) {
|
|
|
|
// additer compare, so it compares on add chunk
|
|
|
|
int32 cmp = addIter->Compare(*subIter);
|
|
|
|
if (cmp > 0) {
|
|
|
|
// If |*sub_iter| < |*add_iter|, retain the sub.
|
|
|
|
*subOut = *subIter;
|
|
|
|
++subOut;
|
|
|
|
++subIter;
|
|
|
|
} else if (cmp < 0) {
|
|
|
|
// If |*add_iter| < |*sub_iter|, retain the add.
|
|
|
|
*addOut = *addIter;
|
|
|
|
++addOut;
|
|
|
|
++addIter;
|
|
|
|
} else {
|
|
|
|
// Drop equal items
|
|
|
|
++addIter;
|
|
|
|
++subIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Erase(aAdds, addOut, addIter);
|
|
|
|
Erase(aSubs, subOut, subIter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove items in |removes| from |fullHashes|. |fullHashes| and
|
|
|
|
// |removes| should be ordered by SBAddPrefix component.
|
|
|
|
template <class T>
|
|
|
|
static void
|
|
|
|
RemoveMatchingPrefixes(const SubPrefixArray& aSubs, nsTArray<T>* aFullHashes)
|
|
|
|
{
|
|
|
|
// Where to store kept items.
|
|
|
|
T* out = aFullHashes->Elements();
|
|
|
|
T* hashIter = out;
|
|
|
|
T* hashEnd = aFullHashes->Elements() + aFullHashes->Length();
|
|
|
|
|
|
|
|
SubPrefix const * removeIter = aSubs.Elements();
|
|
|
|
SubPrefix const * removeEnd = aSubs.Elements() + aSubs.Length();
|
|
|
|
|
|
|
|
while (hashIter != hashEnd && removeIter != removeEnd) {
|
|
|
|
int32 cmp = removeIter->CompareAlt(*hashIter);
|
|
|
|
if (cmp > 0) {
|
|
|
|
// Keep items less than |*removeIter|.
|
|
|
|
*out = *hashIter;
|
|
|
|
++out;
|
|
|
|
++hashIter;
|
|
|
|
} else if (cmp < 0) {
|
|
|
|
// No hit for |*removeIter|, bump it forward.
|
|
|
|
++removeIter;
|
|
|
|
} else {
|
|
|
|
// Drop equal items, there may be multiple hits.
|
|
|
|
do {
|
|
|
|
++hashIter;
|
|
|
|
} while (hashIter != hashEnd &&
|
|
|
|
!(removeIter->CompareAlt(*hashIter) < 0));
|
|
|
|
++removeIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Erase(aFullHashes, out, hashIter);
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:08:51 -07:00
|
|
|
static void
|
|
|
|
RemoveDeadSubPrefixes(SubPrefixArray& aSubs, ChunkSet& aAddChunks)
|
|
|
|
{
|
|
|
|
SubPrefix * subIter = aSubs.Elements();
|
|
|
|
SubPrefix * subEnd = aSubs.Elements() + aSubs.Length();
|
|
|
|
|
|
|
|
for (SubPrefix * iter = subIter; iter != subEnd; iter++) {
|
|
|
|
bool hasChunk = aAddChunks.Has(iter->AddChunk());
|
|
|
|
// Keep the subprefix if the chunk it refers to is one
|
|
|
|
// we haven't seen it yet.
|
|
|
|
if (!hasChunk) {
|
|
|
|
*subIter = *iter;
|
|
|
|
subIter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Removed %u dead SubPrefix entries.", subEnd - subIter));
|
|
|
|
aSubs.SetLength(subIter - aSubs.Elements());
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:08:37 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
template <class T>
|
|
|
|
static void EnsureSorted(nsTArray<T>* aArray)
|
|
|
|
{
|
|
|
|
T* start = aArray->Elements();
|
|
|
|
T* end = aArray->Elements() + aArray->Length();
|
|
|
|
T* iter = start;
|
|
|
|
T* previous = start;
|
|
|
|
|
|
|
|
while (iter != end) {
|
|
|
|
previous = iter;
|
|
|
|
++iter;
|
|
|
|
if (iter != end) {
|
|
|
|
MOZ_ASSERT(iter->Compare(*previous) >= 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult
|
|
|
|
HashStore::ProcessSubs()
|
|
|
|
{
|
2012-08-15 00:08:37 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
EnsureSorted(&mAddPrefixes);
|
|
|
|
EnsureSorted(&mSubPrefixes);
|
|
|
|
EnsureSorted(&mAddCompletes);
|
|
|
|
EnsureSorted(&mSubCompletes);
|
|
|
|
LOG(("All databases seem to have a consistent sort order."));
|
|
|
|
#endif
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
RemoveMatchingPrefixes(mSubPrefixes, &mAddCompletes);
|
|
|
|
RemoveMatchingPrefixes(mSubPrefixes, &mSubCompletes);
|
|
|
|
|
2012-08-15 00:08:09 -07:00
|
|
|
// Clean up temporary subs (without per-client randomization),
|
|
|
|
// that we temporarily stored so we could knock out completes.
|
2012-08-15 00:04:19 -07:00
|
|
|
ChunkSet dummyChunks;
|
|
|
|
dummyChunks.Set(0);
|
|
|
|
ExpireEntries(&mSubPrefixes, dummyChunks);
|
|
|
|
mSubChunks.Remove(dummyChunks);
|
|
|
|
|
2012-08-15 00:08:09 -07:00
|
|
|
// Remove any remaining subbed prefixes from both addprefixes
|
2012-08-15 00:08:51 -07:00
|
|
|
// and addcompletes.
|
|
|
|
KnockoutSubs(&mSubPrefixes, &mAddPrefixes);
|
2012-08-15 00:08:09 -07:00
|
|
|
KnockoutSubs(&mSubCompletes, &mAddCompletes);
|
|
|
|
|
2012-08-15 00:08:51 -07:00
|
|
|
// Remove any remaining subprefixes referring to addchunks that
|
|
|
|
// we have (and hence have been processed above).
|
|
|
|
RemoveDeadSubPrefixes(mSubPrefixes, mAddChunks);
|
|
|
|
|
2012-08-15 00:08:37 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
EnsureSorted(&mAddPrefixes);
|
|
|
|
EnsureSorted(&mSubPrefixes);
|
|
|
|
EnsureSorted(&mAddCompletes);
|
|
|
|
EnsureSorted(&mSubCompletes);
|
|
|
|
LOG(("All databases seem to have a consistent sort order."));
|
|
|
|
#endif
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
HashStore::AugmentAdds(const nsTArray<uint32_t>& aPrefixes)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
uint32 cnt = aPrefixes.Length();
|
|
|
|
if (cnt != mAddPrefixes.Length()) {
|
|
|
|
LOG(("Amount of prefixes in cache not consistent with store (%d vs %d)",
|
|
|
|
aPrefixes.Length(), mAddPrefixes.Length()));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
for (uint32 i = 0; i < cnt; i++) {
|
|
|
|
mAddPrefixes[i].prefix.FromUint32(aPrefixes[i]);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|