<commit_before>package org.mwdb.manager;

import org.mwdb.*;
import org.mwdb.plugin.KFactory;
import org.mwdb.plugin.KResolver;
import org.mwdb.plugin.KScheduler;
import org.mwdb.plugin.KStorage;
import org.mwdb.chunk.*;
import org.mwdb.utility.Buffer;

public class MWGResolver implements KResolver {

    private final KStorage _storage;

    private final KChunkSpace _space;

    private final KNodeTracker _tracker;

    private final KScheduler _scheduler;

    private static final String deadNodeError = "This Node has been tagged destroyed, please don't use it anymore!";

    private KGraph _graph;

    public MWGResolver(KStorage p_storage, KChunkSpace p_space, KNodeTracker p_tracker, KScheduler p_scheduler) {
        this._storage = p_storage;
        this._space = p_space;
        this._tracker = p_tracker;
        this._scheduler = p_scheduler;
    }

    private KStateChunk dictionary;

    @Override
    public void init(KGraph graph) {
        _graph = graph;
        dictionary = (KStateChunk) this._space.getAndMark(Constants.STATE_CHUNK, Constants.GLOBAL_DICTIONARY_KEY[0], Constants.GLOBAL_DICTIONARY_KEY[1], Constants.GLOBAL_DICTIONARY_KEY[2]);
    }

    @Override
    public void initNode(KNode node, long codeType) {
        KStateChunk cacheEntry = (KStateChunk) this._space.create(Constants.STATE_CHUNK, node.world(), node.time(), node.id(), null, null);
        //put and mark
        this._space.putAndMark(cacheEntry);
        //declare dirty now because potentially no insert could be done
        this._space.declareDirty(cacheEntry);

        //initiate superTime management
        KTimeTreeChunk superTimeTree = (KTimeTreeChunk) this._space.create(Constants.TIME_TREE_CHUNK, node.world(), Constants.NULL_LONG, node.id(), null, null);
        superTimeTree = (KTimeTreeChunk) this._space.putAndMark(superTimeTree);
        superTimeTree.insert(node.time());

        //initiate time management
        KTimeTreeChunk timeTree = (KTimeTreeChunk) this._space.create(Constants.TIME_TREE_CHUNK, node.world(), node.time(), node.id(), null, null);
        timeTree = (KTimeTreeChunk) this._space.putAndMark(timeTree);
        timeTree.insert(node.time());

        //initiate universe management
        KWorldOrderChunk objectWorldOrder = (KWorldOrderChunk) this._space.create(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, node.id(), null, null);
        objectWorldOrder = (KWorldOrderChunk) this._space.putAndMark(objectWorldOrder);
        objectWorldOrder.put(node.world(), node.time());
        objectWorldOrder.setExtra(codeType);
        //mark the global

        this._space.getAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG);
        //monitor the node object
        this._tracker.monitor(node);
    }

    @Override
    public void initWorld(long parentWorld, long childWorld) {
        KWorldOrderChunk worldOrder = (KWorldOrderChunk) this._space.getAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG);
        worldOrder.put(childWorld, parentWorld);
        this._space.unmarkChunk(worldOrder);
    }

    @Override
    public void freeNode(KNode node) {
        AbstractNode casted = (AbstractNode) node;
        long nodeId = node.id();
        long[] previous;
        do {
            previous = casted._previousResolveds.get();
        } while (!casted._previousResolveds.compareAndSet(previous, null));
        if (previous != null) {
            this._space.unmark(Constants.STATE_CHUNK, previous[Constants.PREVIOUS_RESOLVED_WORLD_INDEX], previous[Constants.PREVIOUS_RESOLVED_TIME_INDEX], nodeId);//FREE OBJECT CHUNK
            this._space.unmark(Constants.TIME_TREE_CHUNK, previous[Constants.PREVIOUS_RESOLVED_WORLD_INDEX], Constants.NULL_LONG, nodeId);//FREE TIME TREE
            this._space.unmark(Constants.TIME_TREE_CHUNK, previous[Constants.PREVIOUS_RESOLVED_WORLD_INDEX], previous[Constants.PREVIOUS_RESOLVED_SUPER_TIME_INDEX], nodeId);//FREE TIME TREE
            this._space.unmark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, nodeId); //FREE OBJECT UNIVERSE MAP
            this._space.unmark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG); //FREE GLOBAL UNIVERSE MAP
        }
    }

    @Override
    public <A extends KNode> void lookup(long world, long time, long id, KCallback<A> callback) {
        this._scheduler.dispatch(lookupTask(world, time, id, callback));
    }

    @Override
    public <A extends KNode> KCallback lookupTask(final long world, final long time, final long id, final KCallback<A> callback) {
        final MWGResolver selfPointer = this;
        return new KCallback() {
            @Override
            public void on(Object o) {
                try {
                    selfPointer.getOrLoadAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG, new KCallback<KChunk>() {
                        @Override
                        public void on(KChunk theGlobalWorldOrder) {
                            if (theGlobalWorldOrder != null) {
                                selfPointer.getOrLoadAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, id, new KCallback<KChunk>() {
                                    @Override
                                    public void on(KChunk theNodeWorldOrder) {
                                        if (theNodeWorldOrder == null) {
                                            selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                            callback.on(null);
                                        } else {
                                            final long closestWorld = resolve_world((KLongLongMap) theGlobalWorldOrder, (KLongLongMap) theNodeWorldOrder, time, world);
                                            selfPointer.getOrLoadAndMark(Constants.TIME_TREE_CHUNK, closestWorld, Constants.NULL_LONG, id, new KCallback<KChunk>() {
                                                @Override
                                                public void on(KChunk theNodeSuperTimeTree) {
                                                    if (theNodeSuperTimeTree == null) {
                                                        selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                        selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                        callback.on(null);
                                                    } else {
                                                        final long closestSuperTime = ((KLongTree) theNodeSuperTimeTree).previousOrEqual(time);
                                                        if (closestSuperTime == Constants.NULL_LONG) {
                                                            selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                            selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                            selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                            callback.on(null);
                                                            return;
                                                        }
                                                        selfPointer.getOrLoadAndMark(Constants.TIME_TREE_CHUNK, closestWorld, closestSuperTime, id, new KCallback<KChunk>() {
                                                            @Override
                                                            public void on(KChunk theNodeTimeTree) {
                                                                if (theNodeTimeTree == null) {
                                                                    selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                                    selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                                    selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                                    callback.on(null);
                                                                } else {
                                                                    long closestTime = ((KLongTree) theNodeTimeTree).previousOrEqual(time);
                                                                    if (closestTime == Constants.NULL_LONG) {
                                                                        selfPointer._space.unmarkChunk(theNodeTimeTree);
                                                                        selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                                        selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                                        selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                                        callback.on(null);
                                                                        return;
                                                                    }
                                                                    selfPointer.getOrLoadAndMark(Constants.STATE_CHUNK, closestWorld, closestTime, id, new KCallback<KChunk>() {
                                                                        @Override
                                                                        public void on(KChunk theObjectChunk) {
                                                                            if (theObjectChunk == null) {
                                                                                selfPointer._space.unmarkChunk(theNodeTimeTree);
                                                                                selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                                                selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                                                selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                                                callback.on(null);
                                                                            } else {
                                                                                KWorldOrderChunk castedNodeWorldOrder = (KWorldOrderChunk) theNodeWorldOrder;
                                                                                long extraCode = castedNodeWorldOrder.extra();
                                                                                KFactory resolvedFactory = null;
                                                                                if (extraCode == Constants.NULL_LONG) {
                                                                                    resolvedFactory = ((Graph) _graph).factoryByCode(extraCode);
                                                                                }

                                                                                long[] initPreviouslyResolved = new long[6];
                                                                                //init previously resolved values
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_WORLD_INDEX] = closestWorld;
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_SUPER_TIME_INDEX] = closestSuperTime;
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_TIME_INDEX] = closestTime;
                                                                                //init previous magics
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_WORLD_MAGIC] = ((KWorldOrderChunk) theNodeWorldOrder).magic();
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_SUPER_TIME_MAGIC] = ((KLongTree) theNodeSuperTimeTree).magic();
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_TIME_MAGIC] = ((KLongTree) theNodeTimeTree).magic();

                                                                                KNode resolvedNode;
                                                                                if (resolvedFactory == null) {
                                                                                    resolvedNode = new Node(world, time, id, _graph, initPreviouslyResolved);
                                                                                } else {
                                                                                    resolvedNode = resolvedFactory.create(world, time, id, _graph, initPreviouslyResolved);
                                                                                }
                                                                                selfPointer._tracker.monitor(resolvedNode);
                                                                                callback.on((A) resolvedNode);
                                                                            }
                                                                        }
                                                                    });
                                                                }
                                                            }
                                                        });
                                                    }
                                                }
                                            });
                                        }<commit_msg>resolver<commit_after>package org.mwdb.manager;

import org.mwdb.*;
import org.mwdb.plugin.KFactory;
import org.mwdb.plugin.KResolver;
import org.mwdb.plugin.KScheduler;
import org.mwdb.plugin.KStorage;
import org.mwdb.chunk.*;
import org.mwdb.utility.Buffer;

public class MWGResolver implements KResolver {

    private final KStorage _storage;

    private final KChunkSpace _space;

    private final KNodeTracker _tracker;

    private final KScheduler _scheduler;

    private static final String deadNodeError = "This Node has been tagged destroyed, please don't use it anymore!";

    private KGraph _graph;

    public MWGResolver(KStorage p_storage, KChunkSpace p_space, KNodeTracker p_tracker, KScheduler p_scheduler) {
        this._storage = p_storage;
        this._space = p_space;
        this._tracker = p_tracker;
        this._scheduler = p_scheduler;
    }

    private KStateChunk dictionary;

    @Override
    public void init(KGraph graph) {
        _graph = graph;
        dictionary = (KStateChunk) this._space.getAndMark(Constants.STATE_CHUNK, Constants.GLOBAL_DICTIONARY_KEY[0], Constants.GLOBAL_DICTIONARY_KEY[1], Constants.GLOBAL_DICTIONARY_KEY[2]);
    }

    @Override
    public void initNode(KNode node, long codeType) {
        KStateChunk cacheEntry = (KStateChunk) this._space.create(Constants.STATE_CHUNK, node.world(), node.time(), node.id(), null, null);
        //put and mark
        this._space.putAndMark(cacheEntry);
        //declare dirty now because potentially no insert could be done
        this._space.declareDirty(cacheEntry);

        //initiate superTime management
        KTimeTreeChunk superTimeTree = (KTimeTreeChunk) this._space.create(Constants.TIME_TREE_CHUNK, node.world(), Constants.NULL_LONG, node.id(), null, null);
        superTimeTree = (KTimeTreeChunk) this._space.putAndMark(superTimeTree);
        superTimeTree.insert(node.time());

        //initiate time management
        KTimeTreeChunk timeTree = (KTimeTreeChunk) this._space.create(Constants.TIME_TREE_CHUNK, node.world(), node.time(), node.id(), null, null);
        timeTree = (KTimeTreeChunk) this._space.putAndMark(timeTree);
        timeTree.insert(node.time());

        //initiate universe management
        KWorldOrderChunk objectWorldOrder = (KWorldOrderChunk) this._space.create(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, node.id(), null, null);
        objectWorldOrder = (KWorldOrderChunk) this._space.putAndMark(objectWorldOrder);
        objectWorldOrder.put(node.world(), node.time());
        objectWorldOrder.setExtra(codeType);
        //mark the global

        this._space.getAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG);
        //monitor the node object
        this._tracker.monitor(node);
    }

    @Override
    public void initWorld(long parentWorld, long childWorld) {
        KWorldOrderChunk worldOrder = (KWorldOrderChunk) this._space.getAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG);
        worldOrder.put(childWorld, parentWorld);
        this._space.unmarkChunk(worldOrder);
    }

    @Override
    public void freeNode(KNode node) {
        AbstractNode casted = (AbstractNode) node;
        long nodeId = node.id();
        long[] previous;
        do {
            previous = casted._previousResolveds.get();
        } while (!casted._previousResolveds.compareAndSet(previous, null));
        if (previous != null) {
            this._space.unmark(Constants.STATE_CHUNK, previous[Constants.PREVIOUS_RESOLVED_WORLD_INDEX], previous[Constants.PREVIOUS_RESOLVED_TIME_INDEX], nodeId);//FREE OBJECT CHUNK
            this._space.unmark(Constants.TIME_TREE_CHUNK, previous[Constants.PREVIOUS_RESOLVED_WORLD_INDEX], Constants.NULL_LONG, nodeId);//FREE TIME TREE
            this._space.unmark(Constants.TIME_TREE_CHUNK, previous[Constants.PREVIOUS_RESOLVED_WORLD_INDEX], previous[Constants.PREVIOUS_RESOLVED_SUPER_TIME_INDEX], nodeId);//FREE TIME TREE
            this._space.unmark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, nodeId); //FREE OBJECT UNIVERSE MAP
            this._space.unmark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG); //FREE GLOBAL UNIVERSE MAP
        }
    }

    @Override
    public <A extends KNode> void lookup(long world, long time, long id, KCallback<A> callback) {
        this._scheduler.dispatch(lookupTask(world, time, id, callback));
    }

    @Override
    public <A extends KNode> KCallback lookupTask(final long world, final long time, final long id, final KCallback<A> callback) {
        final MWGResolver selfPointer = this;
        return new KCallback() {
            @Override
            public void on(Object o) {
                try {
                    selfPointer.getOrLoadAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, Constants.NULL_LONG, new KCallback<KChunk>() {
                        @Override
                        public void on(KChunk theGlobalWorldOrder) {
                            if (theGlobalWorldOrder != null) {
                                selfPointer.getOrLoadAndMark(Constants.WORLD_ORDER_CHUNK, Constants.NULL_LONG, Constants.NULL_LONG, id, new KCallback<KChunk>() {
                                    @Override
                                    public void on(KChunk theNodeWorldOrder) {
                                        if (theNodeWorldOrder == null) {
                                            selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                            callback.on(null);
                                        } else {
                                            final long closestWorld = resolve_world((KLongLongMap) theGlobalWorldOrder, (KLongLongMap) theNodeWorldOrder, time, world);
                                            selfPointer.getOrLoadAndMark(Constants.TIME_TREE_CHUNK, closestWorld, Constants.NULL_LONG, id, new KCallback<KChunk>() {
                                                @Override
                                                public void on(KChunk theNodeSuperTimeTree) {
                                                    if (theNodeSuperTimeTree == null) {
                                                        selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                        selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                        callback.on(null);
                                                    } else {
                                                        final long closestSuperTime = ((KLongTree) theNodeSuperTimeTree).previousOrEqual(time);
                                                        if (closestSuperTime == Constants.NULL_LONG) {
                                                            selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                            selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                            selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                            callback.on(null);
                                                            return;
                                                        }
                                                        selfPointer.getOrLoadAndMark(Constants.TIME_TREE_CHUNK, closestWorld, closestSuperTime, id, new KCallback<KChunk>() {
                                                            @Override
                                                            public void on(KChunk theNodeTimeTree) {
                                                                if (theNodeTimeTree == null) {
                                                                    selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                                    selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                                    selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                                    callback.on(null);
                                                                } else {
                                                                    long closestTime = ((KLongTree) theNodeTimeTree).previousOrEqual(time);
                                                                    if (closestTime == Constants.NULL_LONG) {
                                                                        selfPointer._space.unmarkChunk(theNodeTimeTree);
                                                                        selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                                        selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                                        selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                                        callback.on(null);
                                                                        return;
                                                                    }
                                                                    selfPointer.getOrLoadAndMark(Constants.STATE_CHUNK, closestWorld, closestTime, id, new KCallback<KChunk>() {
                                                                        @Override
                                                                        public void on(KChunk theObjectChunk) {
                                                                            if (theObjectChunk == null) {
                                                                                selfPointer._space.unmarkChunk(theNodeTimeTree);
                                                                                selfPointer._space.unmarkChunk(theNodeSuperTimeTree);
                                                                                selfPointer._space.unmarkChunk(theNodeWorldOrder);
                                                                                selfPointer._space.unmarkChunk(theGlobalWorldOrder);
                                                                                callback.on(null);
                                                                            } else {
                                                                                KWorldOrderChunk castedNodeWorldOrder = (KWorldOrderChunk) theNodeWorldOrder;
                                                                                long extraCode = castedNodeWorldOrder.extra();
                                                                                KFactory resolvedFactory = null;
                                                                                if (extraCode != Constants.NULL_LONG) {
                                                                                    resolvedFactory = ((Graph) _graph).factoryByCode(extraCode);
                                                                                }

                                                                                long[] initPreviouslyResolved = new long[6];
                                                                                //init previously resolved values
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_WORLD_INDEX] = closestWorld;
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_SUPER_TIME_INDEX] = closestSuperTime;
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_TIME_INDEX] = closestTime;
                                                                                //init previous magics
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_WORLD_MAGIC] = ((KWorldOrderChunk) theNodeWorldOrder).magic();
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_SUPER_TIME_MAGIC] = ((KLongTree) theNodeSuperTimeTree).magic();
                                                                                initPreviouslyResolved[Constants.PREVIOUS_RESOLVED_TIME_MAGIC] = ((KLongTree) theNodeTimeTree).magic();

                                                                                KNode resolvedNode;
                                                                                if (resolvedFactory == null) {
                                                                                    resolvedNode = new Node(world, time, id, _graph, initPreviouslyResolved);
                                                                                } else {
                                                                                    resolvedNode = resolvedFactory.create(world, time, id, _graph, initPreviouslyResolved);
                                                                                }
                                                                                selfPointer._tracker.monitor(resolvedNode);
                                                                                callback.on((A) resolvedNode);
                                                                            }
                                                                        }
                                                                    });
                                                                }
                                                            }
                                                        });
                                                    }
                                                }
                                            });
                                        }
{'fraction_non_alphanumeric': 0.06190393013100437, 'fraction_numerical': 0.00027947598253275107, 'mean_word_length': 1.0174783282824724, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 412, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '525218', 'n_tokens_mistral': 5362, 'n_tokens_neox': 5283, 'n_words': 984}