com.yimidida.shards.id.db.SequenceBolckIdGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.yimidida.shards.id.db.SequenceBolckIdGenerator.java

Source

/*
 * @(#)SequenceBolckIdGenerator.java 2012-8-1 ?10:00:00
 *
 * Copyright (c) 2011-2012 Makersoft.org all rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 */
package com.yimidida.shards.id.db;

import java.io.Serializable;

import org.apache.ibatis.session.SqlSession;

import com.yimidida.shards.ShardId;
import com.yimidida.shards.id.IdGenerator;

/**
 * 
 */
public class SequenceBolckIdGenerator implements IdGenerator {
    protected int idBlockSize = 0;
    protected long nextId = 0;
    protected long lastId = -1;

    protected short nodeId = 0;

    public SequenceBolckIdGenerator() {

    }

    @Override
    public synchronized Serializable generate(SqlSession session, Object object) {
        throw new UnsupportedOperationException();
        //      if (lastId < nextId) {
        //         getNewBlock(session, object);
        //      }
        //      long _nextId = nextId++;
        //      return Long.toString(_nextId);
    }

    protected synchronized void getNewBlock(SqlSession session, Object object) {

        IdBlock idBlock = session.selectOne("", idBlockSize);
        // TODO http://jira.codehaus.org/browse/ACT-45 use a separate
        // 'requiresNew' command executor
        // IdBlock idBlock = commandExecutor.execute(new
        // GetNextIdBlockCmd(idBlockSize));
        this.nextId = idBlock.getNextId();
        this.lastId = idBlock.getLastId();
    }

    @Override
    public ShardId extractShardId(Serializable identifier) {
        throw new UnsupportedOperationException();
    }

    /**
     * Id
     */
    public void setNodeId(short nodeId) {
        this.nodeId = nodeId;
    }

}