任何人都可以建议一个更优雅的解决方案吗? [英] can anyone suggest a more elegant solution to this?

查看:55
本文介绍了任何人都可以建议一个更优雅的解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个Java存储过程,我用它来注册系统在我们的网络上。
我们的网络。在它的中间是一个函数,它返回在C级网络中的第一个

免费IP地址,该网络用于注册

过程。


在这个过程中,一个免费的IP地址被定义为一个介于1和之间的数字

254


目前我正在拿取一个有序的IP地址列表(这将是
,我的范围是1 - 254)然后使用带有几个

变量和2个break语句的while循环来计算输出最低的IP地址

这是免费的。

基本上我要检查列表中的每个条目是否与

不同下一个(更高)值乘以1.如果它没有那么免费地址更低

值+ 1并且你呼出房间。


只是想知道是否有更快的方法可以做到这一点。

Alex

Hi,
I''ve got a Java stored procedure that I use to "register" systems on
our network. In the middle of it is a function that returns the 1st
free ip address in a class C network which is used in the registration
process.

In this process a free ip address is defined as a number between 1 and
254

At the moment I''m fetching an ordered list of ip addresses (which will
be i the range 1 - 254) and then using a while loop with a couple of
variables and 2 break statements to figure out the lowest ip address
that''s free.
Basically i''m checking that each entry in the list differs from the
next (higher) value by 1. If it doesn''t then the free address is lower
value + 1 and you breat out of the room.

just wondering if there might be a faster way to do it.
Alex

推荐答案

alexs写道:
alexs wrote:



我我有一个Java存储过程,我用它来注册系统在我们的网络上。
我们的网络。在它的中间是一个函数,它返回在C级网络中的第一个

免费IP地址,该网络用于注册

过程。


在这个过程中,一个免费的IP地址被定义为一个介于1和之间的数字

254


目前我正在拿取一个有序的IP地址列表(这将是
,我的范围是1 - 254)然后使用带有几个

变量和2个break语句的while循环来计算输出最低的IP地址

这是免费的。

基本上我要检查列表中的每个条目是否与

不同下一个(更高)值乘以1.如果它不是那么免费地址更低

值+ 1并且你呼出房间。
Hi,
I''ve got a Java stored procedure that I use to "register" systems on
our network. In the middle of it is a function that returns the 1st
free ip address in a class C network which is used in the registration
process.

In this process a free ip address is defined as a number between 1 and
254

At the moment I''m fetching an ordered list of ip addresses (which will
be i the range 1 - 254) and then using a while loop with a couple of
variables and 2 break statements to figure out the lowest ip address
that''s free.
Basically i''m checking that each entry in the list differs from the
next (higher) value by 1. If it doesn''t then the free address is lower
value + 1 and you breat out of the room.



你没有指定DB2的平台或版本,所以我会假设

Linux / UNIX / Windows和DB2 V8。你也没有指定一张桌子

的设计,所以这里的东西。

- 设置

create table registered_ips(子网char(11),addr smallint);


插入到registered_ips

值(''192.168.1'',1),

(''192.168.1'',2),

(''192.168.1'',3),

('''192.168.1' ',6),

(''192.168.1'',12),

('10 .0.0'',1),

(''10 .0.0'',2);


- 用于查找特定子网的第一个开放地址的SQL语句。

select

子网,addr + 1 as avail_addr

来自

(选择

子网,

addr,

min(addr)over(按子网分区

由addr命令

行在1以下和1之后)为

next_addr

来自

registered_ips

)作为

其中

subnet =''192.168.1''和

(next_a ddr为null或next_addr< addr + 1)

仅获取前1行

;


显然指定哪个提交上面的where子句。


上面的SQL返回,它发现''4'是

192.168.1子网的最低地址。 />

SUBNET AVAIL_ADDR

----------- -----------

192.168.1 4


如果没有任何漏洞(10.0.0的情况),它会给你相同的
max(addr)+1:


SUBNET AVAIL_ADDR

----------- ---------- -

10.0.0 3

为这个查询找到合适的索引留给读者一个练习


You don''t specify the platform or version of DB2, so I''ll assume
Linux/UNIX/Windows and DB2 V8. You also don''t specify a table
design, so here''s something.
-- setup
create table registered_ips (subnet char(11), addr smallint);

insert into registered_ips
values (''192.168.1'', 1),
(''192.168.1'', 2),
(''192.168.1'', 3),
(''192.168.1'', 6),
(''192.168.1'', 12),
(''10.0.0'', 1),
(''10.0.0'', 2);

-- SQL statement to find first open address for a particular subnet.
select
subnet, addr+1 as avail_addr
from
(select
subnet,
addr,
min(addr) over (partition by subnet
order by addr
rows between 1 following and 1 following) as
next_addr
from
registered_ips
) as a
where
subnet = ''192.168.1'' and
(next_addr is null or next_addr <addr+1)
fetch first 1 row only
;

Obviously specify which submit in the where clause above.

The above SQL returns, it finds ''4'' as the lowest address for the
192.168.1 subnet.

SUBNET AVAIL_ADDR
----------- -----------
192.168.1 4

If there aren''t any holes (as is the case with 10.0.0), it will
give you the equivalent of max(addr)+1:

SUBNET AVAIL_ADDR
----------- -----------
10.0.0 3
Finding appropriate index(es) for this query is left as an exercise
for the reader.


Ian写道:
Ian wrote:

>

为此查询查找适当的索引剩下作为练习

给读者。

>
Finding appropriate index(es) for this query is left as an exercise
for the reader.



另外,博unds-checking也是留给读者的练习:-)


Serge甚至可能写一个声明结合

找到值AND将其插入表中并将其返回给应用程序。 ;-)


Ian

Also, bounds-checking is left as an exercise for the reader, too :-)

Serge could probably even write a single statement that combines
finding the value AND inserting it into the table AND returning it
to the application. ;-)

Ian


如果某个子网没有addr = 1,则可能需要添加更多

代码。

(我不知道这是真的,或者总是有一行addr = 1 for

每个子网)。


---数据---

插入到registered_ips

值(''192.168.0' ',3),

(''192.168.0'',4),

(''192.168.0'',6),

(''192.168.0'',12),

(''192.168.1'',1),

('''192.168.1' ',2),

(''192.168.1'',3),

(''192.168.1'',6),

(''192.168.1'',12),

('10 .0.0'',1),

(''10 .0.0' ',2);


------------------------------输入的命令

------------------------------

选择

子网,
coalesce(nullif(min(addr),min(min_addr)),0)+1 as avail_addr

来自

(选择

子网,

addr,

min(地址)

结束(按子网分区

由addr命令

行之间的1以下

和1以下)作为next_addr,

min(addr)

over(按子网分区)为min_addr

来自

registered_ips

)as a

其中

(next_addr为null或next_addr< addr + 1)

或(min_addr = addr和addr 1)

group by

sub

;

------------------ -------------------------------------------------- ----------


SUBNET AVAIL_ADDR

----------- ------- ----

10.0.0 3

192.168.0 1

192.168.1 4


3条记录已被选中。


以下是另一个不使用OLAP功能的例子。

------------ ------------------输入的命令

---------------------- - ------

选择

sbn.subnet,coalesce(avail_addr,0)+1 as avail_addr

来自

(选择

不同的子网

来自

registered_ips

)sbn

左外连接

(选择

子网,min(addr)avail_addr

来自

registered_ips ips

其中

不存在

(选择*

来自

registered_ips ipn

其中

ipn.subnet = ips.subnet

和ipn.addr = ips.addr + 1



并且存在

(选择*

来自

registered_ips ipe

其中

ipe.subnet = ips.subnet

和ipe.addr = 1



group by

子网

)adr

on sbn.subnet = adr.subnet

;

- -------------------------------------------------- --------------------------


SUBNET AVAIL_ADDR

- --------- ---- -------

10.0.0 3

192.168.0 1

192.168.1 4

3条记录被选中。


如果你想获得特定子网的addr,

你可以替换


(选择

不同的子网

来自

registered_ips

)sbn


with


(值''192.168.1''

)sbn(子网)

If there is not addr = 1 for some subnet, it may need to add some more
code.
(I don''t know this is true or there is always a row with addr = 1 for
each subnet).

--- Data ---
insert into registered_ips
values (''192.168.0'', 3),
(''192.168.0'', 4),
(''192.168.0'', 6),
(''192.168.0'', 12),
(''192.168.1'', 1),
(''192.168.1'', 2),
(''192.168.1'', 3),
(''192.168.1'', 6),
(''192.168.1'', 12),
(''10.0.0'', 1),
(''10.0.0'', 2);

------------------------------ Commands Entered
------------------------------
select
subnet,
coalesce(nullif(min(addr),min(min_addr)),0)+1 as avail_addr
from
(select
subnet,
addr,
min(addr)
over (partition by subnet
order by addr
rows between 1 following
and 1 following) as next_addr,
min(addr)
over (partition by subnet) as min_addr
from
registered_ips
) as a
where
(next_addr is null or next_addr <addr+1)
or (min_addr = addr and addr 1)
group by
subnet
;
------------------------------------------------------------------------------

SUBNET AVAIL_ADDR
----------- -----------
10.0.0 3
192.168.0 1
192.168.1 4

3 record(s) selected.

Following is another example without using OLAP functions.
------------------------------ Commands Entered
------------------------------
select
sbn.subnet, coalesce(avail_addr,0)+1 as avail_addr
from
(select
distinct subnet
from
registered_ips
) sbn
left outer join
(select
subnet, min(addr) avail_addr
from
registered_ips ips
where
not exists
(select *
from
registered_ips ipn
where
ipn.subnet = ips.subnet
and ipn.addr = ips.addr + 1
)
and exists
(select *
from
registered_ips ipe
where
ipe.subnet = ips.subnet
and ipe.addr = 1
)
group by
subnet
) adr
on sbn.subnet = adr.subnet
;
------------------------------------------------------------------------------

SUBNET AVAIL_ADDR
----------- -----------
10.0.0 3
192.168.0 1
192.168.1 4

3 record(s) selected.

If you want get available addr for a specific subnet,
you can replace

(select
distinct subnet
from
registered_ips
) sbn

with

(values ''192.168.1''
) sbn (subnet)


这篇关于任何人都可以建议一个更优雅的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆