Package tokyocabinet

Examples of tokyocabinet.BDBCUR


    private BDBCUR cursor;
    private boolean firstNextCall;

    public TokyoDataStoreIterator(BDB bdb) {
        this.cursor = new BDBCUR(bdb);
        // flag to indicate this is the first call to next()
        this.firstNextCall = true;
    }
View Full Code Here


  public void push_back(String key, byte[] value) throws TException {
   
    //first get the next key.
    List<byte[]> nextKey = bdb.fwmkeys(key.getBytes(),2);
   
    BDBCUR cursor = new BDBCUR(bdb);
   
    //look for edge cases
   
    //no forward keys
    if(nextKey.isEmpty() ){
      this.put(key, value);
      return;
    }
       
    //the input is all that was found. jump to last record.
    if( Arrays.equals(nextKey.get(0),key.getBytes()) && nextKey.size() == 1){
      if(!cursor.last())
        throw new RuntimeException("Can't jump to last record");
    }else{
   
      //jump to key ahead of our input key
      if(nextKey.size() == 1){   
        if(!cursor.jump(nextKey.get(0)))
          throw new RuntimeException("Key should exist but failed to jump to it's location");       
      } else {
        if(!cursor.jump(nextKey.get(1)))
          throw new RuntimeException("Key should exist but failed to jump to it's location");
      }   
    }
   
    //back one should be the input key, if it's not then the key
    //does not exist yet, so we add it
    if(!Arrays.equals(cursor.key(),key.getBytes())){
      if(!cursor.prev()){
        this.put(key, value);
        return;
      }
    }
   
    // the key under this cursor should match, if not we add a new key
    //based on input
    if(!Arrays.equals(cursor.key(),key.getBytes())){
      this.put(key, value);
      return;
    }
   
    //we are at the right spot, now append the value
    if(!cursor.put(value, BDBCUR.CPAFTER))   
      throw new TException(bdb.errmsg());
     
  }
View Full Code Here

  public byte[] pop_back(String key) throws TException{
   
    //first get the next key.
    List<byte[]> nextKey = bdb.fwmkeys(key.getBytes(),2);
   
    BDBCUR cursor = new BDBCUR(bdb);
   
    if(nextKey.isEmpty()){
      return new byte[]{};   
    }
   
    //first entry should match, if not the input key is missing so fail
    if(!Arrays.equals(nextKey.get(0),key.getBytes())){
     
      return new byte[]{};
   
    }
   
    //the input key is at the end of the index so jump to end
    if(nextKey.size() == 1) {
     
      if(!cursor.last())
        throw new RuntimeException("Unable to jump to last key");
     
    } else {
     
      if(!cursor.jump(nextKey.get(1)))
        throw new RuntimeException("Key should exist but failed to jump to it's location");
     
    }
   
     
    //move back one, to tail of the input key
    if(!cursor.prev())
      throw new RuntimeException("Key should exist but failed to jump to it's location");
   
    if(!Arrays.equals(cursor.key(),key.getBytes()))
      throw new TException("key mismatch "+key+" vs "+cursor.key2());
 
    byte[] value = cursor.val();
   
    //delete
    if(!cursor.out())
      throw new TException(bdb.errmsg());
   
   
    return value;
  }
View Full Code Here

    return value;
  }
 
  public void push_front(String key, byte[] value) throws TException {

    BDBCUR cursor = new BDBCUR(bdb);
   
    if(!cursor.jump(key)){
      this.put(key, value);
      return;
    }
   
    //not matching key so add new
    if(!Arrays.equals(cursor.key(),key.getBytes())){
      this.put(key, value);
      return;
    }
   
    if(!cursor.put(value, BDBCUR.CPBEFORE))
      throw new TException(bdb.errmsg());
   
  }
View Full Code Here

   
  }
 
  public byte[] pop_front(String key) throws TException{
   
    BDBCUR cursor = new BDBCUR(bdb);
   
    if(!cursor.jump(key))
      return new byte[]{};
   
    if(!Arrays.equals(cursor.key(),key.getBytes()))
      return new byte[]{};
 
   
    //save value
    byte[] value = cursor.val();
     
   
    //delete
    if(!cursor.out())
      throw new TException(bdb.errmsg());
   
    return value;
  }
View Full Code Here

 
  private BDBCUR goto_position(String key, int position) {
    if(position < 0)
      return null;
   
    BDBCUR cursor = new BDBCUR(bdb);
   
    if(!cursor.jump(key))
      return null;
   
    int currentPos = 0;
    int checkMod = 10; //verify key every few records
    while(currentPos < position){
      cursor.next();
     
      if(currentPos % checkMod == 0){
       
        if(!Arrays.equals(cursor.key(), key.getBytes()))
          return null;
       
      }
     
      currentPos++;
    }
   
    //at position
    if(!Arrays.equals(cursor.key(), key.getBytes()))
      return null;
   
   
    return cursor;
  }
View Full Code Here

    return cursor;
  }
 
  public byte[] remove_at(String key, int position){
   
    BDBCUR cursor = this.goto_position(key, position);
   
    //if position not found
    if(cursor == null)
      return null;
   
    byte[] value = cursor.val();
   
    if(!cursor.out())
      throw new RuntimeException("Unable to remove record");
 
    return value;
  }
View Full Code Here

 
    return value;
  }
 
  public void insert_at(String key, byte[] value, int position) {
    BDBCUR cursor = this.goto_position(key, position);
   
    if(cursor == null)
      throw new RuntimeException("Unable to insert at position: "+position);
   
    if(!cursor.put(value, BDBCUR.CPBEFORE))
      throw new RuntimeException("Unable to replace record");
  }
View Full Code Here

    if(!cursor.put(value, BDBCUR.CPBEFORE))
      throw new RuntimeException("Unable to replace record");
  }
 
  public void replace_at(String key, byte[] value, int position) {
    BDBCUR cursor = this.goto_position(key, position);
   
    if(cursor == null)
      return;
   
    if(!cursor.put(value, BDBCUR.CPCURRENT))
      throw new RuntimeException("Unable to replace record");
  }
View Full Code Here

    if(!cursor.put(value, BDBCUR.CPCURRENT))
      throw new RuntimeException("Unable to replace record");
  }
 
  public byte[] retrieve_at(String key, int position){
    BDBCUR cursor = this.goto_position(key, position);
   
    //if position not found
    if(cursor == null)
      return null;
   
    byte[] value = cursor.val();
   
 
    return value;
  }
View Full Code Here

TOP

Related Classes of tokyocabinet.BDBCUR

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.