Package com.cloudhopper.datastore.tokyo

Source Code of com.cloudhopper.datastore.tokyo.TokyoDataStoreIterator

package com.cloudhopper.datastore.tokyo;

/*
* #%L
* ch-datastore
* %%
* Copyright (C) 2012 Cloudhopper by Twitter
* %%
* 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.
* #L%
*/

import com.cloudhopper.datastore.DataStoreFatalException;
import com.cloudhopper.datastore.DataStoreIterator;
import tokyocabinet.BDB;
import tokyocabinet.BDBCUR;

/**
*
* @author joelauer
*/
public class TokyoDataStoreIterator implements DataStoreIterator {

    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;
    }

    public boolean next() throws DataStoreFatalException {
        // tokyo cabinet does things a little different in that the first record
        // is already selected by default -- our interface says that you need
        // to call next() first, so we will only call next on calls after the
        // first one
        if (this.firstNextCall) {
            this.firstNextCall = false;
            // move to first record
            return this.cursor.first();
        } else {
            return cursor.next();
        }
    }

    public boolean jump(byte[] key) throws DataStoreFatalException {
  if (this.firstNextCall) this.firstNextCall = false;
  return this.cursor.jump(key);
    }

    public Record getRecord() throws DataStoreFatalException {
        byte[] key = cursor.key();
        byte[] value = cursor.val();
        if (key == null || value == null) {
            throw new DataStoreFatalException("Record key or value was null");
        }
        return new Record(key, value);
    }

    public void close() throws DataStoreFatalException {
        // don't really have to do anything to close the cursor
        cursor = null;
    }

}
TOP

Related Classes of com.cloudhopper.datastore.tokyo.TokyoDataStoreIterator

TOP
Copyright © 2018 www.massapi.com. 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.