Пример #1
1
  @Test
  public void submapToString() {

    for (int i = 0; i < 20; i++) {
      m.put(i, "aa" + i);
    }

    Map submap = m.subMap(10, true, 13, true);
    assertEquals("{10=aa10, 11=aa11, 12=aa12, 13=aa13}", submap.toString());
  }
Пример #2
0
  @Test
  public void concurrent_first_key() {
    DB db = DBMaker.memoryDB().transactionDisable().make();
    final BTreeMap m = db.treeMap("name");

    // fill
    final int c = 1000000 * TT.scale();
    for (int i = 0; i <= c; i++) {
      m.put(i, i);
    }

    Thread t =
        new Thread() {
          @Override
          public void run() {
            for (int i = 0; i <= c; i++) {
              m.remove(c);
            }
          }
        };
    t.run();
    while (t.isAlive()) {
      assertNotNull(m.firstKey());
    }
  }
Пример #3
0
  @Test
  public void test_next_dir_infinity() {

    BTreeMap.DirNode d =
        new BTreeMap.DirNode(
            new Object[] {62, 68, 71}, true, false, false, mkchild(10, 20, 30, 40));
    assertEquals(10, m.nextDir(d, 33));
    assertEquals(10, m.nextDir(d, 62));
    assertEquals(20, m.nextDir(d, 63));

    d = new BTreeMap.DirNode(new Object[] {44, 62, 68}, false, true, false, mkchild(10, 20, 30, 0));

    assertEquals(10, m.nextDir(d, 62));
    assertEquals(10, m.nextDir(d, 44));
    assertEquals(10, m.nextDir(d, 48));

    assertEquals(20, m.nextDir(d, 63));
    assertEquals(20, m.nextDir(d, 64));
    assertEquals(20, m.nextDir(d, 68));

    assertEquals(30, m.nextDir(d, 69));
    assertEquals(30, m.nextDir(d, 70));
    assertEquals(30, m.nextDir(d, 71));

    assertEquals(30, m.nextDir(d, 72));
    assertEquals(30, m.nextDir(d, 73));
  }
Пример #4
0
  @Test
  public void mod_listener_lock() {
    DB db = DBMaker.memoryDB().transactionDisable().make();
    final BTreeMap m = db.treeMap("name");

    final long rootRecid = db.getEngine().get(m.rootRecidRef, Serializer.RECID);
    final AtomicInteger counter = new AtomicInteger();

    m.modificationListenerAdd(
        new Bind.MapListener() {
          @Override
          public void update(Object key, Object oldVal, Object newVal) {
            assertTrue(m.nodeLocks.get(rootRecid) == Thread.currentThread());
            assertEquals(1, m.nodeLocks.size());
            counter.incrementAndGet();
          }
        });

    m.put("aa", "aa");
    m.put("aa", "bb");
    m.remove("aa");

    m.put("aa", "aa");
    m.remove("aa", "aa");
    m.putIfAbsent("aa", "bb");
    m.replace("aa", "bb", "cc");
    m.replace("aa", "cc");

    assertEquals(8, counter.get());
  }
Пример #5
0
  @Test
  public void batch_insert() {

    for (int i = 0; i < 1000; i++) {
      m.put(i * 10, i * 10 + 1);
    }

    for (int i = 0; i < 10000; i++) {
      assertEquals(i % 10 == 0 ? i + 1 : null, m.get(i));
    }
  }
Пример #6
0
  @Test
  public void test_size() {

    assertTrue(m.isEmpty());
    assertEquals(0, m.size());
    for (int i = 1; i < 30; i++) {
      m.put(i, i);
      assertEquals(i, m.size());
      assertFalse(m.isEmpty());
    }
  }
Пример #7
0
  @Test
  public void test_key_iterator() {

    for (int i = 0; i < 20; i++) {
      m.put(i, i * 10);
    }

    Iterator iter = m.keySet().iterator();

    for (int i = 0; i < 20; i++) {
      assertTrue(iter.hasNext());
      assertEquals(i, iter.next());
    }
    assertFalse(iter.hasNext());
  }
Пример #8
0
  @Test
  public void delete() {

    for (int i :
        new int[] {
          10, 50, 20, 42,
          // 44, 68, 20, 93, 85, 71, 62, 77, 4, 37, 66
        }) {
      m.put(i, i);
    }
    assertEquals(10, m.remove(10));
    assertEquals(20, m.remove(20));
    assertEquals(42, m.remove(42));

    assertEquals(null, m.remove(42999));
  }
Пример #9
0
 public synchronized void rollback() {
   try {
     for (WeakReference<Object> o : collections.values()) {
       Object c = o.get();
       if (c != null && c instanceof BTreeMap) {
         // reload tree
         BTreeMap m = (BTreeMap) c;
         m.tree = fetch(m.tree.getRecid());
       }
       if (c != null && c instanceof BTreeSet) {
         // reload tree
         BTreeSet m = (BTreeSet) c;
         m.map.tree = fetch(m.map.tree.getRecid());
       }
     }
   } catch (IOException e) {
     throw new IOError(e);
   }
 }
Пример #10
0
  @Test
  public void root_leaf_insert() {
    if (valsOutside) return;

    m.put(11, 12);
    final long rootRecid = engine.get(m.rootRecidRef, Serializer.RECID);
    BTreeMap.LeafNode n = (BTreeMap.LeafNode) engine.get(rootRecid, m.nodeSerializer);
    assertTrue(Arrays.equals(new Object[] {null, 11, null}, nodeKeysToArray(n)));
    assertTrue(Arrays.equals(new Object[] {12}, (Object[]) n.vals));
    assertEquals(0, n.next);
  }
Пример #11
0
  public synchronized <K> NavigableSet<K> createTreeSet(BTreeSetMaker m) {
    checkNameNotExists(m.name);
    m.serializer = fillNulls(m.serializer);
    m.serializer =
        catPut(
            m.name + ".keySerializer",
            m.serializer,
            new BTreeKeySerializer.BasicKeySerializer(getDefaultSerializer()));
    m.comparator = catPut(m.name + ".comparator", m.comparator, Utils.COMPARABLE_COMPARATOR);

    if (m.pumpPresortBatchSize != -1) {
      m.pumpSource =
          Pump.sort(
              m.pumpSource,
              m.pumpPresortBatchSize,
              Collections.reverseOrder(m.comparator),
              getDefaultSerializer());
    }

    long counterRecid = !m.keepCounter ? 0L : engine.put(0L, Serializer.LONG);
    long rootRecidRef;

    if (m.pumpSource == null) {
      rootRecidRef = BTreeMap.createRootRef(engine, m.serializer, null, m.comparator);
    } else {
      rootRecidRef =
          Pump.buildTreeMap(
              m.pumpSource,
              engine,
              Fun.noTransformExtractor(),
              null,
              m.nodeSize,
              false,
              counterRecid,
              m.serializer,
              null,
              m.comparator);
    }

    NavigableSet<K> ret =
        new BTreeMap<K, Object>(
                engine,
                catPut(m.name + ".rootRecidRef", rootRecidRef),
                catPut(m.name + ".maxNodeSize", m.nodeSize),
                false,
                catPut(m.name + ".counterRecid", counterRecid),
                m.serializer,
                null,
                m.comparator)
            .keySet();
    catalog.put(m.name + ".type", "TreeSet");
    collections.put(m.name, new WeakReference<Object>(ret));
    return ret;
  }
Пример #12
0
  @Test
  public void randomStructuralCheck() {
    Random r = new Random();
    BTreeMap map =
        DBMaker.memoryDB()
            .transactionDisable()
            .make()
            .treeMapCreate("aa")
            .keySerializer(BTreeKeySerializer.INTEGER)
            .valueSerializer(Serializer.INTEGER)
            .make();

    int max = 100000 * TT.scale();

    for (int i = 0; i < max * 10; i++) {
      map.put(r.nextInt(max), r.nextInt());
    }

    map.checkStructure();
  }
Пример #13
0
  /** delete record/collection with given name */
  public synchronized void delete(String name) {
    Object r = get(name);
    if (r instanceof Atomic.Boolean) {
      engine.delete(((Atomic.Boolean) r).recid, Serializer.BOOLEAN);
    } else if (r instanceof Atomic.Integer) {
      engine.delete(((Atomic.Integer) r).recid, Serializer.INTEGER);
    } else if (r instanceof Atomic.Long) {
      engine.delete(((Atomic.Long) r).recid, Serializer.LONG);
    } else if (r instanceof Atomic.String) {
      engine.delete(((Atomic.String) r).recid, Serializer.STRING_NOSIZE);
    } else if (r instanceof Atomic.Var) {
      engine.delete(((Atomic.Var) r).recid, ((Atomic.Var) r).serializer);
    } else if (r instanceof Queue) {
      // drain queue
      Queue q = (Queue) r;
      while (q.poll() != null) {
        // do nothing
      }
    } else if (r instanceof HTreeMap || r instanceof HTreeMap.KeySet) {
      HTreeMap m = (r instanceof HTreeMap) ? (HTreeMap) r : ((HTreeMap.KeySet) r).parent();
      m.clear();
      // delete segments
      for (long segmentRecid : m.segmentRecids) {
        engine.delete(segmentRecid, HTreeMap.DIR_SERIALIZER);
      }
    } else if (r instanceof BTreeMap || r instanceof BTreeMap.KeySet) {
      BTreeMap m = (r instanceof BTreeMap) ? (BTreeMap) r : (BTreeMap) ((BTreeMap.KeySet) r).m;

      // TODO on BTreeMap recursively delete all nodes
      m.clear();

      if (m.counter != null) engine.delete(m.counter.recid, Serializer.LONG);
    }

    for (String n : catalog.keySet()) {
      if (!n.startsWith(name)) continue;
      catalog.remove(n);
    }
    namesInstanciated.remove(name);
    namesLookup.remove(r);
  }
Пример #14
0
  protected synchronized <K, V> BTreeMap<K, V> createTreeMap(BTreeMapMaker m) {
    String name = m.name;
    checkNameNotExists(name);
    m.keySerializer = fillNulls(m.keySerializer);
    m.keySerializer =
        catPut(
            name + ".keySerializer",
            m.keySerializer,
            new BTreeKeySerializer.BasicKeySerializer(getDefaultSerializer()));
    m.valueSerializer =
        catPut(name + ".valueSerializer", m.valueSerializer, getDefaultSerializer());
    m.comparator = catPut(name + ".comparator", m.comparator, Utils.COMPARABLE_COMPARATOR);

    long counterRecid = !m.keepCounter ? 0L : engine.put(0L, Serializer.LONG);

    long rootRecidRef;
    if (m.pumpSource == null) {
      rootRecidRef =
          BTreeMap.createRootRef(engine, m.keySerializer, m.valueSerializer, m.comparator);
    } else {
      rootRecidRef =
          Pump.buildTreeMap(
              m.pumpSource,
              engine,
              m.pumpKeyExtractor,
              m.pumpValueExtractor,
              m.nodeSize,
              m.valuesStoredOutsideNodes,
              counterRecid,
              m.keySerializer,
              m.valueSerializer,
              m.comparator);
    }

    BTreeMap<K, V> ret =
        new BTreeMap<K, V>(
            engine,
            catPut(name + ".rootRecidRef", rootRecidRef),
            catPut(name + ".maxNodeSize", m.nodeSize),
            catPut(name + ".valuesOutsideNodes", m.valuesStoredOutsideNodes),
            catPut(name + ".counterRecid", counterRecid),
            m.keySerializer,
            m.valueSerializer,
            m.comparator);
    catalog.put(name + ".type", "TreeMap");
    collections.put(name, new WeakReference<Object>(ret));
    return ret;
  }
Пример #15
0
 @Before
 public void init() {
   engine = new StoreDirect(null);
   engine.init();
   m =
       new BTreeMap(
           engine,
           false,
           BTreeMap.createRootRef(engine, BTreeKeySerializer.BASIC, Serializer.BASIC, 0),
           6,
           valsOutside,
           0,
           BTreeKeySerializer.BASIC,
           Serializer.BASIC,
           0);
 }
Пример #16
0
  protected void reinit() {
    // load serializer
    final CopyOnWriteArrayList<SerializerPojo.ClassInfo> classInfos =
        engine.get(Engine.CLASS_INFO_RECID, SerializerPojo.serializer);
    this.defaultSerializer =
        new SerializerPojo(classInfos) {
          @Override
          protected void saveClassInfo() {
            // hook to save classes if they are updated
            // I did not want to create direct dependency between SerialierPojo and Engine
            engine.update(Engine.CLASS_INFO_RECID, registered, SerializerPojo.serializer);
          }
        };

    // open name dir
    catalog = BTreeMap.preinitCatalog(this);
  }
Пример #17
0
  @Test
  public void floorTestFill() {

    m.put(1, "val1");
    m.put(2, "val2");
    m.put(5, "val3");

    assertEquals(5, m.floorKey(5));
    assertEquals(1, m.floorKey(1));
    assertEquals(2, m.floorKey(2));
    assertEquals(2, m.floorKey(3));
    assertEquals(2, m.floorKey(4));
    assertEquals(5, m.floorKey(5));
    assertEquals(5, m.floorKey(6));
  }
Пример #18
0
  @Test
  public void test_next_dir() {
    BTreeMap.DirNode d =
        new BTreeMap.DirNode(
            new Integer[] {44, 62, 68, 71}, false, false, false, mkchild(10, 20, 30, 40));

    assertEquals(10, m.nextDir(d, 62));
    assertEquals(10, m.nextDir(d, 44));
    assertEquals(10, m.nextDir(d, 48));

    assertEquals(20, m.nextDir(d, 63));
    assertEquals(20, m.nextDir(d, 64));
    assertEquals(20, m.nextDir(d, 68));

    assertEquals(30, m.nextDir(d, 69));
    assertEquals(30, m.nextDir(d, 70));
    assertEquals(30, m.nextDir(d, 71));

    assertEquals(40, m.nextDir(d, 72));
    assertEquals(40, m.nextDir(d, 73));
  }
Пример #19
0
  @Test
  public void simple_root_get() {

    if (valsOutside) return;

    BTreeMap.LeafNode l =
        new BTreeMap.LeafNode(
            new Object[] {10, 20, 30}, true, true, false, new Object[] {10, 20, 30}, 0);
    long rootRecid = engine.put(l, m.nodeSerializer);
    engine.update(m.rootRecidRef, rootRecid, Serializer.RECID);

    assertEquals(null, m.get(1));
    assertEquals(null, m.get(9));
    assertEquals(10, m.get(10));
    assertEquals(null, m.get(11));
    assertEquals(null, m.get(19));
    assertEquals(20, m.get(20));
    assertEquals(null, m.get(21));
    assertEquals(null, m.get(29));
    assertEquals(30, m.get(30));
    assertEquals(null, m.get(31));
  }
Пример #20
0
  @Test
  public void findSmaller() {

    for (int i = 0; i < 10000; i += 3) {
      m.put(i, "aa" + i);
    }

    for (int i = 0; i < 10000; i += 1) {
      Integer s = i - i % 3;
      Map.Entry e = m.findSmaller(i, true);
      assertEquals(s, e != null ? e.getKey() : null);
    }

    assertEquals(9999, m.findSmaller(100000, true).getKey());

    assertNull(m.findSmaller(0, false));
    for (int i = 1; i < 10000; i += 1) {
      Integer s = i - i % 3;
      if (s == i) s -= 3;
      Map.Entry e = m.findSmaller(i, false);
      assertEquals(s, e != null ? e.getKey() : null);
    }
    assertEquals(9999, m.findSmaller(100000, false).getKey());
  }
Пример #21
0
 protected void reinit() {
   // open name dir
   catalog = BTreeMap.preinitCatalog(this);
 }
Пример #22
0
  @Test
  public void findSmallerNodeLeaf() {
    BTreeMap.LeafNode n =
        new BTreeMap.LeafNode(
            new Object[] {2, 4, 6, 8, 10},
            true,
            true,
            false,
            new Object[] {"two", "four", "six", "eight", "ten"},
            0);

    assertNull(m.findSmallerNodeRecur(n, 1, true));
    assertNull(m.findSmallerNodeRecur(n, 1, false));
    assertNull(m.findSmallerNodeRecur(n, 2, false));
    assertEquals(new Fun.Pair(1, n), m.findSmallerNodeRecur(n, 2, true));

    assertEquals(new Fun.Pair(1, n), m.findSmallerNodeRecur(n, 3, true));
    assertEquals(new Fun.Pair(1, n), m.findSmallerNodeRecur(n, 3, false));

    assertEquals(new Fun.Pair(2, n), m.findSmallerNodeRecur(n, 4, true));
    assertEquals(new Fun.Pair(1, n), m.findSmallerNodeRecur(n, 3, false));

    assertEquals(new Fun.Pair(5, n), m.findSmallerNodeRecur(n, 10, true));
    assertEquals(new Fun.Pair(4, n), m.findSmallerNodeRecur(n, 10, false));

    assertEquals(new Fun.Pair(5, n), m.findSmallerNodeRecur(n, 12, true));
    assertEquals(new Fun.Pair(5, n), m.findSmallerNodeRecur(n, 12, false));
  }
Пример #23
0
  @Test
  public void test_empty_iterator() {

    assertFalse(m.keySet().iterator().hasNext());
    assertFalse(m.values().iterator().hasNext());
  }