<commit_before>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 *
 * 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 cas

import (
	"github.com/s3git/s3git-go/internal/backend"
	"github.com/s3git/s3git-go/internal/config"
	"github.com/s3git/s3git-go/internal/kv"
	"os"
	"path"
)

// Upon writing, make sure the size of the repository does not exceed the max local size,
// prune stale chunks otherwise
func checkRepoSize() error {

	// TODO: [perf] Maybe cache sizes of stage & cache area as they may be expensive for large repositories

	stageSize, err := kv.GetLevel0StageSize()
	if err != nil {
		return err
	}

	cacheSize, err := kv.GetLevel0CacheSize()
	if err != nil {
		return err
	}

	if stageSize+cacheSize > config.Config.MaxRepoSize {

		threshold95Pct := uint64(float64(config.Config.MaxRepoSize) * 0.95)

		// Prune leaf nodes in caching area in a number of iterations...
		for {
			if cacheSize == 0 || // Stop when no leaves are left in cache area to delete
				stageSize+cacheSize < threshold95Pct { // Or when we've gone under the threshold
				break
			}<commit_msg>Leave minimally 100 leaves when pruning<commit_after>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 *
 * 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 cas

import (
	"github.com/s3git/s3git-go/internal/backend"
	"github.com/s3git/s3git-go/internal/config"
	"github.com/s3git/s3git-go/internal/kv"
	"os"
	"path"
)

// Upon writing, make sure the size of the repository does not exceed the max local size,
// prune stale chunks otherwise
func checkRepoSize() error {

	// TODO: [perf] Maybe cache sizes of stage & cache area as they may be expensive for large repositories

	stageSize, err := kv.GetLevel0StageSize()
	if err != nil {
		return err
	}

	cacheSize, err := kv.GetLevel0CacheSize()
	if err != nil {
		return err
	}

	if stageSize+cacheSize > config.Config.MaxRepoSize {

		threshold95Pct := uint64(float64(config.Config.MaxRepoSize) * 0.95)

		// Prune leaf nodes in caching area in a number of iterations...
		for {
			if cacheSize < 100 || // Stop when less than 100 leaves are left in cache area to delete
				stageSize+cacheSize < threshold95Pct { // Or when we've gone under the threshold
				break
			}
{'fraction_non_alphanumeric': 0.0922360248447205, 'fraction_numerical': 0.01987577639751553, 'mean_word_length': 3.575284090909091, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, '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': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807597', 'n_tokens_mistral': 1026, 'n_tokens_neox': 943, 'n_words': 385}