<commit_before>// Copyright 2015 <NAME>. All Rights Reserved.
// Author: <EMAIL> (<NAME>)
//
// 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 wiring

import (
	"github.com/jacobsa/comeback/internal/blob"
	"github.com/jacobsa/comeback/internal/crypto"
	"github.com/jacobsa/comeback/internal/util"
	"github.com/jacobsa/gcloud/gcs"
)

const (
	BlobObjectNamePrefix = "blobs/"

	// A generous lower bound for where the OS starts to tell us to fuck off if
	// we have too many files. This may also cover the case where we get "no such
	// host" errors, apparently because we do too many lookups all at once.
	osFileLimit = 96
)

func minInt(a, b int) int {
	if a < b {
		return a
	}

	return b
}

// Create a blob store that stores blobs in the supplied bucket, encrypting
// with the supplied crypter.
//
// existingScores must contain only scores that are known to exist in the
// bucket, in hex form. It will be updated as the blob store is used.
func MakeBlobStore(
	bucket gcs.Bucket,
	crypter crypto.Crypter,
	existingScores util.StringSet) (bs blob.Store, err error) {
	// Store blobs in GCS.
	bs = blob.NewGCSStore(bucket, BlobObjectNamePrefix)

	// Respond efficiently to Contains requests.
	bs = blob.NewExistingScoresStore(existingScores, bs)

	// Buffer around GCS with bounded parallelism, allowing file system scanning
	// to proceed independent of waiting for GCS to ack writes.
	const latencySecs = 2
	const bandwidthBytesPerSec = 50e6
	const bandwidthHz = 512

	bs = blob.NewBufferingStore(
		3*bandwidthBytesPerSec*latencySecs,
		minInt(osFileLimit, 3*bandwidthHz*latencySecs),
		bs)

	// Make paranoid checks on the results.<commit_msg>Fixed blob store wiring, broken by 536c6a7.<commit_after>// Copyright 2015 <NAME>. All Rights Reserved.
// Author: <EMAIL> (<NAME>)
//
// 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 wiring

import (
	"github.com/jacobsa/comeback/internal/blob"
	"github.com/jacobsa/comeback/internal/crypto"
	"github.com/jacobsa/comeback/internal/util"
	"github.com/jacobsa/gcloud/gcs"
)

const (
	BlobObjectNamePrefix = "blobs/"
)

func minInt(a, b int) int {
	if a < b {
		return a
	}

	return b
}

// Create a blob store that stores blobs in the supplied bucket, encrypting
// with the supplied crypter.
//
// existingScores must contain only scores that are known to exist in the
// bucket, in hex form. It will be updated as the blob store is used.
func MakeBlobStore(
	bucket gcs.Bucket,
	crypter crypto.Crypter,
	existingScores util.StringSet) (bs blob.Store, err error) {
	// Store blobs in GCS.
	bs = blob.NewGCSStore(bucket, BlobObjectNamePrefix)

	// Respond efficiently to Contains requests.
	bs = blob.NewExistingScoresStore(existingScores, bs)

	// Make paranoid checks on the results.
{'fraction_non_alphanumeric': 0.09192348565356004, 'fraction_numerical': 0.008501594048884165, 'mean_word_length': 3.6596534653465347, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4125393', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1122, 'n_words': 456}