<commit_before>//  Copyright (c) 2016 Couchbase, Inc.
//  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 moss

import (
	"fmt"
	"os"
	"path"
)

type CompactionConcern int // See StorePersistOptions.CompactionConcern.

// CompactionDisable means no compaction.
var CompactionDisable = CompactionConcern(0)

// CompactionAllow means compaction decision is automated and based
// on the configed policy and parameters.
var CompactionAllow = CompactionConcern(1)

// CompactionForce means compaction should be performed immediately.
var CompactionForce = CompactionConcern(2)

// --------------------------------------------------------

func (s *Store) compactMaybe(higher Snapshot, persistOptions StorePersistOptions) (
	bool, error) {
	compactionConcern := persistOptions.CompactionConcern
	if compactionConcern <= 0 {
		return false, nil
	}

	footer, err := s.snapshot()
	if err != nil {
		return false, err
	}

	defer footer.DecRef()

	mref, ss := footer.mrefSegmentStack()

	defer mref.DecRef()

	if compactionConcern == CompactionAllow {
		totUpperLen := 0
		if ss != nil && len(ss.a) >= 2 {
			for i := 1; i < len(ss.a); i++ {
				totUpperLen += ss.a[i].Len()
			}
		}

		if higher != nil {
			higherSS, ok := higher.(*segmentStack)
			if ok {
				higherStats := higherSS.Stats()
				if higherStats != nil {
					totUpperLen += int(higherStats.CurOps)
				}
			}
		}

		var pct float64
		if ss != nil && len(ss.a) > 0 && ss.a[0].Len() > 0 {
			pct = float64(totUpperLen) / float64(ss.a[0].Len())
		}

		if pct >= s.options.CompactionPercentage {
			compactionConcern = CompactionForce
		}
	}

	if compactionConcern != CompactionForce {
		return false, nil
	}

	err = s.compact(footer, higher, persistOptions)
	if err != nil {<commit_msg>MB-21477 - consider compaction only when there's >1 segment<commit_after>//  Copyright (c) 2016 Couchbase, Inc.
//  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 moss

import (
	"fmt"
	"os"
	"path"
)

type CompactionConcern int // See StorePersistOptions.CompactionConcern.

// CompactionDisable means no compaction.
var CompactionDisable = CompactionConcern(0)

// CompactionAllow means compaction decision is automated and based
// on the configed policy and parameters.
var CompactionAllow = CompactionConcern(1)

// CompactionForce means compaction should be performed immediately.
var CompactionForce = CompactionConcern(2)

// --------------------------------------------------------

func (s *Store) compactMaybe(higher Snapshot, persistOptions StorePersistOptions) (
	bool, error) {
	compactionConcern := persistOptions.CompactionConcern
	if compactionConcern <= 0 {
		return false, nil
	}

	footer, err := s.snapshot()
	if err != nil {
		return false, err
	}

	defer footer.DecRef()

	mref, ss := footer.mrefSegmentStack()

	defer mref.DecRef()

	if compactionConcern == CompactionAllow {
		totUpperLen := 0
		if ss != nil && len(ss.a) >= 2 {
			for i := 1; i < len(ss.a); i++ {
				totUpperLen += ss.a[i].Len()
			}
		}

		if higher != nil {
			higherSS, ok := higher.(*segmentStack)
			if ok {
				higherStats := higherSS.Stats()
				if higherStats != nil {
					totUpperLen += int(higherStats.CurOps)
				}
			}
		}

		if totUpperLen > 0 {
			var pct float64
			if ss != nil && len(ss.a) > 0 && ss.a[0].Len() > 0 {
				pct = float64(totUpperLen) / float64(ss.a[0].Len())
			}

			if pct >= s.options.CompactionPercentage {
				compactionConcern = CompactionForce
			}
		}
	}

	if compactionConcern != CompactionForce {
		return false, nil
	}

	err = s.compact(footer, higher, persistOptions)
	if err != nil {
{'fraction_non_alphanumeric': 0.13137254901960785, 'fraction_numerical': 0.01241830065359477, 'mean_word_length': 3.1064400715563507, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '6226773', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1404, 'n_words': 462}