All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cool.taomu.box.storage.indexed.impl.Indexed Maven / Gradle / Ivy

/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.box.storage.indexed.impl;

import cool.taomu.box.storage.indexed.abs.AbsIndexed;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.xtend.lib.annotations.ToString;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

@ToString
@SuppressWarnings("all")
public class Indexed extends AbsIndexed {
  private static final ConcurrentSkipListMap> storage = new ConcurrentSkipListMap>();
  
  private static final ConcurrentSkipListMap keyPathMap = new ConcurrentSkipListMap();
  
  private AtomicLong index = new AtomicLong(0);
  
  private String name;
  
  @Override
  public void open(final String name) {
    this.name = name;
    boolean _containsKey = Indexed.storage.containsKey(this.name);
    boolean _not = (!_containsKey);
    if (_not) {
      ConcurrentSkipListMap _concurrentSkipListMap = new ConcurrentSkipListMap();
      Indexed.storage.put(this.name, _concurrentSkipListMap);
    }
  }
  
  @Override
  public void open(final String name, final String version) {
    this.open(IterableExtensions.join(Collections.unmodifiableList(CollectionLiterals.newArrayList(name, version)), "@"));
  }
  
  @Override
  public void createKeyPath(final String id) {
    Objects.requireNonNull(this.name);
    Indexed.keyPathMap.put(this.name, id);
  }
  
  @Override
  public void createIndex(final String fieldName, final boolean unique) {
    throw new UnsupportedOperationException("TODO: auto-generated method stub");
  }
  
  @Override
  public Object get(final String key) {
    String _xblockexpression = null;
    {
      Objects.requireNonNull(this.name);
      _xblockexpression = Indexed.storage.get(this.name).get(key);
    }
    return _xblockexpression;
  }
  
  @Override
  public void put(final String result) {
    Objects.requireNonNull(this.name);
    String keyPath = null;
    boolean _containsKey = Indexed.keyPathMap.containsKey(this.name);
    if (_containsKey) {
      Object _keyPath = this.getKeyPath(Indexed.keyPathMap.get(this.name), result);
      keyPath = ((String) _keyPath);
    } else {
      keyPath = String.valueOf(this.index.incrementAndGet());
    }
    Indexed.storage.get(this.name).put(((String) keyPath), result);
  }
  
  @Override
  public void remove(final String key) {
    Objects.requireNonNull(this.name);
    Indexed.storage.get(this.name).remove(key);
  }
  
  @Override
  public void clear() {
    Objects.requireNonNull(this.name);
    Indexed.storage.get(this.name).clear();
  }
  
  public static void main(final String[] args) {
    Indexed im = new Indexed();
    im.open("aa");
    im.createKeyPath("id");
    im.put("{\"id\":\"aa\",\"name\":\"Hello World\"}");
    InputOutput.println(im.get("aa"));
    Indexed im1 = new Indexed();
    im1.open("aa");
    InputOutput.println(im1.get("aa"));
  }
  
  @Override
  @Pure
  public String toString() {
    return new ToStringBuilder(this)
    	.addAllFields()
    	.toString();
  }
}