<commit_before>/*
 * Copyright 2013-2014 eBay Software Foundation
 *
 * 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 com.kylinolap.storage.filter;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import com.kylinolap.common.util.BytesUtil;
import com.kylinolap.storage.filter.TupleFilter.FilterOperatorEnum;

/**
 * http://eli.thegreenplace.net/2011/09/29/an-interesting-tree-serialization-
 * algorithm-from-dwarf
 * 
 * @author xjiang
 * 
 */
public class TupleFilterSerializer {

    public static interface Decorator {
        TupleFilter onSerialize(TupleFilter filter);
    }

    private static final int BUFFER_SIZE = 65536;
    private static final Map<Integer, FilterOperatorEnum> ID_OP_MAP = new HashMap<Integer, FilterOperatorEnum>();

    static {
        for (FilterOperatorEnum op : FilterOperatorEnum.values()) {
            ID_OP_MAP.put(op.getValue(), op);
        }
    }

    public static byte[] serialize(TupleFilter rootFilter) {
        return serialize(rootFilter, null);
    }

    public static byte[] serialize(TupleFilter rootFilter, Decorator decorator) {
        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
        internalSerialize(rootFilter, decorator, buffer);
        byte[] result = new byte[buffer.position()];
        System.arraycopy(buffer.array(), 0, result, 0, buffer.position());
        return result;
    }<commit_msg>minor format change<commit_after>/*
 * Copyright 2013-2014 eBay Software Foundation
 *
 * 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 com.kylinolap.storage.filter;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import com.kylinolap.common.util.BytesUtil;
import com.kylinolap.storage.filter.TupleFilter.FilterOperatorEnum;

/**
 * http://eli.thegreenplace.net/2011/09/29/an-interesting-tree-serialization-algorithm-from-dwarf
 * 
 * @author xjiang
 * 
 */
public class TupleFilterSerializer {

    public static interface Decorator {
        TupleFilter onSerialize(TupleFilter filter);
    }

    private static final int BUFFER_SIZE = 65536;
    private static final Map<Integer, FilterOperatorEnum> ID_OP_MAP = new HashMap<Integer, FilterOperatorEnum>();

    static {
        for (FilterOperatorEnum op : FilterOperatorEnum.values()) {
            ID_OP_MAP.put(op.getValue(), op);
        }
    }

    public static byte[] serialize(TupleFilter rootFilter) {
        return serialize(rootFilter, null);
    }

    public static byte[] serialize(TupleFilter rootFilter, Decorator decorator) {
        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
        internalSerialize(rootFilter, decorator, buffer);
        byte[] result = new byte[buffer.position()];
        System.arraycopy(buffer.array(), 0, result, 0, buffer.position());
        return result;
    }
{'fraction_non_alphanumeric': 0.09480986639260021, 'fraction_numerical': 0.013874614594039054, 'mean_word_length': 3.6566985645933014, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6892097', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1055, 'n_words': 333}